Events.php 70 KB

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