聊天,基本

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

@@ -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,19 +259,21 @@ 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(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));
}
// 加入未读计数
if(!Db::name('chat_unread_count')->where(['user_id' => $grep['user_id']])->find()) {
Db::name('chat_unread_count')->where(['user_id' => $grep['user_id'], 'session_id' => $session_id])->update(['count' => 1, 'updated_time' => d()]);
}else {
Db::name('chat_unread_count')->where(['user_id' => $grep['user_id'], 'session_id' => $session_id])->inc('count')->update(['updated_time' => d()]);
// 群成员是否在线,在线发 不在线存起来等上线了发 免打扰也不发消息
if($grep['is_dist'] == 0) {
if(Gateway::isUidOnline($str_uid)) {
Gateway::sendToUid($str_uid, self::sendReceive($notice_data));
return;
}else {
// 存消息
Business::setRedisMessage($grep['user_id'], $user_type, $notice_data);
}
// 加入未读计数
if(!Db::name('chat_unread_count')->where(['user_id' => $grep['user_id']])->find()) {
Db::name('chat_unread_count')->where(['user_id' => $grep['user_id'], 'session_id' => $session_id])->update(['count' => 1, 'updated_time' => d()]);
}else {
Db::name('chat_unread_count')->where(['user_id' => $grep['user_id'], 'session_id' => $session_id])->inc('count')->update(['updated_time' => d()]);
}
}
}
}

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;
}
}