修改了已知问题

This commit is contained in:
2025-11-27 16:30:28 +08:00
parent 44c979d3c7
commit 37ea8758b8
3 changed files with 68 additions and 26 deletions

View File

@@ -21,13 +21,11 @@ class ChatMessage extends BaseModel
if($sessionIds) {
foreach ($sessionIds as $k => $session_id) {
foreach ($group_arr as $gk => &$group) {
$group['session_id'] = '';
$group['chat_type'] = 2;
if($session_id['chat_type'] == 2) {
if ($group['group_id'] == $session_id['group_id']) {
unset($group_arr[$gk]);
}else {
// $group['session_id'] = Random::build('alnum', 32);
$group['session_id'] = '';
$group['chat_type'] = 2;
}
}
}
@@ -36,14 +34,25 @@ class ChatMessage extends BaseModel
$group_id = '';
$i = 0;
foreach ($sessionIds as $k => $session) {
if($session['session_id']) {
// 未读计数
$unreadCount = Db::name('chat_unread_count')->where(['user_id' => $uid, 'session_id' => $session['session_id']])->value('count')?:0;
}else {
$unreadCount = 0;
}
$data[$i]['unread_count'] = $unreadCount;
// // 最后一条消息
if($session['session_id']) {
// 最后一条消息
$lastMsg = Db::name('chat_message')
->where('session_id', $session['session_id'])
->order('send_time', 'desc')
->find();
}else {
$lastMsg = '';
}
if($session['chat_type'] == 1) {
$chat_user = Db::name('chat_user')->where(['user_id' => $session['group_id']])->field('nick_name,avatar')->find();
$avatar = $chat_user['avatar'];

View File

@@ -20,7 +20,7 @@ namespace extend\chat\model;
Gateway::sendToClient($client_id, json_encode(['msg' => 'type/operate_type/shop_id/token参数必传']));
return;
}
self::$user_info = self::getUser($message['token']);
// 发送消息
if($message['operate_type'] == 'sendMsg') {
if (
@@ -114,7 +114,6 @@ namespace extend\chat\model;
public static function init(string $client_id, array $message)
{
self::$user_info = self::getUser($message['token']);
// 存入chat_user
self::insertChatUser();
$list_arr = [];
@@ -185,11 +184,9 @@ namespace extend\chat\model;
if($to_message_one && !$from_message_one) {
$session_id = $to_message_one['session_id'];
}
// 加入未读计数
if(!Db::name('chat_unread_count')->where(['user_id' => $message['to_id']])->find()) {
Db::name('chat_unread_count')->where(['user_id' => $message['to_id'], 'session_id' => $session_id])->update(['count' => 1, 'updated_time' => d()]);
}else {
Db::name('chat_unread_count')->where(['user_id' => $message['to_id'], 'session_id' => $session_id])->inc('count')->update(['updated_time' => d()]);
// 如果我给我发过消息并且他没给我发过消息
if($from_message_one && !$to_message_one) {
$session_id = $from_message_one['session_id'];
}
}elseif ($message['chat_type'] == 2) {
// 群聊
@@ -207,7 +204,6 @@ namespace extend\chat\model;
$notice_data = [
'from_id' => self::$user_info['uid'],
'to_id' => $message['to_id'],
'operate_type' => $message['operate_type'],
'group_id' => $group_id,
'chat_type' => $message['chat_type'],
'msg_type' => $message['msg_type'],
@@ -218,6 +214,7 @@ namespace extend\chat\model;
'session_id' => $session_id,
];
$res = Db::name('chat_message')->insert($notice_data);
$notice_data['operate_type'] = $message['operate_type'];
if(!$res) {
// 插入失败
Gateway::sendToClient($client_id, self::sendGormat($message, [], 0));
@@ -236,11 +233,16 @@ namespace extend\chat\model;
$notice_data['nick_name'] = $chat_user['nick_name'];
$str_uid = self::getUid($message['to_id'], $message['to_user_type']);
// 更新对方会话列表
ChatMessage::getconverlist($message['to_id'], $message['to_user_type']);
// ChatMessage::getconverlist($message['to_id'], $message['to_user_type']);
// 对方是否在线,在线发 不在线存起来等上线了发
if(Gateway::isUidOnline($str_uid)) {
// 加入未读计数
if(!Db::name('chat_unread_count')->where(['user_id' => $message['to_id'], 'session_id' => $session_id])->find()) {
Db::name('chat_unread_count')->insert(['user_id' => $message['to_id'], 'session_id' => $session_id, 'count' => 1, 'updated_time' => d()]);
}else {
Db::name('chat_unread_count')->where(['user_id' => $message['to_id'], 'session_id' => $session_id])->inc('count')->update(['updated_time' => d()]);
}
Gateway::sendToUid($str_uid, self::sendReceive($notice_data));
return;
}else {
// 存消息
Business::setRedisMessage($message['to_id'], $message['to_user_type'], $notice_data);
@@ -263,14 +265,13 @@ namespace extend\chat\model;
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()]);
if(!Db::name('chat_unread_count')->where(['user_id' => $grep['user_id'], 'session_id' => $session_id])->find()) {
Db::name('chat_unread_count')->insert(['count' => 1, 'updated_time' => d(), 'user_id' => $grep['user_id'], 'session_id' => $session_id]);
}else {
Db::name('chat_unread_count')->where(['user_id' => $grep['user_id'], 'session_id' => $session_id])->inc('count')->update(['updated_time' => d()]);
}

View File

@@ -3,12 +3,13 @@
namespace extend\chat\model;
use ba\Random;
use EasyWeChat\Factory;
use support\Redis;
class Business
{
// 存消息
public static function setRedisMessage($uid, $user_type, $data)
public static function setRedisMessage($uid, $user_type, $data, $open_id = '')
{
$str_uid = Base::getUid($uid, $user_type);
$rand_msg_str = $str_uid . Random::build();
@@ -23,6 +24,37 @@ class Business
Redis::set($set_str, json_encode([$set_info_str]));
}
Redis::set($set_info_str, json_encode($data));
if($open_id) {
// 保存的同时发送模版消息通知用户有未读消息
$config = [
'app_id' => config('cons.app_id'),
'secret' => config('cons.secret'),
'response_type' => 'array',
];
$app = Factory::officialAccount($config);
$acc_token = Redis::get('wx:ac:AccessToken');
if($acc_token) {
$app['access_token']->setToken($acc_token);
}
$wechat_res = $app->template_message->send([
'touser' => $open_id,
'template_id' => 'hGsUGZlWqWC9TMm4ZGZuz0OwE4gwSmvgbr5ecmTDvq4',
'miniprogram' => [
'appid' => 'wxd88fffa983758a30', // 零点八零
'pagepath' => 'pages/index/index', // 零点八零
],
'data' => [
'thing7' => $record['shop_name'],
'thing14' => $record['activity_detail'],
'time3' => $record['activity_time'],
'thing16' => $record['address'],
],
]);
}
}
// 拉消息