Events.php 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796
  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. self::writeLogKfStatus(0, 0, 0);
  76. // 1分钟统计一次实时数据
  77. Timer::add(60 * 1, function () {
  78. self::writeLog(1);
  79. });
  80. // 40分钟写一次当前日期点数的log数据
  81. Timer::add(60 * 40, function () {
  82. self::writeLog(2);
  83. });
  84. //每1分钟发一次本组排队数
  85. Timer::add(60 * 1, function () {
  86. self::lineup();
  87. });
  88. //初始化.....
  89. self::upsystemconfig();
  90. //每5分钟更新一次系统配置文件
  91. Timer::add(60 * 10, function () {
  92. self::upsystemconfig();
  93. });
  94. // 检查对话时效给出.
  95. Timer::add(60, function () {
  96. self::overTime();
  97. });
  98. // 实时监控.
  99. Timer::add(60, function () {
  100. $adminList = self::$global->adminList ?? [];
  101. self::systemMonitoring($adminList);
  102. });
  103. }
  104. }
  105. /**
  106. * 每分钟定时向客服发送一次排队情况
  107. */
  108. public static function lineup()
  109. {
  110. $userlist = self::$global->userList;
  111. $kflist = self::$global->kfList;
  112. if (empty($userlist) || empty($kflist)) {
  113. return;
  114. }
  115. $return = [];
  116. foreach ($userlist as $val) {
  117. $return[$val['group']] = isset($return[$val['group']]) ? $return[$val['group']] + 1 : 1;
  118. }
  119. $krgroupclientid = [];
  120. foreach ($kflist as $gid => $sval) {
  121. foreach ($sval as $tval) {
  122. $krgroupclientid[$gid][] = $tval['client_id'];
  123. }
  124. }
  125. foreach ($return as $fgroup => $fval) {
  126. foreach ($krgroupclientid as $sikey => $sival) {
  127. if ($fgroup == $sikey) {
  128. Gateway::sendToClient($sival, json_encode(['type' => 'lineupCount', $fval], 256));
  129. }
  130. }
  131. }
  132. return;
  133. }
  134. /**
  135. * 当客户端连接时触发
  136. * 如果业务不需此回调可以删除onConnect
  137. *
  138. * @param int $client_id 连接id
  139. */
  140. public static function onConnect($client_id)
  141. {
  142. // 检测是否开启自动应答
  143. $sayHello = self::$db->query('select `word`,`status` from `ws_reply` where `id` = 1');
  144. if (!empty($sayHello) && 1 == $sayHello['0']['status']) {
  145. $hello = [
  146. 'message_type' => 'helloMessage',
  147. 'data' => [
  148. 'name' => '智能助手',
  149. 'time' => date('H:i'),
  150. 'content' => $sayHello['0']['word']
  151. ]
  152. ];
  153. Gateway::sendToClient($client_id, json_encode($hello, 256));
  154. unset($hello);
  155. }
  156. unset($sayHello);
  157. // 检测是否开启广告
  158. $advertisement = self::$db->query('select `*` from `ws_advertisement` where `advertisement_status` = 1');
  159. if (!empty($advertisement)) {
  160. $chat_message = [
  161. 'message_type' => 'advertisement',
  162. 'data' => $advertisement
  163. ];
  164. Gateway::sendToClient($client_id, json_encode($chat_message, 256));
  165. unset($chat_message);
  166. }
  167. unset($advertisement);
  168. }
  169. /**
  170. * 当客户端发来消息时触发
  171. * @param int $client_id 连接id
  172. * @param mixed $message 具体消息
  173. */
  174. public static function onMessage($client_id, $message)
  175. {
  176. if ($message == '{"type":"ping"}' || $message == '{"type":"pong"}') {
  177. //Gateway::sendToCurrentClient('{"type":"pong"}');
  178. return;
  179. } else {
  180. //echo "onMessage: " . $message . "\r\n";
  181. //print_r([self::$global->kfList, self::$global->userList, self::$global->uidSimpleList, self::$global->userToKf]);
  182. }
  183. $message = json_decode($message, true);
  184. if (isset($message['type'])) {
  185. switch ($message['type']) {
  186. // 管理员初始化
  187. case 'adminInit':
  188. $token = $message['token'];
  189. self::adminInit($client_id, $token);
  190. break;
  191. // 客服初始化
  192. case 'init':
  193. $data = $message['data'];
  194. self::Kfinit($client_id, $data);
  195. break;
  196. // 顾客初始化
  197. case 'userInit';
  198. $data = $message['data'];
  199. self::userInitEnt($client_id, $data);
  200. break;
  201. //在线客服信息
  202. case 'getkfonlines':
  203. Gateway::sendToCurrentClient(json_encode(self::getkfonlines(), 256));
  204. break;
  205. case 'chatMessage':
  206. $client = Gateway::getClientIdByUid($message['data']['to_id']);
  207. if (!empty($client)) {
  208. $chat_message = [
  209. 'message_type' => 'chatMessage',
  210. 'data' => [
  211. 'name' => $message['data']['from_name'],
  212. 'id' => $message['data']['from_id'],
  213. 'time' => date('H:i'),
  214. 'content' => $message['data']['content'],
  215. ]
  216. ];
  217. Gateway::sendToClient($client['0'], json_encode($chat_message));
  218. unset($chat_message);
  219. // 聊天信息入库
  220. $serviceLog = [
  221. 'from_id' => $message['data']['from_id'],
  222. 'from_name' => $message['data']['from_name'],
  223. 'to_id' => $message['data']['to_id'],
  224. 'to_name' => $message['data']['to_name'],
  225. 'content' => $message['data']['content'],
  226. 'servicelog_id' => $message['data']['conversationId'],
  227. 'time_line' => time()
  228. ];
  229. self::$db->insert('ws_chat_log')->cols($serviceLog)->query();
  230. unset($serviceLog);
  231. }
  232. if (isset($message['data']['isFirst']) && $message['data']['isFirst']) {
  233. $servicelog_id = $message['data']['conversationId'];
  234. $serviceLog = self::$db->query("select `start_time` from `ws_service_log` where `servicelog_id`= '$servicelog_id'");
  235. $corresponding = time() - $serviceLog[0]['start_time'];
  236. self::$db->query("update `ws_alarm` set `alarm_corresponding` = '$corresponding',alarm_respond=2 where `servicelog_id`= '$servicelog_id'");
  237. }
  238. break;
  239. // 转接
  240. case 'changeGroup':
  241. // 通知客户端转接中
  242. $simpleList = self::$global->uidSimpleList;
  243. if (!isset($simpleList[$message['uid']])) { // 客户已经退出
  244. return;
  245. }
  246. $userClient = $simpleList[$message['uid']]['0'];
  247. $userGroup = $simpleList[$message['uid']]['1']; // 会员原来的分组也是客服的分组
  248. $reLink = [
  249. 'message_type' => 'relinkMessage'
  250. ];
  251. Gateway::sendToClient($userClient, json_encode($reLink, 256));
  252. unset($reLink);
  253. // 记录该客服与该会员的服务结束
  254. $servicelog_id = $message['data']['conversationId'];
  255. self::$db->query("update `ws_service_log` set `end_time` = " . time() . " , `status` = '2' where `servicelog_id`= '" . $servicelog_id . "'");
  256. // 修改会话时长
  257. $serviceLog = self::$db->query("select `start_time` from `ws_service_log` where `servicelog_id`= '$servicelog_id'");
  258. $cvtOvertime = time() - $serviceLog[0]['start_time'];
  259. self::$db->query("update `ws_alarm` set `alarm_cvtOvertime` = '$cvtOvertime' where `servicelog_id`= '$servicelog_id'");
  260. // 从当前客服的服务表中删除这个会员
  261. $old = $kfList = self::$global->kfList;
  262. if (!isset($kfList[$userGroup])) {
  263. $waitMsg = '暂时没有相关客服上班,请稍后再咨询。';
  264. // 逐一通知
  265. foreach (self::$global->userList as $vo) {
  266. $waitMessage = [
  267. 'message_type' => 'wait',
  268. 'data' => [
  269. 'content' => $waitMsg,
  270. ]
  271. ];
  272. Gateway::sendToClient($userClient, json_encode($waitMessage, 256));
  273. unset($waitMessage);
  274. }
  275. return;
  276. }
  277. $myList = $kfList[$userGroup]; // 该客服分组数组
  278. foreach ($myList as $key => $vo) {
  279. if (in_array($userClient, $vo['user_info'])) {
  280. // 维护现在的该客服的服务信息
  281. $kfList[$userGroup][$key]['task'] -= 1; // 当前服务的人数 -1
  282. foreach ($vo['user_info'] as $k => $v) {
  283. if ($userClient == $v) {
  284. unset($kfList[$userGroup][$key]['user_info'][$k]);
  285. break;
  286. }
  287. }
  288. break;
  289. }
  290. }
  291. while (!self::$global->cas('kfList', $old, $kfList)) {
  292. }; // 刷新内存中客服的服务列表
  293. unset($old, $kfList, $myList);
  294. // 将会员加入队列中
  295. $userList = self::$global->userList;
  296. do {
  297. $NewUserList = $userList;
  298. $NewUserList[$message['uid']] = [
  299. 'id' => $message['uid'],
  300. 'name' => $message['name'],
  301. 'avatar' => $message['avatar'],
  302. 'ip' => $message['ip'],
  303. 'group' => $message['group'], // 指定要链接的分组
  304. 'client_id' => $userClient
  305. ];
  306. } while (!self::$global->cas('userList', $userList, $NewUserList));
  307. unset($NewUserList, $userList);
  308. // 执行会员分配通知双方
  309. self::userOnlineTask($userClient, $message['group']);
  310. unset($userClient, $userGroup);
  311. break;
  312. case 'closeUser':
  313. $userInfo = self::$global->uidSimpleList;
  314. if (isset($userInfo[$message['uid']])) {
  315. $waitMessage = [
  316. 'message_type' => 'wait',
  317. 'data' => [
  318. 'content' => '暂时没有客服上班,请稍后再咨询。',
  319. ]
  320. ];
  321. Gateway::sendToClient($userInfo[$message['uid']]['0'], json_encode($waitMessage, 256));
  322. unset($waitMessage);
  323. }
  324. unset($userInfo);
  325. break;
  326. // 机器人问答.
  327. case 'toRobot':
  328. self::toRobot($client_id, $message);
  329. break;
  330. // 评价.
  331. case 'evaluate':
  332. self::evaluate($client_id, $message);
  333. break;
  334. // 客服关闭会话.
  335. case 'kfCloseUser':
  336. $userId = $message['data']['to_id'];
  337. $client = Gateway::getClientIdByUid($userId);
  338. if (!empty($client)) {
  339. $clientId = $client['0'];
  340. self::serverClose($clientId, $message['data']['conversationId']);
  341. }
  342. break;
  343. // 客服更改状态.
  344. case 'kfOnline':
  345. $kfList = self::$global->kfList;
  346. $userId = $message['data']['uid'];
  347. $status = $message['data']['status'];
  348. foreach ($kfList as $k => $v) {
  349. foreach ($v as $ke => $va) {
  350. if ($ke == $userId) {
  351. $kfList[$k][$ke]['status'] = $status;
  352. }
  353. }
  354. }
  355. self::$global->kfList = $kfList;
  356. self::writeLogKfStatus($userId, $status);
  357. break;
  358. case 'changeOtherhKeFu';
  359. self::changeOtherhKeFu($client_id, $message);
  360. break;
  361. // 弹出评价.
  362. case 'getEvaluate';
  363. $client = Gateway::getClientIdByUid($message['data']['to_id']);
  364. if (!empty($client)) {
  365. $chat_message = [
  366. 'message_type' => 'getEvaluate',
  367. 'data' => [
  368. 'content' => '欢迎你的咨询,请对我们的服务做出评价',
  369. ]
  370. ];
  371. Gateway::sendToClient($client['0'], json_encode($chat_message, 256));
  372. unset($chat_message);
  373. }
  374. }
  375. }
  376. }
  377. //获取在线客服列表
  378. public static function getkfonlines()
  379. {
  380. $return = [
  381. 'message_type' => 'onlinekfs',
  382. 'data' => [],
  383. ];
  384. $nowkfid = $_SESSION['uid'];
  385. $kfs = self::$global->kfList;
  386. $groupnamemap = self::$global->groupmap;
  387. if (!$kfs || empty($nowkfid)) {
  388. return $return;
  389. }
  390. $ret = [];
  391. foreach ($kfs as $gruop => $users) {
  392. foreach ($users as $uid=>$uinfo){
  393. if ($uid != $nowkfid){
  394. $ret[] = ['groupid'=>$gruop,'groupname'=>$groupnamemap[$gruop],'kfuid'=>$uid,'kfname'=>$uinfo['name'],'kfjobname'=>$uinfo['job_name']];
  395. }
  396. }
  397. }
  398. $return['data'] = $ret;
  399. return $return;
  400. }
  401. //客户工单内部组转接
  402. public static function changeOtherhKeFu($client_id, $smessage)
  403. {
  404. $message = $smessage['data'];
  405. $groupid = isset($message['group']) ? intval($message['group']) : 0;
  406. $toukfid = isset($message['toukfuid']) ? $message['toukfuid'] : 0;
  407. $fromkfuid = isset($message['fromkfuid']) ? $message['fromkfuid'] : 0;
  408. $uid = isset($message['uid']) ? $message['uid'] : 0;
  409. $word = isset($message['word']) ? $message['word'] : '';
  410. if (empty($groupid) || empty($toukfid) || empty($fromkfuid) || empty($uid) || empty($word) || ($toukfid == $fromkfuid)) {
  411. return false;
  412. }
  413. if (!Gateway::isUidOnline($toukfid) || !Gateway::isUidOnline($uid)) {
  414. return false;
  415. }
  416. $tokfidclientid = Gateway::getClientIdByUid($toukfid);
  417. $tokfidclientid = $tokfidclientid['0'];
  418. $uidclientid = Gateway::getClientIdByUid($uid);
  419. $uidclientid = $uidclientid['0'];
  420. $kfList = $kfList_new = self::$global->kfList;
  421. $userToKf = $userToKf_new = self::$global->userToKf;
  422. if (!isset($kfList[$groupid]) || !isset($kfList[$groupid][$toukfid]) || !isset($kfList[$groupid][$fromkfuid])) {
  423. return false;
  424. }
  425. if ($kfList[$groupid][$toukfid]['status'] != 1) {
  426. return false;
  427. }
  428. foreach ($kfList[$groupid] as $key => $val) {
  429. if ($key == $fromkfuid) {
  430. $kfList_new[$groupid][$fromkfuid]['task']--;
  431. foreach ($kfList[$groupid][$key]['user_info'] as $skey => $sval) {
  432. if ($sval == $uidclientid) {
  433. unset($kfList_new[$groupid][$key]['user_info'][$skey]);
  434. }
  435. }
  436. }
  437. if ($key == $toukfid) {
  438. $kfList_new[$groupid][$toukfid]['task']++;
  439. array_push($kfList_new[$groupid][$key]['user_info'], $uidclientid);
  440. }
  441. }
  442. do {
  443. } while (!self::$global->cas('kfList', $kfList, $kfList_new));
  444. if (isset($userToKf[$uid])) {
  445. $userToKf_new[$uid]['1'] = $toukfid;
  446. }
  447. do {
  448. } while (!self::$global->cas('userToKf', $userToKf, $userToKf_new));
  449. /////////取消原有会话,开启新会话
  450. $histarttimelimit = time() - 3600 * 24;
  451. $bindval = ['user_id' => $uid, 'client_id' => $uidclientid, 'kf_id' => intval(trim($fromkfuid, 'KF')), 'histime' => $histarttimelimit];
  452. $oldlog = self::$db->select('*')->from('ws_service_log')->where('user_id= :user_id and client_id=:client_id and kf_id=:kf_id and status!=2 and start_time>=:histime ')->bindValues($bindval)->row();
  453. if (!$oldlog) {
  454. return false;
  455. }
  456. self::$db->update('ws_service_log')->cols(['status' => 2, 'end_time' => time()])->where('servicelog_id=' . $oldlog['servicelog_id'])->query();
  457. unset($oldlog['servicelog_id']);
  458. // 修改会话时长
  459. $servicelog_id = $oldlog['servicelog_id'];
  460. $serviceLog = self::$db->query("select `start_time` from `ws_service_log` where `servicelog_id`= '$servicelog_id'");
  461. $cvtOvertime = time() - $serviceLog[0]['start_time'];
  462. self::$db->query("update `ws_alarm` set `alarm_cvtOvertime` = '$cvtOvertime' where `servicelog_id`= '$servicelog_id'");
  463. $oldlog = array_merge($oldlog, ['kf_id' => intval(trim($toukfid, 'KF')), 'start_time' => time(), 'end_time' => 0, 'status' => 1, 'evaluate_id' => 0]);
  464. $new_id = self::$db->insert('ws_service_log')->cols($oldlog)->query();
  465. if (!$new_id) {
  466. return false;
  467. }
  468. ///通知消息发送--------------
  469. // 通知会员发送信息绑定客服的id
  470. $noticeUser = [
  471. 'message_type' => 'connect',
  472. 'data' => [
  473. 'kf_id' => $toukfid,
  474. 'conversationId' => $new_id,
  475. 'kf_name' => Gateway::getSession(Gateway::getClientIdByUid($toukfid)['0'])['name'],
  476. 'serverInfo' => self::$global->kfList[$groupid][$toukfid],
  477. ]
  478. ];
  479. Gateway::sendToClient($uidclientid, json_encode($noticeUser, 256));
  480. unset($noticeUser);
  481. // 通知客服端绑定会员的信息
  482. $noticeKf = [
  483. 'message_type' => 'connect',
  484. 'data' => [
  485. 'user_info' => $uid,
  486. 'conversationId' => $new_id,
  487. ]
  488. ];
  489. Gateway::sendToClient($tokfidclientid, json_encode($noticeKf, 256));
  490. unset($noticeKf);
  491. //回转接人,转接成功
  492. Gateway::sendToCurrentClient(json_encode(['message_type' => 'trunconnect', 'data' => ['status' => 1], 256]));
  493. return;
  494. }
  495. //客服接入sock,及初始化
  496. public static function Kfinit($client_id, $message)
  497. {
  498. $kfList = self::$global->kfList;
  499. //客服登陆验证 不符合的直接断掉
  500. //$logcheck = true; //开发时使用
  501. $kfinfo = self::KfloginChedk($client_id, $message);
  502. if (empty($kfinfo)) {
  503. Gateway::sendToClient($client_id, json_encode(["message_type" => 'checkfalse', 'data' => "验证失败"], 256));
  504. Gateway::closeCurrentClient();
  505. return true;
  506. } elseif ($kfinfo['status'] != 1) {
  507. Gateway::sendToClient($client_id, json_encode(["message_type" => 'checkfalse', 'data' => "禁用中..."], 256));
  508. Gateway::closeCurrentClient();
  509. return true;
  510. }
  511. if (isset($kfList[$message['group']][$message['uid']])) {
  512. Gateway::sendToCurrentClient(json_encode(['message_type' => 'reLoginErr', 'msg' => '你的账号已经登陆过,请先退出再登陆'], 256));
  513. Gateway::closeCurrentClient();
  514. return;
  515. }
  516. // 如果该客服未在内存中记录则记录
  517. if (!isset($kfList[$message['group']]) || !array_key_exists($message['uid'], $kfList[$message['group']])) {
  518. do {
  519. $newKfList = $kfList;
  520. $newKfList[$message['group']][$message['uid']] = [
  521. 'id' => 'KF' . $kfinfo['id'],
  522. 'name' => $kfinfo['user_name'],
  523. 'job_name' => $kfinfo['user_job_number'],
  524. 'avatar' => $kfinfo['user_avatar'],
  525. 'client_id' => $client_id,
  526. 'task' => 0,
  527. 'signature' => $kfinfo['signature'],
  528. 'status' => 2,// 1为在线(接收分配、接收消息)2为隐身(不接收分配、只接收消息)3、休息
  529. 'user_info' => []
  530. ];
  531. } while (!self::$global->cas('kfList', $kfList, $newKfList));
  532. unset($newKfList, $kfList);
  533. } else if (isset($kfList[$message['group']][$message['uid']])) {
  534. do {
  535. $newKfList = $kfList;
  536. $newKfList[$message['group']][$message['uid']]['client_id'] = $client_id;
  537. } while (!self::$global->cas('kfList', $kfList, $newKfList));
  538. unset($newKfList, $kfList);
  539. }
  540. // 绑定 client_id 和 uid
  541. Gateway::bindUid($client_id, $message['uid']);
  542. $_SESSION['group'] = $message['group'];
  543. $_SESSION['iskefu'] = 1;
  544. $_SESSION['uid'] = $message['uid'];
  545. $_SESSION['name'] = $message['name'];
  546. Gateway::joinGroup($client_id, 'group_' . $message['group']);
  547. $chat_message = [
  548. 'message_type' => 'loginSuccess',
  549. ];
  550. Gateway::sendToClient($client_id, json_encode($chat_message, 256));
  551. unset($chat_message);
  552. self::writeLogKfStatus($message['uid'], 2);
  553. // TODO 尝试拉取用户来服务 [二期规划]
  554. }
  555. /**
  556. * 管理员
  557. * @param $client_id 服务ID
  558. * @param $message 数据
  559. */
  560. public static function adminInit($client_id, $token)
  561. {
  562. // 查询token是否存在.
  563. $systemConfigData = self::$db->query("SELECT `id` FROM `ws_admins` where `token`= '$token'");
  564. //print_r(self::$global->adminList);
  565. if ($systemConfigData) {
  566. $adminList = self::$global->adminList;
  567. $adminList[] = $client_id;
  568. self::$global->adminList = $adminList;
  569. self::systemMonitoring([$client_id]);
  570. } else {
  571. Gateway::closeClient($client_id);
  572. }
  573. }
  574. //客服登陆验证
  575. public static function KfloginChedk($client, $messageArray)
  576. {
  577. $uid = isset($messageArray['uid']) ? $messageArray['uid'] : '';
  578. $token = isset($messageArray['token']) ? $messageArray['token'] : '';
  579. if (empty($uid) || empty($token)) {
  580. return false;
  581. }
  582. $expire_time_vali = time() - 60 * 60 * 24 * 3;
  583. $kfid = intval(substr($uid, 2));
  584. $ret = self::$db->select('*')->from('ws_users')->where('id= :id and token=:token and expire_time>=:expire_time')->bindValues(array('id' => $kfid, 'token' => $token, 'expire_time' => $expire_time_vali))->row();
  585. if ($ret) {
  586. self::$db->update('ws_users')->cols(array('online_status' => 1, 'online_connectid' => $client))->where('id=' . $kfid)->query();
  587. return $ret;
  588. }
  589. return false;
  590. }
  591. //用户发送邦定用户事件
  592. public static function userInitEnt($client_id, $message)
  593. {
  594. $userList = self::$global->userList;
  595. // 如果该顾客未在内存中记录则记录
  596. $uidSimpleList = self::$global->uidSimpleList;
  597. if (isset($uidSimpleList[$message['uid']])) {
  598. $uidSimpleList = self::$global->uidSimpleList;
  599. $oldclientid = $uidSimpleList[$message['uid']]['0'];
  600. Gateway::sendToClient($oldclientid, json_encode(['type' => 'reLoginErr', 'msg' => '相同账号登陆,本次退出'], 256));
  601. Gateway::closeClient($oldclientid);
  602. sleep(2);
  603. }
  604. $group = $message['group'];
  605. $onlinekf = self::$global->kfList;
  606. if (!isset($onlinekf[$group]) || count($onlinekf[$group]) <= 0) {
  607. Gateway::sendToClient($client_id, json_encode(['message_type' => 'notice', 'content' => '暂时没有客服上班,请稍后再咨询。'], 256));
  608. sleep(2);
  609. Gateway::closeClient($client_id);
  610. return;
  611. }
  612. if (!array_key_exists($message['uid'], $userList)) {
  613. do {
  614. $NewUserList = $userList;
  615. $NewUserList[$message['uid']] = [
  616. 'id' => $message['uid'],
  617. 'name' => $message['name'],
  618. 'avatar' => $message['avatar'],
  619. 'website' => $_SESSION['origin'],//$_SERVER['HTTP_ORIGIN'],
  620. 'browse' => Gateway::browse_info(),
  621. 'system' => Gateway::get_os(),
  622. 'ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '',
  623. 'group' => $message['group'],
  624. 'intime' => time(),
  625. 'client_id' => $client_id
  626. ];
  627. } while (!self::$global->cas('userList', $userList, $NewUserList));
  628. unset($NewUserList, $userList);
  629. // 维护 UID对应的client_id 数组
  630. do {
  631. $old = $newList = self::$global->uidSimpleList;
  632. $newList[$message['uid']] = [
  633. $client_id,
  634. $message['group']
  635. ];
  636. } while (!self::$global->cas('uidSimpleList', $old, $newList));
  637. unset($old, $newList);
  638. // 写入接入值
  639. $key = date('Ymd') . 'total_in';
  640. self::$global->$key = 0;
  641. do {
  642. $oldKey = date('Ymd', strtotime('-1 day')); // 删除前一天的统计值
  643. unset(self::$global->$oldKey);
  644. } while (!self::$global->increment($key));
  645. unset($key);
  646. }
  647. // 绑定 client_id 和 uid
  648. Gateway::bindUid($client_id, $message['uid']);
  649. $_SESSION['iskefu'] = 0;
  650. $_SESSION['uid'] = $message['uid'];
  651. // 尝试分配新会员进入服务
  652. self::userOnlineTask($client_id, $message['group'], $message['uid']);
  653. }
  654. /**
  655. * 当用户断开连接时触发
  656. * @param int $client_id 连接id
  657. *
  658. * tips: 当服务端主动退出的时候,会出现 exit status 9.原因是:服务端主动断开之后,连接的客户端会走这个方法,而短时间内进程
  659. * 需要处理这多的逻辑,又有cas操作,导致进程退出会超时,然后会被内核杀死,从而报出错误 9.实际对真正的业务没有任何的影响。
  660. */
  661. public static function onClose($client_id)
  662. {
  663. $isKefuoff = isset($_SESSION['iskefu']) ? $_SESSION['iskefu'] : 0;
  664. $uid = isset($_SESSION['uid']) ? $_SESSION['uid'] : false;
  665. //echo "下线:$uid - $client_id - $isKefuoff \n";
  666. $adminList = self::$global->adminList ?? [];
  667. $key = array_search($client_id ,$adminList);
  668. if (strlen($key)) {
  669. array_splice($adminList,$key,1);
  670. self::$global->adminList = $adminList;
  671. }
  672. if (empty($uid)) {
  673. return;
  674. }
  675. if ($isKefuoff) {
  676. self::serviceOffline($client_id, $uid);
  677. } else {
  678. self::guestOffline($client_id, $uid);
  679. }
  680. return;
  681. }
  682. //客服下线了
  683. public static function serviceOffline($client_id, $uid)
  684. {
  685. $group = $_SESSION['group'];
  686. $kefuinfo_old = $kefuinfo_old_new = self::$global->kfList;
  687. $user_info = $kefuinfo_old_new[$group][$uid]['user_info'];
  688. $simpliUsers = self::$global->uidSimpleList;
  689. $simpliUsersID_UID_Arr = [];
  690. if (!empty($simpliUsers)) {
  691. foreach ($simpliUsers as $key => $val) {
  692. $simpliUsersID_UID_Arr[$val['0']] = $key;
  693. }
  694. }
  695. $now = time();
  696. if (!empty($user_info)) {
  697. foreach ($user_info as $val) {
  698. Gateway::sendToClient($val, json_encode(['type' => 'serviceoffline', 'msg' => '客户人员下线!'], 256));
  699. if (isset($simpliUsersID_UID_Arr[$val])) {
  700. self::$db->query("update `ws_service_log` set `status` = '2',end_time=$now where `user_id`= '$simpliUsersID_UID_Arr[$val]' and kf_id='$uid' and group_id=$group and `status`!=2 ");
  701. // 修改会话时长
  702. $serviceLog = self::$db->query("select `start_time`,`servicelog_id` from `ws_service_log` where `user_id`= '$simpliUsersID_UID_Arr[$val]' and kf_id='$uid' and group_id=$group and `status`!=2");
  703. $servicelog_id = $serviceLog[0]['servicelog_id'];
  704. $cvtOvertime = time() - $serviceLog[0]['start_time'];
  705. self::$db->query("update `ws_alarm` set `alarm_cvtOvertime` = '$cvtOvertime' where `servicelog_id`= '$servicelog_id'");
  706. }
  707. Gateway::closeClient($val);
  708. }
  709. }
  710. unset($kefuinfo_old_new[$group][$uid]);
  711. $kfid = intval(substr($uid, 2));
  712. self::$db->update('ws_users')->cols(array('online_status' => 0, 'online_connectid' => ''))->where('id=' . $kfid)->query();
  713. do {
  714. } while (!self::$global->cas('kfList', $kefuinfo_old, $kefuinfo_old_new));
  715. self::writeLogKfStatus($uid, 0);
  716. return;
  717. }
  718. //用户下线了
  719. public static function guestOffline($client_id, $uid)
  720. {
  721. $kfuid = -1;
  722. $krclient_id = 0;
  723. $kfgroup = -1;
  724. $userToKf = $userToKfNew = self::$global->userToKf;
  725. if (isset($userToKfNew[$uid])) {
  726. $kfuid = isset($userToKfNew[$uid]['1']) ? $userToKfNew[$uid]['1'] : -1;
  727. $krclient_id = isset(Gateway::getClientIdByUid($kfuid)['0']) ? Gateway::getClientIdByUid($kfuid)['0'] : 0;
  728. unset($userToKfNew[$uid]);
  729. do {
  730. } while (!self::$global->cas('userToKf', $userToKf, $userToKfNew));
  731. }
  732. $uidSimpleList = $uidSimpleListNew = self::$global->uidSimpleList;
  733. if (isset($uidSimpleListNew[$uid])) {
  734. $kfgroup = $uidSimpleListNew[$uid]['1'];
  735. unset($uidSimpleListNew[$uid]);
  736. do {
  737. } while (!self::$global->cas('uidSimpleList', $uidSimpleList, $uidSimpleListNew));
  738. }
  739. $userList = $userListNew = self::$global->userList;
  740. $group_wait_count = 0;
  741. if (!empty($userList)) {
  742. $ischange = 0;
  743. foreach ($userList as $key => $val) {
  744. if ($val['group'] == $kfuid) {
  745. $group_wait_count++;
  746. }
  747. if ($val['id'] == $uid) {
  748. unset($userListNew[$key]);
  749. $ischange = 1;
  750. $group_wait_count--;
  751. break;
  752. }
  753. }
  754. if ($ischange) {
  755. do {
  756. } while (!self::$global->cas('userList', $userList, $userListNew));
  757. Gateway::sendToGroup('group_' . $kfgroup, json_encode(['message_type' => 'kfqueuelength', 'leng' => $group_wait_count], 256));
  758. }
  759. }
  760. if ($kfuid != -1 && $kfgroup != -1) {
  761. $kefuinfo_old = $kefuinfo_old_new = self::$global->kfList;
  762. $ischange_kf_list = 0;
  763. if (isset($kefuinfo_old[$kfgroup][$kfuid])) {
  764. $infos = $kefuinfo_old[$kfgroup][$kfuid]['user_info'];
  765. if ($infos) {
  766. if (is_array($infos)) {
  767. foreach ($infos as $key => $val) {
  768. if ($val == $client_id) {
  769. $ischange_kf_list = 1;
  770. unset($kefuinfo_old_new[$kfgroup][$kfuid]['user_info'][$key]);
  771. $kefuinfo_old_new[$kfgroup][$kfuid]['task'] = $kefuinfo_old_new[$kfgroup][$kfuid]['task'] - 1;
  772. }
  773. }
  774. }
  775. if ($ischange_kf_list) {
  776. do {
  777. } while (!self::$global->cas('kfList', $kefuinfo_old, $kefuinfo_old_new));
  778. $chat_message = [
  779. 'message_type' => 'userClose',
  780. 'data' => [
  781. 'content' => '用户连接已断开',
  782. 'id' => $uid,
  783. 'time' => date('H:i'),
  784. ]
  785. ];
  786. $now = time();
  787. $kf__uid = substr($kfuid, 2);
  788. $sql = "update `ws_service_log` set `status` = '3',end_time=$now where `user_id`= '$uid' and kf_id='$kf__uid' and group_id=$kfgroup and status=1 ";
  789. //echo "客户退出:". $sql ."\n";
  790. self::$db->query($sql);
  791. Gateway::sendToClient($krclient_id, json_encode($chat_message, 256));
  792. }
  793. }
  794. }
  795. }
  796. }
  797. /**
  798. * 客服结束会话
  799. * @param int $client_id 连接id
  800. *
  801. * tips: 当服务端主动退出的时候,会出现 exit status 9.原因是:服务端主动断开之后,连接的客户端会走这个方法,而短时间内进程
  802. * 需要处理这多的逻辑,又有cas操作,导致进程退出会超时,然后会被内核杀死,从而报出错误 9.实际对真正的业务没有任何的影响。
  803. */
  804. public static function serverClose($client_id, $servicelog_id)
  805. {
  806. // 返回.
  807. $chat_message = [
  808. 'message_type' => 'closeBysever',
  809. 'data' => [
  810. 'content' => '客服停止了该会话',
  811. 'time' => date('H:i'),
  812. ]
  813. ];
  814. Gateway::sendToClient($client_id, json_encode($chat_message, 256));
  815. Gateway::closeClient($client_id);
  816. $now = time();
  817. $sql = "update `ws_service_log` set `status`='2',end_time=$now where `servicelog_id`= '$servicelog_id'";
  818. //echo "客户退出:". $sql ."\n";
  819. self::$db->query($sql);
  820. $isServiceUserOut = false;
  821. // 修改会话时长
  822. $serviceLog = self::$db->query("select `start_time` from `ws_service_log` where `servicelog_id`= '$servicelog_id'");
  823. $cvtOvertime = time() - $serviceLog[0]['start_time'];
  824. self::$db->query("update `ws_alarm` set `alarm_cvtOvertime` = '$cvtOvertime' where `servicelog_id`= '$servicelog_id'");
  825. // 将会员服务信息,从客服的服务列表中移除
  826. $old = $kfList = self::$global->kfList;
  827. foreach ($kfList as $k => $v) {
  828. foreach ($v as $key => $vo) {
  829. if (in_array($client_id, $vo['user_info'])) {
  830. $isServiceUserOut = true;
  831. // 根据client id 去更新会话工单一些信息
  832. self::$db->query("update `ws_service_log` set `end_time` = " . time() . " , `status` = '2' where `client_id`= '" . $client_id . "'");
  833. // 从会员的内存表中检索出该会员的信息,并更新内存
  834. $oldSimple = $simpleList = self::$global->uidSimpleList;
  835. $outUser = [];
  836. foreach ($simpleList as $u => $c) {
  837. if ($c['0'] == $client_id) {
  838. $outUser[] = [
  839. 'user_id' => $u,
  840. 'group_id' => $c['1']
  841. ];
  842. unset($simpleList[$u]);
  843. break;
  844. }
  845. }
  846. while (!self::$global->cas('uidSimpleList', $oldSimple, $simpleList)) {
  847. };
  848. unset($oldSimple, $simpleList);
  849. $outUser = self::$db->query("select `user_id`,`group_id` from `ws_service_log` where `client_id`= '" . $client_id . "'");
  850. // 通知 客服删除退出的用户
  851. if (!empty($outUser)) {
  852. $del_message = [
  853. 'message_type' => 'delUser',
  854. 'data' => [
  855. 'id' => $outUser['0']['user_id']
  856. ]
  857. ];
  858. Gateway::sendToClient($vo['client_id'], json_encode($del_message, 256));
  859. unset($del_message);
  860. // 尝试分配新会员进入服务
  861. self::userOfflineTask($outUser['0']['group_id']);
  862. }
  863. unset($outUser);
  864. // 维护现在的该客服的服务信息
  865. $kfList[$k][$key]['task'] -= 1; // 当前服务的人数 -1
  866. foreach ($vo['user_info'] as $m => $l) {
  867. if ($client_id == $l) {
  868. unset($kfList[$k][$key]['user_info'][$m]);
  869. break;
  870. }
  871. }
  872. // 刷新内存中客服的服务列表
  873. while (!self::$global->cas('kfList', $old, $kfList)) {
  874. };
  875. unset($old, $kfList);
  876. break;
  877. }
  878. }
  879. if ($isServiceUserOut) break;
  880. }
  881. // 尝试从排队的用户中删除退出的客户端
  882. if (false == $isServiceUserOut) {
  883. $old = $userList = self::$global->userList;
  884. foreach (self::$global->userList as $key => $vo) {
  885. if ($client_id == $vo['client_id']) {
  886. $isServiceUserOut = true;
  887. unset($userList[$key]);
  888. break;
  889. }
  890. }
  891. while (!self::$global->cas('userList', $old, $userList)) {
  892. };
  893. // 从会员的内存表中检索出该会员的信息,并更新内存
  894. $oldSimple = $simpleList = self::$global->uidSimpleList;
  895. foreach ($simpleList as $u => $c) {
  896. if ($c['0'] == $client_id) {
  897. unset($simpleList[$u]);
  898. break;
  899. }
  900. }
  901. while (!self::$global->cas('uidSimpleList', $oldSimple, $simpleList)) {
  902. };
  903. unset($oldSimple, $simpleList);
  904. }
  905. // 尝试是否是客服退出
  906. if (false == $isServiceUserOut) {
  907. $old = $kfList = self::$global->kfList;
  908. foreach (self::$global->kfList as $k => $v) {
  909. foreach ($v as $key => $vo) {
  910. // 客服服务列表中无数据,才去删除客服内存信息
  911. if ($client_id == $vo['client_id'] && (0 == count($vo['user_info']))) {
  912. unset($kfList[$k][$key]);
  913. break;
  914. }
  915. }
  916. }
  917. while (!self::$global->cas('kfList', $old, $kfList)) {
  918. };
  919. }
  920. }
  921. /**
  922. * 有人退出
  923. * @param $group
  924. */
  925. private static function userOfflineTask($group)
  926. {
  927. // TODO 此处查询最大的可服务人数,后面可以用其他的方式,存储这个数值,让其更高效的访问
  928. $maxNumber = self::getMaxServiceNum();
  929. $res = self::assignmentTask(self::$global->kfList, self::$global->userList, $group, $maxNumber);
  930. unset($maxNumber);
  931. if (1 == $res['code']) {
  932. while (!self::$global->cas('kfList', self::$global->kfList, $res['data']['4'])) {
  933. }; // 更新客服数据
  934. while (!self::$global->cas('userList', self::$global->userList, $res['data']['5'])) {
  935. }; // 更新会员数据
  936. // 服务信息入库
  937. $serviceLog = [
  938. 'user_id' => $res['data']['3']['id'],
  939. 'client_id' => $res['data']['3']['client_id'],
  940. 'user_name' => $res['data']['3']['name'],
  941. 'user_ip' => $res['data']['3']['ip'],
  942. 'user_avatar' => $res['data']['3']['avatar'],
  943. 'kf_id' => intval(ltrim($res['data']['0'], 'KF')),
  944. 'start_time' => time(),
  945. 'group_id' => $group,
  946. 'website' => $res['data']['3']['website'],
  947. 'system' => $res['data']['3']['system'],
  948. 'browse' => $res['data']['3']['browse'],
  949. 'status' => 1,
  950. 'intime' => $res['data']['3']['intime'],
  951. 'end_time' => 0
  952. ];
  953. $hisSession = self::$db->select('*')->from('ws_service_log')->where('user_id=:user_id and kf_id=:kf_id and group_id=:group_id and status in (1,3)')->bindValues(array('user_id' => $res['data']['3']['id'], 'kf_id' => intval(ltrim($res['data']['0'], 'KF')), 'group_id' => $group))->row();
  954. if (!$hisSession) {
  955. $conversationId = self::$db->insert('ws_service_log')->cols($serviceLog)->query();
  956. } else {
  957. self::$db->update('ws_service_log')->cols(['status' => 1])->where('servicelog_id=' . $hisSession['servicelog_id'])->query();
  958. $conversationId = $hisSession['servicelog_id'];
  959. }
  960. unset($serviceLog);
  961. // 通知会员发送信息绑定客服的id
  962. $noticeUser = [
  963. 'message_type' => 'connect',
  964. 'data' => [
  965. 'kf_id' => $res['data']['0'],
  966. 'kf_name' => $res['data']['1'],
  967. 'conversationId' => $conversationId,
  968. 'serverInfo' => self::$global->kfList[$group][$res['data']['0']],
  969. ]
  970. ];
  971. Gateway::sendToClient($res['data']['3']['client_id'], json_encode($noticeUser, 256));
  972. unset($noticeUser);
  973. // 通知客服端绑定会员的信息
  974. $noticeKf = [
  975. 'message_type' => 'connect',
  976. 'data' => [
  977. 'user_info' => $res['data']['3'],
  978. 'conversationId' => $conversationId,
  979. ]
  980. ];
  981. Gateway::sendToClient($res['data']['2'], json_encode($noticeKf, 256));
  982. unset($noticeKf);
  983. // 逐一通知
  984. $number = 1;
  985. foreach (self::$global->userList as $vo) {
  986. $waitMsg = '您前面还有 ' . $number . ' 位会员在等待。';
  987. $waitMessage = [
  988. 'message_type' => 'wait',
  989. 'data' => [
  990. 'content' => $waitMsg,
  991. ]
  992. ];
  993. Gateway::sendToClient($vo['client_id'], json_encode($waitMessage, 256));
  994. $number++;
  995. }
  996. unset($waitMessage, $number);
  997. // 写入接入值
  998. $key = date('Ymd') . 'success_in';
  999. self::$global->$key = 0;
  1000. do {
  1001. $oldKey = date('Ymd', strtotime('-1 day')); // 删除前一天的统计值
  1002. unset(self::$global->$oldKey);
  1003. } while (!self::$global->increment($key));
  1004. unset($key);
  1005. } else {
  1006. switch ($res['code']) {
  1007. case -1:
  1008. $waitMsg = '暂时没有客服上班,请稍后再咨询。';
  1009. // 逐一通知
  1010. foreach (self::$global->userList as $vo) {
  1011. $waitMessage = [
  1012. 'message_type' => 'wait',
  1013. 'data' => [
  1014. 'content' => $waitMsg,
  1015. ]
  1016. ];
  1017. Gateway::sendToClient($vo['client_id'], json_encode($waitMessage, 256));
  1018. }
  1019. break;
  1020. case -2:
  1021. break;
  1022. case -3:
  1023. break;
  1024. case -4:
  1025. // 逐一通知
  1026. $number = 1;
  1027. foreach (self::$global->userList as $vo) {
  1028. $waitMsg = '您前面还有 ' . $number . ' 位会员在等待。';
  1029. $waitMessage = [
  1030. 'message_type' => 'wait',
  1031. 'data' => [
  1032. 'content' => $waitMsg,
  1033. ]
  1034. ];
  1035. Gateway::sendToClient($vo['client_id'], json_encode($waitMessage, 256));
  1036. $number++;
  1037. }
  1038. break;
  1039. }
  1040. unset($waitMessage, $number);
  1041. }
  1042. }
  1043. /**
  1044. * 有人进入执行分配
  1045. * @param $client_id
  1046. * @param $group
  1047. * @param $uid
  1048. */
  1049. private static function userOnlineTask($client_id, $group, $uid = 0)
  1050. {
  1051. // TODO 此处查询最大的可服务人数,后面可以用其他的方式,存储这个数值,让其更高效的访问
  1052. $maxNumber = self::getMaxServiceNum();
  1053. $res = self::assignmentTask(self::$global->kfList, self::$global->userList, $group, $maxNumber, $uid);
  1054. unset($maxNumber);
  1055. if (1 == $res['code']) {
  1056. while (!self::$global->cas('kfList', self::$global->kfList, $res['data']['4'])) {
  1057. }; // 更新客服数据
  1058. while (!self::$global->cas('userList', self::$global->userList, $res['data']['5'])) {
  1059. }; // 更新会员数据
  1060. $userToKf = self::$global->userToKf;
  1061. $userToKf[$res['data']['3']['id']] = [
  1062. $res['data']['3']['id'],
  1063. $res['data']['0']
  1064. ];
  1065. self::$global->userToKf = $userToKf;
  1066. // 服务信息入库
  1067. $serviceLog = [
  1068. 'user_id' => $res['data']['3']['id'],
  1069. 'client_id' => $res['data']['3']['client_id'],
  1070. 'user_name' => $res['data']['3']['name'],
  1071. 'user_ip' => $res['data']['3']['ip'],
  1072. 'user_avatar' => $res['data']['3']['avatar'],
  1073. 'kf_id' => intval(ltrim($res['data']['0'], 'KF')),
  1074. 'start_time' => time(),
  1075. 'group_id' => $group,
  1076. 'website' => $res['data']['3']['website'],
  1077. 'system' => $res['data']['3']['system'],
  1078. 'browse' => $res['data']['3']['browse'],
  1079. 'status' => 1,
  1080. 'intime' => $res['data']['3']['intime'],
  1081. 'end_time' => 0
  1082. ];
  1083. $hisSession = self::$db->select('*')->from('ws_service_log')->where('user_id=:user_id and kf_id=:kf_id and group_id=:group_id and status in (1,3)')->bindValues(array('user_id' => $res['data']['3']['id'], 'kf_id' => intval(ltrim($res['data']['0'], 'KF')), 'group_id' => $group))->row();
  1084. if (!$hisSession) {
  1085. $conversationId = self::$db->insert('ws_service_log')->cols($serviceLog)->query();
  1086. $alarmData = [
  1087. 'servicelog_id' => $conversationId,
  1088. ];
  1089. self::$db->insert('ws_alarm')->cols($alarmData)->query();
  1090. } else {
  1091. self::$db->update('ws_service_log')->cols(['status' => 1])->where('servicelog_id=' . $hisSession['servicelog_id'])->query();
  1092. $conversationId = $hisSession['servicelog_id'];
  1093. }
  1094. unset($serviceLog);
  1095. // 通知会员发送信息绑定客服的id
  1096. $noticeUser = [
  1097. 'message_type' => 'connect',
  1098. 'data' => [
  1099. 'kf_id' => $res['data']['0'],
  1100. 'conversationId' => $conversationId,
  1101. 'serverInfo' => self::$global->kfList[$group][$res['data']['0']],
  1102. 'kf_name' => $res['data']['1']
  1103. ]
  1104. ];
  1105. Gateway::sendToClient($client_id, json_encode($noticeUser, 256));
  1106. unset($noticeUser);
  1107. // 发送客服欢迎语
  1108. $sayHello = self::$db->query('select `word`,`status` from `ws_reply` where `id` = 2');
  1109. if (!empty($sayHello) && 1 == $sayHello['0']['status']) {
  1110. $chat_message = [
  1111. 'message_type' => 'chatMessage',
  1112. 'data' => [
  1113. 'name' => $res['data']['1'],
  1114. //'avatar' => self::$global->kfList[$group][$res['data']['0']],
  1115. 'id' => $res['data']['0'],
  1116. 'time' => date('H:i'),
  1117. 'content' => $sayHello['0']['word']
  1118. ]
  1119. ];
  1120. Gateway::sendToClient($client_id, json_encode($chat_message, 256));
  1121. unset($chat_message);
  1122. }
  1123. unset($sayHello);
  1124. // 通知客服端绑定会员的信息
  1125. $noticeKf = [
  1126. 'message_type' => 'connect',
  1127. 'data' => [
  1128. 'user_info' => $res['data']['3'],
  1129. 'conversationId' => $conversationId,
  1130. ]
  1131. ];
  1132. Gateway::sendToClient($res['data']['2'], json_encode($noticeKf, 256));
  1133. unset($noticeKf);
  1134. // 写入接入值
  1135. $key = date('Ymd') . 'success_in';
  1136. self::$global->$key = 0;
  1137. do {
  1138. $oldKey = date('Ymd', strtotime('-1 day')); // 删除前一天的统计值
  1139. unset(self::$global->$oldKey);
  1140. } while (!self::$global->increment($key));
  1141. unset($key);
  1142. } else {
  1143. $waitMsg = '';
  1144. switch ($res['code']) {
  1145. case -1:
  1146. $waitMsg = '暂时没有客服上班,请稍后再咨询。';
  1147. break;
  1148. case -2:
  1149. break;
  1150. case -3:
  1151. break;
  1152. case -4:
  1153. $number = count(self::$global->userList);
  1154. $waitMsg = '您前面还有 ' . $number . ' 位会员在等待。';
  1155. break;
  1156. }
  1157. $waitMessage = [
  1158. 'message_type' => 'wait',
  1159. 'data' => [
  1160. 'content' => $waitMsg,
  1161. ]
  1162. ];
  1163. Gateway::sendToClient($client_id, json_encode($waitMessage, 256));
  1164. unset($waitMessage);
  1165. }
  1166. $userlist = self::$global->userList;
  1167. $waitcount = 0;
  1168. if ($userlist) {
  1169. foreach ($userlist as $val) {
  1170. if ($val['group'] == $group) {
  1171. $waitcount++;
  1172. }
  1173. }
  1174. Gateway::sendToGroup('group_' . $group, json_encode(['message_type' => 'kfqueuelength', 'leng' => $waitcount], 256));
  1175. }
  1176. }
  1177. /**
  1178. * 给客服分配会员【均分策略】
  1179. * @param $kfList
  1180. * @param $userList
  1181. * @param $group
  1182. * @param $total
  1183. */
  1184. private static function assignmentTask($kfList, $userList, $group, $total, $uid = 0)
  1185. {
  1186. // 注:修改为已上线(status为1上线status为2不接受分配)
  1187. $onlineKF = [];
  1188. foreach ($kfList as $k => $v) {
  1189. foreach ($v as $ke => $va) {
  1190. if ($va['status'] == 1) {
  1191. $onlineKF[$k][$ke] = $va;
  1192. }
  1193. }
  1194. }
  1195. // 没有客服上线
  1196. if (empty($onlineKF) || empty($onlineKF[$group])) {
  1197. return ['code' => -1];
  1198. }
  1199. // 没有待分配的会员
  1200. if (empty($userList)) {
  1201. return ['code' => -2];
  1202. }
  1203. // 未设置每个客服可以服务多少人
  1204. if (0 == $total) {
  1205. return ['code' => -3];
  1206. }
  1207. // 查看该组的客服是否在线
  1208. if (!isset($onlineKF[$group])) {
  1209. return ['code' => -1];
  1210. }
  1211. //上次用户掉线后,还可以继续上一次 (如果没有关闭) 的会话 --1
  1212. $odltalksession = false;
  1213. $user = $user_first = array_shift($userList);
  1214. if ($uid > 0 && $user['id'] != $uid && count($userList) > 1) {
  1215. $timevalielimit = time() - 60 * 5;
  1216. $odltalksession = self::$db->select('*')->from('ws_service_log')->where('user_id=:uid and `group`=:group and `status`=3 and end_time>=:timevalielimit"')->bindValues(array('uid' => $uid, 'group' => $group, 'timevalielimit' => $timevalielimit))->row();
  1217. if ($odltalksession) {
  1218. foreach ($userList as $ttkey => $ttval) {
  1219. if ($ttval['id'] == $uid) {
  1220. array_unshift($userList, $user);
  1221. $user = $userList[$ttkey];
  1222. unset($userList[$ttkey]);
  1223. break;
  1224. }
  1225. }
  1226. }
  1227. }
  1228. //上次用户掉线后,还可以继续上一次 (如果没有关闭) 的会话 --2
  1229. if ($odltalksession) {
  1230. $oldkrid = 'KF' . $odltalksession['kf_id'];
  1231. if (isset($onlineKF[$group][$oldkrid])) {
  1232. $kf = $onlineKF[$group][$oldkrid];
  1233. $min = $kf['task'];
  1234. $flag = $kf['id'];
  1235. unset($onlineKF[$group][$oldkrid]);
  1236. } else {
  1237. goto NOSIGNKF;
  1238. }
  1239. } else {
  1240. NOSIGNKF:
  1241. $kf = $onlineKF[$group];
  1242. $kf = array_shift($kf);
  1243. $min = $kf['task'];
  1244. $flag = $kf['id'];
  1245. foreach ($onlineKF[$group] as $key => $vo) {
  1246. if ($vo['task'] < $min) {
  1247. $min = $vo['task'];
  1248. $flag = $key;
  1249. }
  1250. }
  1251. unset($kf);
  1252. }
  1253. // 需要排队了
  1254. if ($onlineKF[$group][$flag]['task'] == $total) {
  1255. array_unshift($userList, $user);
  1256. return ['code' => -4];
  1257. }
  1258. $kfList[$group][$flag]['task'] += 1;
  1259. array_push($kfList[$group][$flag]['user_info'], $user['client_id']); // 被分配的用户信息
  1260. return [
  1261. 'code' => 1,
  1262. 'data' => [
  1263. $onlineKF[$group][$flag]['id'],
  1264. $onlineKF[$group][$flag]['name'],
  1265. $onlineKF[$group][$flag]['client_id'],
  1266. $user,
  1267. $kfList,
  1268. $userList
  1269. ]
  1270. ];
  1271. }
  1272. /**
  1273. * 获取最大的服务人数
  1274. * @return int
  1275. */
  1276. private static function getMaxServiceNum()
  1277. {
  1278. $maxNumber = self::$db->query('select `max_service` from `ws_kf_config` where `id` = 1');
  1279. if (!empty($maxNumber)) {
  1280. $maxNumber = 5;
  1281. } else {
  1282. $maxNumber = $maxNumber['0']['max_service'];
  1283. }
  1284. return $maxNumber;
  1285. }
  1286. /**
  1287. * 将内存中的数据写入统计表
  1288. * @param int $flag
  1289. */
  1290. private static function writeLog($flag = 1)
  1291. {
  1292. // 上午 8点 到 22 点开始统计
  1293. if (date('H') < 8 || date('H') > 22) {
  1294. return;
  1295. }
  1296. // 当前正在接入的人 和 在线客服数
  1297. $kfList = self::$global->kfList;
  1298. $nowTalking = 0;
  1299. $onlineKf = 0;
  1300. if (!empty($kfList)) {
  1301. foreach ($kfList as $key => $vo) {
  1302. $onlineKf += count($vo);
  1303. foreach ($vo as $k => $v) {
  1304. $nowTalking += count($v['user_info']);
  1305. }
  1306. }
  1307. }
  1308. // 在队列中的用户
  1309. $inQueue = count(self::$global->userList);
  1310. $key = date('Ymd') . 'total_in';
  1311. $key2 = date('Ymd') . 'success_in';
  1312. $param = [
  1313. 'is_talking' => $nowTalking,
  1314. 'in_queue' => $inQueue,
  1315. 'online_kf' => $onlineKf,
  1316. 'success_in' => self::$global->$key2,
  1317. 'total_in' => self::$global->$key,
  1318. 'now_date' => date('Y-m-d')
  1319. ];
  1320. self::$db->update('ws_now_data')->cols($param)->where('id=1')->query();
  1321. if (2 == $flag) {
  1322. $param = [
  1323. 'is_talking' => $nowTalking,
  1324. 'in_queue' => $inQueue,
  1325. 'online_kf' => $onlineKf,
  1326. 'success_in' => self::$global->$key2,
  1327. 'total_in' => self::$global->$key,
  1328. 'add_date' => date('Y-m-d'),
  1329. 'add_hour' => date('H'),
  1330. 'add_minute' => date('i'),
  1331. ];
  1332. self::$db->insert('ws_service_data')->cols($param)->query();
  1333. }
  1334. unset($kfList, $nowTalking, $inQueue, $onlineKf, $key, $key2, $param);
  1335. }
  1336. /**
  1337. * 机器人问答
  1338. * @param $client_id 服务ID
  1339. * @param $message 数据
  1340. */
  1341. private static function toRobot($client_id, $message)
  1342. {
  1343. $groups_id = $message['data']['groups_id'];
  1344. $robot_name = $message['data']['robot_name'];
  1345. $robotgroups_id = $message['data']['robotgroups_id'];
  1346. // 查询问题.
  1347. $getRobot = self::$db->query("select `robot_content` from `ws_robot` where `robot_status`= 1 and `groups_id`= '" . $groups_id . "' and `robot_name`= '" . $robot_name . "' and `robotgroups_id`= '" . $robotgroups_id . "'");
  1348. $chat_message = [
  1349. 'message_type' => 'robotMessage',
  1350. //'message_type' => 'chatMessage',
  1351. 'data' => [
  1352. 'name' => '智能助手',
  1353. 'time' => date('H:i'),
  1354. 'content' => $getRobot ? $getRobot[0]['robot_content'] : 'error',
  1355. ]
  1356. ];
  1357. sleep(1);
  1358. Gateway::sendToClient($client_id, json_encode($chat_message, 256));
  1359. }
  1360. /**
  1361. * 评价
  1362. * @param $client_id 服务ID
  1363. * @param $message 数据
  1364. */
  1365. private static function evaluate($client_id, $message)
  1366. {
  1367. // 修改数据库.
  1368. $evaluate_id = $message['data']['evaluate_id'];
  1369. $result = self::$db->query("UPDATE `ws_service_log` SET `evaluate_id` = '" . $evaluate_id . "' WHERE `client_id`='" . $client_id . "'");
  1370. if ($result) {
  1371. $chat_message = [
  1372. 'message_type' => 'evaluate',
  1373. 'data' => [
  1374. 'status' => 1,
  1375. 'time' => date('H:i'),
  1376. ]
  1377. ];
  1378. } else {
  1379. $chat_message = [
  1380. 'message_type' => 'evaluate',
  1381. 'data' => [
  1382. 'status' => 2,
  1383. 'time' => date('H:i'),
  1384. ]
  1385. ];
  1386. }
  1387. Gateway::sendToClient($client_id, json_encode($chat_message, 256));
  1388. }
  1389. //获取系统配置
  1390. private static function upsystemconfig()
  1391. {
  1392. $systemConfigData = self::$db->query("SELECT * FROM `ws_systemconfig`");
  1393. $arr = [];
  1394. if ($systemConfigData) {
  1395. foreach ($systemConfigData as $item) {
  1396. $arr[$item['systemconfig_enName']] = $item;
  1397. }
  1398. self::$global->systemconfig = $arr;
  1399. }
  1400. $group = self::$db->query("SELECT * FROM `ws_groups`");
  1401. $arr = [];
  1402. if ($group){
  1403. foreach ($group as $val){
  1404. $arr[$val['id']] = $val['name'];
  1405. }
  1406. self::$global->groupmap = $arr;
  1407. }
  1408. }
  1409. /**
  1410. * 超时
  1411. * @param $client_id 服务ID
  1412. * @param $message 数据
  1413. */
  1414. private static function overTime()
  1415. {
  1416. // 查询对话时效设置.
  1417. $systemConfigData = self::$db->query("SELECT `systemconfig_data`,`systemconfig_enName`,`systemconfig_content` FROM `ws_systemconfig`");
  1418. foreach ($systemConfigData as $k => $v) {
  1419. if ($v['systemconfig_enName'] == 'overtime') {
  1420. self::$global->overtime = $v;
  1421. } elseif ($v['systemconfig_enName'] == 'unoperated') {
  1422. self::$global->unoperated = $v;
  1423. } elseif ($v['systemconfig_enName'] == 'noResponse') {
  1424. self::$global->noResponse = $v;
  1425. }
  1426. }
  1427. // 查询未断开的工单.
  1428. $serviceLog = self::$db->query("SELECT `servicelog_id`,`client_id`,`start_time` FROM `ws_service_log` WHERE `status`='1' OR `status`='3'");
  1429. $whereOr = '1=0';
  1430. foreach ($serviceLog as $k => $v) {
  1431. if ($k == 0) {
  1432. $whereOr = "`servicelog_id`=" . $v['servicelog_id'];
  1433. } else {
  1434. $whereOr .= " OR `servicelog_id`=" . $v['servicelog_id'];
  1435. }
  1436. }
  1437. // 查询最后一次会话.
  1438. //$chatLog = self::$db->query("SELECT `servicelog_id`,MAX(`time_line`) FROM `ws_chat_log` WHERE ".$whereOr." group by `servicelog_id`");
  1439. $chatLog = self::$db->query("
  1440. select * from ws_chat_log as a where time_line=(
  1441. select max(b.time_line) from ws_chat_log as b where a.servicelog_id = b.servicelog_id and (" . $whereOr . ") group by servicelog_id
  1442. )
  1443. ");
  1444. $setOvertime = strtotime('-' . (self::$global->overtime['systemconfig_data'] - 60) . ' second');
  1445. $overtime = strtotime('-' . (self::$global->overtime['systemconfig_data']) . ' second');
  1446. $setUnoperated = strtotime('-' . (self::$global->unoperated['systemconfig_data'] - 60) . ' second');
  1447. $unoperated = strtotime('-' . (self::$global->unoperated['systemconfig_data']) . ' second');
  1448. $noResponse = strtotime('-' . (self::$global->noResponse['systemconfig_data']) . ' second');
  1449. foreach ($serviceLog as $k => $v) {//注意该循环时间
  1450. if (!strlen(array_search($v['servicelog_id'], array_column($chatLog, 'servicelog_id')))) {
  1451. // 如果小于设定时间则关闭会话.
  1452. if ($v['start_time'] <= $unoperated) {
  1453. self::serverClose($v['client_id'], $v['servicelog_id']);
  1454. // 如果小于设定时间前一分钟则给出提示.
  1455. } elseif ($v['start_time'] <= $setUnoperated) {
  1456. $chat_message = [
  1457. 'message_type' => 'overtime',
  1458. 'data' => [
  1459. 'content' => self::$global->unoperated['systemconfig_content'],
  1460. ]
  1461. ];
  1462. Gateway::sendToClient($v['client_id'], json_encode($chat_message, 256));
  1463. }
  1464. }
  1465. }
  1466. // 循环检测会话时效.
  1467. foreach ($chatLog as $k => $v) {
  1468. $toWho = substr($v['to_id'], 0, 2);
  1469. // 如果对话为客服的最后一次对话且时间小于设定时间则结束工单.
  1470. if ($v['time_line'] <= $overtime) {
  1471. $found_key = array_search($v['servicelog_id'], array_column($serviceLog, 'servicelog_id'));
  1472. self::serverClose($serviceLog[$found_key]['client_id'], $v['servicelog_id']);
  1473. // 如果对话为客服的最后一次对话且时间小于设定时间前一分钟则给出提示.
  1474. } elseif ($v['time_line'] <= $setOvertime) {
  1475. $chat_message = [
  1476. 'message_type' => 'overtime',
  1477. 'data' => [
  1478. 'content' => self::$global->overtime['systemconfig_content'],
  1479. ]
  1480. ];
  1481. $found_key = array_search($v['servicelog_id'], array_column($serviceLog, 'servicelog_id'));
  1482. Gateway::sendToClient($serviceLog[$found_key]['client_id'], json_encode($chat_message, 256));
  1483. }
  1484. }
  1485. }
  1486. /**
  1487. * 系统监控
  1488. * @param $message 数据
  1489. */
  1490. private static function systemMonitoring($adminList)
  1491. {
  1492. // 查询未结束工单.
  1493. $serviceLog = self::$db->query("select ws_service_log.servicelog_id,ws_users.user_name as server_name,ws_service_log.user_name,kf_id,start_time,end_time,ws_service_log.group_id,evaluate_id,intime,ws_service_log.status,alarm_userSensitive,alarm_serverSensitive,alarm_corresponding
  1494. from `ws_service_log`
  1495. join `ws_alarm` on ws_service_log.servicelog_id=ws_alarm.servicelog_id
  1496. join `ws_users` on ws_service_log.kf_id=ws_users.id
  1497. WHERE ws_service_log.status='1' OR ws_service_log.status='3'");
  1498. // 查询系统设置表.
  1499. $systemconfig = self::$db->query("SELECT `systemconfig_data` FROM `ws_systemconfig` WHERE `systemconfig_enName`='verifyReturnTime' or `systemconfig_enName`='verifyAllTime'");
  1500. $found_key = array_search('verifyReturnTime', array_column($systemconfig, 'systemconfig_enName'));
  1501. // 质检会话响应时长.
  1502. $verifyReturnTime = $systemconfig[$found_key]['systemconfig_data'];
  1503. $found_key = array_search('verifyAllTime', array_column($systemconfig, 'systemconfig_enName'));
  1504. // 质检会话时长.
  1505. $verifyAllTime = $systemconfig[$found_key]['systemconfig_data'];
  1506. // 差评次数.
  1507. $evaluateCount = 0;
  1508. // 未结束工单id.
  1509. $servicelog_ids = '';
  1510. $overtimeNumber = 0; // 会话超时次数.
  1511. $overtimeTime = []; // 会话超时时间.
  1512. $userSensitive = 0; // 用户敏感词报警次数.
  1513. $serverSensitive = 0; // 客服敏感词报警次数.
  1514. $csdNumber = 0; // 响应超时次数.
  1515. $csdTime = []; // 响应超时时间.
  1516. foreach ($serviceLog as $k => $v) {
  1517. // 工单报警总次数.
  1518. $allCount = 0;
  1519. // 差评次数.
  1520. if ($v['evaluate_id'] == 3) {
  1521. $evaluateCount++;
  1522. $allCount++;
  1523. }
  1524. $duration = time() - $v['start_time'];
  1525. // 会话超时.
  1526. if ($duration > $verifyAllTime) {
  1527. $overtimeNumber++;
  1528. $allCount++;
  1529. $overtimeTime[] = $duration;
  1530. }
  1531. // 敏感词报警.
  1532. $userSensitive += $v['alarm_userSensitive'];
  1533. $allCount += $v['alarm_userSensitive'];
  1534. $serverSensitive += $v['alarm_serverSensitive'];
  1535. $allCount += $v['alarm_serverSensitive'];
  1536. // 响应超时.
  1537. if ($v['alarm_corresponding'] > $verifyReturnTime) {
  1538. $csdTime[] = $v['alarm_corresponding'];
  1539. $csdNumber++;
  1540. $allCount++;
  1541. }
  1542. $serviceLog[$k]['allCount'] = $allCount;
  1543. }
  1544. // 查询对话时效设置.
  1545. foreach ($adminList as $v) {
  1546. $chat_message = [
  1547. 'message_type' => 'monitor',
  1548. 'data' => [
  1549. 'cvtList' => $serviceLog,
  1550. 'userSensitive' => $userSensitive,
  1551. 'serverSensitive' => $serverSensitive,
  1552. 'csdNumber' => $csdNumber,
  1553. 'csdTime' => $csdTime,
  1554. 'overtimeNumber' => $overtimeNumber,
  1555. 'overtimeTime' => $overtimeTime,
  1556. 'evaluateCount' => $evaluateCount,
  1557. ]
  1558. ];
  1559. Gateway::sendToClient($v, json_encode($chat_message, 256));
  1560. }
  1561. }
  1562. //客服在线状态写组
  1563. private static function writeLogKfStatus($kf, $status, $flag = 1)
  1564. {
  1565. if ($flag == 1) {
  1566. $status = intval($status);
  1567. if ($status == 0) {
  1568. self::$db->delete('ws_kfonline')->where("uid='$kf'")->query();
  1569. } else {
  1570. $now = date('Y-m-d H:i;s');
  1571. $sql = "insert into ws_kfonline(uid,status,uptime) values('$kf',$status,'$now') ON DUPLICATE KEY UPDATE status=$status,uptime='$now' ";
  1572. self::$db->query($sql);
  1573. }
  1574. } else {
  1575. self::$db->query("delete from ws_kfonline ");
  1576. }
  1577. }
  1578. //用户下线通知
  1579. private static function userCloseNotice($client_id, $cuid, $group)
  1580. {
  1581. }
  1582. //踢掉同一用户的旧用户
  1583. private static function tickOlduser($uid)
  1584. {
  1585. }
  1586. }