174 lines
6.1 KiB
PHP
174 lines
6.1 KiB
PHP
<?php
|
|
namespace extend\chat\model;
|
|
|
|
use app\chat\model\ChatGroupMute;
|
|
use app\exception\MyBusinessException;
|
|
use ba\Random;
|
|
use GatewayWorker\Lib\Gateway;
|
|
use extend\chat\model\OnbocChat;
|
|
use extend\chat\model\ShoppingChat;
|
|
use support\Redis;
|
|
use support\think\Db;
|
|
|
|
class Base
|
|
{
|
|
public static $user_info;
|
|
public function handles($client_id, $message)
|
|
{
|
|
if(empty($message['type']) || empty($message['operate_type']) || empty($message['shop_id']) || empty($message['token'])) {
|
|
Gateway::sendToClient($client_id, json_encode(['msg' => 'type/operate_type/shop_id/token参数必传']));
|
|
return;
|
|
}
|
|
|
|
// 发送消息
|
|
if($message['operate_type'] == 'sendMsg') {
|
|
if (
|
|
empty($message['to_id']) ||
|
|
empty($message['to_id_type']) ||
|
|
empty($message['chat_type']) ||
|
|
empty($message['msg_type'])
|
|
) {
|
|
Gateway::sendToClient($client_id, json_encode(['msg' => 'to_id/to_user_type/chat_type/msg_type参数必传']));
|
|
return;
|
|
}
|
|
|
|
// 文本消息
|
|
if($message['msg_type'] == 1 && empty($message['content'])) {
|
|
Gateway::sendToClient($client_id, json_encode(['msg' => 'content参数必传']));
|
|
return;
|
|
}
|
|
// 图片消息
|
|
if($message['msg_type'] == 2 && empty($message['image_url'])) {
|
|
Gateway::sendToClient($client_id, json_encode(['msg' => 'image_url参数必传']));
|
|
return;
|
|
}
|
|
// 订单消息
|
|
if($message['msg_type'] == 3 && empty($message['order_id'])) {
|
|
Gateway::sendToClient($client_id, json_encode(['msg' => 'order_id参数必传']));
|
|
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)
|
|
{
|
|
self::$user_info = self::getUser($message['token']);
|
|
// 先拿到会话列表
|
|
$list_json = Redis::get('usermsg:list:' . self::$user_info['user_type'] . ':' . self::$user_info['uid']);
|
|
$list_arr = [];
|
|
// 绑定自己的UID
|
|
Gateway::bindUid($client_id, self::getUid(self::$user_info['uid'], self::$user_info['user_type']));
|
|
if($list_json) {
|
|
$list_arr = json_decode($list_json, true);
|
|
// 挨个加入分组
|
|
foreach ($list_arr as $k => $list) {
|
|
if($list['chat_type'] == 2) {
|
|
// 如果是群聊 按照会话ID加入分组 不能按照会话ID绑定
|
|
Gateway::joinGroup($client_id, $list['group_id']);
|
|
}
|
|
}
|
|
}
|
|
return $list_arr;
|
|
}
|
|
|
|
|
|
// 发送消息
|
|
public static function sendMsg(string $client_id, array $message)
|
|
{
|
|
$group_id = null;
|
|
if($message['chat_type'] == 2) {
|
|
$group_id = $message['to_id'];
|
|
// 查询此人是否被禁言
|
|
$chat_group_mute = ChatGroupMute::getUserMuteStatus(self::$user_info['uid'], $group_id);
|
|
if($chat_group_mute['is_mute'] == 1) {
|
|
Gateway::sendToClient($client_id, json_encode(['msg' => '你已经被禁言,' . $chat_group_mute['ex_mute_time'] . '解除']));
|
|
return;
|
|
}
|
|
if($chat_group_mute['is_mute'] == 2) {
|
|
Gateway::sendToClient($client_id, json_encode(['msg' => '你已被永久禁言']));
|
|
return;
|
|
}
|
|
|
|
// 是否退群
|
|
|
|
|
|
|
|
|
|
}
|
|
$session_id = $message['session_id'];
|
|
if(empty($session_id)) {
|
|
// 单聊
|
|
if($message['chat_type'] == 1) {
|
|
// 他是否给我发过消息
|
|
$to_message_one = Db::name('chat_message')->where(['to_id' => self::$user_info['uid'], 'from_id' => $message['to_id'] , 'chat_type' => 1])->find();
|
|
// 我是否给他发过消息
|
|
$from_message_one = Db::name('chat_message')->where(['from_id' => self::$user_info['uid'], 'to_id' => $message['to_id'], 'chat_type' => 1])->find();
|
|
// 如果都没发过消息
|
|
if(!$to_message_one && !$from_message_one) {
|
|
$session_id = Random::build('alnum', 32);
|
|
}
|
|
// 如果他给我发过消息并且我没给他发过消息
|
|
if($to_message_one && !$from_message_one) {
|
|
$session_id = $to_message_one['session_id'];
|
|
}
|
|
}elseif ($message['chat_type'] == 2) {
|
|
// 群聊
|
|
// 如果发过群聊消息
|
|
$to_message_one = Db::name('chat_message')->where(['from_id' => self::$user_info['uid'], 'to_id' => $message['to_id'], 'chat_type' => 2])->find();
|
|
if($to_message_one) {
|
|
$session_id = $to_message_one['session_id'];
|
|
}else {
|
|
$session_id = Random::build('alnum', 32);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
Db::name()->insert();
|
|
// 通知
|
|
|
|
}
|
|
|
|
|
|
// uid
|
|
public static function getUid($uid, $user_type)
|
|
{
|
|
return $user_type . '_' . $uid;
|
|
}
|
|
|
|
public static function sendGormat($message, $data, $status = 1):string
|
|
{
|
|
|
|
$data = [
|
|
'type' => $message['type'],
|
|
'operate_type' => $message['operate_type'],
|
|
'status' => $status,
|
|
'data' => $data
|
|
];
|
|
$json = json_encode($data);
|
|
return $json;
|
|
}
|
|
|
|
|
|
|
|
} |