From 4e2ec57f909704f9f5390eb7535d0d80ac4d7421 Mon Sep 17 00:00:00 2001 From: ASUS <515617283@qq.com> Date: Wed, 19 Nov 2025 19:31:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=B3=E6=97=B6=E6=B6=88=E6=81=AF=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=AE=8C=E7=BB=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/chat/model/ChatMessage.php | 90 ++++++++++++++++++---------- app/middleware/JwtAuthMiddleware.php | 18 ++---- extend/chat/model/Base.php | 59 ++++++++++++++++++ extend/chat/model/OnbocChat.php | 3 +- plugin/webman/gateway/Events.php | 2 +- 5 files changed, 126 insertions(+), 46 deletions(-) diff --git a/app/chat/model/ChatMessage.php b/app/chat/model/ChatMessage.php index 43cac9c..f709df8 100644 --- a/app/chat/model/ChatMessage.php +++ b/app/chat/model/ChatMessage.php @@ -2,6 +2,7 @@ namespace app\chat\model; +use ba\Random; use support\Redis; use support\think\Db; @@ -11,41 +12,70 @@ class ChatMessage extends BaseModel public static function getconverlist($uid, $user_type) { - // 获取所有会话ID + // 获取所有会话ID 自己发送过消息的 $sessionIds = Db::name('chat_message')->where('from_id', $uid)->WhereOr('to_id', $uid) - ->group('session_id')->column('session_id'); + ->group('session_id')->field('session_id,to_id as group_id,chat_type')->select()->toArray(); + // 查看自己加了那些群 + $group_arr = Db::name('chat_group_member')->where(['user_id' => $uid])->field('group_id')->select()->toArray(); $data = []; - $i = 0; - foreach ($sessionIds as $k => $session_id) { - // 未读计数 - $unreadCount = Db::name('chat_unread_count')->where(['user_id' => $uid, 'session_id' => $session_id])->value('count')?:0; - $data[$i]['unread_count'] = $unreadCount; - // 最后一条消息 - $lastMsg = Db::name('chat_message') - ->where('session_id', $session_id) - ->order('send_time', 'desc') - ->find(); - if($lastMsg) { - $chat_user = Db::name('chat_user')->where(['user_id' => $lastMsg['from_id']])->field('nick_name,avatar')->find(); - // 如果是自己发的不显示发送人 - if($lastMsg['from_id'] == $uid) { - $data[$i]['msg'] = $lastMsg['content']; - }else { - // 如果是别人发的显示昵称 - $data[$i]['msg'] = $chat_user['nick_name'] . ':' . $lastMsg['content']; + if($sessionIds) { + foreach ($sessionIds as $k => $session_id) { + foreach ($group_arr as $gk => &$group) { + if($session_id['chat_type'] == 2) { + if ($group['group_id'] == $session_id['group_id']) { + unset($group_arr[$gk]); + }else { + $group['session_id'] = Random::build('alnum', 32); + $group['chat_type'] = 2; + } + } } - $data[$i]['avatar'] = $chat_user['avatar']; - $data[$i]['chat_type'] = $lastMsg['chat_type']; - }else { - $data[$i][] = ''; } - $data[$i]['send_time'] = formatWeChatTime($lastMsg['send_time']); - $data[$i]['session_id'] = $session_id; - $i ++; - } - if(Redis::set('usermsg:list:' . $user_type . ':' . $uid, json_encode($data))) { - return $data; + $sessionIds = array_merge($sessionIds, $group_arr); + $i = 0; + foreach ($sessionIds as $k => $session) { + // 未读计数 + $unreadCount = Db::name('chat_unread_count')->where(['user_id' => $uid, 'session_id' => $session['session_id']])->value('count')?:0; + $data[$i]['unread_count'] = $unreadCount; +// // 最后一条消息 + $lastMsg = Db::name('chat_message') + ->where('session_id', $session['session_id']) + ->order('send_time', 'desc') + ->find(); + if($session['chat_type'] == 1) { + $chat_user = Db::name('chat_user')->where(['user_id' => $session['group_id']])->field('nick_name,avatar')->find(); + $avatar = $chat_user['avatar']; + $name = $chat_user['nick_name']; + }else { + $group_info = Db::name('chat_group')->where(['id' => $session['group_id']])->field('id,name,avatar')->find(); + $avatar = $group_info['avatar']; + $name = $group_info['name']; + if($lastMsg) { + $chat_user = Db::name('chat_user')->where(['user_id' => $lastMsg['from_id']])->field('nick_name,avatar')->find(); + } + } + // 单聊的话 两个人对发用一样的会话ID + $msg = ''; + if($lastMsg) { + // 如果是自己发的不显示发送人 + if($lastMsg['from_id'] == $uid) { + $msg = $lastMsg['content']; + }else { + // 如果是别人发的显示昵称 + $msg = $chat_user['nick_name'] . ':' . $lastMsg['content']; + } + $data[$i]['send_time'] = formatWeChatTime($lastMsg['send_time']); + } + $data[$i]['avatar'] = $avatar; + $data[$i]['chat_type'] = $session['chat_type']; + $data[$i]['session_id'] = $session['session_id']; + $data[$i]['name'] = $name; + $data[$i]['msg'] = $msg; + $i ++; + } + Redis::set('usermsg:list:' . $user_type . ':' . $uid, json_encode($data)); } + return $data; } diff --git a/app/middleware/JwtAuthMiddleware.php b/app/middleware/JwtAuthMiddleware.php index 381d085..4341dad 100644 --- a/app/middleware/JwtAuthMiddleware.php +++ b/app/middleware/JwtAuthMiddleware.php @@ -3,6 +3,7 @@ namespace app\middleware; use app\exception\MyBusinessException; +use extend\chat\model\Base; use support\exception\BusinessException; use support\Redis; use Webman\Http\Response; @@ -13,20 +14,9 @@ class JwtAuthMiddleware implements MiddlewareInterface { public function process(Request $request, callable $handler) : Response { - $uid = Redis::get('token:client:token:' . $request->header('token')); - if($uid) { - // 用户 - $user_type = 1; - }else{ - $uid = Redis::get('token:admin:token:' . $request->header('token')); - if(!$uid) { - throw new MyBusinessException('请登录', 3000); - } - // 商家 - $user_type = 2; - } - $request->setPost('uid', $uid); - $request->setPost('user_type', $user_type); + $user_info = Base::getUser($request->header('token')); + $request->setPost('uid', $user_info['uid']); + $request->setPost('user_type', $user_info['user_type']); return $handler($request); } } diff --git a/extend/chat/model/Base.php b/extend/chat/model/Base.php index f725653..433ebc7 100644 --- a/extend/chat/model/Base.php +++ b/extend/chat/model/Base.php @@ -1,9 +1,12 @@ $uid, 'user_type' => $user_type]; + } + + + public static function init(string $client_id, array $message) + { + $user_info = self::getUser($message['token']); + // 先拿到会话列表 + $list_json = Redis::get('usermsg:list:' . $user_info['user_type'] . ':' . $user_info['uid']); + if($list_json) { + $list_arr = json_decode($list_json, true); + // 挨个加入分组 + foreach ($list_arr as $k => $list) { + // 按照会话ID加入分组 不能按照会话ID绑定 + Gateway::joinGroup($client_id, $list['session_id']); + } + }else { + // 如果没有会话列表,则绑定uid + Gateway::bindUid($client_id, self::getUid($user_info['uid'], $user_info['user_type'])); + } + } + + // uid + public static function getUid($uid, $user_type) + { + return $user_type . '_' . $uid; + } + + // 商家与用户的分组 (单聊) + public static function storeAndUserGroup($store_id, $user_id) + { + return 'store_' . $store_id . '-' . 'user_' . $user_id; + } + + // 群聊的分组 + public static function storeGroup($group_id) + { + return 'group_' . $group_id ; + } + } \ No newline at end of file diff --git a/extend/chat/model/OnbocChat.php b/extend/chat/model/OnbocChat.php index a3280df..3674057 100644 --- a/extend/chat/model/OnbocChat.php +++ b/extend/chat/model/OnbocChat.php @@ -18,7 +18,8 @@ class OnbocChat extends Base // 初始化 public static function init(string $client_id, array $message):void { - print_r(date('H:i:s') . '--初始化运营聊天' . $client_id . $client_id . "\r\n"); + Base::init($client_id, $message); + Gateway::sendToClient($client_id, json_encode(['msg' => 'success'])); } } \ No newline at end of file diff --git a/plugin/webman/gateway/Events.php b/plugin/webman/gateway/Events.php index 47c4190..f034331 100644 --- a/plugin/webman/gateway/Events.php +++ b/plugin/webman/gateway/Events.php @@ -112,7 +112,7 @@ class Events return; } - // 即时聊天 手机端 + // 即时聊天 转发 手机端 if(strstr($message['type'], 'Chat')) { (new \extend\chat\model\Base)->handles($client_id, $message); return;