vali 6 年之前
父節點
當前提交
aa19a471c3
共有 1 個文件被更改,包括 98 次插入51 次删除
  1. 98 51
      vendor/GatewayWorker_windows/Applications/whisper/Events.php

+ 98 - 51
vendor/GatewayWorker_windows/Applications/whisper/Events.php

@@ -98,7 +98,22 @@ class Events
      */
     public static function onConnect($client_id)
     {
+        // 检测是否开启自动应答
+        $sayHello = self::$db->query('select `word`,`status` from `ws_reply` where `id` = 1');
+        if(!empty($sayHello) && 1 == $sayHello['0']['status']){
 
+            $hello = [
+                'message_type' => 'helloMessage',
+                'data' => [
+                    'name' => '智能助手',
+                    'time' => date('H:i'),
+                    'content' => htmlspecialchars($sayHello['0']['word'])
+                ]
+            ];
+            Gateway::sendToClient($client_id, json_encode($hello));
+            unset($hello);
+        }
+        unset($sayHello);
     }
 
     /**
@@ -108,8 +123,7 @@ class Events
      */
     public static function onMessage($client_id, $message)
     {
-
-        if ($message=='{"type":"ping"}'){
+         if ($message=='{"type":"ping"}'){
             Gateway::sendToCurrentClient('{"type":"pong"}');
             return;
         }else{
@@ -202,7 +216,6 @@ class Events
                 break;
             // 聊天
             case 'chatMessage':
-
                 $client = Gateway::getClientIdByUid($message['data']['to_id']);
                 if(!empty($client)){
                     $chat_message = [
@@ -226,6 +239,7 @@ class Events
                         'to_id' => $message['data']['to_id'],
                         'to_name' => $message['data']['to_name'],
                         'content' => $message['data']['content'],
+                        'servicelog_id' => $message['data']['conversationId'],
                         'time_line' => time()
                     ];
 
@@ -252,7 +266,7 @@ class Events
                 unset($reLink);
 
                 // 记录该客服与该会员的服务结束
-                self::$db->query("update `ws_service_log` set `end_time` = " . time() . " where `client_id`= '" . $userClient . "'");
+                self::$db->query("update `ws_service_log` set `end_time` = " . time() . " , `status` = '2' where `client_id`= '" . $userClient . "'");
 
                 // 从当前客服的服务表中删除这个会员
                 $old = $kfList = self::$global->kfList;
@@ -335,11 +349,11 @@ class Events
             case 'evaluate':
                 self::evaluate($client_id, $message);
                 break;
-            // 关闭会话.
-            case 'closeUser':
+            // 客服关闭会话.
+            case 'kfCloseUser':
                 $client = Gateway::getClientIdByUid($message['data']['to_id']);
                 if(!empty($client)){
-                    self::onClose($client['0']);
+                    self::serverClose($client['0']);
                 }
                 break;
         }
@@ -354,8 +368,51 @@ class Events
      */
     public static function onClose($client_id)
     {
-        print_r([$client_id,self::$global->uidSimpleList,self::$global->userList]);
+        $uidSimpleList = self::$global->uidSimpleList;
+        $userId = '';
+        foreach ($uidSimpleList as $k => $v) {
+            if ($v[0] == $client_id) {
+                $userId = $k;
+            }
+        }
+        $kfId = '';
+        if ($userId) {
+            $userToKf = self::$global->userToKf;
+            $kfId = $userToKf[$userId][1];
+        }
+        if ($kfId) {
+            $client = Gateway::getClientIdByUid($kfId);
+            // 返回.
+            $chat_message = [
+                'message_type' => 'userClose',
+                'data' => [
+                    'content' => '用户连接已断开',
+                    'time' => date('H:i'),
+                ]
+            ];
+            self::$db->query("update `ws_service_log` set `status` = '3' where `client_id`= '" . $client_id . "'");
+            Gateway::sendToClient($client['0'], json_encode($chat_message));
+        }
+    }
 
+    /**
+     * 客服结束会话
+     * @param int $client_id 连接id
+     *
+     * tips: 当服务端主动退出的时候,会出现 exit status 9.原因是:服务端主动断开之后,连接的客户端会走这个方法,而短时间内进程
+     * 需要处理这多的逻辑,又有cas操作,导致进程退出会超时,然后会被内核杀死,从而报出错误 9.实际对真正的业务没有任何的影响。
+     */
+    public static function serverClose($client_id)
+    {
+        // 返回.
+        $chat_message = [
+            'message_type' => 'closeBysever',
+            'data' => [
+                'content' => '客服停止了该会话',
+                'time' => date('H:i'),
+            ]
+        ];
+        Gateway::sendToClient($client_id, json_encode($chat_message));
         $isServiceUserOut = false;
         // 将会员服务信息,从客服的服务列表中移除
         $old = $kfList = self::$global->kfList;
@@ -365,7 +422,7 @@ class Events
 
                     $isServiceUserOut = true;
 
-                    // 根据client id 去更新这个会员离线的一些信息
+                    // 根据client id 去更新会话工单一些信息
                     self::$db->query("update `ws_service_log` set `end_time` = " . time() . " , `status` = '2' where `client_id`= '" . $client_id . "'");
 
                     // 从会员的内存表中检索出该会员的信息,并更新内存
@@ -384,7 +441,7 @@ class Events
                     while(!self::$global->cas('uidSimpleList', $oldSimple, $simpleList)){};
                     unset($oldSimple, $simpleList);
 
-                    //$outUser = self::$db->query("select `user_id`,`group_id` from `ws_service_log` where `client_id`= '" . $client_id . "'");
+                    $outUser = self::$db->query("select `user_id`,`group_id` from `ws_service_log` where `client_id`= '" . $client_id . "'");
                     // 通知 客服删除退出的用户
                     if(!empty($outUser)){
                         $del_message = [
@@ -591,46 +648,12 @@ class Events
 
             while(!self::$global->cas('kfList', self::$global->kfList, $res['data']['4'])){}; // 更新客服数据
             while(!self::$global->cas('userList', self::$global->userList, $res['data']['5'])){}; // 更新会员数据
-
-            // 通知会员发送信息绑定客服的id
-            $noticeUser = [
-                'message_type' => 'connect',
-                'data' => [
-                    'kf_id' => $res['data']['0'],
-                    'kf_name' => $res['data']['1']
-                ]
-            ];
-            Gateway::sendToClient($client_id, json_encode($noticeUser));
-            unset($noticeUser);
-
-            // 检测是否开启自动应答
-            $sayHello = self::$db->query('select `word`,`status` from `ws_reply` where `id` = 1');
-            if(!empty($sayHello) && 1 == $sayHello['0']['status']){
-
-                $hello = [
-                    'message_type' => 'helloMessage',
-                    'data' => [
-                        'name' => $res['data']['1'],
-                        'avatar' => '',
-                        'id' => $res['data']['0'],
-                        'time' => date('H:i'),
-                        'content' => htmlspecialchars($sayHello['0']['word'])
-                    ]
-                ];
-                Gateway::sendToClient($client_id, json_encode($hello));
-                unset($hello);
-            }
-            unset($sayHello);
-
-            // 通知客服端绑定会员的信息
-            $noticeKf = [
-                'message_type' => 'connect',
-                'data' => [
-                    'user_info' => $res['data']['3']
-                ]
+            $userToKf = self::$global->userToKf;
+            $userToKf[$res['data']['3']['id']] = [
+                $res['data']['3']['id'],
+                $res['data']['0']
             ];
-            Gateway::sendToClient($res['data']['2'], json_encode($noticeKf));
-            unset($noticeKf);
+            self::$global->userToKf = $userToKf;
 
             // 服务信息入库
             $serviceLog = [
@@ -649,9 +672,33 @@ class Events
                 'end_time' => 0
             ];
 
-            self::$db->insert('ws_service_log')->cols($serviceLog)->query();
+            $conversationId = self::$db->insert('ws_service_log')->cols($serviceLog)->query();
             unset($serviceLog);
 
+
+            // 通知会员发送信息绑定客服的id
+            $noticeUser = [
+                'message_type' => 'connect',
+                'data' => [
+                    'kf_id' => $res['data']['0'],
+                    'conversationId' => $conversationId,
+                    'kf_name' => $res['data']['1']
+                ]
+            ];
+            Gateway::sendToClient($client_id, json_encode($noticeUser));
+            unset($noticeUser);
+
+            // 通知客服端绑定会员的信息
+            $noticeKf = [
+                'message_type' => 'connect',
+                'data' => [
+                    'user_info' => $res['data']['3'],
+                    'conversationId' => $conversationId,
+                ]
+            ];
+            Gateway::sendToClient($res['data']['2'], json_encode($noticeKf));
+            unset($noticeKf);
+
             // 写入接入值
             $key = date('Ymd') . 'success_in';
             self::$global->$key = 0;
@@ -861,7 +908,7 @@ class Events
      */
     private static function evaluate($client_id, $message)
     {
-        // 查询问题.
+        // 修改数据库.
         $evaluate_id = $message['data']['evaluate_id'];
         $result = self::$db->query("UPDATE `ws_service_log` SET `evaluate_id` = '" . $evaluate_id . "' WHERE `client_id`='" . $client_id . "'");
         if ($result) {