vali 6 éve
szülő
commit
c05896e4cb
1 módosított fájl, 126 hozzáadás és 73 törlés
  1. 126 73
      vendor/GatewayWorker_windows/Applications/whisper/Events.php

+ 126 - 73
vendor/GatewayWorker_windows/Applications/whisper/Events.php

@@ -88,9 +88,11 @@ class Events
             Timer::add(60 * 40, function () {
                 self::writeLog(2);
             });
+
         }
     }
 
+
     /**
      * 当客户端连接时触发
      * 如果业务不需此回调可以删除onConnect
@@ -128,8 +130,8 @@ class Events
             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]);
+            echo "onMessage: " . $message . "\r\n";
+            print_r([self::$global->kfList, self::$global->userList, self::$global->uidSimpleList, self::$global->userToKf]);
         }
 
         $message = json_decode($message, true);
@@ -172,62 +174,7 @@ class Events
                 break;
             // 顾客初始化
             case 'userInit';
-                $userList = self::$global->userList;
-                // 如果该顾客未在内存中记录则记录
-                if (array_key_exists($message['uid'], $userList)) {
-                    $uidSimpleList = self::$global->uidSimpleList;
-                    $oldclientid = $uidSimpleList[$message['uid']]['0'];
-                    Gateway::sendToClient($oldclientid, json_encode(['type' => 'reLoginErr', 'msg' => '相同账号登陆,本次退出'], 256));
-                    sleep(1);
-                    Gateway::closeClient($oldclientid);
-                }
-
-                if (!array_key_exists($message['uid'], $userList)) {
-                    do {
-                        $NewUserList = $userList;
-                        $NewUserList[$message['uid']] = [
-                            'id' => $message['uid'],
-                            'name' => $message['name'],
-                            'avatar' => $message['avatar'],
-                            'website' => $_SESSION['origin'],//$_SERVER['HTTP_ORIGIN'],
-                            'browse' => Gateway::browse_info(),
-                            'system' => Gateway::get_os(),
-                            'ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '',
-                            'group' => $message['group'],
-                            'client_id' => $client_id
-                        ];
-
-                    } while (!self::$global->cas('userList', $userList, $NewUserList));
-                    unset($NewUserList, $userList);
-
-                    // 维护 UID对应的client_id 数组
-                    do {
-                        $old = $newList = self::$global->uidSimpleList;
-                        $newList[$message['uid']] = [
-                            $client_id,
-                            $message['group']
-                        ];
-
-                    } while (!self::$global->cas('uidSimpleList', $old, $newList));
-                    unset($old, $newList);
-
-                    // 写入接入值
-                    $key = date('Ymd') . 'total_in';
-                    self::$global->$key = 0;
-                    do {
-                        $oldKey = date('Ymd', strtotime('-1 day')); // 删除前一天的统计值
-                        unset(self::$global->$oldKey);
-                    } while (!self::$global->increment($key));
-                    unset($key);
-                }
-
-                // 绑定 client_id 和 uid
-                Gateway::bindUid($client_id, $message['uid']);
-                $_SESSION['iskefu'] = 0;
-                $_SESSION['uid'] = $message['uid'];
-
-                // 尝试分配新会员进入服务
-                self::userOnlineTask($client_id, $message['group']);
+                self::userInitEnt($client_id, $message);
                 break;
             // 聊天
             case 'chatMessage':
@@ -378,6 +325,70 @@ class Events
 
     }
 
+    //用户发送邦定用户事件
+    public static function userInitEnt($client_id, $message)
+    {
+        $userList = self::$global->userList;
+        // 如果该顾客未在内存中记录则记录
+
+        $uidSimpleList = self::$global->uidSimpleList;
+        if (isset($uidSimpleList[$message['uid']])) {
+            $uidSimpleList = self::$global->uidSimpleList;
+            $oldclientid = $uidSimpleList[$message['uid']]['0'];
+            Gateway::sendToClient($oldclientid, json_encode(['type' => 'reLoginErr', 'msg' => '相同账号登陆,本次退出'], 256));
+            Gateway::closeClient($oldclientid);
+            sleep(2);
+        }
+
+        if (!array_key_exists($message['uid'], $userList)) {
+            do {
+                $NewUserList = $userList;
+                $NewUserList[$message['uid']] = [
+                    'id' => $message['uid'],
+                    'name' => $message['name'],
+                    'avatar' => $message['avatar'],
+                    'website' => $_SESSION['origin'],//$_SERVER['HTTP_ORIGIN'],
+                    'browse' => Gateway::browse_info(),
+                    'system' => Gateway::get_os(),
+                    'ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '',
+                    'group' => $message['group'],
+                    'client_id' => $client_id
+                ];
+
+            } while (!self::$global->cas('userList', $userList, $NewUserList));
+            unset($NewUserList, $userList);
+
+            // 维护 UID对应的client_id 数组
+            do {
+                $old = $newList = self::$global->uidSimpleList;
+                $newList[$message['uid']] = [
+                    $client_id,
+                    $message['group']
+                ];
+
+            } while (!self::$global->cas('uidSimpleList', $old, $newList));
+            unset($old, $newList);
+
+            // 写入接入值
+            $key = date('Ymd') . 'total_in';
+            self::$global->$key = 0;
+            do {
+                $oldKey = date('Ymd', strtotime('-1 day')); // 删除前一天的统计值
+                unset(self::$global->$oldKey);
+            } while (!self::$global->increment($key));
+            unset($key);
+        }
+
+        // 绑定 client_id 和 uid
+        Gateway::bindUid($client_id, $message['uid']);
+        $_SESSION['iskefu'] = 0;
+        $_SESSION['uid'] = $message['uid'];
+
+        // 尝试分配新会员进入服务
+        self::userOnlineTask($client_id, $message['group'], $message['uid']);
+
+    }
+
     /**
      * 当用户断开连接时触发
      * @param int $client_id 连接id
@@ -499,7 +510,11 @@ class Events
                                 '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  and  status=1 ");
+                        $now = time();
+                        $kf__uid = substr($kfuid, 2);
+                        $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 ";
+                        //echo "客户退出:". $sql ."\n";
+                        self::$db->query($sql);
                         Gateway::sendToClient($krclient_id, json_encode($chat_message, 256));
                     }
                 }
@@ -556,6 +571,7 @@ class Events
                     };
                     unset($oldSimple, $simpleList);
 
+
                     $outUser = self::$db->query("select `user_id`,`group_id` from `ws_service_log` where `client_id`= '" . $client_id . "'");
                     // 通知 客服删除退出的用户
                     if (!empty($outUser)) {
@@ -756,13 +772,14 @@ class Events
      * 有人进入执行分配
      * @param $client_id
      * @param $group
+     * @param $uid
      */
-    private static function userOnlineTask($client_id, $group)
+    private static function userOnlineTask($client_id, $group, $uid = 0)
     {
         // TODO 此处查询最大的可服务人数,后面可以用其他的方式,存储这个数值,让其更高效的访问
         $maxNumber = self::getMaxServiceNum();
 
-        $res = self::assignmentTask(self::$global->kfList, self::$global->userList, $group, $maxNumber);
+        $res = self::assignmentTask(self::$global->kfList, self::$global->userList, $group, $maxNumber, $uid);
         unset($maxNumber);
 
         if (1 == $res['code']) {
@@ -794,8 +811,13 @@ class Events
                 'status' => 1,
                 '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('id=' . $hisSession['id'])->query();
+            }
 
-            $conversationId = self::$db->insert('ws_service_log')->cols($serviceLog)->query();
             unset($serviceLog);
 
 
@@ -869,7 +891,7 @@ class Events
      * @param $group
      * @param $total
      */
-    private static function assignmentTask($kfList, $userList, $group, $total)
+    private static function assignmentTask($kfList, $userList, $group, $total, $uid = 0)
     {
         // 没有客服上线
         if (empty($kfList) || empty($kfList[$group])) {
@@ -891,20 +913,51 @@ class Events
             return ['code' => -1];
         }
 
-        $kf = $kfList[$group];
-        $user = array_shift($userList);
+        //上次用户掉线后,还可以继续上一次 (如果没有关闭) 的会话  --1
+        $odltalksession = false;
+        $user = $user_first = array_shift($userList);
+        if ($uid > 0 && $user['id'] != $uid && count($userList) > 1) {
+            $timevalielimit = time() - 60 * 5;
+            $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();
+            if ($odltalksession) {
+                foreach ($userList as $ttkey => $ttval) {
+                    if ($ttval['id'] == $uid) {
+                        array_unshift($userList, $user);
+                        $user = $userList[$ttkey];
+                        unset($userList[$ttkey]);
+                        break;
+                    }
+                }
+            }
+        }
 
-        $kf = array_shift($kf);
-        $min = $kf['task'];
-        $flag = $kf['id'];
 
-        foreach ($kfList[$group] as $key => $vo) {
-            if ($vo['task'] < $min) {
-                $min = $vo['task'];
-                $flag = $key;
+        //上次用户掉线后,还可以继续上一次 (如果没有关闭) 的会话  --2
+        if ($odltalksession) {
+            $oldkrid = 'KF' . $odltalksession['kf_id'];
+            if (isset($kfList[$group][$oldkrid])) {
+                $kf = $kfList[$group][$oldkrid];
+                $min = $kf['task'];
+                $flag = $kf['id'];
+                unset($kfList[$group][$oldkrid]);
+            } else {
+                goto NOSIGNKF;
+            }
+        } else {
+            NOSIGNKF:
+            $kf = $kfList[$group];
+            $kf = array_shift($kf);
+            $min = $kf['task'];
+            $flag = $kf['id'];
+
+            foreach ($kfList[$group] as $key => $vo) {
+                if ($vo['task'] < $min) {
+                    $min = $vo['task'];
+                    $flag = $key;
+                }
             }
+            unset($kf);
         }
-        unset($kf);
 
         // 需要排队了
         if ($kfList[$group][$flag]['task'] == $total) {