Events.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. <?php
  2. /**
  3. * This file is part of workerman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. /**
  15. * 用于检测业务代码死循环或者长时间阻塞等问题
  16. * 如果发现业务卡死,可以将下面declare打开(去掉//注释),并执行php start.php reload
  17. * 然后观察一段时间workerman.log看是否有process_timeout异常
  18. */
  19. //declare(ticks=1);
  20. use \GatewayWorker\Lib\Gateway;
  21. use Workerman\Lib\Timer;
  22. /**
  23. * 主逻辑
  24. * 主要是处理 onConnect onMessage onClose 三个方法
  25. * onConnect 和 onClose 如果不需要可以不用实现并删除
  26. */
  27. class Events
  28. {
  29. /**
  30. * 新建一个类的静态成员,用来保存数据库实例
  31. */
  32. public static $db = null;
  33. public static $global = null;
  34. /**
  35. * 进程启动后初始化数据库连接
  36. */
  37. public static function onWorkerStart($worker)
  38. {
  39. if (empty(self::$db)) {
  40. self::$db = new \Workerman\MySQL\Connection('192.168.2.186', '3306', 'root', '', 'customer_service');
  41. }
  42. if (empty(self::$global)) {
  43. self::$global = new \GlobalData\Client('127.0.0.1:2207');
  44. // 客服列表
  45. if(is_null(self::$global->kfList)){
  46. self::$global->kfList = [];
  47. }
  48. // 会员列表[动态的,这里面只是目前未被分配的会员信息]
  49. if(is_null(self::$global->userList)){
  50. self::$global->userList = [];
  51. }
  52. // 会员以 uid 为key的信息简表,只有在用户退出的时候,才去执行修改
  53. if(is_null(self::$global->uidSimpleList)){
  54. self::$global->uidSimpleList = [];
  55. }
  56. // 当天的累积接入值
  57. $key = date('Ymd') . 'total_in';
  58. if(is_null(self::$global->$key)){
  59. self::$global->$key = 0;
  60. $oldKey = date('Ymd', strtotime('-1 day')); // 删除前一天的统计值
  61. unset(self::$global->$oldKey);
  62. unset($oldKey, $key);
  63. }
  64. // 成功接入值
  65. $key = date('Ymd') . 'success_in';
  66. if(is_null(self::$global->$key)){
  67. self::$global->$key = 0;
  68. $oldKey = date('Ymd', strtotime('-1 day')); // 删除前一天的统计值
  69. unset(self::$global->$oldKey);
  70. unset($oldKey, $key);
  71. }
  72. }
  73. // 定时统计数据
  74. if(0 === $worker->id){
  75. // 1分钟统计一次实时数据
  76. Timer::add(60 * 1, function(){
  77. self::writeLog(1);
  78. });
  79. // 40分钟写一次当前日期点数的log数据
  80. Timer::add(60 * 40, function(){
  81. self::writeLog(2);
  82. });
  83. }
  84. }
  85. /**
  86. * 当客户端连接时触发
  87. * 如果业务不需此回调可以删除onConnect
  88. *
  89. * @param int $client_id 连接id
  90. */
  91. public static function onConnect($client_id)
  92. {
  93. }
  94. /**
  95. * 当客户端发来消息时触发
  96. * @param int $client_id 连接id
  97. * @param mixed $message 具体消息
  98. */
  99. public static function onMessage($client_id, $message)
  100. {
  101. if ($message!='{"type":"ping"}'){
  102. echo "onMessage: ".$message."\r\n";
  103. // print_r([self::$global->kfList,self::$global->userList, self::$global->uidSimpleList]);
  104. }
  105. $message = json_decode($message, true);
  106. switch ($message['type']) {
  107. // 客服初始化
  108. case 'init':
  109. $kfList = self::$global->kfList;
  110. // 如果该客服未在内存中记录则记录
  111. if(!isset($kfList[$message['group']]) || !array_key_exists($message['uid'], $kfList[$message['group']])){
  112. do{
  113. $newKfList = $kfList;
  114. $newKfList[$message['group']][$message['uid']] = [
  115. 'id' => $message['uid'],
  116. 'name' => $message['name'],
  117. 'avatar' => $message['avatar'],
  118. 'client_id' => $client_id,
  119. 'task' => 0,
  120. 'user_info' => []
  121. ];
  122. }while(!self::$global->cas('kfList', $kfList, $newKfList));
  123. unset($newKfList, $kfList);
  124. }else if(isset($kfList[$message['group']][$message['uid']])){
  125. do{
  126. $newKfList = $kfList;
  127. $newKfList[$message['group']][$message['uid']]['client_id'] = $client_id;
  128. }while(!self::$global->cas('kfList', $kfList, $newKfList));
  129. unset($newKfList, $kfList);
  130. }
  131. // 绑定 client_id 和 uid
  132. Gateway::bindUid($client_id, $message['uid']);
  133. // TODO 尝试拉取用户来服务 [二期规划]
  134. break;
  135. // 顾客初始化
  136. case 'userInit';
  137. $userList = self::$global->userList;
  138. // 如果该顾客未在内存中记录则记录
  139. if(!array_key_exists($message['uid'], $userList)){
  140. do{
  141. $NewUserList = $userList;
  142. $NewUserList[$message['uid']] = [
  143. 'id' => $message['uid'],
  144. 'name' => $message['name'],
  145. 'avatar' => $message['avatar'],
  146. 'ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '',
  147. 'group' => $message['group'],
  148. 'client_id' => $client_id
  149. ];
  150. }while(!self::$global->cas('userList', $userList, $NewUserList));
  151. unset($NewUserList, $userList);
  152. // 维护 UID对应的client_id 数组
  153. do{
  154. $old = $newList = self::$global->uidSimpleList;
  155. $newList[$message['uid']] = [
  156. $client_id,
  157. $message['group']
  158. ];
  159. }while(!self::$global->cas('uidSimpleList', $old, $newList));
  160. unset($old, $newList);
  161. // 写入接入值
  162. $key = date('Ymd') . 'total_in';
  163. self::$global->$key = 0;
  164. do{
  165. $oldKey = date('Ymd', strtotime('-1 day')); // 删除前一天的统计值
  166. unset(self::$global->$oldKey);
  167. }while(!self::$global->increment($key));
  168. unset($key);
  169. }
  170. // 绑定 client_id 和 uid
  171. Gateway::bindUid($client_id, $message['uid']);
  172. // 尝试分配新会员进入服务
  173. self::userOnlineTask($client_id, $message['group']);
  174. break;
  175. // 聊天
  176. case 'chatMessage':
  177. $client = Gateway::getClientIdByUid($message['data']['to_id']);
  178. if(!empty($client)){
  179. $chat_message = [
  180. 'message_type' => 'chatMessage',
  181. 'data' => [
  182. 'name' => $message['data']['from_name'],
  183. 'avatar' => $message['data']['from_avatar'],
  184. 'id' => $message['data']['from_id'],
  185. 'time' => date('H:i'),
  186. 'content' => htmlspecialchars($message['data']['content']),
  187. ]
  188. ];
  189. Gateway::sendToClient($client['0'], json_encode($chat_message));
  190. unset($chat_message);
  191. // 聊天信息入库
  192. $serviceLog = [
  193. 'from_id' => $message['data']['from_id'],
  194. 'from_name' => $message['data']['from_name'],
  195. 'from_avatar' => $message['data']['from_avatar'],
  196. 'to_id' => $message['data']['to_id'],
  197. 'to_name' => $message['data']['to_name'],
  198. 'content' => $message['data']['content'],
  199. 'time_line' => time()
  200. ];
  201. self::$db->insert('ws_chat_log')->cols($serviceLog)->query();
  202. unset($serviceLog);
  203. }
  204. break;
  205. // 转接
  206. case 'changeGroup':
  207. // 通知客户端转接中
  208. $simpleList = self::$global->uidSimpleList;
  209. if(!isset($simpleList[$message['uid']])){ // 客户已经退出
  210. return ;
  211. }
  212. $userClient = $simpleList[$message['uid']]['0'];
  213. $userGroup = $simpleList[$message['uid']]['1']; // 会员原来的分组也是客服的分组
  214. $reLink = [
  215. 'message_type' => 'relinkMessage'
  216. ];
  217. Gateway::sendToClient($userClient, json_encode($reLink));
  218. unset($reLink);
  219. // 记录该客服与该会员的服务结束
  220. self::$db->query("update `ws_service_log` set `end_time` = " . time() . " where `client_id`= '" . $userClient . "'");
  221. // 从当前客服的服务表中删除这个会员
  222. $old = $kfList = self::$global->kfList;
  223. if(!isset($kfList[$userGroup])){
  224. $waitMsg = '暂时没有相关客服上班,请稍后再咨询。';
  225. // 逐一通知
  226. foreach(self::$global->userList as $vo){
  227. $waitMessage = [
  228. 'message_type' => 'wait',
  229. 'data' => [
  230. 'content' => $waitMsg,
  231. ]
  232. ];
  233. Gateway::sendToClient($userClient, json_encode($waitMessage));
  234. unset($waitMessage);
  235. }
  236. return ;
  237. }
  238. $myList = $kfList[$userGroup]; // 该客服分组数组
  239. foreach($myList as $key=>$vo){
  240. if(in_array($userClient, $vo['user_info'])){
  241. // 维护现在的该客服的服务信息
  242. $kfList[$userGroup][$key]['task'] -= 1; // 当前服务的人数 -1
  243. foreach($vo['user_info'] as $k=>$v){
  244. if($userClient == $v){
  245. unset($kfList[$userGroup][$key]['user_info'][$k]);
  246. break;
  247. }
  248. }
  249. break;
  250. }
  251. }
  252. while(!self::$global->cas('kfList', $old, $kfList)){}; // 刷新内存中客服的服务列表
  253. unset($old, $kfList, $myList);
  254. // 将会员加入队列中
  255. $userList = self::$global->userList;
  256. do{
  257. $NewUserList = $userList;
  258. $NewUserList[$message['uid']] = [
  259. 'id' => $message['uid'],
  260. 'name' => $message['name'],
  261. 'avatar' => $message['avatar'],
  262. 'ip' => $message['ip'],
  263. 'group' => $message['group'], // 指定要链接的分组
  264. 'client_id' => $userClient
  265. ];
  266. }while(!self::$global->cas('userList', $userList, $NewUserList));
  267. unset($NewUserList, $userList);
  268. // 执行会员分配通知双方
  269. self::userOnlineTask($userClient, $message['group']);
  270. unset($userClient, $userGroup);
  271. break;
  272. case 'closeUser':
  273. $userInfo = self::$global->uidSimpleList;
  274. if(isset($userInfo[$message['uid']])){
  275. $waitMessage = [
  276. 'message_type' => 'wait',
  277. 'data' => [
  278. 'content' => '暂时没有客服上班,请稍后再咨询。',
  279. ]
  280. ];
  281. Gateway::sendToClient($userInfo[$message['uid']]['0'], json_encode($waitMessage));
  282. unset($waitMessage);
  283. }
  284. unset($userInfo);
  285. break;
  286. }
  287. }
  288. /**
  289. * 当用户断开连接时触发
  290. * @param int $client_id 连接id
  291. *
  292. * tips: 当服务端主动退出的时候,会出现 exit status 9.原因是:服务端主动断开之后,连接的客户端会走这个方法,而短时间内进程
  293. * 需要处理这多的逻辑,又有cas操作,导致进程退出会超时,然后会被内核杀死,从而报出错误 9.实际对真正的业务没有任何的影响。
  294. */
  295. public static function onClose($client_id)
  296. {
  297. print_r([$client_id,self::$global->uidSimpleList,self::$global->userList]);
  298. $isServiceUserOut = false;
  299. // 将会员服务信息,从客服的服务列表中移除
  300. $old = $kfList = self::$global->kfList;
  301. foreach($kfList as $k=>$v){
  302. foreach($v as $key=>$vo){
  303. if(in_array($client_id, $vo['user_info'])){
  304. $isServiceUserOut = true;
  305. // 根据client id 去更新这个会员离线的一些信息
  306. self::$db->query("update `ws_service_log` set `end_time` = " . time() . " where `client_id`= '" . $client_id . "'");
  307. // 从会员的内存表中检索出该会员的信息,并更新内存
  308. $oldSimple = $simpleList = self::$global->uidSimpleList;
  309. $outUser = [];
  310. foreach($simpleList as $u=>$c){
  311. if($c['0'] == $client_id){
  312. $outUser[] = [
  313. 'user_id' => $u,
  314. 'group_id' => $c['1']
  315. ];
  316. unset($simpleList[$u]);
  317. break;
  318. }
  319. }
  320. while(!self::$global->cas('uidSimpleList', $oldSimple, $simpleList)){};
  321. unset($oldSimple, $simpleList);
  322. //$outUser = self::$db->query("select `user_id`,`group_id` from `ws_service_log` where `client_id`= '" . $client_id . "'");
  323. // 通知 客服删除退出的用户
  324. if(!empty($outUser)){
  325. $del_message = [
  326. 'message_type' => 'delUser',
  327. 'data' => [
  328. 'id' => $outUser['0']['user_id']
  329. ]
  330. ];
  331. Gateway::sendToClient($vo['client_id'], json_encode($del_message));
  332. unset($del_message);
  333. // 尝试分配新会员进入服务
  334. self::userOfflineTask($outUser['0']['group_id']);
  335. }
  336. unset($outUser);
  337. // 维护现在的该客服的服务信息
  338. $kfList[$k][$key]['task'] -= 1; // 当前服务的人数 -1
  339. foreach($vo['user_info'] as $m=>$l){
  340. if($client_id == $l){
  341. unset($kfList[$k][$key]['user_info'][$m]);
  342. break;
  343. }
  344. }
  345. // 刷新内存中客服的服务列表
  346. while(!self::$global->cas('kfList', $old, $kfList)){};
  347. unset($old, $kfList);
  348. break;
  349. }
  350. }
  351. if($isServiceUserOut) break;
  352. }
  353. // 尝试从排队的用户中删除退出的客户端
  354. if(false == $isServiceUserOut){
  355. $old = $userList = self::$global->userList;
  356. foreach(self::$global->userList as $key=>$vo){
  357. if($client_id == $vo['client_id']){
  358. $isServiceUserOut = true;
  359. unset($userList[$key]);
  360. break;
  361. }
  362. }
  363. while(!self::$global->cas('userList', $old, $userList)){};
  364. // 从会员的内存表中检索出该会员的信息,并更新内存
  365. $oldSimple = $simpleList = self::$global->uidSimpleList;
  366. foreach($simpleList as $u=>$c){
  367. if($c['0'] == $client_id){
  368. unset($simpleList[$u]);
  369. break;
  370. }
  371. }
  372. while(!self::$global->cas('uidSimpleList', $oldSimple, $simpleList)){};
  373. unset($oldSimple, $simpleList);
  374. }
  375. // 尝试是否是客服退出
  376. if(false == $isServiceUserOut){
  377. $old = $kfList = self::$global->kfList;
  378. foreach(self::$global->kfList as $k=>$v){
  379. foreach($v as $key=>$vo){
  380. // 客服服务列表中无数据,才去删除客服内存信息
  381. if($client_id == $vo['client_id'] && (0 == count($vo['user_info']))){
  382. unset($kfList[$k][$key]);
  383. break;
  384. }
  385. }
  386. }
  387. while(!self::$global->cas('kfList', $old, $kfList)){};
  388. }
  389. }
  390. /**
  391. * 有人退出
  392. * @param $group
  393. */
  394. private static function userOfflineTask($group)
  395. {
  396. // TODO 此处查询最大的可服务人数,后面可以用其他的方式,存储这个数值,让其更高效的访问
  397. $maxNumber = self::getMaxServiceNum();
  398. $res = self::assignmentTask(self::$global->kfList, self::$global->userList, $group, $maxNumber);
  399. unset($maxNumber);
  400. if(1 == $res['code']){
  401. while(!self::$global->cas('kfList', self::$global->kfList, $res['data']['4'])){}; // 更新客服数据
  402. while(!self::$global->cas('userList', self::$global->userList, $res['data']['5'])){}; // 更新会员数据
  403. // 通知会员发送信息绑定客服的id
  404. $noticeUser = [
  405. 'message_type' => 'connect',
  406. 'data' => [
  407. 'kf_id' => $res['data']['0'],
  408. 'kf_name' => $res['data']['1']
  409. ]
  410. ];
  411. Gateway::sendToClient($res['data']['3']['client_id'], json_encode($noticeUser));
  412. unset($noticeUser);
  413. // 通知客服端绑定会员的信息
  414. $noticeKf = [
  415. 'message_type' => 'connect',
  416. 'data' => [
  417. 'user_info' => $res['data']['3']
  418. ]
  419. ];
  420. Gateway::sendToClient($res['data']['2'], json_encode($noticeKf));
  421. unset($noticeKf);
  422. // 逐一通知
  423. $number = 1;
  424. foreach(self::$global->userList as $vo){
  425. $waitMsg = '您前面还有 ' . $number . ' 位会员在等待。';
  426. $waitMessage = [
  427. 'message_type' => 'wait',
  428. 'data' => [
  429. 'content' => $waitMsg,
  430. ]
  431. ];
  432. Gateway::sendToClient($vo['client_id'], json_encode($waitMessage));
  433. $number++;
  434. }
  435. unset($waitMessage, $number);
  436. // 写入接入值
  437. $key = date('Ymd') . 'success_in';
  438. self::$global->$key = 0;
  439. do{
  440. $oldKey = date('Ymd', strtotime('-1 day')); // 删除前一天的统计值
  441. unset(self::$global->$oldKey);
  442. }while(!self::$global->increment($key));
  443. unset($key);
  444. }else{
  445. switch ($res['code']){
  446. case -1:
  447. $waitMsg = '暂时没有客服上班,请稍后再咨询。';
  448. // 逐一通知
  449. foreach(self::$global->userList as $vo){
  450. $waitMessage = [
  451. 'message_type' => 'wait',
  452. 'data' => [
  453. 'content' => $waitMsg,
  454. ]
  455. ];
  456. Gateway::sendToClient($vo['client_id'], json_encode($waitMessage));
  457. }
  458. break;
  459. case -2:
  460. break;
  461. case -3:
  462. break;
  463. case -4:
  464. // 逐一通知
  465. $number = 1;
  466. foreach(self::$global->userList as $vo){
  467. $waitMsg = '您前面还有 ' . $number . ' 位会员在等待。';
  468. $waitMessage = [
  469. 'message_type' => 'wait',
  470. 'data' => [
  471. 'content' => $waitMsg,
  472. ]
  473. ];
  474. Gateway::sendToClient($vo['client_id'], json_encode($waitMessage));
  475. $number++;
  476. }
  477. break;
  478. }
  479. unset($waitMessage, $number);
  480. }
  481. }
  482. /**
  483. * 有人进入执行分配
  484. * @param $client_id
  485. * @param $group
  486. */
  487. private static function userOnlineTask($client_id, $group)
  488. {
  489. // TODO 此处查询最大的可服务人数,后面可以用其他的方式,存储这个数值,让其更高效的访问
  490. $maxNumber = self::getMaxServiceNum();
  491. $res = self::assignmentTask(self::$global->kfList, self::$global->userList, $group, $maxNumber);
  492. unset($maxNumber);
  493. if(1 == $res['code']){
  494. while(!self::$global->cas('kfList', self::$global->kfList, $res['data']['4'])){}; // 更新客服数据
  495. while(!self::$global->cas('userList', self::$global->userList, $res['data']['5'])){}; // 更新会员数据
  496. // 通知会员发送信息绑定客服的id
  497. $noticeUser = [
  498. 'message_type' => 'connect',
  499. 'data' => [
  500. 'kf_id' => $res['data']['0'],
  501. 'kf_name' => $res['data']['1']
  502. ]
  503. ];
  504. Gateway::sendToClient($client_id, json_encode($noticeUser));
  505. unset($noticeUser);
  506. // 检测是否开启自动应答
  507. $sayHello = self::$db->query('select `word`,`status` from `ws_reply` where `id` = 1');
  508. if(!empty($sayHello) && 1 == $sayHello['0']['status']){
  509. $hello = [
  510. 'message_type' => 'helloMessage',
  511. 'data' => [
  512. 'name' => $res['data']['1'],
  513. 'avatar' => '',
  514. 'id' => $res['data']['0'],
  515. 'time' => date('H:i'),
  516. 'content' => htmlspecialchars($sayHello['0']['word'])
  517. ]
  518. ];
  519. Gateway::sendToClient($client_id, json_encode($hello));
  520. unset($hello);
  521. }
  522. unset($sayHello);
  523. // 通知客服端绑定会员的信息
  524. $noticeKf = [
  525. 'message_type' => 'connect',
  526. 'data' => [
  527. 'user_info' => $res['data']['3']
  528. ]
  529. ];
  530. Gateway::sendToClient($res['data']['2'], json_encode($noticeKf));
  531. unset($noticeKf);
  532. // 服务信息入库
  533. $serviceLog = [
  534. 'user_id' => $res['data']['3']['id'],
  535. 'client_id' => $res['data']['3']['client_id'],
  536. 'user_name' => $res['data']['3']['name'],
  537. 'user_ip' => $res['data']['3']['ip'],
  538. 'user_avatar' => $res['data']['3']['avatar'],
  539. 'kf_id' => intval(ltrim($res['data']['0'], 'KF')),
  540. 'start_time' => time(),
  541. 'group_id' => $group,
  542. 'end_time' => 0
  543. ];
  544. self::$db->insert('ws_service_log')->cols($serviceLog)->query();
  545. unset($serviceLog);
  546. // 写入接入值
  547. $key = date('Ymd') . 'success_in';
  548. self::$global->$key = 0;
  549. do{
  550. $oldKey = date('Ymd', strtotime('-1 day')); // 删除前一天的统计值
  551. unset(self::$global->$oldKey);
  552. }while(!self::$global->increment($key));
  553. unset($key);
  554. }else{
  555. $waitMsg = '';
  556. switch ($res['code']){
  557. case -1:
  558. $waitMsg = '暂时没有客服上班,请稍后再咨询。';
  559. break;
  560. case -2:
  561. break;
  562. case -3:
  563. break;
  564. case -4:
  565. $number = count(self::$global->userList);
  566. $waitMsg = '您前面还有 ' . $number . ' 位会员在等待。';
  567. break;
  568. }
  569. $waitMessage = [
  570. 'message_type' => 'wait',
  571. 'data' => [
  572. 'content' => $waitMsg,
  573. ]
  574. ];
  575. Gateway::sendToClient($client_id, json_encode($waitMessage));
  576. unset($waitMessage);
  577. }
  578. }
  579. /**
  580. * 给客服分配会员【均分策略】
  581. * @param $kfList
  582. * @param $userList
  583. * @param $group
  584. * @param $total
  585. */
  586. private static function assignmentTask($kfList, $userList, $group, $total)
  587. {
  588. // 没有客服上线
  589. if(empty($kfList) || empty($kfList[$group])){
  590. return ['code' => -1];
  591. }
  592. // 没有待分配的会员
  593. if(empty($userList)){
  594. return ['code' => -2];
  595. }
  596. // 未设置每个客服可以服务多少人
  597. if(0 == $total){
  598. return ['code' => -3];
  599. }
  600. // 查看该组的客服是否在线
  601. if(!isset($kfList[$group])){
  602. return ['code' => -1];
  603. }
  604. $kf = $kfList[$group];
  605. $user = array_shift($userList);
  606. $kf = array_shift($kf);
  607. $min = $kf['task'];
  608. $flag = $kf['id'];
  609. foreach($kfList[$group] as $key=>$vo){
  610. if($vo['task'] < $min){
  611. $min = $vo['task'];
  612. $flag = $key;
  613. }
  614. }
  615. unset($kf);
  616. // 需要排队了
  617. if($kfList[$group][$flag]['task'] == $total){
  618. return ['code' => -4];
  619. }
  620. $kfList[$group][$flag]['task'] += 1;
  621. array_push($kfList[$group][$flag]['user_info'], $user['client_id']); // 被分配的用户信息
  622. return [
  623. 'code' => 1,
  624. 'data' => [
  625. $kfList[$group][$flag]['id'],
  626. $kfList[$group][$flag]['name'],
  627. $kfList[$group][$flag]['client_id'],
  628. $user,
  629. $kfList,
  630. $userList
  631. ]
  632. ];
  633. }
  634. /**
  635. * 获取最大的服务人数
  636. * @return int
  637. */
  638. private static function getMaxServiceNum()
  639. {
  640. $maxNumber = self::$db->query('select `max_service` from `ws_kf_config` where `id` = 1');
  641. if(!empty($maxNumber)){
  642. $maxNumber = 5;
  643. }else{
  644. $maxNumber = $maxNumber['0']['max_service'];
  645. }
  646. return $maxNumber;
  647. }
  648. /**
  649. * 将内存中的数据写入统计表
  650. * @param int $flag
  651. */
  652. private static function writeLog($flag = 1)
  653. {
  654. // 上午 8点 到 22 点开始统计
  655. if(date('H') < 8 || date('H') > 22){
  656. return ;
  657. }
  658. // 当前正在接入的人 和 在线客服数
  659. $kfList = self::$global->kfList;
  660. $nowTalking = 0;
  661. $onlineKf = 0;
  662. if(!empty($kfList)){
  663. foreach($kfList as $key=>$vo){
  664. $onlineKf += count($vo);
  665. foreach($vo as $k=>$v){
  666. $nowTalking += count($v['user_info']);
  667. }
  668. }
  669. }
  670. // 在队列中的用户
  671. $inQueue = count(self::$global->userList);
  672. $key = date('Ymd') . 'total_in';
  673. $key2 = date('Ymd') . 'success_in';
  674. $param = [
  675. 'is_talking' => $nowTalking,
  676. 'in_queue' => $inQueue,
  677. 'online_kf' => $onlineKf,
  678. 'success_in' => self::$global->$key2,
  679. 'total_in' => self::$global->$key,
  680. 'now_date' => date('Y-m-d')
  681. ];
  682. self::$db->update('ws_now_data')->cols($param)->where('id=1')->query();
  683. if(2 == $flag){
  684. $param = [
  685. 'is_talking' => $nowTalking,
  686. 'in_queue' => $inQueue,
  687. 'online_kf' => $onlineKf,
  688. 'success_in' => self::$global->$key2,
  689. 'total_in' => self::$global->$key,
  690. 'add_date' => date('Y-m-d'),
  691. 'add_hour' => date('H'),
  692. 'add_minute' => date('i'),
  693. ];
  694. self::$db->insert('ws_service_data')->cols($param)->query();
  695. }
  696. unset($kfList, $nowTalking, $inQueue, $onlineKf, $key, $key2, $param);
  697. }
  698. }