OnlineCustomerService.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace app\common\service;
  3. use app\common\model\works\ServiceWork;
  4. use think\facade\Cache;
  5. use think\facade\Log;
  6. class OnlineCustomerService
  7. {
  8. /*
  9. * 当期登录开通服务的客服定义:customer_online_service_list
  10. * 地区业务分配值 : regional_business_allocation_value
  11. * 某客服不同类型消息提醒: consultation_notific service_work_notific
  12. * $business_type: consultation servicework
  13. */
  14. // 客服主管账号能看所有的
  15. public static array $manager_admin_ids = [4];
  16. public static array $admin_ids = [
  17. 'consultation'=>[4,16],
  18. 'servicework'=>[4,16],
  19. ];
  20. public static array $business_types = [
  21. 'consultation'=>[
  22. 'to_router' => '/works/external_consultation',
  23. 'notificationTitle' => '客咨消息',
  24. ],
  25. 'servicework'=>[
  26. 'to_router' => '/works/service_work',
  27. 'notificationTitle' => '订单消息',
  28. ],
  29. ];
  30. public static function isOnline($admin_id){
  31. $customerOnlineServiceArr = Cache::store('common_redis')->get('customer_online_service_list');
  32. if(!empty($customerOnlineServiceArr)){
  33. //$max_code => $admin_id
  34. $max_code = array_search($admin_id, $customerOnlineServiceArr, true);
  35. if($max_code){
  36. return true;
  37. }
  38. }
  39. return false;
  40. }
  41. public static function addOnlineCustomerService($admin_id){
  42. if(!in_array($admin_id,self::$admin_ids['consultation']) && !in_array($admin_id,self::$admin_ids['servicework'])){
  43. return [];
  44. }
  45. $customerOnlineServiceArr = Cache::store('common_redis')->get('customer_online_service_list');
  46. $max_code = 1;
  47. if(!empty($customerOnlineServiceArr)){
  48. //$max_code => $admin_id
  49. $max_code = array_search($admin_id, $customerOnlineServiceArr, true);
  50. if(empty($max_code)){
  51. $max_code = max(array_keys($customerOnlineServiceArr));
  52. $max_code++;
  53. }
  54. }
  55. $customerOnlineServiceArr[$max_code] = $admin_id;
  56. Cache::store('common_redis')->set('customer_online_service_list',empty($customerOnlineServiceArr)?[]:$customerOnlineServiceArr);
  57. return $customerOnlineServiceArr;
  58. }
  59. public static function subOnlineCustomerService($admin_id){
  60. $customerOnlineServiceArr = Cache::store('common_redis')->get('customer_online_service_list');
  61. if(!empty($customerOnlineServiceArr)){
  62. //$max_code => $admin_id
  63. $max_code = array_search($admin_id, $customerOnlineServiceArr, true);
  64. if($max_code){
  65. unset($customerOnlineServiceArr[$max_code]);
  66. Cache::store('common_redis')->set('customer_online_service_list',empty($customerOnlineServiceArr)?[]:$customerOnlineServiceArr);
  67. }
  68. }
  69. return $customerOnlineServiceArr;
  70. }
  71. // 新来业务分配给哪个客服编号
  72. public static function getBusinessAllocationValue($business_type,$business_id){
  73. $code = Cache::store('common_redis')->get('business:'.$business_type.'_allocation_value');
  74. $customerOnlineServiceArr = Cache::store('common_redis')->get('customer_online_service_list');
  75. if(empty($customerOnlineServiceArr)){
  76. Log::info("没有在线客服:{$business_type}:{$business_id}");
  77. Cache::store('common_redis')->set('business:'.$business_type.'_allocation_value',0);
  78. return 0;
  79. }
  80. foreach ($customerOnlineServiceArr as $admin_code => $adminid) {
  81. if($code < $admin_code){
  82. Cache::store('common_redis')->set('business:'.$business_type.'_allocation_value',$admin_code);
  83. Log::info("分给在线客服:{$adminid}:{$business_type}:{$business_id}");
  84. return $adminid;
  85. }
  86. }
  87. $min_code = min(array_keys($customerOnlineServiceArr));
  88. Cache::store('common_redis')->set('business:'.$business_type.'_allocation_value',$min_code);
  89. $admin_id = $customerOnlineServiceArr[$min_code];
  90. Log::info("分给在线客服:{$admin_id}:{$business_type}:{$business_id}");
  91. return $admin_id;
  92. }
  93. // 向客服发送消息提醒 $business_type = 地区编码+业务类型 consultation_notific service_work_notific
  94. public static function customerSendMessage($admin_id,$business_type,$business_id){
  95. $notificList = Cache::store('common_redis')->get($business_type.'_notific');
  96. if(empty($notificList)) $notificList = [];
  97. $notificList[$business_id] = $admin_id;
  98. Cache::store('common_redis')->set($business_type.'_notific',$notificList);
  99. return true;
  100. }
  101. public static function customerClsMessage($business_type,$business_id){
  102. $notificList = Cache::store('common_redis')->get($business_type.'_notific');
  103. if(isset($notificList[$business_id])){
  104. unset($notificList[$business_id]);
  105. Cache::store('common_redis')->set($business_type.'_notific',$notificList);
  106. }
  107. return true;
  108. }
  109. // 新来业务分配给某个客服,并向客服发送消息提醒
  110. public static function assignBusinessToCustomer($business_type,$business_id,$admin_id = 0){
  111. empty($admin_id) && $admin_id = self::getBusinessAllocationValue($business_type,$business_id);
  112. self::customerSendMessage($admin_id,$business_type,$business_id);
  113. return $admin_id;
  114. }
  115. public static function getNotificList($admin_id,$business_type,$prefix = ''){
  116. $notificList = Cache::store('common_redis')->get($prefix.$business_type.'_notific');
  117. if(empty($notificList)) $notificList = [];
  118. $count = 0;
  119. if (!empty($notificList) && (in_array($admin_id,self::$admin_ids['consultation']) || in_array($admin_id,self::$admin_ids['servicework']))) {
  120. // TODO 测试 tmp 暂不区分客服
  121. //$count = count($notificList);
  122. $counts = array_count_values($notificList);
  123. $count = $counts[$admin_id] ?? 0;
  124. $count += ($counts[0] ?? 0);
  125. }
  126. $to_router = $notificationTitle = '';
  127. if(isset(self::$business_types[$business_type]) && self::$business_types[$business_type]){
  128. $to_router = self::$business_types[$business_type]['to_router']??'/workbench';
  129. $notificationTitle = self::$business_types[$business_type]['notificationTitle']??'';
  130. }
  131. return ['type'=>$business_type,'unique_code'=> md5($count),'to_router'=>$to_router,'notificationTitle' => $notificationTitle,'notific_count' => $count];
  132. }
  133. public static function getAllNotificList($admin_id,$business_codes = [],$prefix = '',$is_test = 0){
  134. if($is_test){
  135. return [
  136. ['type'=>'consultation','mp3_url'=>'','unique_code'=> md5('consultation'.time()),'to_router'=>'/works/external_consultation','notificationTitle' => '客咨消息','notific_count' => 5],
  137. ['type'=>'servicework','mp3_url'=>'','unique_code'=> md5('servicework'.time()),'to_router'=>'/works/service_work','notificationTitle' => '订单消息','notific_count' => 6]
  138. ];
  139. }
  140. $res = [];
  141. //$prefix = ''; // 地区业务值
  142. foreach (self::$business_types as $business_type => $info) {
  143. if(!empty($business_codes) && in_array($business_type,$business_codes)){
  144. $res[] = self::getNotificList($admin_id,$business_type,$prefix);
  145. }else{
  146. $res[] = self::getNotificList($admin_id,$business_type,$prefix);
  147. }
  148. }
  149. return $res;
  150. }
  151. // 分配客服派单
  152. public static function serviceWorkMessage($work_id = '',$admin_id = 0){
  153. $admin_id = self::assignBusinessToCustomer('servicework',$work_id,$admin_id);
  154. // 绑定 $res['data']['admin_id']
  155. $admin_id > 0 && ServiceWork::where('id',$work_id)->update([
  156. 'admin_id' => $admin_id,
  157. ]);
  158. return $admin_id;
  159. }
  160. }