From 87bf284943b5938815363cb6c758421c2ea0cbae Mon Sep 17 00:00:00 2001 From: wong <106998207@qq.com> Date: Thu, 16 Oct 2025 16:04:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/WechatFriendController.php | 51 +++++++++++-------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/Server/application/chukebao/controller/WechatFriendController.php b/Server/application/chukebao/controller/WechatFriendController.php index 32325c7d..0dce6dc2 100644 --- a/Server/application/chukebao/controller/WechatFriendController.php +++ b/Server/application/chukebao/controller/WechatFriendController.php @@ -9,24 +9,24 @@ use think\Db; class WechatFriendController extends BaseController { - public function getList(){ + public function getList() + { $page = $this->request->param('page', 1); - $limit = $this->request->param('limit', 10); + $limit = $this->request->param('limit', 10); $accountId = $this->getUserInfo('s2_accountId'); - if (empty($accountId)){ + if (empty($accountId)) { return ResponseHelper::error('请先登录'); } $query = Db::table('s2_wechat_friend') - ->where(['accountId' => $accountId,'isDeleted' => 0]) + ->where(['accountId' => $accountId, 'isDeleted' => 0]) ->order('id desc'); $total = $query->count(); $list = $query->page($page, $limit)->select(); - // 提取所有好友ID $friendIds = array_column($list, 'id'); - + // 一次性查询所有好友的未读消息数量 $unreadCounts = []; if (!empty($friendIds)) { @@ -36,12 +36,13 @@ class WechatFriendController extends BaseController ->where('isRead', 0) ->group('wechatFriendId') ->select(); - - foreach ($unreadResults as $result) { - $unreadCounts[$result['wechatFriendId']] = $result['count']; + if (!empty($unreadResults)) { + foreach ($unreadResults as $result) { + $unreadCounts[$result['wechatFriendId']] = $result['count']; + } } } - + // 一次性查询所有好友的最新消息 $latestMessages = []; if (!empty($friendIds)) { @@ -51,17 +52,23 @@ class WechatFriendController extends BaseController ->where('wechatFriendId', 'in', $friendIds) ->group('wechatFriendId') ->buildSql(); - - // 查询最新消息的详细信息 - $messageResults = Db::table('s2_wechat_message') - ->alias('m') - ->join([$subQuery => 'sub'], 'm.id = sub.max_id') - ->field('m.*, sub.wechatFriendId') - ->select(); - - foreach ($messageResults as $message) { - $latestMessages[$message['wechatFriendId']] = $message; + + if (!empty($subQuery)) { + // 查询最新消息的详细信息 + $messageResults = Db::table('s2_wechat_message') + ->alias('m') + ->join([$subQuery => 'sub'], 'm.id = sub.max_id') + ->field('m.*, sub.wechatFriendId') + ->select(); + + if (!empty($messageResults)) { + foreach ($messageResults as $message) { + $latestMessages[$message['wechatFriendId']] = $message; + } + } } + + } @@ -85,13 +92,13 @@ class WechatFriendController extends BaseController 'chat' => isset($latestMessages[$v['id']]), 'msgTime' => isset($latestMessages[$v['id']]) ? $latestMessages[$v['id']]['wechatTime'] : 0 ]; - + // 将消息配置添加到好友数据中 $v['config'] = $config; $v['aiType'] = isset($aiTypeData[$v['id']]) ? $aiTypeData[$v['id']] : 0; } unset($v); - return ResponseHelper::success(['list'=>$list,'total'=>$total]); + return ResponseHelper::success(['list' => $list, 'total' => $total]); } } \ No newline at end of file