2025-09-16 09:57:06 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace app\chukebao\controller;
|
|
|
|
|
|
|
2025-10-28 16:46:38 +08:00
|
|
|
|
use app\chukebao\model\FriendSettings;
|
2025-09-16 09:57:06 +08:00
|
|
|
|
use library\ResponseHelper;
|
|
|
|
|
|
use think\Db;
|
|
|
|
|
|
|
|
|
|
|
|
class MessageController extends BaseController
|
|
|
|
|
|
{
|
|
|
|
|
|
|
2025-10-21 16:46:55 +08:00
|
|
|
|
public function getList()
|
|
|
|
|
|
{
|
2025-09-16 09:57:06 +08:00
|
|
|
|
$page = $this->request->param('page', 1);
|
2025-10-21 16:46:55 +08:00
|
|
|
|
$limit = $this->request->param('limit', 10);
|
2025-09-16 09:57:06 +08:00
|
|
|
|
$accountId = $this->getUserInfo('s2_accountId');
|
2025-10-21 16:46:55 +08:00
|
|
|
|
if (empty($accountId)) {
|
2025-09-16 09:57:06 +08:00
|
|
|
|
return ResponseHelper::error('请先登录');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-21 16:46:55 +08:00
|
|
|
|
$friends = Db::table('s2_wechat_friend')
|
|
|
|
|
|
->where(['accountId' => $accountId, 'isDeleted' => 0])
|
2025-10-23 09:34:43 +08:00
|
|
|
|
->column('id,nickname,avatar,conRemark,labels,groupId,wechatAccountId,wechatId');
|
2025-10-21 16:46:55 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 构建好友子查询
|
|
|
|
|
|
$friendSubQuery = Db::table('s2_wechat_friend')
|
|
|
|
|
|
->where(['accountId' => $accountId, 'isDeleted' => 0])
|
|
|
|
|
|
->field('id')
|
|
|
|
|
|
->buildSql();
|
|
|
|
|
|
|
2025-10-23 14:12:40 +08:00
|
|
|
|
// 优化后的查询:使用MySQL兼容的查询方式
|
2025-10-21 16:46:55 +08:00
|
|
|
|
$unionQuery = "
|
2025-10-28 09:25:39 +08:00
|
|
|
|
(SELECT m.id, m.content, m.wechatFriendId, m.wechatChatroomId, m.createTime, m.wechatTime,m.wechatAccountId, 2 as msgType, wc.nickname, wc.chatroomAvatar as avatar, wc.chatroomId
|
2025-10-21 16:46:55 +08:00
|
|
|
|
FROM s2_wechat_chatroom wc
|
2025-10-23 14:12:40 +08:00
|
|
|
|
INNER JOIN s2_wechat_message m ON wc.id = m.wechatChatroomId AND m.type = 2
|
|
|
|
|
|
INNER JOIN (
|
|
|
|
|
|
SELECT wechatChatroomId, MAX(wechatTime) as maxTime, MAX(id) as maxId
|
|
|
|
|
|
FROM s2_wechat_message
|
|
|
|
|
|
WHERE type = 2
|
|
|
|
|
|
GROUP BY wechatChatroomId
|
|
|
|
|
|
) latest ON m.wechatChatroomId = latest.wechatChatroomId AND m.wechatTime = latest.maxTime AND m.id = latest.maxId
|
|
|
|
|
|
WHERE wc.accountId = {$accountId} AND wc.isDeleted = 0
|
|
|
|
|
|
)
|
2025-10-21 16:46:55 +08:00
|
|
|
|
UNION ALL
|
2025-10-28 09:25:39 +08:00
|
|
|
|
(SELECT m.id, m.content, m.wechatFriendId, m.wechatChatroomId, m.createTime, m.wechatTime, 1 as msgType, 1 as nickname, 1 as avatar, 1 as chatroomId, 1 as wechatAccountId
|
2025-10-21 16:46:55 +08:00
|
|
|
|
FROM s2_wechat_message m
|
2025-10-23 14:12:40 +08:00
|
|
|
|
INNER JOIN (
|
|
|
|
|
|
SELECT wechatFriendId, MAX(wechatTime) as maxTime, MAX(id) as maxId
|
|
|
|
|
|
FROM s2_wechat_message
|
|
|
|
|
|
WHERE type = 1 AND wechatFriendId IN {$friendSubQuery}
|
|
|
|
|
|
GROUP BY wechatFriendId
|
|
|
|
|
|
) latest ON m.wechatFriendId = latest.wechatFriendId AND m.wechatTime = latest.maxTime AND m.id = latest.maxId
|
2025-10-21 16:46:55 +08:00
|
|
|
|
WHERE m.type = 1 AND m.wechatFriendId IN {$friendSubQuery}
|
2025-10-23 14:12:40 +08:00
|
|
|
|
)
|
|
|
|
|
|
ORDER BY wechatTime DESC
|
2025-10-21 16:46:55 +08:00
|
|
|
|
LIMIT " . (($page - 1) * $limit) . ", {$limit}
|
|
|
|
|
|
";
|
|
|
|
|
|
|
|
|
|
|
|
$list = Db::query($unionQuery);
|
|
|
|
|
|
|
2025-10-23 14:12:40 +08:00
|
|
|
|
// 对分页后的结果进行排序(按wechatTime降序)
|
|
|
|
|
|
usort($list, function ($a, $b) {
|
|
|
|
|
|
return $b['wechatTime'] <=> $a['wechatTime'];
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-10-21 16:46:55 +08:00
|
|
|
|
// 批量统计未读数量(isRead=0),按好友/群聊分别聚合
|
|
|
|
|
|
$friendIds = [];
|
|
|
|
|
|
$chatroomIds = [];
|
|
|
|
|
|
foreach ($list as $row) {
|
|
|
|
|
|
if (!empty($row['wechatFriendId'])) {
|
|
|
|
|
|
$friendIds[] = $row['wechatFriendId'];
|
2025-09-16 09:57:06 +08:00
|
|
|
|
}
|
2025-10-21 16:46:55 +08:00
|
|
|
|
if (!empty($row['wechatChatroomId'])) {
|
|
|
|
|
|
$chatroomIds[] = $row['wechatChatroomId'];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
$friendIds = array_values(array_unique(array_filter($friendIds)));
|
|
|
|
|
|
$chatroomIds = array_values(array_unique(array_filter($chatroomIds)));
|
|
|
|
|
|
|
|
|
|
|
|
$friendUnreadMap = [];
|
|
|
|
|
|
if (!empty($friendIds)) {
|
2025-10-23 14:12:40 +08:00
|
|
|
|
// 获取未读消息数量
|
2025-10-21 16:46:55 +08:00
|
|
|
|
$friendUnreadMap = Db::table('s2_wechat_message')
|
|
|
|
|
|
->where(['isRead' => 0])
|
|
|
|
|
|
->whereIn('wechatFriendId', $friendIds)
|
|
|
|
|
|
->group('wechatFriendId')
|
|
|
|
|
|
->column('COUNT(*) AS cnt', 'wechatFriendId');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$chatroomUnreadMap = [];
|
|
|
|
|
|
if (!empty($chatroomIds)) {
|
2025-10-23 14:12:40 +08:00
|
|
|
|
// 获取未读消息数量
|
2025-10-21 16:46:55 +08:00
|
|
|
|
$chatroomUnreadMap = Db::table('s2_wechat_message')
|
|
|
|
|
|
->where(['isRead' => 0])
|
|
|
|
|
|
->whereIn('wechatChatroomId', $chatroomIds)
|
|
|
|
|
|
->group('wechatChatroomId')
|
|
|
|
|
|
->column('COUNT(*) AS cnt', 'wechatChatroomId');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-28 16:46:38 +08:00
|
|
|
|
$aiTypeData = [];
|
|
|
|
|
|
if (!empty($friendIds)) {
|
|
|
|
|
|
$aiTypeData = FriendSettings::where('friendId', 'in', $friendIds)->column('friendId,type');
|
|
|
|
|
|
}
|
2025-10-23 14:12:40 +08:00
|
|
|
|
|
2025-10-21 16:46:55 +08:00
|
|
|
|
foreach ($list as $k => &$v) {
|
|
|
|
|
|
|
|
|
|
|
|
$createTime = !empty($v['createTime']) ? date('Y-m-d H:i:s', $v['createTime']) : '';
|
|
|
|
|
|
$wechatTime = !empty($v['wechatTime']) ? date('Y-m-d H:i:s', $v['wechatTime']) : '';
|
2025-09-16 09:57:06 +08:00
|
|
|
|
|
2025-10-23 14:12:40 +08:00
|
|
|
|
|
|
|
|
|
|
$unreadCount = 0;
|
2025-10-28 16:46:38 +08:00
|
|
|
|
$v['aiType'] = 0;
|
2025-10-21 16:46:55 +08:00
|
|
|
|
if (!empty($v['wechatFriendId'])) {
|
|
|
|
|
|
$v['nickname'] = !empty($friends[$v['wechatFriendId']]) ? $friends[$v['wechatFriendId']]['nickname'] : '';
|
|
|
|
|
|
$v['avatar'] = !empty($friends[$v['wechatFriendId']]) ? $friends[$v['wechatFriendId']]['avatar'] : '';
|
2025-10-23 09:34:43 +08:00
|
|
|
|
$v['conRemark'] = !empty($friends[$v['wechatFriendId']]) ? $friends[$v['wechatFriendId']]['conRemark'] : '';
|
|
|
|
|
|
$v['groupId'] = !empty($friends[$v['wechatFriendId']]) ? $friends[$v['wechatFriendId']]['groupId'] : '';
|
|
|
|
|
|
$v['wechatAccountId'] = !empty($friends[$v['wechatFriendId']]) ? $friends[$v['wechatFriendId']]['wechatAccountId'] : '';
|
|
|
|
|
|
$v['wechatId'] = !empty($friends[$v['wechatFriendId']]) ? $friends[$v['wechatFriendId']]['wechatId'] : '';
|
2025-10-23 14:12:40 +08:00
|
|
|
|
$v['labels'] = !empty($friends[$v['wechatFriendId']]) ? json_decode($friends[$v['wechatFriendId']]['labels'], true) : [];
|
2025-10-23 09:34:43 +08:00
|
|
|
|
$unreadCount = isset($friendUnreadMap[$v['wechatFriendId']]) ? (int)$friendUnreadMap[$v['wechatFriendId']] : 0;
|
2025-10-28 16:46:38 +08:00
|
|
|
|
$v['aiType'] = isset($aiTypeData[$v['wechatFriendId']]) ? $aiTypeData[$v['wechatFriendId']] : 0;
|
2025-10-23 09:34:43 +08:00
|
|
|
|
unset($v['chatroomId']);
|
2025-09-16 09:57:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-21 16:46:55 +08:00
|
|
|
|
if (!empty($v['wechatChatroomId'])) {
|
2025-10-23 09:34:43 +08:00
|
|
|
|
$v['conRemark'] = '';
|
|
|
|
|
|
$unreadCount = isset($chatroomUnreadMap[$v['wechatChatroomId']]) ? (int)$chatroomUnreadMap[$v['wechatChatroomId']] : 0;
|
2025-10-21 16:46:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$v['id'] = !empty($v['wechatFriendId']) ? $v['wechatFriendId'] : $v['wechatChatroomId'];
|
|
|
|
|
|
$v['config'] = [
|
2025-10-23 09:34:43 +08:00
|
|
|
|
'top' => false,
|
|
|
|
|
|
'unreadCount' => $unreadCount,
|
2025-10-21 16:46:55 +08:00
|
|
|
|
'chat' => true,
|
|
|
|
|
|
'msgTime' => $v['wechatTime'],
|
|
|
|
|
|
];
|
|
|
|
|
|
$v['createTime'] = $createTime;
|
2025-10-23 09:34:43 +08:00
|
|
|
|
$v['lastUpdateTime'] = $wechatTime;
|
2025-10-23 14:12:40 +08:00
|
|
|
|
|
|
|
|
|
|
// 最新消息内容已经在UNION查询中获取,直接使用
|
|
|
|
|
|
$v['latestMessage'] = [
|
|
|
|
|
|
'content' => $v['content'],
|
|
|
|
|
|
'wechatTime' => $wechatTime
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
unset($v['wechatFriendId'], $v['wechatChatroomId']);
|
2025-10-21 16:46:55 +08:00
|
|
|
|
|
2025-09-16 09:57:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
unset($v);
|
|
|
|
|
|
|
|
|
|
|
|
return ResponseHelper::success($list);
|
|
|
|
|
|
}
|
2025-09-17 16:51:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-10-21 16:46:55 +08:00
|
|
|
|
public function readMessage()
|
|
|
|
|
|
{
|
2025-09-17 16:51:11 +08:00
|
|
|
|
$wechatFriendId = $this->request->param('wechatFriendId', '');
|
2025-10-21 16:46:55 +08:00
|
|
|
|
$wechatChatroomId = $this->request->param('wechatChatroomId', '');
|
2025-09-17 16:51:11 +08:00
|
|
|
|
$accountId = $this->getUserInfo('s2_accountId');
|
2025-10-21 16:46:55 +08:00
|
|
|
|
if (empty($accountId)) {
|
2025-09-17 16:51:11 +08:00
|
|
|
|
return ResponseHelper::error('请先登录');
|
|
|
|
|
|
}
|
2025-10-21 16:46:55 +08:00
|
|
|
|
if (empty($wechatChatroomId) && empty($wechatFriendId)) {
|
2025-09-17 16:51:11 +08:00
|
|
|
|
return ResponseHelper::error('参数缺失');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$where = [];
|
2025-10-21 16:46:55 +08:00
|
|
|
|
if (!empty($wechatChatroomId)) {
|
|
|
|
|
|
$where[] = ['wechatChatroomId', '=', $wechatChatroomId];
|
2025-09-17 16:51:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-21 16:46:55 +08:00
|
|
|
|
if (!empty($wechatFriendId)) {
|
|
|
|
|
|
$where[] = ['wechatFriendId', '=', $wechatFriendId];
|
2025-09-17 16:51:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Db::table('s2_wechat_message')->where($where)->update(['isRead' => 1]);
|
2025-09-24 14:09:09 +08:00
|
|
|
|
return ResponseHelper::success([]);
|
|
|
|
|
|
}
|
2025-09-17 16:51:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-10-21 16:46:55 +08:00
|
|
|
|
public function details()
|
|
|
|
|
|
{
|
2025-09-24 14:09:09 +08:00
|
|
|
|
$wechatFriendId = $this->request->param('wechatFriendId', '');
|
2025-10-21 16:46:55 +08:00
|
|
|
|
$wechatChatroomId = $this->request->param('wechatChatroomId', '');
|
|
|
|
|
|
$wechatAccountId = $this->request->param('wechatAccountId', '');
|
|
|
|
|
|
$page = $this->request->param('page', 1);
|
|
|
|
|
|
$limit = $this->request->param('limit', 10);
|
|
|
|
|
|
$from = $this->request->param('From', '');
|
|
|
|
|
|
$to = $this->request->param('To', '');
|
|
|
|
|
|
$olderData = $this->request->param('olderData', false);
|
2025-09-24 14:09:09 +08:00
|
|
|
|
$accountId = $this->getUserInfo('s2_accountId');
|
2025-10-21 16:46:55 +08:00
|
|
|
|
if (empty($accountId)) {
|
2025-09-24 14:09:09 +08:00
|
|
|
|
return ResponseHelper::error('请先登录');
|
|
|
|
|
|
}
|
2025-10-21 16:46:55 +08:00
|
|
|
|
if (empty($wechatChatroomId) && empty($wechatFriendId)) {
|
2025-09-24 14:09:09 +08:00
|
|
|
|
return ResponseHelper::error('参数缺失');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$where = [];
|
2025-10-21 16:46:55 +08:00
|
|
|
|
if (!empty($wechatChatroomId)) {
|
|
|
|
|
|
$where[] = ['wechatChatroomId', '=', $wechatChatroomId];
|
2025-09-24 14:09:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-21 16:46:55 +08:00
|
|
|
|
if (!empty($wechatFriendId)) {
|
|
|
|
|
|
$where[] = ['wechatFriendId', '=', $wechatFriendId];
|
2025-09-24 14:09:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-21 16:46:55 +08:00
|
|
|
|
if (!empty($From) && !empty($To)) {
|
|
|
|
|
|
$where[] = ['wechatTime', 'between', [$from, $to]];
|
2025-09-24 14:09:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$total = Db::table('s2_wechat_message')->where($where)->count();
|
2025-10-21 16:46:55 +08:00
|
|
|
|
$list = Db::table('s2_wechat_message')->where($where)->page($page, $limit)->order('id DESC')->select();
|
2025-09-24 14:09:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-10-21 16:46:55 +08:00
|
|
|
|
foreach ($list as $k => &$v) {
|
|
|
|
|
|
$v['wechatTime'] = !empty($v['wechatTime']) ? date('Y-m-d H:i:s', $v['wechatTime']) : '';
|
2025-09-24 14:09:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-10-21 16:46:55 +08:00
|
|
|
|
return ResponseHelper::success(['total' => $total, 'list' => $list]);
|
2025-09-17 16:51:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-09-16 09:57:06 +08:00
|
|
|
|
}
|