76 lines
2.3 KiB
PHP
76 lines
2.3 KiB
PHP
<?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)
|
||
{
|
||
if(empty($message['type']) || empty($message['operate_type']) || empty($message['user_id']) || empty($message['shop_id'])) {
|
||
Gateway::sendToClient($client_id, json_encode(['msg' => '参数不完整']));
|
||
return;
|
||
}
|
||
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 ;
|
||
}
|
||
|
||
} |