Events.php 29 KB

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