聊天,基本

This commit is contained in:
2025-11-21 17:03:52 +08:00
parent 57319e03ce
commit 8982010423
3 changed files with 76 additions and 33 deletions

View File

@@ -317,38 +317,33 @@ class GroupController extends ApiController
}
/**
* 设置群免打扰 (废弃)
* 设置群免打扰
*/
public function setDoNotDisturb(Request $request): Response
{
$groupId = $request->post('group_id');
$status = $request->post('status'); // 0=关闭1=开启
if (!$groupId) {
return $this->error('缺少group_id');
}
// 验证是否在群内
$isMember = Db::name('chat_group_member')->where([
'group_id' => $groupId,
'user_id' => $this->uid,
'quit_time' => null,
'is_kicked' => 0
])->exists();
])->find();
if (!$isMember) {
return $this->error('不在群内');
}
// 生成会话ID
$sessionId = Session::generateSessionId(2, $uid, $groupId);
// 更新免打扰状态
ChatDoNotDisturb::updateOrCreate(
['user_id' => $uid, 'session_id' => $sessionId],
['status' => $status, 'updated_at' => time()]
);
return json(['code' => 200, 'msg' => $status ? '开启免打扰成功' : '关闭免打扰成功']);
$res = Db::name('chat_group_member')->where([
'group_id' => $groupId,
'user_id' => $this->uid,
])->update(['is_dist' => $status]);
if($res) {
return $this->success();
}
return $this->error();
}
/**

View File

@@ -129,11 +129,12 @@ namespace extend\chat\model;
// 回馈
Gateway::sendToClient($client_id, self::sendGormat($message, $list_arr));
$str_uid = self::getUid(self::$user_info['uid'], self::$user_info['user_type']);
// 上线后立马发送没有发送的消息
$send_data = Redis::get('nosuccessful:message:sent:' . $str_uid);
$send_data = Business::getRedisMessage(self::$user_info['uid'], self::$user_info['user_type']);
if($send_data) {
Gateway::sendToClient($client_id, $send_data);
foreach ($send_data as $k => $v) {
Gateway::sendToClient($client_id, $v);
}
}
}
@@ -241,8 +242,8 @@ namespace extend\chat\model;
Gateway::sendToUid($str_uid, self::sendReceive($notice_data));
return;
}else {
// 不在线的存下来等上线了发
Redis::set('nosuccessful:message:sent:' . $str_uid, json_encode($notice_data));
// 存消息
Business::setRedisMessage($message['to_id'], $message['to_user_type'], $notice_data);
}
}elseif ($message['chat_type'] == 2) {
@@ -258,13 +259,14 @@ namespace extend\chat\model;
if($grep['user_id'] != self::$user_info['uid']) {
$user_type = $grep['role']==1?2:1;
$str_uid = self::getUid($grep['user_id'], $user_type);
// 群成员是否在线,在线发 不在线存起来等上线了发
// 群成员是否在线,在线发 不在线存起来等上线了发 免打扰也不发消息
if($grep['is_dist'] == 0) {
if(Gateway::isUidOnline($str_uid)) {
Gateway::sendToUid($str_uid, self::sendReceive($notice_data));
return;
}else {
// 不在线的存下来等上线了发
Redis::set('nosuccessful:message:sent:' . $str_uid, json_encode($notice_data));
// 存消息
Business::setRedisMessage($grep['user_id'], $user_type, $notice_data);
}
// 加入未读计数
if(!Db::name('chat_unread_count')->where(['user_id' => $grep['user_id']])->find()) {
@@ -275,6 +277,7 @@ namespace extend\chat\model;
}
}
}
}
// 给发送者反馈
Gateway::sendToClient($client_id, self::sendGormat($message));
}

View File

@@ -0,0 +1,45 @@
<?php
namespace extend\chat\model;
use ba\Random;
use support\Redis;
class Business
{
// 存消息
public static function setRedisMessage($uid, $user_type, $data)
{
$str_uid = Base::getUid($uid, $user_type);
$rand_msg_str = $str_uid . Random::build();
$set_str = 'nomsg:message:' . $str_uid;
$set_info_str = 'nomsg:message:' . $rand_msg_str;
$sent = Redis::get($set_str);
if($sent) {
$sent = json_decode($sent, true);
$sent[count($sent - 1)] = $set_info_str;
Redis::set($set_str, json_encode($sent));
}else {
Redis::set($set_str, json_encode([$set_info_str]));
}
Redis::set($set_info_str, json_encode($data));
}
// 拉消息
public static function getRedisMessage($uid, $user_type)
{
$str_uid = Base::getUid($uid, $user_type);
$set_str = 'nomsg:message:' . $str_uid;
$sent = Redis::get($set_str);
$data = [];
if($sent) {
$sent = json_decode($sent, true);
foreach ($sent as $k => $v) {
$data[] = json_decode(Redis::get($v), true);
Redis::del($v);
}
Redis::del($set_str);
}
return $data;
}
}