代码提交

This commit is contained in:
wong
2025-10-21 16:46:55 +08:00
parent 19734415f0
commit 7475dbd07c
3 changed files with 126 additions and 89 deletions

View File

@@ -8,67 +8,102 @@ use think\Db;
class MessageController extends BaseController class MessageController extends BaseController
{ {
public function getList(){ public function getList()
{
$page = $this->request->param('page', 1); $page = $this->request->param('page', 1);
$limit = $this->request->param('limit', 10); $limit = $this->request->param('limit', 10);
$accountId = $this->getUserInfo('s2_accountId'); $accountId = $this->getUserInfo('s2_accountId');
if (empty($accountId)){ if (empty($accountId)) {
return ResponseHelper::error('请先登录'); return ResponseHelper::error('请先登录');
} }
$chatroomList = Db::table('s2_wechat_chatroom')->alias('wc') $friends = Db::table('s2_wechat_friend')
->join(['s2_wechat_message' => 'm'], 'wc.id = m.wechatChatroomId', 'LEFT') ->where(['accountId' => $accountId, 'isDeleted' => 0])
->where(['wc.accountId' => $accountId,'m.type' => 2,' wc.isDeleted' => 0]) ->column('id,nickname,avatar');
->order('m.id desc')
->group('m.wechatChatroomId')
->page($page, $limit)
->select();
$friendIds = Db::table('s2_wechat_friend')
->where(['accountId' => $accountId,' isDeleted' => 0])
->group('id')
->column('id');
$friendList = Db::table('s2_wechat_message')
->where(['type' => 1])
->whereIn('wechatFriendId',$friendIds)
->order('id desc')
->group('wechatFriendId')
->page($page, $limit)
->select();
$list = array_merge($chatroomList,$friendList);
// 按createTime字段从大到小排序
usort($list, function($a, $b) {
return $b['createTime'] <=> $a['createTime'];
});
foreach ($list as $k=>&$v){ // 构建好友子查询
$v['createTime'] = !empty($v['createTime']) ? date('Y-m-d H:i:s',$v['createTime']) : ''; $friendSubQuery = Db::table('s2_wechat_friend')
$v['wechatTime'] = !empty($v['wechatTime']) ? date('Y-m-d H:i:s',$v['wechatTime']) : ''; ->where(['accountId' => $accountId, 'isDeleted' => 0])
->field('id')
->buildSql();
if (!empty($v['wechatFriendId'])){ // 使用 UNION 合并群聊和好友消息,然后统一排序和分页
$friend = Db::table('s2_wechat_friend') $unionQuery = "
->where(['id'=>$v['wechatFriendId']]) (SELECT m.id, m.content, m.wechatFriendId, m.wechatChatroomId, m.createTime, m.wechatTime, 2 as msgType, wc.nickname, wc.chatroomAvatar as avatar
->field('id,nickname,avatar') FROM s2_wechat_chatroom wc
->find(); LEFT JOIN s2_wechat_message m ON wc.id = m.wechatChatroomId
$v['msgInfo'] = $friend; WHERE wc.accountId = {$accountId} AND m.type = 2 AND wc.isDeleted = 0
$v['unreadCount'] = Db::table('s2_wechat_message') GROUP BY m.wechatChatroomId)
->where(['wechatFriendId' => $v['wechatFriendId'],'isRead' => 0]) UNION ALL
->count(); (SELECT m.id, m.content, m.wechatFriendId, m.wechatChatroomId, m.createTime, m.wechatTime, 1 as msgType, 1 as nickname, 1 avatar
FROM s2_wechat_message m
WHERE m.type = 1 AND m.wechatFriendId IN {$friendSubQuery}
GROUP BY m.wechatFriendId)
ORDER BY createTime DESC
LIMIT " . (($page - 1) * $limit) . ", {$limit}
";
$list = Db::query($unionQuery);
// 批量统计未读数量isRead=0按好友/群聊分别聚合
$friendIds = [];
$chatroomIds = [];
foreach ($list as $row) {
if (!empty($row['wechatFriendId'])) {
$friendIds[] = $row['wechatFriendId'];
}
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)) {
$friendUnreadMap = Db::table('s2_wechat_message')
->where(['isRead' => 0])
->whereIn('wechatFriendId', $friendIds)
->group('wechatFriendId')
->column('COUNT(*) AS cnt', 'wechatFriendId');
}
$chatroomUnreadMap = [];
if (!empty($chatroomIds)) {
$chatroomUnreadMap = Db::table('s2_wechat_message')
->where(['isRead' => 0])
->whereIn('wechatChatroomId', $chatroomIds)
->group('wechatChatroomId')
->column('COUNT(*) AS cnt', 'wechatChatroomId');
}
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']) : '';
$vunreadCount = 0;
if (!empty($v['wechatFriendId'])) {
$v['nickname'] = !empty($friends[$v['wechatFriendId']]) ? $friends[$v['wechatFriendId']]['nickname'] : '';
$v['avatar'] = !empty($friends[$v['wechatFriendId']]) ? $friends[$v['wechatFriendId']]['avatar'] : '';
$vunreadCount = isset($friendUnreadMap[$v['wechatFriendId']]) ? (int)$friendUnreadMap[$v['wechatFriendId']] : 0;
} }
if (!empty($v['wechatChatroomId'])){ if (!empty($v['wechatChatroomId'])) {
$chatroom = Db::table('s2_wechat_chatroom') $vunreadCount = isset($chatroomUnreadMap[$v['wechatChatroomId']]) ? (int)$chatroomUnreadMap[$v['wechatChatroomId']] : 0;
->where(['id'=>$v['wechatChatroomId']])
->field('id,nickname,chatroomAvatar as avatar')
->find();
$v['msgInfo'] = $chatroom;
$v['unreadCount'] = Db::table('s2_wechat_message')
->where(['wechatChatroomId' => $v['wechatChatroomId'],'isRead' => 0])
->count();
} }
$v['id'] = !empty($v['wechatFriendId']) ? $v['wechatFriendId'] : $v['wechatChatroomId'];
$v['config'] = [
'vunreadCount' => $vunreadCount,
'chat' => true,
'msgTime' => $v['wechatTime'],
];
$v['createTime'] = $createTime;
$v['wechatTime'] = $wechatTime;
unset($v['wechatFriendId'],$v['wechatChatroomId']);
} }
unset($v); unset($v);
@@ -76,24 +111,25 @@ class MessageController extends BaseController
} }
public function readMessage(){ public function readMessage()
{
$wechatFriendId = $this->request->param('wechatFriendId', ''); $wechatFriendId = $this->request->param('wechatFriendId', '');
$wechatChatroomId = $this->request->param('wechatChatroomId', ''); $wechatChatroomId = $this->request->param('wechatChatroomId', '');
$accountId = $this->getUserInfo('s2_accountId'); $accountId = $this->getUserInfo('s2_accountId');
if (empty($accountId)){ if (empty($accountId)) {
return ResponseHelper::error('请先登录'); return ResponseHelper::error('请先登录');
} }
if (empty($wechatChatroomId) && empty($wechatFriendId)){ if (empty($wechatChatroomId) && empty($wechatFriendId)) {
return ResponseHelper::error('参数缺失'); return ResponseHelper::error('参数缺失');
} }
$where = []; $where = [];
if (!empty($wechatChatroomId)){ if (!empty($wechatChatroomId)) {
$where[] = ['wechatChatroomId','=',$wechatChatroomId]; $where[] = ['wechatChatroomId', '=', $wechatChatroomId];
} }
if (!empty($wechatFriendId)){ if (!empty($wechatFriendId)) {
$where[] = ['wechatFriendId','=',$wechatFriendId]; $where[] = ['wechatFriendId', '=', $wechatFriendId];
} }
Db::table('s2_wechat_message')->where($where)->update(['isRead' => 1]); Db::table('s2_wechat_message')->where($where)->update(['isRead' => 1]);
@@ -101,49 +137,47 @@ class MessageController extends BaseController
} }
public function details()
{
public function details(){
$wechatFriendId = $this->request->param('wechatFriendId', ''); $wechatFriendId = $this->request->param('wechatFriendId', '');
$wechatChatroomId = $this->request->param('wechatChatroomId', ''); $wechatChatroomId = $this->request->param('wechatChatroomId', '');
$wechatAccountId = $this->request->param('wechatAccountId', ''); $wechatAccountId = $this->request->param('wechatAccountId', '');
$page = $this->request->param('page', 1); $page = $this->request->param('page', 1);
$limit = $this->request->param('limit', 10); $limit = $this->request->param('limit', 10);
$from = $this->request->param('From', ''); $from = $this->request->param('From', '');
$to = $this->request->param('To', ''); $to = $this->request->param('To', '');
$olderData = $this->request->param('olderData', false); $olderData = $this->request->param('olderData', false);
$accountId = $this->getUserInfo('s2_accountId'); $accountId = $this->getUserInfo('s2_accountId');
if (empty($accountId)){ if (empty($accountId)) {
return ResponseHelper::error('请先登录'); return ResponseHelper::error('请先登录');
} }
if (empty($wechatChatroomId) && empty($wechatFriendId)){ if (empty($wechatChatroomId) && empty($wechatFriendId)) {
return ResponseHelper::error('参数缺失'); return ResponseHelper::error('参数缺失');
} }
$where = []; $where = [];
if (!empty($wechatChatroomId)){ if (!empty($wechatChatroomId)) {
$where[] = ['wechatChatroomId','=',$wechatChatroomId]; $where[] = ['wechatChatroomId', '=', $wechatChatroomId];
} }
if (!empty($wechatFriendId)){ if (!empty($wechatFriendId)) {
$where[] = ['wechatFriendId','=',$wechatFriendId]; $where[] = ['wechatFriendId', '=', $wechatFriendId];
} }
if (!empty($From) && !empty($To)){ if (!empty($From) && !empty($To)) {
$where[] = ['wechatTime','between',[$from,$to]]; $where[] = ['wechatTime', 'between', [$from, $to]];
} }
$total = Db::table('s2_wechat_message')->where($where)->count(); $total = Db::table('s2_wechat_message')->where($where)->count();
$list = Db::table('s2_wechat_message')->where($where)->page($page,$limit)->order('id DESC')->select(); $list = Db::table('s2_wechat_message')->where($where)->page($page, $limit)->order('id DESC')->select();
foreach ($list as $k => &$v) {
foreach ($list as $k=>&$v){ $v['wechatTime'] = !empty($v['wechatTime']) ? date('Y-m-d H:i:s', $v['wechatTime']) : '';
$v['wechatTime'] = !empty($v['wechatTime']) ? date('Y-m-d H:i:s',$v['wechatTime']) : '';
} }
return ResponseHelper::success(['total'=>$total,'list'=>$list]); return ResponseHelper::success(['total' => $total, 'list' => $list]);
} }

View File

@@ -22,19 +22,19 @@ class GetChatroomListV1Controller extends BaseController
$keyword = $this->request->param('keyword', ''); $keyword = $this->request->param('keyword', '');
try { try {
/*$companyId = (int)$this->getUserInfo('companyId'); $companyId = (int)$this->getUserInfo('companyId');
$wechatIds = Db::name('device')->alias('d') $wechatIds = Db::name('device')->alias('d')
// 仅关联每个设备在 device_wechat_login 中的最新一条记录 // 仅关联每个设备在 device_wechat_login 中的最新一条记录
->join('(SELECT MAX(id) AS id, deviceId FROM ck_device_wechat_login WHERE companyId='.$companyId.' GROUP BY deviceId) dwl_max','dwl_max.deviceId = d.id') ->join('(SELECT MAX(id) AS id, deviceId FROM ck_device_wechat_login WHERE companyId='.$companyId.' GROUP BY deviceId) dwl_max','dwl_max.deviceId = d.id')
->join('device_wechat_login dwl','dwl.id = dwl_max.id') ->join('device_wechat_login dwl','dwl.id = dwl_max.id')
->where(['d.companyId' => $companyId,'d.deleteTime' => 0]) ->where(['d.companyId' => $companyId,'d.deleteTime' => 0])
->column('dwl.wechatId');*/ ->column('dwl.wechatId');
$wechatIds = Db::name('device')->alias('d') /* $wechatIds = Db::name('device')->alias('d')
->join('device_wechat_login dwl','dwl.deviceId=d.id AND dwl.companyId='.$this->getUserInfo('companyId')) ->join('device_wechat_login dwl','dwl.deviceId=d.id AND dwl.companyId='.$this->getUserInfo('companyId'))
->where(['d.companyId' => $this->getUserInfo('companyId'),'d.deleteTime' => 0]) ->where(['d.companyId' => $this->getUserInfo('companyId'),'d.deleteTime' => 0])
->column('dwl.wechatId'); ->column('dwl.wechatId');*/
$where = []; $where = [];

View File

@@ -42,16 +42,20 @@ class GetFriendListV1Controller extends BaseController
$where[] = ['nickname|alias|wechatId','like','%'.$keyword.'%']; $where[] = ['nickname|alias|wechatId','like','%'.$keyword.'%'];
} }
$wechatIds = Db::name('device')->alias('d') /* $wechatIds = Db::name('device')->alias('d')
->join('device_wechat_login dwl','dwl.deviceId=d.id AND dwl.companyId='.$this->getUserInfo('companyId')) ->join('device_wechat_login dwl','dwl.deviceId=d.id AND dwl.companyId='.$this->getUserInfo('companyId'))
->where(['d.companyId' => $this->getUserInfo('companyId'),'d.deleteTime' => 0])->group('dwl.deviceId')->order('dwl.id desc'); ->where(['d.companyId' => $this->getUserInfo('companyId'),'d.deleteTime' => 0])
->group('dwl.deviceId')
->order('dwl.id desc');*/
/*$wechatIds = Db::name('device')->alias('d') $companyId = $this->getUserInfo('companyId');
$wechatIds = Db::name('device')->alias('d')
// 仅关联每个设备在 device_wechat_login 中的最新一条记录 // 仅关联每个设备在 device_wechat_login 中的最新一条记录
->join('(SELECT MAX(id) AS id, deviceId FROM ck_device_wechat_login WHERE companyId='.$companyId.' GROUP BY deviceId) dwl_max','dwl_max.deviceId = d.id') ->join('(SELECT MAX(id) AS id, deviceId FROM ck_device_wechat_login WHERE companyId='.$companyId.' GROUP BY deviceId) dwl_max','dwl_max.deviceId = d.id')
->join('device_wechat_login dwl','dwl.id = dwl_max.id') ->join('device_wechat_login dwl','dwl.id = dwl_max.id')
->where(['d.companyId' => $companyId,'d.deleteTime' => 0]);*/ ->where(['d.companyId' => $companyId,'d.deleteTime' => 0]);
if (!empty($deviceIds)){ if (!empty($deviceIds)){
@@ -59,7 +63,6 @@ class GetFriendListV1Controller extends BaseController
} }
$wechatIds = $wechatIds->column('dwl.wechatId'); $wechatIds = $wechatIds->column('dwl.wechatId');
$where[] = ['ownerWechatId','in',$wechatIds]; $where[] = ['ownerWechatId','in',$wechatIds];
$data = Db::table('s2_wechat_friend') $data = Db::table('s2_wechat_friend')