فهرست منبع

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

Jonlin 6 سال پیش
والد
کامیت
20cd37a02a
2فایلهای تغییر یافته به همراه152 افزوده شده و 40 حذف شده
  1. 3 3
      application/service/controller/Words.php
  2. 149 37
      vendor/GatewayWorker_windows/Applications/whisper/Events.php

+ 3 - 3
application/service/controller/Words.php

@@ -107,7 +107,7 @@ class Words extends Common
 
 
     /**
-     * 快捷语修改
+     * 删除快捷语
      *
      * @access public
      * @return array JsonString
@@ -145,7 +145,7 @@ class Words extends Common
 
 
     /**
-     * 快捷语修改
+     * 新增快捷语
      *
      * @access public
      * @return array JsonString
@@ -182,7 +182,7 @@ class Words extends Common
             return json(['code' => $code, 'data' => [], 'msg' => $msg]);
         }//end try
 
-    }//end deleteWords()
+    }//end addWords()
 
 
 }

+ 149 - 37
vendor/GatewayWorker_windows/Applications/whisper/Events.php

@@ -90,6 +90,80 @@ class Events
             });
 
         }
+
+
+        // 检查对话时效给出.
+        Timer::add(3, function () {
+            // 查询对话时效设置.
+            $systemConfigData = self::$db->query("SELECT `systemconfig_data`,`systemconfig_enName`,`systemconfig_content` FROM `ws_systemconfig`");
+            foreach ($systemConfigData as $k => $v) {
+                if ($v['systemconfig_enName'] == 'overtime') {
+                    self::$global->overtime = $v;
+                } elseif ($v['systemconfig_enName'] == 'unoperated') {
+                    self::$global->unoperated = $v;
+                } elseif ($v['systemconfig_enName'] == 'noResponse') {
+                    self::$global->noResponse = $v;
+                }
+            }
+            // 查询未断开的工单.
+            $serviceLog = self::$db->query("SELECT `servicelog_id`,`client_id`,`start_time` FROM `ws_service_log` WHERE `status`='1' OR `status`='3'");
+            $whereOr = '1=0';
+            foreach ($serviceLog as $k => $v) {
+                if ($k == 0) {
+                    $whereOr = "`servicelog_id`=".$v['servicelog_id'];
+                } else {
+                    $whereOr .= " OR `servicelog_id`=".$v['servicelog_id'];
+                }
+            }
+            // 查询最后一次会话.
+            //$chatLog = self::$db->query("SELECT `servicelog_id`,MAX(`time_line`) FROM `ws_chat_log` WHERE ".$whereOr." group by `servicelog_id`");
+            $chatLog = self::$db->query("
+                select * from ws_chat_log as a where  time_line=(
+                  select max(b.time_line) from ws_chat_log as b where a.servicelog_id = b.servicelog_id and (".$whereOr.") group by servicelog_id
+                )
+            ");
+            $setOvertime = strtotime('-'.(self::$global->overtime['systemconfig_data']-60).' second');
+            $overtime = strtotime('-'.(self::$global->overtime['systemconfig_data']).' second');
+            $setUnoperated = strtotime('-'.(self::$global->unoperated['systemconfig_data']-60).' second');
+            $unoperated = strtotime('-'.(self::$global->unoperated['systemconfig_data']).' second');
+            $noResponse = strtotime('-'.(self::$global->noResponse['systemconfig_data']).' second');
+            foreach ($serviceLog as $k => $v) {//注意该循环时间
+                if (!strlen(array_search($v['servicelog_id'], array_column($chatLog, 'servicelog_id')))) {
+                    // 如果小于设定时间则关闭会话.
+                    if ($v['start_time'] <= $unoperated) {
+                        self::serverClose($v['client_id']);
+                    // 如果小于设定时间前一分钟则给出提示.
+                    } elseif ($v['start_time'] <= $setUnoperated) {
+                        $chat_message = [
+                            'message_type' => 'overtime',
+                            'data' => [
+                                'content' => htmlspecialchars(self::$global->unoperated['systemconfig_content']),
+                            ]
+                        ];
+                        Gateway::sendToClient($v['client_id'], json_encode($chat_message));
+                    }
+                }
+            }
+            // 循环检测会话时效.
+            foreach ($chatLog as $k => $v) {
+                $toWho = substr($v['to_id'],0,2);
+                // 如果对话为客服的最后一次对话且时间小于设定时间则结束工单.
+                if ($toWho != 'KF' && $v['time_line'] <= $overtime) {
+                    $found_key = array_search($v['servicelog_id'], array_column($serviceLog, 'servicelog_id'));
+                    self::serverClose($serviceLog[$found_key]['client_id']);
+                // 如果对话为客服的最后一次对话且时间小于设定时间前一分钟则给出提示.
+                } elseif ($toWho != 'KF' && $v['time_line'] <= $setOvertime) {
+                    $chat_message = [
+                        'message_type' => 'overtime',
+                        'data' => [
+                            'content' => htmlspecialchars(self::$global->overtime['systemconfig_content']),
+                        ]
+                    ];
+                    $found_key = array_search($v['servicelog_id'], array_column($serviceLog, 'servicelog_id'));
+                    Gateway::sendToClient($serviceLog[$found_key]['client_id'], json_encode($chat_message));
+                }
+            }
+        });
     }
 
 
@@ -130,47 +204,15 @@ 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);
         switch ($message['type']) {
             // 客服初始化
             case 'init':
-                $kfList = self::$global->kfList;
-                // 如果该客服未在内存中记录则记录
-                if (!isset($kfList[$message['group']]) || !array_key_exists($message['uid'], $kfList[$message['group']])) {
-
-                    do {
-                        $newKfList = $kfList;
-                        $newKfList[$message['group']][$message['uid']] = [
-                            'id' => $message['uid'],
-                            'name' => $message['name'],
-                            'avatar' => $message['avatar'],
-                            'client_id' => $client_id,
-                            'task' => 0,
-                            'user_info' => []
-                        ];
-                    } while (!self::$global->cas('kfList', $kfList, $newKfList));
-                    unset($newKfList, $kfList);
-                } else if (isset($kfList[$message['group']][$message['uid']])) {
-
-                    do {
-                        $newKfList = $kfList;
-                        $newKfList[$message['group']][$message['uid']]['client_id'] = $client_id;
-                    } while (!self::$global->cas('kfList', $kfList, $newKfList));
-                    unset($newKfList, $kfList);
-                }
-
-                // 绑定 client_id 和 uid
-                Gateway::bindUid($client_id, $message['uid']);
-                $_SESSION['group'] = $message['group'];
-                $_SESSION['iskefu'] = 1;
-                $_SESSION['uid'] = $message['uid'];
-
-                // TODO 尝试拉取用户来服务 [二期规划]
-
+                self::Kfinit($client_id, $message);
                 break;
             // 顾客初始化
             case 'userInit';
@@ -325,6 +367,71 @@ class Events
 
     }
 
+    //客服接入sock,及初始化
+    public static  function  Kfinit($client_id, $message){
+        $kfList = self::$global->kfList;
+
+        //客服登陆验证 不符合的直接断掉
+        $logcheck = true ;     //开发时使用
+        //$logcheck = self::KfloginChedk($client_id, $message);
+        if (!$logcheck) {
+            Gateway::closeCurrentClient();
+            return true;
+        }
+
+        // 如果该客服未在内存中记录则记录
+        if (!isset($kfList[$message['group']]) || !array_key_exists($message['uid'], $kfList[$message['group']])) {
+
+            do {
+                $newKfList = $kfList;
+                $newKfList[$message['group']][$message['uid']] = [
+                    'id' => $message['uid'],
+                    'name' => $message['name'],
+                    'avatar' => $message['avatar'],
+                    'client_id' => $client_id,
+                    'task' => 0,
+                    'user_info' => []
+                ];
+            } while (!self::$global->cas('kfList', $kfList, $newKfList));
+            unset($newKfList, $kfList);
+        } else if (isset($kfList[$message['group']][$message['uid']])) {
+
+            do {
+                $newKfList = $kfList;
+                $newKfList[$message['group']][$message['uid']]['client_id'] = $client_id;
+            } while (!self::$global->cas('kfList', $kfList, $newKfList));
+            unset($newKfList, $kfList);
+        }
+
+        // 绑定 client_id 和 uid
+        Gateway::bindUid($client_id, $message['uid']);
+        $_SESSION['group'] = $message['group'];
+        $_SESSION['iskefu'] = 1;
+        $_SESSION['uid'] = $message['uid'];
+
+        // TODO 尝试拉取用户来服务 [二期规划]
+
+    }
+
+    //客服登陆验证
+    public static function KfloginChedk($client, $messageArray)
+    {
+        $uid = isset($messageArray['uid']) ? ($messageArray['uid']) : '';
+        $token = isset($messageArray['token']) ?   ($messageArray['token']) : '';
+        if (empty($uid) || empty($token)) {
+            return false;
+        }
+        $expire_time_vali = time() - 60 * 60 * 24 * 3;
+
+        $kfid = intval(substr($uid, 2));
+        $ret = self::$db->select('*')->from('ws_users')->where('id= :id and  token=:token and  expire_time>=:expire_time')->bindValues(array('id' => $kfid, 'token' => $token, 'expire_time' => $expire_time_vali))->row();
+        if ($ret) {
+            self::$db->update('ws_users')->cols(array('online_status'=>1,'online_connectid'=>$client))->where('id='.$kfid)->query();
+            return $ret;
+        }
+        return false;
+    }
+
     //用户发送邦定用户事件
     public static function userInitEnt($client_id, $message)
     {
@@ -438,6 +545,10 @@ class Events
             }
         }
         unset($kefuinfo_old_new[$group][$uid]);
+
+        $kfid = intval(substr($uid,2));
+        self::$db->update('ws_users')->cols(array('online_status'=>0,'online_connectid'=>''))->where('id='.$kfid)->query();
+
         do {
         } while (!self::$global->cas('kfList', $kefuinfo_old, $kefuinfo_old_new));
         return;
@@ -542,6 +653,7 @@ class Events
             ]
         ];
         Gateway::sendToClient($client_id, json_encode($chat_message, 256));
+        Gateway::closeClient($client_id);
         $isServiceUserOut = false;
         // 将会员服务信息,从客服的服务列表中移除
         $old = $kfList = self::$global->kfList;
@@ -815,9 +927,9 @@ class Events
             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();
+                self::$db->update('ws_service_log')->cols(['status' => 1])->where('servicelog_id=' . $hisSession['servicelog_id'])->query();
+                $conversationId = $hisSession['servicelog_id'];
             }
-
             unset($serviceLog);