Ver Fonte

Merge branch 'master' of http://git.bocai108.com:10180/Ethan/Customer-Service

Jonlin há 6 anos atrás
pai
commit
bc886faa0a

+ 81 - 4
vendor/GatewayWorker_windows/Applications/whisper/Events.php

@@ -192,13 +192,14 @@ class Events
      */
     public static function onMessage($client_id, $message)
     {
-        /*if ($message == '{"type":"ping"}' || $message == '{"type":"pong"}') {
+        if ($message == '{"type":"ping"}' || $message == '{"type":"pong"}') {
             //Gateway::sendToCurrentClient('{"type":"pong"}');
             return;
         } else {
             echo "onMessage: " . $message . "\r\n";
             print_r([self::$global->kfList, self::$global->userList, self::$global->uidSimpleList, self::$global->userToKf]);
-        }*/
+        }
+
         $message = json_decode($message, true);
         if (isset($message['type'])) {
             switch ($message['type']) {
@@ -531,6 +532,7 @@ class Events
     public static function Kfinit($client_id, $message)
     {
         $kfList = self::$global->kfList;
+
         //客服登陆验证 不符合的直接断掉
         //$logcheck = true;     //开发时使用
         $kfinfo = self::KfloginChedk($client_id, $message);
@@ -544,6 +546,12 @@ class Events
             return true;
         }
 
+        if (isset($kfList[$message['group']][$message['uid']])) {
+            Gateway::sendToCurrentClient(json_encode(['type' => 'reLoginErr', 'msg' => '你的账号已经登陆过,请先退出再登陆'], 256));
+            Gateway::closeCurrentClient();
+            return;
+        }
+
         // 如果该客服未在内存中记录则记录
         if (!isset($kfList[$message['group']]) || !array_key_exists($message['uid'], $kfList[$message['group']])) {
             do {
@@ -577,6 +585,8 @@ class Events
         $_SESSION['uid'] = $message['uid'];
         $_SESSION['name'] = $message['name'];
 
+        Gateway::joinGroup($client_id, 'group_' . $message['group']);
+
         self::writeLogKfStatus($message['uid'], 2);
 
         // TODO 尝试拉取用户来服务 [二期规划]
@@ -617,6 +627,15 @@ class Events
             sleep(2);
         }
 
+        $group = $message['group'];
+        $onlinekf = self::$global->kfList;
+        if (!isset($onlinekf[$group]) || count($onlinekf[$group]) <= 0) {
+            Gateway::sendToClient($client_id, json_encode(['message_type' => 'notice', 'content' => '暂时没有客服上班,请稍后再咨询。'], 256));
+            sleep(2);
+            Gateway::closeClient($client_id);
+            return;
+        }
+
         if (!array_key_exists($message['uid'], $userList)) {
             do {
                 $NewUserList = $userList;
@@ -754,18 +773,24 @@ class Events
         }
 
         $userList = $userListNew = self::$global->userList;
+        $group_wait_count = 0;
         if (!empty($userList)) {
             $ischange = 0;
             foreach ($userList as $key => $val) {
+                if ($val['group'] == $kfuid) {
+                    $group_wait_count++;
+                }
                 if ($val['id'] == $uid) {
                     unset($userListNew[$key]);
                     $ischange = 1;
+                    $group_wait_count--;
                     break;
                 }
             }
             if ($ischange) {
                 do {
                 } while (!self::$global->cas('userList', $userList, $userListNew));
+                Gateway::sendToGroup('group_' . $kfgroup, json_encode(['message_type' => 'kfqueuelength', 'leng' => $group_wait_count], 256));
             }
         }
 
@@ -793,6 +818,7 @@ class Events
                             'message_type' => 'userClose',
                             'data' => [
                                 'content' => '用户连接已断开',
+                                'id' => $uid,
                                 'time' => date('H:i'),
                             ]
                         ];
@@ -968,12 +994,42 @@ class Events
             while (!self::$global->cas('userList', self::$global->userList, $res['data']['5'])) {
             }; // 更新会员数据
 
+
+            // 服务信息入库
+            $serviceLog = [
+                'user_id' => $res['data']['3']['id'],
+                'client_id' => $res['data']['3']['client_id'],
+                'user_name' => $res['data']['3']['name'],
+                'user_ip' => $res['data']['3']['ip'],
+                'user_avatar' => $res['data']['3']['avatar'],
+                'kf_id' => intval(ltrim($res['data']['0'], 'KF')),
+                'start_time' => time(),
+                'group_id' => $group,
+                'website' => $res['data']['3']['website'],
+                'system' => $res['data']['3']['system'],
+                'browse' => $res['data']['3']['browse'],
+                'status' => 1,
+                'intime' => $res['data']['3']['intime'],
+                'end_time' => 0
+            ];
+            $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();
+            if (!$hisSession) {
+                $conversationId = self::$db->insert('ws_service_log')->cols($serviceLog)->query();
+            } else {
+                self::$db->update('ws_service_log')->cols(['status' => 1])->where('servicelog_id=' . $hisSession['servicelog_id'])->query();
+                $conversationId = $hisSession['servicelog_id'];
+            }
+            unset($serviceLog);
+
+
             // 通知会员发送信息绑定客服的id
             $noticeUser = [
                 'message_type' => 'connect',
                 'data' => [
                     'kf_id' => $res['data']['0'],
-                    'kf_name' => $res['data']['1']
+                    'kf_name' => $res['data']['1'],
+                    'conversationId' => $conversationId,
+                    'serverInfo' => self::$global->kfList[$group][$res['data']['0']],
                 ]
             ];
             Gateway::sendToClient($res['data']['3']['client_id'], json_encode($noticeUser, 256));
@@ -983,7 +1039,8 @@ class Events
             $noticeKf = [
                 'message_type' => 'connect',
                 'data' => [
-                    'user_info' => $res['data']['3']
+                    'user_info' => $res['data']['3'],
+                    'conversationId' => $conversationId,
                 ]
             ];
             Gateway::sendToClient($res['data']['2'], json_encode($noticeKf, 256));
@@ -1193,6 +1250,18 @@ class Events
             Gateway::sendToClient($client_id, json_encode($waitMessage, 256));
             unset($waitMessage);
         }
+
+        $userlist = self::$global->userList;
+        $waitcount = 0;
+        if ($userlist) {
+            foreach ($userlist as $val) {
+                if ($val['group'] == $group) {
+                    $waitcount++;
+                }
+            }
+            Gateway::sendToGroup('group_' . $group, json_encode(['message_type' => 'kfqueuelength', 'leng' => $waitcount], 256));
+        }
+
     }
 
 
@@ -1282,6 +1351,7 @@ class Events
 
         // 需要排队了
         if ($onlineKF[$group][$flag]['task'] == $total) {
+            array_unshift($userList, $user);
             return ['code' => -4];
         }
 
@@ -1541,6 +1611,13 @@ class Events
     }
 
 
+    //用户下线通知
+    private static function userCloseNotice($client_id, $cuid, $group)
+    {
+
+
+    }
+
     //踢掉同一用户的旧用户
     private static function tickOlduser($uid)
     {