From 71def21369dd62563d38cedef2d8a4082bfa9828 Mon Sep 17 00:00:00 2001 From: wong <106998207@qq.com> Date: Wed, 3 Dec 2025 14:06:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B6=88=E6=81=AF=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chukebao/controller/MessageController.php | 142 +++++++++++++++++- 1 file changed, 138 insertions(+), 4 deletions(-) diff --git a/Server/application/chukebao/controller/MessageController.php b/Server/application/chukebao/controller/MessageController.php index 5f59e1e0..9008fb00 100644 --- a/Server/application/chukebao/controller/MessageController.php +++ b/Server/application/chukebao/controller/MessageController.php @@ -2,12 +2,24 @@ namespace app\chukebao\controller; +use app\api\model\WechatMessageModel; use app\chukebao\model\FriendSettings; use library\ResponseHelper; use think\Db; +use think\facade\Env; +use app\common\service\AuthService; class MessageController extends BaseController { + protected $baseUrl; + protected $authorization; + + public function __construct() + { + parent::__construct(); + $this->baseUrl = Env::get('api.wechat_url'); + $this->authorization = AuthService::getSystemAuthorization(); + } public function getList() { @@ -64,7 +76,6 @@ class MessageController extends BaseController return $b['wechatTime'] <=> $a['wechatTime']; }); - // 批量统计未读数量(isRead=0),按好友/群聊分别聚合 $friendIds = []; $chatroomIds = []; @@ -219,14 +230,137 @@ class MessageController extends BaseController $total = Db::table('s2_wechat_message')->where($where)->count(); $list = Db::table('s2_wechat_message')->where($where)->page($page, $limit)->order('id DESC')->select(); - - foreach ($list as $k => &$v) { - $v['wechatTime'] = !empty($v['wechatTime']) ? date('Y-m-d H:i:s', $v['wechatTime']) : ''; + // 检查消息是否有sendStatus字段,如果有且不为0,则请求线上最新接口 + foreach ($list as $k => &$item) { + // 检查是否存在sendStatus字段且不为0(0表示已发送成功) + if (isset($item['sendStatus']) && $item['sendStatus'] != 0) { + // 需要请求新的数据 + $messageRequest = [ + 'id' => $item['id'], + 'wechatAccountId' => $wechatAccountId, + 'wechatFriendId' => $wechatFriendId, + 'wechatChatroomId' => $wechatChatroomId, + 'from' => '', + 'to' => '', + ]; + $newData = $this->fetchLatestMessageFromApi($messageRequest); + if (!empty($newData)){ + $item['sendStatus'] = 0; + } + } + // 格式化时间 + $item['wechatTime'] = !empty($item['wechatTime']) ? date('Y-m-d H:i:s', $item['wechatTime']) : ''; } + unset($item); return ResponseHelper::success(['total' => $total, 'list' => $list]); } + /** + * 从线上接口获取最新消息 + * @param array $messageRequest 消息项(包含wechatAccountId、wechatFriendId或wechatChatroomId、id等) + * @return array|null 最新消息数据,失败返回null + */ + private function fetchLatestMessageFromApi($messageRequest) + { + if (empty($this->baseUrl) || empty($this->authorization)) { + return null; + } + + try { + // 设置请求头 + $headerData = ['client:system']; + $header = setHeader($headerData, $this->authorization, 'json'); + + // 判断是好友消息还是群聊消息 + if (!empty($messageRequest['wechatFriendId'])) { + // 好友消息接口 + $params = [ + 'keyword' => '', + 'msgType' => '', + 'accountId' => '', + 'count' => 20, // 获取多条消息以便找到对应的消息 + 'messageId' => isset($messageRequest['id']) ? $messageRequest['id'] : '', + 'olderData' => true, + 'wechatAccountId' => $messageRequest['wechatAccountId'], + 'wechatFriendId' => $messageRequest['wechatFriendId'], + 'from' => $messageRequest['from'], + 'to' => $messageRequest['to'], + 'searchFrom' => 'admin' + ]; + $result = requestCurl($this->baseUrl . 'api/FriendMessage/searchMessage', $params, 'GET', $header, 'json'); + $response = handleApiResponse($result); + // 查找对应的消息 + if (!empty($response) && is_array($response)) { + $data = $response[0]; + if ($data['sendStatus'] == 0){ + WechatMessageModel::where(['id' => $data['id']])->update(['sendStatus' => 0]); + return true; + } + } + return false; + } elseif (!empty($messageRequest['wechatChatroomId'])) { + // 群聊消息接口 + $params = [ + 'keyword' => '', + 'msgType' => '', + 'accountId' => '', + 'count' => 20, // 获取多条消息以便找到对应的消息 + 'messageId' => isset($messageRequest['id']) ? $messageRequest['id'] : '', + 'olderData' => true, + 'wechatId' => '', + 'wechatAccountId' => $messageRequest['wechatAccountId'], + 'wechatChatroomId' => $messageRequest['wechatChatroomId'], + 'from' => $messageRequest['from'], + 'to' => $messageRequest['to'], + 'searchFrom' => 'admin' + ]; + + $result = requestCurl($this->baseUrl . 'api/ChatroomMessage/searchMessage', $params, 'GET', $header, 'json'); + $response = handleApiResponse($result); + + // 查找对应的消息 + if (!empty($response) && is_array($response)) { + $data = $response[0]; + if ($data['sendStatus'] == 0){ + WechatMessageModel::where(['id' => $data['id']])->update(['sendStatus' => 0]); + return true; + } + } + return false; + } + } catch (\Exception $e) { + // 记录错误日志,但不影响主流程 + \think\facade\Log::error('获取线上最新消息失败:' . $e->getMessage()); + } + + return null; + } + + /** + * 更新数据库中的消息 + * @param array $latestMessage 线上获取的最新消息 + * @param array $oldMessage 旧消息数据 + */ + private function updateMessageInDatabase($latestMessage, $oldMessage) + { + try { + // 使用API模块的MessageController来保存消息 + $apiMessageController = new \app\api\controller\MessageController(); + + // 判断是好友消息还是群聊消息 + if (!empty($oldMessage['wechatFriendId'])) { + // 保存好友消息 + $apiMessageController->saveMessage($latestMessage); + } elseif (!empty($oldMessage['wechatChatroomId'])) { + // 保存群聊消息 + $apiMessageController->saveChatroomMessage($latestMessage); + } + } catch (\Exception $e) { + // 记录错误日志,但不影响主流程 + \think\facade\Log::error('更新数据库消息失败:' . $e->getMessage()); + } + } } \ No newline at end of file