即时消息接口完结

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

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