| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <?php
- namespace app\common\service;
- use app\common\model\works\ServiceWork;
- use think\facade\Cache;
- use think\facade\Log;
- class OnlineCustomerService
- {
- /*
- * 当期登录开通服务的客服定义:customer_online_service_list
- * 地区业务分配值 : regional_business_allocation_value
- * 某客服不同类型消息提醒: consultation_notific service_work_notific
- * $business_type: consultation servicework
- */
- // 客服主管账号能看所有的
- public static array $manager_admin_ids = [4];
- public static array $admin_ids = [
- 'consultation'=>[4,16],
- 'servicework'=>[4,16],
- ];
- public static array $business_types = [
- 'consultation'=>[
- 'to_router' => '/works/external_consultation',
- 'notificationTitle' => '客咨消息',
- ],
- 'servicework'=>[
- 'to_router' => '/works/service_work',
- 'notificationTitle' => '订单消息',
- ],
- ];
- public static function isOnline($admin_id){
- $customerOnlineServiceArr = Cache::store('common_redis')->get('customer_online_service_list');
- if(!empty($customerOnlineServiceArr)){
- //$max_code => $admin_id
- $max_code = array_search($admin_id, $customerOnlineServiceArr, true);
- if($max_code){
- return true;
- }
- }
- return false;
- }
- public static function addOnlineCustomerService($admin_id){
- if(!in_array($admin_id,self::$admin_ids['consultation']) && !in_array($admin_id,self::$admin_ids['servicework'])){
- return [];
- }
- $customerOnlineServiceArr = Cache::store('common_redis')->get('customer_online_service_list');
- $max_code = 1;
- if(!empty($customerOnlineServiceArr)){
- //$max_code => $admin_id
- $max_code = array_search($admin_id, $customerOnlineServiceArr, true);
- if(empty($max_code)){
- $max_code = max(array_keys($customerOnlineServiceArr));
- $max_code++;
- }
- }
- $customerOnlineServiceArr[$max_code] = $admin_id;
- Cache::store('common_redis')->set('customer_online_service_list',empty($customerOnlineServiceArr)?[]:$customerOnlineServiceArr);
- return $customerOnlineServiceArr;
- }
- public static function subOnlineCustomerService($admin_id){
- $customerOnlineServiceArr = Cache::store('common_redis')->get('customer_online_service_list');
- if(!empty($customerOnlineServiceArr)){
- //$max_code => $admin_id
- $max_code = array_search($admin_id, $customerOnlineServiceArr, true);
- if($max_code){
- unset($customerOnlineServiceArr[$max_code]);
- Cache::store('common_redis')->set('customer_online_service_list',empty($customerOnlineServiceArr)?[]:$customerOnlineServiceArr);
- }
- }
- return $customerOnlineServiceArr;
- }
- // 新来业务分配给哪个客服编号
- public static function getBusinessAllocationValue($business_type,$business_id){
- $code = Cache::store('common_redis')->get('business:'.$business_type.'_allocation_value');
- $customerOnlineServiceArr = Cache::store('common_redis')->get('customer_online_service_list');
- if(empty($customerOnlineServiceArr)){
- Log::info("没有在线客服:{$business_type}:{$business_id}");
- Cache::store('common_redis')->set('business:'.$business_type.'_allocation_value',0);
- return 0;
- }
- foreach ($customerOnlineServiceArr as $admin_code => $adminid) {
- if($code < $admin_code){
- Cache::store('common_redis')->set('business:'.$business_type.'_allocation_value',$admin_code);
- Log::info("分给在线客服:{$adminid}:{$business_type}:{$business_id}");
- return $adminid;
- }
- }
- $min_code = min(array_keys($customerOnlineServiceArr));
- Cache::store('common_redis')->set('business:'.$business_type.'_allocation_value',$min_code);
- $admin_id = $customerOnlineServiceArr[$min_code];
- Log::info("分给在线客服:{$admin_id}:{$business_type}:{$business_id}");
- return $admin_id;
- }
- // 向客服发送消息提醒 $business_type = 地区编码+业务类型 consultation_notific service_work_notific
- public static function customerSendMessage($admin_id,$business_type,$business_id){
- $notificList = Cache::store('common_redis')->get($business_type.'_notific');
- if(empty($notificList)) $notificList = [];
- $notificList[$business_id] = $admin_id;
- Cache::store('common_redis')->set($business_type.'_notific',$notificList);
- return true;
- }
- public static function customerClsMessage($business_type,$business_id){
- $notificList = Cache::store('common_redis')->get($business_type.'_notific');
- if(isset($notificList[$business_id])){
- unset($notificList[$business_id]);
- Cache::store('common_redis')->set($business_type.'_notific',$notificList);
- }
- return true;
- }
- // 新来业务分配给某个客服,并向客服发送消息提醒
- public static function assignBusinessToCustomer($business_type,$business_id,$admin_id = 0){
- empty($admin_id) && $admin_id = self::getBusinessAllocationValue($business_type,$business_id);
- self::customerSendMessage($admin_id,$business_type,$business_id);
- return $admin_id;
- }
- public static function getNotificList($admin_id,$business_type,$prefix = ''){
- $notificList = Cache::store('common_redis')->get($prefix.$business_type.'_notific');
- if(empty($notificList)) $notificList = [];
- $count = 0;
- if (!empty($notificList) && (in_array($admin_id,self::$admin_ids['consultation']) || in_array($admin_id,self::$admin_ids['servicework']))) {
- // TODO 测试 tmp 暂不区分客服
- //$count = count($notificList);
- $counts = array_count_values($notificList);
- $count = $counts[$admin_id] ?? 0;
- $count += ($counts[0] ?? 0);
- }
- $to_router = $notificationTitle = '';
- if(isset(self::$business_types[$business_type]) && self::$business_types[$business_type]){
- $to_router = self::$business_types[$business_type]['to_router']??'/workbench';
- $notificationTitle = self::$business_types[$business_type]['notificationTitle']??'';
- }
- return ['type'=>$business_type,'unique_code'=> md5($count),'to_router'=>$to_router,'notificationTitle' => $notificationTitle,'notific_count' => $count];
- }
- public static function getAllNotificList($admin_id,$business_codes = [],$prefix = '',$is_test = 0){
- if($is_test){
- return [
- ['type'=>'consultation','mp3_url'=>'','unique_code'=> md5('consultation'.time()),'to_router'=>'/works/external_consultation','notificationTitle' => '客咨消息','notific_count' => 5],
- ['type'=>'servicework','mp3_url'=>'','unique_code'=> md5('servicework'.time()),'to_router'=>'/works/service_work','notificationTitle' => '订单消息','notific_count' => 6]
- ];
- }
- $res = [];
- //$prefix = ''; // 地区业务值
- foreach (self::$business_types as $business_type => $info) {
- if(!empty($business_codes) && in_array($business_type,$business_codes)){
- $res[] = self::getNotificList($admin_id,$business_type,$prefix);
- }else{
- $res[] = self::getNotificList($admin_id,$business_type,$prefix);
- }
- }
- return $res;
- }
- // 分配客服派单
- public static function serviceWorkMessage($work_id = '',$admin_id = 0){
- $admin_id = self::assignBusinessToCustomer('servicework',$work_id,$admin_id);
- // 绑定 $res['data']['admin_id']
- $admin_id > 0 && ServiceWork::where('id',$work_id)->update([
- 'admin_id' => $admin_id,
- ]);
- return $admin_id;
- }
- }
|