vali 6 years ago
parent
commit
946f81c361
1 changed files with 111 additions and 100 deletions
  1. 111 100
      vendor/GatewayWorker_windows/Applications/whisper/Events.php

+ 111 - 100
vendor/GatewayWorker_windows/Applications/whisper/Events.php

@@ -17,6 +17,7 @@
  * 如果发现业务卡死,可以将下面declare打开(去掉//注释),并执行php start.php reload
  * 然后观察一段时间workerman.log看是否有process_timeout异常
  */
+
 //declare(ticks=1);
 use \GatewayWorker\Lib\Gateway;
 use Workerman\Lib\Timer;
@@ -46,20 +47,20 @@ class Events
         if (empty(self::$global)) {
             self::$global = new \GlobalData\Client('127.0.0.1:2207');
             // 客服列表
-            if(is_null(self::$global->kfList)){
+            if (is_null(self::$global->kfList)) {
                 self::$global->kfList = [];
             }
             // 会员列表[动态的,这里面只是目前未被分配的会员信息]
-            if(is_null(self::$global->userList)){
+            if (is_null(self::$global->userList)) {
                 self::$global->userList = [];
             }
             // 会员以 uid 为key的信息简表,只有在用户退出的时候,才去执行修改
-            if(is_null(self::$global->uidSimpleList)){
+            if (is_null(self::$global->uidSimpleList)) {
                 self::$global->uidSimpleList = [];
             }
             // 当天的累积接入值
             $key = date('Ymd') . 'total_in';
-            if(is_null(self::$global->$key)){
+            if (is_null(self::$global->$key)) {
                 self::$global->$key = 0;
 
                 $oldKey = date('Ymd', strtotime('-1 day')); // 删除前一天的统计值
@@ -68,7 +69,7 @@ class Events
             }
             // 成功接入值
             $key = date('Ymd') . 'success_in';
-            if(is_null(self::$global->$key)){
+            if (is_null(self::$global->$key)) {
                 self::$global->$key = 0;
 
                 $oldKey = date('Ymd', strtotime('-1 day')); // 删除前一天的统计值
@@ -78,13 +79,13 @@ class Events
         }
 
         // 定时统计数据
-        if(0 === $worker->id){
+        if (0 === $worker->id) {
             // 1分钟统计一次实时数据
-            Timer::add(60 * 1, function(){
+            Timer::add(60 * 1, function () {
                 self::writeLog(1);
             });
             // 40分钟写一次当前日期点数的log数据
-            Timer::add(60 * 40, function(){
+            Timer::add(60 * 40, function () {
                 self::writeLog(2);
             });
         }
@@ -100,7 +101,7 @@ class Events
     {
         // 检测是否开启自动应答
         $sayHello = self::$db->query('select `word`,`status` from `ws_reply` where `id` = 1');
-        if(!empty($sayHello) && 1 == $sayHello['0']['status']){
+        if (!empty($sayHello) && 1 == $sayHello['0']['status']) {
 
             $hello = [
                 'message_type' => 'helloMessage',
@@ -123,23 +124,23 @@ class Events
      */
     public static function onMessage($client_id, $message)
     {
-         if ($message=='{"type":"ping"}'){
+        if ($message == '{"type":"ping"}') {
             Gateway::sendToCurrentClient('{"type":"pong"}');
             return;
-        }else{
-            echo "onMessage: ".$message."\r\n";
-            print_r([self::$global->kfList,self::$global->userList,  self::$global->uidSimpleList]);
+        } 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);
         switch ($message['type']) {
             // 客服初始化
             case 'init':
-               $kfList = self::$global->kfList;
+                $kfList = self::$global->kfList;
                 // 如果该客服未在内存中记录则记录
-                if(!isset($kfList[$message['group']]) || !array_key_exists($message['uid'], $kfList[$message['group']])){
+                if (!isset($kfList[$message['group']]) || !array_key_exists($message['uid'], $kfList[$message['group']])) {
 
-                    do{
+                    do {
                         $newKfList = $kfList;
                         $newKfList[$message['group']][$message['uid']] = [
                             'id' => $message['uid'],
@@ -149,14 +150,14 @@ class Events
                             'task' => 0,
                             'user_info' => []
                         ];
-                    }while(!self::$global->cas('kfList', $kfList, $newKfList));
+                    } while (!self::$global->cas('kfList', $kfList, $newKfList));
                     unset($newKfList, $kfList);
-                }else if(isset($kfList[$message['group']][$message['uid']])){
+                } else if (isset($kfList[$message['group']][$message['uid']])) {
 
-                    do{
+                    do {
                         $newKfList = $kfList;
                         $newKfList[$message['group']][$message['uid']]['client_id'] = $client_id;
-                    }while(!self::$global->cas('kfList', $kfList, $newKfList));
+                    } while (!self::$global->cas('kfList', $kfList, $newKfList));
                     unset($newKfList, $kfList);
                 }
 
@@ -169,8 +170,8 @@ class Events
             case 'userInit';
                 $userList = self::$global->userList;
                 // 如果该顾客未在内存中记录则记录
-                if(!array_key_exists($message['uid'], $userList)){
-                    do{
+                if (!array_key_exists($message['uid'], $userList)) {
+                    do {
                         $NewUserList = $userList;
                         $NewUserList[$message['uid']] = [
                             'id' => $message['uid'],
@@ -184,27 +185,27 @@ class Events
                             'client_id' => $client_id
                         ];
 
-                    }while(!self::$global->cas('userList', $userList, $NewUserList));
+                    } while (!self::$global->cas('userList', $userList, $NewUserList));
                     unset($NewUserList, $userList);
 
                     // 维护 UID对应的client_id 数组
-                    do{
+                    do {
                         $old = $newList = self::$global->uidSimpleList;
                         $newList[$message['uid']] = [
                             $client_id,
                             $message['group']
                         ];
 
-                    }while(!self::$global->cas('uidSimpleList', $old, $newList));
+                    } while (!self::$global->cas('uidSimpleList', $old, $newList));
                     unset($old, $newList);
 
                     // 写入接入值
                     $key = date('Ymd') . 'total_in';
                     self::$global->$key = 0;
-                    do{
+                    do {
                         $oldKey = date('Ymd', strtotime('-1 day')); // 删除前一天的统计值
                         unset(self::$global->$oldKey);
-                    }while(!self::$global->increment($key));
+                    } while (!self::$global->increment($key));
                     unset($key);
                 }
 
@@ -217,7 +218,7 @@ class Events
             // 聊天
             case 'chatMessage':
                 $client = Gateway::getClientIdByUid($message['data']['to_id']);
-                if(!empty($client)){
+                if (!empty($client)) {
                     $chat_message = [
                         'message_type' => 'chatMessage',
                         'data' => [
@@ -252,8 +253,8 @@ class Events
                 // 通知客户端转接中
                 $simpleList = self::$global->uidSimpleList;
 
-                if(!isset($simpleList[$message['uid']])){ // 客户已经退出
-                    return ;
+                if (!isset($simpleList[$message['uid']])) { // 客户已经退出
+                    return;
                 }
 
                 $userClient = $simpleList[$message['uid']]['0'];
@@ -270,10 +271,10 @@ class Events
 
                 // 从当前客服的服务表中删除这个会员
                 $old = $kfList = self::$global->kfList;
-                if(!isset($kfList[$userGroup])){
+                if (!isset($kfList[$userGroup])) {
                     $waitMsg = '暂时没有相关客服上班,请稍后再咨询。';
                     // 逐一通知
-                    foreach(self::$global->userList as $vo){
+                    foreach (self::$global->userList as $vo) {
 
                         $waitMessage = [
                             'message_type' => 'wait',
@@ -284,16 +285,16 @@ class Events
                         Gateway::sendToClient($userClient, json_encode($waitMessage));
                         unset($waitMessage);
                     }
-                    return ;
+                    return;
                 }
                 $myList = $kfList[$userGroup]; // 该客服分组数组
-                foreach($myList as $key=>$vo){
-                    if(in_array($userClient, $vo['user_info'])){
+                foreach ($myList as $key => $vo) {
+                    if (in_array($userClient, $vo['user_info'])) {
 
                         // 维护现在的该客服的服务信息
                         $kfList[$userGroup][$key]['task'] -= 1; // 当前服务的人数 -1
-                        foreach($vo['user_info'] as $k=>$v){
-                            if($userClient == $v){
+                        foreach ($vo['user_info'] as $k => $v) {
+                            if ($userClient == $v) {
                                 unset($kfList[$userGroup][$key]['user_info'][$k]);
                                 break;
                             }
@@ -302,12 +303,13 @@ class Events
                         break;
                     }
                 }
-                while(!self::$global->cas('kfList', $old, $kfList)){}; // 刷新内存中客服的服务列表
+                while (!self::$global->cas('kfList', $old, $kfList)) {
+                }; // 刷新内存中客服的服务列表
                 unset($old, $kfList, $myList);
 
                 // 将会员加入队列中
                 $userList = self::$global->userList;
-                do{
+                do {
                     $NewUserList = $userList;
                     $NewUserList[$message['uid']] = [
                         'id' => $message['uid'],
@@ -318,7 +320,7 @@ class Events
                         'client_id' => $userClient
                     ];
 
-                }while(!self::$global->cas('userList', $userList, $NewUserList));
+                } while (!self::$global->cas('userList', $userList, $NewUserList));
                 unset($NewUserList, $userList);
 
                 // 执行会员分配通知双方
@@ -326,10 +328,10 @@ class Events
                 unset($userClient, $userGroup);
 
                 break;
-			case 'closeUser':
+            case 'closeUser':
 
                 $userInfo = self::$global->uidSimpleList;
-                if(isset($userInfo[$message['uid']])){
+                if (isset($userInfo[$message['uid']])) {
                     $waitMessage = [
                         'message_type' => 'wait',
                         'data' => [
@@ -352,7 +354,7 @@ class Events
             // 客服关闭会话.
             case 'kfCloseUser':
                 $client = Gateway::getClientIdByUid($message['data']['to_id']);
-                if(!empty($client)){
+                if (!empty($client)) {
                     self::serverClose($client['0']);
                 }
                 break;
@@ -416,9 +418,9 @@ class Events
         $isServiceUserOut = false;
         // 将会员服务信息,从客服的服务列表中移除
         $old = $kfList = self::$global->kfList;
-        foreach($kfList as $k=>$v){
-            foreach($v as $key=>$vo){
-                if(in_array($client_id, $vo['user_info'])){
+        foreach ($kfList as $k => $v) {
+            foreach ($v as $key => $vo) {
+                if (in_array($client_id, $vo['user_info'])) {
 
                     $isServiceUserOut = true;
 
@@ -428,8 +430,8 @@ class Events
                     // 从会员的内存表中检索出该会员的信息,并更新内存
                     $oldSimple = $simpleList = self::$global->uidSimpleList;
                     $outUser = [];
-                    foreach($simpleList as $u=>$c){
-                        if($c['0'] == $client_id){
+                    foreach ($simpleList as $u => $c) {
+                        if ($c['0'] == $client_id) {
                             $outUser[] = [
                                 'user_id' => $u,
                                 'group_id' => $c['1']
@@ -438,12 +440,13 @@ class Events
                             break;
                         }
                     }
-                    while(!self::$global->cas('uidSimpleList', $oldSimple, $simpleList)){};
+                    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 . "'");
                     // 通知 客服删除退出的用户
-                    if(!empty($outUser)){
+                    if (!empty($outUser)) {
                         $del_message = [
                             'message_type' => 'delUser',
                             'data' => [
@@ -460,30 +463,31 @@ class Events
 
                     // 维护现在的该客服的服务信息
                     $kfList[$k][$key]['task'] -= 1; // 当前服务的人数 -1
-                    foreach($vo['user_info'] as $m=>$l){
-                        if($client_id == $l){
+                    foreach ($vo['user_info'] as $m => $l) {
+                        if ($client_id == $l) {
                             unset($kfList[$k][$key]['user_info'][$m]);
                             break;
                         }
                     }
 
                     // 刷新内存中客服的服务列表
-                    while(!self::$global->cas('kfList', $old, $kfList)){};
+                    while (!self::$global->cas('kfList', $old, $kfList)) {
+                    };
                     unset($old, $kfList);
 
                     break;
                 }
             }
 
-            if($isServiceUserOut) break;
+            if ($isServiceUserOut) break;
         }
 
 
         // 尝试从排队的用户中删除退出的客户端
-        if(false == $isServiceUserOut){
+        if (false == $isServiceUserOut) {
             $old = $userList = self::$global->userList;
-            foreach(self::$global->userList as $key=>$vo){
-                if($client_id == $vo['client_id']){
+            foreach (self::$global->userList as $key => $vo) {
+                if ($client_id == $vo['client_id']) {
 
                     $isServiceUserOut = true;
 
@@ -491,34 +495,37 @@ class Events
                     break;
                 }
             }
-            while(!self::$global->cas('userList', $old, $userList)){};
+            while (!self::$global->cas('userList', $old, $userList)) {
+            };
 
             // 从会员的内存表中检索出该会员的信息,并更新内存
             $oldSimple = $simpleList = self::$global->uidSimpleList;
-            foreach($simpleList as $u=>$c){
-                if($c['0'] == $client_id){
+            foreach ($simpleList as $u => $c) {
+                if ($c['0'] == $client_id) {
                     unset($simpleList[$u]);
                     break;
                 }
             }
-            while(!self::$global->cas('uidSimpleList', $oldSimple, $simpleList)){};
+            while (!self::$global->cas('uidSimpleList', $oldSimple, $simpleList)) {
+            };
             unset($oldSimple, $simpleList);
         }
 
         // 尝试是否是客服退出
-        if(false == $isServiceUserOut){
+        if (false == $isServiceUserOut) {
             $old = $kfList = self::$global->kfList;
-            foreach(self::$global->kfList as $k=>$v){
-                foreach($v as $key=>$vo){
+            foreach (self::$global->kfList as $k => $v) {
+                foreach ($v as $key => $vo) {
                     // 客服服务列表中无数据,才去删除客服内存信息
-                    if($client_id == $vo['client_id'] && (0 == count($vo['user_info']))){
+                    if ($client_id == $vo['client_id'] && (0 == count($vo['user_info']))) {
                         unset($kfList[$k][$key]);
                         break;
                     }
                 }
             }
 
-            while(!self::$global->cas('kfList', $old, $kfList)){};
+            while (!self::$global->cas('kfList', $old, $kfList)) {
+            };
         }
     }
 
@@ -535,10 +542,12 @@ class Events
         $res = self::assignmentTask(self::$global->kfList, self::$global->userList, $group, $maxNumber);
         unset($maxNumber);
 
-        if(1 == $res['code']){
+        if (1 == $res['code']) {
 
-            while(!self::$global->cas('kfList', self::$global->kfList, $res['data']['4'])){}; // 更新客服数据
-            while(!self::$global->cas('userList', self::$global->userList, $res['data']['5'])){}; // 更新会员数据
+            while (!self::$global->cas('kfList', self::$global->kfList, $res['data']['4'])) {
+            }; // 更新客服数据
+            while (!self::$global->cas('userList', self::$global->userList, $res['data']['5'])) {
+            }; // 更新会员数据
 
             // 通知会员发送信息绑定客服的id
             $noticeUser = [
@@ -563,7 +572,7 @@ class Events
 
             // 逐一通知
             $number = 1;
-            foreach(self::$global->userList as $vo){
+            foreach (self::$global->userList as $vo) {
 
                 $waitMsg = '您前面还有 ' . $number . ' 位会员在等待。';
                 $waitMessage = [
@@ -581,20 +590,20 @@ class Events
             // 写入接入值
             $key = date('Ymd') . 'success_in';
             self::$global->$key = 0;
-            do{
+            do {
                 $oldKey = date('Ymd', strtotime('-1 day')); // 删除前一天的统计值
                 unset(self::$global->$oldKey);
-            }while(!self::$global->increment($key));
+            } while (!self::$global->increment($key));
             unset($key);
 
-        }else{
+        } else {
 
-            switch ($res['code']){
+            switch ($res['code']) {
 
                 case -1:
                     $waitMsg = '暂时没有客服上班,请稍后再咨询。';
                     // 逐一通知
-                    foreach(self::$global->userList as $vo){
+                    foreach (self::$global->userList as $vo) {
 
                         $waitMessage = [
                             'message_type' => 'wait',
@@ -612,7 +621,7 @@ class Events
                 case -4:
                     // 逐一通知
                     $number = 1;
-                    foreach(self::$global->userList as $vo){
+                    foreach (self::$global->userList as $vo) {
 
                         $waitMsg = '您前面还有 ' . $number . ' 位会员在等待。';
                         $waitMessage = [
@@ -644,10 +653,12 @@ class Events
         $res = self::assignmentTask(self::$global->kfList, self::$global->userList, $group, $maxNumber);
         unset($maxNumber);
 
-        if(1 == $res['code']){
+        if (1 == $res['code']) {
 
-            while(!self::$global->cas('kfList', self::$global->kfList, $res['data']['4'])){}; // 更新客服数据
-            while(!self::$global->cas('userList', self::$global->userList, $res['data']['5'])){}; // 更新会员数据
+            while (!self::$global->cas('kfList', self::$global->kfList, $res['data']['4'])) {
+            }; // 更新客服数据
+            while (!self::$global->cas('userList', self::$global->userList, $res['data']['5'])) {
+            }; // 更新会员数据
             $userToKf = self::$global->userToKf;
             $userToKf[$res['data']['3']['id']] = [
                 $res['data']['3']['id'],
@@ -702,16 +713,16 @@ class Events
             // 写入接入值
             $key = date('Ymd') . 'success_in';
             self::$global->$key = 0;
-            do{
+            do {
                 $oldKey = date('Ymd', strtotime('-1 day')); // 删除前一天的统计值
                 unset(self::$global->$oldKey);
-            }while(!self::$global->increment($key));
+            } while (!self::$global->increment($key));
             unset($key);
 
-        }else{
+        } else {
 
             $waitMsg = '';
-            switch ($res['code']){
+            switch ($res['code']) {
 
                 case -1:
                     $waitMsg = '暂时没有客服上班,请稍后再咨询。';
@@ -749,22 +760,22 @@ class Events
     private static function assignmentTask($kfList, $userList, $group, $total)
     {
         // 没有客服上线
-        if(empty($kfList) || empty($kfList[$group])){
+        if (empty($kfList) || empty($kfList[$group])) {
             return ['code' => -1];
         }
 
         // 没有待分配的会员
-        if(empty($userList)){
+        if (empty($userList)) {
             return ['code' => -2];
         }
 
         // 未设置每个客服可以服务多少人
-        if(0 == $total){
+        if (0 == $total) {
             return ['code' => -3];
         }
 
         // 查看该组的客服是否在线
-        if(!isset($kfList[$group])){
+        if (!isset($kfList[$group])) {
             return ['code' => -1];
         }
 
@@ -775,8 +786,8 @@ class Events
         $min = $kf['task'];
         $flag = $kf['id'];
 
-        foreach($kfList[$group] as $key=>$vo){
-            if($vo['task'] < $min){
+        foreach ($kfList[$group] as $key => $vo) {
+            if ($vo['task'] < $min) {
                 $min = $vo['task'];
                 $flag = $key;
             }
@@ -784,7 +795,7 @@ class Events
         unset($kf);
 
         // 需要排队了
-        if($kfList[$group][$flag]['task'] == $total){
+        if ($kfList[$group][$flag]['task'] == $total) {
             return ['code' => -4];
         }
 
@@ -811,9 +822,9 @@ class Events
     private static function getMaxServiceNum()
     {
         $maxNumber = self::$db->query('select `max_service` from `ws_kf_config` where `id` = 1');
-        if(!empty($maxNumber)){
+        if (!empty($maxNumber)) {
             $maxNumber = 5;
-        }else{
+        } else {
             $maxNumber = $maxNumber['0']['max_service'];
         }
 
@@ -827,8 +838,8 @@ class Events
     private static function writeLog($flag = 1)
     {
         // 上午 8点 到 22 点开始统计
-        if(date('H') < 8 || date('H') > 22){
-            return ;
+        if (date('H') < 8 || date('H') > 22) {
+            return;
         }
 
         // 当前正在接入的人 和 在线客服数
@@ -836,12 +847,12 @@ class Events
 
         $nowTalking = 0;
         $onlineKf = 0;
-        if(!empty($kfList)){
+        if (!empty($kfList)) {
 
-            foreach($kfList as $key=>$vo){
+            foreach ($kfList as $key => $vo) {
 
                 $onlineKf += count($vo);
-                foreach($vo as $k=>$v){
+                foreach ($vo as $k => $v) {
                     $nowTalking += count($v['user_info']);
                 }
             }
@@ -862,7 +873,7 @@ class Events
         ];
         self::$db->update('ws_now_data')->cols($param)->where('id=1')->query();
 
-        if(2 == $flag){
+        if (2 == $flag) {
             $param = [
                 'is_talking' => $nowTalking,
                 'in_queue' => $inQueue,
@@ -933,10 +944,10 @@ class Events
 
 
     //踢掉同一用户的旧用户
-    private static function   tickOlduser($uid){
+    private static function tickOlduser($uid)
+    {
 
     }
 
 
-
 }