lip 3 месяцев назад
Родитель
Сommit
31ccc97e44

+ 85 - 1
app/admin/controller/Kefu.php

@@ -11,8 +11,9 @@ use app\admin\model\User;
 use app\admin\model\UserView;
 use app\admin\model\KefuTime;
 use app\admin\model\OperationData;
+use app\enterprise\model\{Message,Friend};
 use Exception;
-
+use think\facade\Db;
 
 class Kefu extends BaseController
 {
@@ -178,4 +179,87 @@ class Kefu extends BaseController
         return $this->success([], '签到成功');
     }
 
+    /**
+     * 客服接线
+     */
+    public function handleChat()
+    { 
+        try {
+            Db::startTrans();
+            $user_id = $this->request->param('user_id');
+            $user = User::where('user_id', $user_id)->find();
+            if (!$user) {
+                return $this->error('用户不存在');
+            }
+            if ($user->service_status >= 1) {
+                return $this->error('用户已接线');
+            }
+            $user->cs_uid = $this->admin_id;
+            $user->service_status = 1;
+            $user->save();
+
+            $friend = Friend::where('create_user', $user_id)->order('create_time', 'desc')->find();
+            $robot_id = $friend->friend_user_id;
+            $friend->friend_user_id = $this->admin_id;
+            $friend->save();
+
+            Message::where(['from_user' => $user_id, 'to_user' => $robot_id])->update(['to_user' => $this->admin_id]);
+            Message::where(['from_user' => $robot_id, 'to_user' => $user_id])->update(['from_user' => $this->admin_id]);
+
+            KefuWork::addNum($this->admin_id, 'chat_num');//客服接线次数更新
+
+            Db::commit();
+            //通知客服已接线
+            //wsSendMsg($user_id,'isChat',['is_chat'=>1]);
+        } catch (\Exception $e) {
+            Db::rollback();
+            return $this->error($e->getMessage());
+        }
+    }
+
+    /**
+     * 客服转线
+     */
+    public function transferChat()
+    { 
+        try {
+            Db::startTrans();
+            $user_id = $this->request->param('user_id');
+            $admin_id = $this->request->param('admin_id');
+
+            $user = User::where('user_id', $user_id)->find();
+            if (!$user) {
+                return $this->error('用户不存在');
+            }
+            $user->cs_uid = $admin_id;
+            $user->service_status = 1;
+            $user->save();
+
+            $friend = Friend::where('create_user', $user_id)->order('create_time', 'desc')->find();
+            $old_admin_id = $friend->friend_user_id;
+            $friend->friend_user_id = $admin_id;
+            $friend->save();
+
+            Message::where(['from_user' => $user_id, 'to_user' => $old_admin_id])->update(['to_user' => $admin_id]);
+            Message::where(['from_user' => $old_admin_id, 'to_user' => $user_id])->update(['from_user' => $admin_id]);
+            KefuWork::addNum($admin_id, 'chat_num');//客服接线次数更新
+            KefuTime::endData($old_admin_id, 3); //结束接线时间
+
+            Db::commit();
+            //通知客服已接线
+            //wsSendMsg($user_id,'isChat',['is_chat'=>1]);
+        } catch (\Exception $e) {
+            Db::rollback();
+            return $this->error($e->getMessage());
+        }
+    }
+
+    /**
+     * 选择上级及部门一下的其他客服
+     */
+    public function select() 
+    {
+        
+    }
+
 }

+ 6 - 0
app/common.php

@@ -142,6 +142,12 @@ function getSelectData($data, $key_name = 'value', $value_name = 'label')
 }
 
 
+function getMessageId()
+{
+    return time().mt_rand(1000, 9999);
+}
+
+
 if (!function_exists('linear_to_tree')) {
     function linear_to_tree($data, $sub_key_name = 'children', $id_name = 'id', $parent_id_name = 'parent_id', $parent_id = 0)
     {

+ 0 - 101
app/common/controller/User.php

@@ -5,13 +5,10 @@ namespace app\common\controller;
 use think\App;
 use app\enterprise\model\User as UserModel;
 use app\enterprise\model\Group;
-use app\admin\model\GuessAskLanguages;
-use app\admin\model\QuestionLanguages;
 use app\admin\model\UserView;
 use think\facade\Session;
 use think\facade\Db;
 use GatewayClient\Gateway;
-use Exception;
 
 /**
  * 控制器基础类
@@ -153,103 +150,5 @@ class User
         wsSendMsg(0,'isOnline',['id'=>$user_id,'is_online'=>1]);
     }
 
-    /**
-     * 猜你想问列表
-     */
-    function guessask()
-    {
-        try {
-            $params = $this->request->param();
-            $page = $params['page'] ?? 1;
-            $limit = $params['limit'] ?? 15;
-            $language_code = $this->lang;
-            $query = GuessAskLanguages::where('language_code', $language_code)->where('status', 1); 
-            if (!empty($params['name'])) {
-                $query = $query->where('name', 'like', '%'.$params['name'].'%');
-            }
-            if (isset($params['type']) && $params['type'] != '') {
-                $query = $query->where('type', $params['type']);
-            }
-            $count = $query->count();
-            $list = $query->order('is_top','desc')
-                        ->order('is_rec','desc')
-                        ->order('click_num','desc')
-                        ->limit($limit)
-                        ->page($page)
-                        ->select();
-            
-        } catch (Exception $e) {
-            return error($e->getMessage());
-        }
-        return success('', [ 'count' => $count, 'list' => $list]);
-    }
-
-    /**
-     * 猜你想问点击使用
-     */
-    function guessaskClick()
-    {
-        try {
-            $id = $this->request->param('id');
-            $info = GuessAskLanguages::where('id', $id)->where('status', 1)->find();
-            if ($info) {
-                $info->click_num = $info->click_num + 1;
-                $info->save();
-            }
-        } catch (Exception $e) {
-            return error($e->getMessage());
-        }
-        return success('');
-    }
-
-    /**
-     * 问题列表
-     */
-    function question()
-    {
-        try {
-            $params = $this->request->param();
-            $page = $params['page'] ?? 1;
-            $limit = $params['limit'] ?? 15;
-            $language_code = $this->lang;
-            $query = QuestionLanguages::where('language_code', $language_code)->where('status', 1); 
-            
-            if (isset($params['question_type']) && $params['question_type'] != '') {
-                $query = $query->where('question_type', $params['question_type']);
-            }
-            $count = $query->count();
-            $list = $query->order('weight','desc')
-                        ->limit($limit)
-                        ->page($page)
-                        ->select();
-            
-        } catch (Exception $e) {
-            return error($e->getMessage());
-        }
-        return success('', [ 'count' => $count, 'list' => $list]);
-    }
-
-    /**
-     * 问题点赞、点否
-     */
-    function questionClick()
-    {
-        try {
-            $id = $this->request->param('id');
-            $is_like = $this->request->param('is_like');
-            $question = QuestionLanguages::where('id', $id)->where('status', 1)->find();
-            if ($question) {
-                if ($is_like == 1) {
-                    $question->like_count = $question->like_count + 1;
-                } else {
-                    $question->dislike_count = $question->dislike_count + 1;
-                }
-                $question->save();
-            }
-        } catch (Exception $e) {
-            return error($e->getMessage());
-        }
-        return success('');
-    }
 
 }

+ 140 - 0
app/enterprise/controller/Im.php

@@ -11,10 +11,150 @@ use Exception;
 use League\Flysystem\Util;
 use think\facade\Cache;
 use app\manage\model\{Config};
+use app\admin\model\GuessAskLanguages;
+use app\admin\model\QuestionLanguages;
 
 class Im extends BaseController
 {
     protected $fileType = ['file', 'image','video','voice','emoji'];
+
+    /**
+     * 猜你想问列表
+     */
+    function guessask()
+    {
+        try {
+            $params = $this->request->param();
+            $page = $params['page'] ?? 1;
+            $limit = $params['limit'] ?? 15;
+            $language_code = $this->lang;
+            $query = GuessAskLanguages::where('language_code', $language_code)->where('status', 1); 
+            if (!empty($params['name'])) {
+                $query = $query->where('name', 'like', '%'.$params['name'].'%');
+            }
+            if (isset($params['type']) && $params['type'] != '') {
+                $query = $query->where('type', $params['type']);
+            }
+            $count = $query->count();
+            $list = $query->order('is_top','desc')
+                        ->order('is_rec','desc')
+                        ->order('click_num','desc')
+                        ->limit($limit)
+                        ->page($page)
+                        ->select();
+            
+        } catch (Exception $e) {
+            return error($e->getMessage());
+        }
+        return success('', [ 'count' => $count, 'list' => $list]);
+    }
+
+    /**
+     * 猜你想问点击使用
+     */
+    function guessaskClick()
+    {
+        try {
+            $id = $this->request->param('id');
+            $info = GuessAskLanguages::where('id', $id)->where('status', 1)->find();
+            if ($info) {
+                $info->click_num = $info->click_num + 1;
+                $info->save();
+            }
+        } catch (Exception $e) {
+            return error($e->getMessage());
+        }
+        return success('');
+    }
+
+    /**
+     * 问题列表
+     */
+    function question()
+    {
+        try {
+            $params = $this->request->param();
+            $page = $params['page'] ?? 1;
+            $limit = $params['limit'] ?? 15;
+            $language_code = $this->lang;
+            $query = QuestionLanguages::where('language_code', $language_code)->where('status', 1); 
+            
+            if (isset($params['question_type']) && $params['question_type'] != '') {
+                $query = $query->where('question_type', $params['question_type']);
+            }
+            $count = $query->count();
+            $list = $query->order('weight','desc')
+                        ->limit($limit)
+                        ->page($page)
+                        ->select();
+        } catch (Exception $e) {
+            return error($e->getMessage());
+        }
+        return success('', [ 'count' => $count, 'list' => $list]);
+    }
+
+    /**
+     * 问题点赞、点否
+     */
+    function questionClick()
+    {
+        try {
+            $id = $this->request->param('id');
+            $is_like = $this->request->param('is_like');
+            $question = QuestionLanguages::where('id', $id)->where('status', 1)->find();
+            if ($question) {
+                if ($is_like == 1) {
+                    $question->like_count = $question->like_count + 1;
+                } else {
+                    $question->dislike_count = $question->dislike_count + 1;
+                }
+                $question->save();
+            }
+        } catch (Exception $e) {
+            return error($e->getMessage());
+        }
+        return success('');
+    }
+
+    /**
+     * 转人工客服
+     */
+    function transferHuman()
+    {
+        try {
+            $user_id = $this->userInfo['user_id'];
+            $user = User::where('user_id', $user_id)->find();
+            if (!$user) {
+                return error('用户不存在');
+            }
+            if ($user->role > 0) {
+                return error('系统管理员无法转人工');
+            }
+            if ($user['service_status'] == -1) {
+                $user->service_status = 0;
+                $user->service->service_start = time();
+                $user->save();
+
+                //机器人客服
+                $autoTask = Config::autoTask();
+                $param = [
+                    'id' => \utils\Str::getUuid(),
+                    'type' => 'text',
+                    'status' => 'going',
+                    'sendTime' => time(),
+                    'toContactId' => !empty($autoTask['user_id']) ? $autoTask['user_id'] : 1,
+                    'content' => $this->globalConfig['transfer_to_human'],
+                    'file_id' => 0,
+                    'is_group' => 0
+                ];
+                Message::sendMsg($param, 0);
+            }
+            
+        } catch (Exception $e) {
+            return error($e->getMessage());
+        }
+        return success('');
+    }
     // 获取联系人列表
     public function getContacts()
     {

+ 23 - 16
app/enterprise/model/Message.php

@@ -160,27 +160,25 @@ class Message extends BaseModel
                 $param['content'] = Config::where('field','reply_keyword_math_fail')->value('val');
             }
             
-            $fromUserId = $param['toContactId'];
-            $param['toContactId'] = $param['fromUser']['id'];
+            $fromUserId = (int)$param['toContactId'];
+            $user_id = (int)$param['user_id'];
+            $param['toContactId'] = $user_id;
             $param['user_id'] = $fromUserId;
-            $param['fromUser'] = [
-                'id' => $fromUserId,
-                'displayName' => '88888',
-                'avatar' => '',
-                'account' => 'admin',
-            ];
-            $param['content'] = Config::where('field','reply_keyword_math_fail')->value('val');
-            //$data2 = self::sendMsg($param,$is_group, 0, $param['user_id']);
+            $param['fromUser'] = null;
+            $param['id'] = \utils\Str::getUuid();
+
+            self::sendMsg($param,$is_group, 0, $user_id);
             
             //机器人自动回复消息
-            event('AutoReplyMessage', ['param' => $param, 'globalConfig' => $globalConfig]);
+            //event('AutoReplyMessage', ['param' => $param, 'globalConfig' => $globalConfig]);
         }
         return $data;
     }
 
     //实际发送消息
     public static function sendMsg($param,$is_group=0,$is_sys=0, $uid=0){
-        $uid = $uid ?: ($param['user_id'] ?? 1);
+        // $uid = $uid ?: ($param['user_id'] ?? 1);
+        $uid = self::$uid ?: ($param['user_id'] ?? 1);
         $toContactId=$param['toContactId'];
         $manage=[];
         // 重新建立会话,更新会话删除记录
@@ -243,7 +241,7 @@ class Message extends BaseModel
         $sendData['at']=$atList;
         $sendData['msg_id']=$message->msg_id;
         $sendData['is_read']=0;
-        $sendData['to_user']=$toContactId;
+        $sendData['to_user']=(string)$toContactId;
         $sendData['role']=$manage[self::$uid] ?? 3;
         $sendData['sendTime']=(int)$sendData['sendTime'];
         //这里单聊中发送对方的消息,对方是接受状态,自己是对方的联系人,要把发送对象设置为发送者的ID。
@@ -252,9 +250,9 @@ class Message extends BaseModel
             // 将团队所有成员的未读状态+1
             GroupUser::editGroupUser([['group_id','=',$toContactId],['user_id','<>',$uid]],['unread'=>Db::raw('unread+1')]);
         }else{
-            $sendData['toContactId']=$toContactId;//$uid;
+            //$sendData['toContactId']=$toContactId;//$uid;
+            $sendData['toContactId'] = $param['user_id'];
         }
-        $sendData['fromUser']['id']=(int)$sendData['fromUser']['id'];
         $sendData['fileSize']=$fileSzie;
         $sendData['fileName']=$fileName;
         if(in_array($sendData['type'],self::$fileType)){
@@ -276,11 +274,20 @@ class Message extends BaseModel
             $toContactId=[$toContactId,$param['user_id']];
         }
         
-        $sendData['toUser']=$param['toContactId'];
+        $sendData['toUser']=(string)$param['toContactId'];
         $user=new User();
         // 将聊天窗口的联系人信息带上,方便临时会话
         
         $sendData['contactInfo']=$user->setContact($forContactId,$is_group,$sendData['type'],$sendData['content']);
+        if (empty($sendData['fromUser'])) {
+            $sendData['fromUser'] = [
+                'id' => $sendData['contactInfo']['id'],
+                'displayName' => $sendData['contactInfo']['displayName'],
+                'avatar' => $sendData['contactInfo']['avatar'],
+                'account' => '',
+            ];
+        }
+        $sendData['fromUser']['id']=(int)$sendData['fromUser']['id'];
         // 向发送方发送消息
         wsSendMsg($toContactId,$type,$sendData,$is_group);
         $sendData['toContactId']=$param['toContactId'];

+ 3 - 1
app/lang/zh.php

@@ -97,4 +97,6 @@ return [
     '三级' => '三级',
     '签到成功' => '签到成功',
     '退出成功' => '退出成功',
-];
+    '用户不存在' => '用户不存在',
+    '系统管理员无法转人工' => '系统管理员无法转人工',
+]