即时消息接口完结

This commit is contained in:
2025-11-19 19:31:47 +08:00
parent 2a89361d3e
commit 4e2ec57f90
5 changed files with 126 additions and 46 deletions

View File

@@ -2,6 +2,7 @@
namespace app\chat\model;
use ba\Random;
use support\Redis;
use support\think\Db;
@@ -11,42 +12,71 @@ 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;
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;
}
}
}
}
$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_id])->value('count')?:0;
$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_id)
->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) {
$data[$i]['msg'] = $lastMsg['content'];
$msg = $lastMsg['content'];
}else {
// 如果是别人发的显示昵称
$data[$i]['msg'] = $chat_user['nick_name'] . '' . $lastMsg['content'];
}
$data[$i]['avatar'] = $chat_user['avatar'];
$data[$i]['chat_type'] = $lastMsg['chat_type'];
}else {
$data[$i][] = '';
$msg = $chat_user['nick_name'] . '' . $lastMsg['content'];
}
$data[$i]['send_time'] = formatWeChatTime($lastMsg['send_time']);
$data[$i]['session_id'] = $session_id;
}
$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 ++;
}
if(Redis::set('usermsg:list:' . $user_type . ':' . $uid, json_encode($data))) {
Redis::set('usermsg:list:' . $user_type . ':' . $uid, json_encode($data));
}
return $data;
}
}
}

View File

@@ -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);
}
}

View File

@@ -1,9 +1,12 @@
<?php
namespace extend\chat\model;
use app\exception\MyBusinessException;
use GatewayWorker\Lib\Gateway;
use extend\chat\model\OnbocChat;
use extend\chat\model\ShoppingChat;
use support\Redis;
class Base
{
public function handles($client_id, $message)
@@ -14,4 +17,60 @@ namespace extend\chat\model;
}
call_user_func_array(['extend\chat\model\\' . $message['type'], $message['operate_type']], [$client_id, $message]);
}
public static function getUser($token)
{
$uid = Redis::get('token:client:token:' . $token);
if($uid) {
// 用户
$user_type = 1;
}else{
$uid = Redis::get('token:admin:token:' . $token);
if(!$uid) {
throw new MyBusinessException('请登录', 3000);
}
// 商家
$user_type = 2;
}
return ['uid' => $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 ;
}
}

View File

@@ -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']));
}
}

View File

@@ -112,7 +112,7 @@ class Events
return;
}
// 即时聊天 手机端
// 即时聊天 转发 手机端
if(strstr($message['type'], 'Chat')) {
(new \extend\chat\model\Base)->handles($client_id, $message);
return;