vali hace 6 años
padre
commit
9a94b5a8ec
Se han modificado 1 ficheros con 121 adiciones y 24 borrados
  1. 121 24
      vendor/GatewayWorker_windows/Applications/whisper/Events.php

+ 121 - 24
vendor/GatewayWorker_windows/Applications/whisper/Events.php

@@ -128,7 +128,7 @@ class Events
             Gateway::sendToCurrentClient('{"type":"pong"}');
             return;
         } else {
-            echo "onMessage: " . $message . "\r\n";
+            //echo "onMessage: " . $message . "\r\n";
             //print_r([self::$global->kfList, self::$global->userList, self::$global->uidSimpleList, self::$global->userToKf]);
         }
 
@@ -163,6 +163,10 @@ class Events
 
                 // 绑定 client_id 和 uid
                 Gateway::bindUid($client_id, $message['uid']);
+                $_SESSION['group'] = $message['group'];
+                $_SESSION['iskefu'] = 1;
+                $_SESSION['uid'] = $message['uid'];
+
                 // TODO 尝试拉取用户来服务 [二期规划]
 
                 break;
@@ -170,6 +174,15 @@ class Events
             case 'userInit';
                 $userList = self::$global->userList;
                 // 如果该顾客未在内存中记录则记录
+                if (array_key_exists($message['uid'], $userList)) {
+                    $uidSimpleList = self::$global->uidSimpleList;
+                    $oldclientid = $uidSimpleList[$message['uid']]['0'];
+                    echo "存在旧用户,我被下线..." . $message['uid'] . "...\n";
+                    Gateway::sendToUid($oldclientid, json_encode(['type' => 'reLoginErr', 'msg' => '相同账号登陆,本次退出'], true));
+                    Gateway::closeClient($oldclientid);
+                }
+
+
                 if (!array_key_exists($message['uid'], $userList)) {
                     do {
                         $NewUserList = $userList;
@@ -211,6 +224,8 @@ class Events
 
                 // 绑定 client_id 和 uid
                 Gateway::bindUid($client_id, $message['uid']);
+                $_SESSION['iskefu'] = 0;
+                $_SESSION['uid'] = $message['uid'];
 
                 // 尝试分配新会员进入服务
                 self::userOnlineTask($client_id, $message['group']);
@@ -282,7 +297,7 @@ class Events
                                 'content' => $waitMsg,
                             ]
                         ];
-                        Gateway::sendToClient($userClient, json_encode($waitMessage));
+                        Gateway::sendToClient($userClient, json_encode($waitMessage, 256));
                         unset($waitMessage);
                     }
                     return;
@@ -338,7 +353,7 @@ class Events
                             'content' => '暂时没有客服上班,请稍后再咨询。',
                         ]
                     ];
-                    Gateway::sendToClient($userInfo[$message['uid']]['0'], json_encode($waitMessage));
+                    Gateway::sendToClient($userInfo[$message['uid']]['0'], json_encode($waitMessage, 256));
                     unset($waitMessage);
                 }
                 unset($userInfo);
@@ -370,33 +385,115 @@ class Events
      */
     public static function onClose($client_id)
     {
-        $uidSimpleList = self::$global->uidSimpleList;
-        $userId = '';
-        foreach ($uidSimpleList as $k => $v) {
-            if ($v[0] == $client_id) {
-                $userId = $k;
+        $isKefuoff = $_SESSION['iskefu'];
+        $uid = $_SESSION['uid'];
+        if (empty($uid)) {
+            return;
+        }
+
+        if ($isKefuoff) {
+            self::serviceOffline($client_id, $uid);
+        } else {
+            self::guestOffline($client_id, $uid);
+        }
+        return;
+    }
+
+    //客服下线了
+    public static function serviceOffline($client_id, $uid)
+    {
+        $group = $_SESSION['group'];
+        $kefuinfo_old = $kefuinfo_old_new = self::$global->kfList;
+        $user_info = $kefuinfo_old_new[$group][$uid]['user_info'];
+        if (!empty($user_info)) {
+            foreach ($user_info as $val) {
+                Gateway::sendToClient($val, json_encode(['type' => 'serviceoffline', 'msg' => '客户人员下线!'], true));
+                Gateway::closeClient($val);
             }
         }
-        $kfId = '';
-        if ($userId) {
-            $userToKf = self::$global->userToKf;
-            $kfId = $userToKf[$userId][1];
+        unset($kefuinfo_old_new[$group][$uid]);
+        do {
+        } while (!self::$global->cas('kfList', $kefuinfo_old, $kefuinfo_old_new));
+        return;
+    }
+
+    //用户下线了
+    public static function guestOffline($client_id, $uid)
+    {
+        $kfuid = -1;
+        $krclient_id = 0;
+        $kfgroup = -1;
+
+        $userToKf = $userToKfNew = self::$global->userToKf;
+        if (isset($userToKfNew[$uid])) {
+            $kfuid = isset($userToKfNew[$uid]['1']) ? $userToKfNew[$uid]['1'] : -1;
+            $krclient_id = isset(Gateway::getClientIdByUid($kfuid)['0']) ? Gateway::getClientIdByUid($kfuid)['0'] : 0;
+            unset($userToKfNew[$uid]);
+            do {
+            } while (!self::$global->cas('userToKf', $userToKf, $userToKfNew));
         }
-        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));
+
+        $uidSimpleList = $uidSimpleListNew = self::$global->uidSimpleList;
+        if (isset($uidSimpleListNew[$uid])) {
+            $kfgroup = $uidSimpleListNew[$uid]['1'];
+            unset($uidSimpleListNew[$uid]);
+            do {
+            } while (!self::$global->cas('uidSimpleList', $uidSimpleList, $uidSimpleListNew));
+        }
+
+        $userList = $userListNew = self::$global->userList;
+        if (!empty($userList)) {
+            $ischange = 0;
+            foreach ($userList as $key => $val) {
+                if ($val['id'] == $uid) {
+                    unset($userListNew[$key]);
+                    $ischange = 1;
+                    break;
+                }
+            }
+            if ($ischange) {
+                do {
+                } while (!self::$global->cas('userList', $userList, $userListNew));
+            }
         }
+
+
+        if ($kfuid != -1 && $kfgroup != -1) {
+            $kefuinfo_old = $kefuinfo_old_new = self::$global->kfList;
+            $ischange_kf_list = 0;
+            if (isset($kefuinfo_old[$kfgroup][$kfuid])) {
+                $infos = $kefuinfo_old[$kfgroup][$kfuid]['user_info'];
+                if ($infos) {
+                    if (is_array($infos)) {
+                        foreach ($infos as $key => $val) {
+                            if ($val == $client_id) {
+                                $ischange_kf_list = 1;
+                                unset($kefuinfo_old_new[$kfgroup][$kfuid]['user_info'][$key]);
+                                $kefuinfo_old_new[$kfgroup][$kfuid]['task'] = $kefuinfo_old_new[$kfgroup][$kfuid]['task'] - 1;
+                            }
+                        }
+                    }
+                    if ($ischange_kf_list) {
+                        do {
+                        } while (!self::$global->cas('kfList', $kefuinfo_old, $kefuinfo_old_new));
+
+                        $chat_message = [
+                            'message_type' => 'userClose',
+                            'data' => [
+                                'content' => '用户连接已断开',
+                                'time' => date('H:i'),
+                            ]
+                        ];
+                        self::$db->query("update `ws_service_log` set `status` = '3' where `user_id`= '$uid' and  kf_id='$kfuid' and  group_id=$kfgroup  ");
+                        Gateway::sendToClient($krclient_id, json_encode($chat_message, 256));
+                    }
+                }
+            }
+        }
+
     }
 
+
     /**
      * 客服结束会话
      * @param int $client_id 连接id