lip 3 недель назад
Родитель
Сommit
c1635bb837

+ 14 - 0
app/admin/command/UserTimeout.php

@@ -10,6 +10,8 @@ use app\admin\model\KefuTime;
 use app\admin\model\KefuLog;
 use think\facade\Db;
 use GatewayClient\Gateway;
+use app\enterprise\model\{Message,Friend};
+
 
 /**
  * Worker 命令行类
@@ -111,6 +113,18 @@ class UserTimeout extends Command
                     //客服对接记录表
                     KefuLog::addData($cs_uid, $user['user_id'], 3);
 
+                    $user_id = $user['user_id'];
+                    $friend = Friend::where('create_user', $user_id)->order('create_time', 'desc')->find();
+                    if ($friend) {
+                        $old_cs_uid = $friend->friend_user_id;
+                        $friend->friend_user_id = $cs_uid;
+                        $friend->save();
+                        
+                        $chat_identify = chat_identify($cs_uid,$user_id);
+                        Message::where(['from_user' => $user_id, 'to_user' => $old_cs_uid])->update(['to_user' => $cs_uid, 'chat_identify' => $chat_identify, 'is_read' => 1]);
+                        Message::where(['from_user' => $old_cs_uid, 'to_user' => $user_id])->update(['from_user' => $cs_uid, 'chat_identify' => $chat_identify, 'is_read' => 1]);
+                    }
+
                     Db::commit();
                     $is_warning = $user['service_status'] == 1 || $user['service_status'] == 2 ? 1 : 0;
                     //通知关闭聊天框

+ 4 - 2
app/enterprise/controller/Im.php

@@ -607,6 +607,7 @@ class Im extends BaseController
                 $where[] = ['create_time', '>=', $start_time];
             }
 
+            $auto_cs_uid = getAutoCsUid();//获取机器人ID
             $toUser = User::where('user_id', $param['toContactId'])->find();
             if ($toUser && $toUser->service_status == 1 && $toUser->cs_uid == $this->userInfo['user_id']) {
                 //将用户待处理的状态更新成服务中
@@ -616,8 +617,9 @@ class Im extends BaseController
                 
                 //通知客服-会话服务中
                 wsSendMsg(0,'handleChat',['user_id'=>$param['toContactId']]);
-            } else {
-                // $map['chat_identify'] = $toUser->cs_uid.'-'.$param['toContactId'];
+            } elseif ($toUser->cs_uid == $auto_cs_uid) {
+                //如果当前用户归属到机器人,则修改查询条件
+                $map['chat_identify'] = $auto_cs_uid.'-'.$param['toContactId'];
             }
         }
         $list = Message::getList($map, $where, 'msg_id desc', $listRows, $pageSize);

+ 2 - 3
app/enterprise/model/User.php

@@ -309,11 +309,10 @@ class User extends BaseModel
 
          //离线后,取最近一条消息
          if ($is_auto && $lastSendTime == 0) {
-             $map1 = [['to_user', '=', $auto_cs_uid], ['is_last', '=', 1], ['is_group', '=', 0]];
-             $map2 = [['from_user', '=', $auto_cs_uid], ['is_last', '=', 1], ['is_group', '=', 0]];
+             $map = [['chat_identify', '=', $auto_cs_uid.'-'.$v['user_id']],['is_last', '=', 1], ['is_group', '=', 0]];
              $lastMessage = Db::name('message')
                  ->field($msgField)
-                 ->whereOr([$map1, $map2])
+                 ->where($map)
                  ->order('create_time desc')
                  ->find();
              if ($lastMessage) {