初始化聊天,完结
This commit is contained in:
@@ -1,20 +1,53 @@
|
||||
<?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['user_id']) || empty($message['shop_id'])) {
|
||||
Gateway::sendToClient($client_id, json_encode(['msg' => '参数不完整']));
|
||||
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]);
|
||||
}
|
||||
|
||||
@@ -39,38 +72,103 @@ namespace extend\chat\model;
|
||||
|
||||
public static function init(string $client_id, array $message)
|
||||
{
|
||||
$user_info = self::getUser($message['token']);
|
||||
self::$user_info = self::getUser($message['token']);
|
||||
// 先拿到会话列表
|
||||
$list_json = Redis::get('usermsg:list:' . $user_info['user_type'] . ':' . $user_info['uid']);
|
||||
$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) {
|
||||
// 按照会话ID加入分组 不能按照会话ID绑定
|
||||
Gateway::joinGroup($client_id, $list['session_id']);
|
||||
if($list['chat_type'] == 2) {
|
||||
// 如果是群聊 按照会话ID加入分组 不能按照会话ID绑定
|
||||
Gateway::joinGroup($client_id, $list['group_id']);
|
||||
}
|
||||
}
|
||||
}else {
|
||||
// 如果没有会话列表,则绑定uid
|
||||
Gateway::bindUid($client_id, self::getUid($user_info['uid'], $user_info['user_type']));
|
||||
}
|
||||
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 storeAndUserGroup($store_id, $user_id)
|
||||
public static function sendGormat($message, $data, $status = 1):string
|
||||
{
|
||||
return 'store_' . $store_id . '-' . 'user_' . $user_id;
|
||||
|
||||
$data = [
|
||||
'type' => $message['type'],
|
||||
'operate_type' => $message['operate_type'],
|
||||
'status' => $status,
|
||||
'data' => $data
|
||||
];
|
||||
$json = json_encode($data);
|
||||
return $json;
|
||||
}
|
||||
|
||||
// 群聊的分组
|
||||
public static function storeGroup($group_id)
|
||||
{
|
||||
return 'group_' . $group_id ;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -16,10 +16,15 @@ use think\Model;
|
||||
class OnbocChat extends Base
|
||||
{
|
||||
// 初始化
|
||||
public static function init(string $client_id, array $message):void
|
||||
public static function init(string $client_id, array $message)
|
||||
{
|
||||
Base::init($client_id, $message);
|
||||
Gateway::sendToClient($client_id, json_encode(['msg' => 'success']));
|
||||
Gateway::sendToClient($client_id, self::sendGormat($message, Base::init($client_id, $message)));
|
||||
}
|
||||
|
||||
public static function sendMsg(string $client_id, array $message)
|
||||
{
|
||||
Base::sendMsg($client_id, $message);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -18,8 +18,15 @@ class ShoppingChat extends Base
|
||||
// 初始化
|
||||
public static function init(string $client_id, array $message):void
|
||||
{
|
||||
print_r( date('H:i:s') . '--初始化运用户聊天' . $client_id . "\r\n");
|
||||
Gateway::sendToClient($client_id, self::sendGormat($message, Base::init($client_id, $message)));
|
||||
}
|
||||
|
||||
public static function sendMsg(string $client_id, array $message)
|
||||
{
|
||||
Base::sendMsg($client_id, $message);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user