diff --git a/Server/application/chukebao/config/route.php b/Server/application/chukebao/config/route.php index ce1a477b..b22a01b7 100644 --- a/Server/application/chukebao/config/route.php +++ b/Server/application/chukebao/config/route.php @@ -34,11 +34,12 @@ Route::group('v1/', function () { Route::get('list', 'app\chukebao\controller\AccountsController@getList'); // 获取账号列表 }); - //客服相关 + //消息相关 Route::group('message/', function () { Route::get('list', 'app\chukebao\controller\MessageController@getList'); // 获取好友列表 Route::get('readMessage', 'app\chukebao\controller\MessageController@readMessage'); // 读取消息 Route::get('details', 'app\chukebao\controller\MessageController@details'); // 消息详情 + Route::get('getMessageStatus', 'app\chukebao\controller\MessageController@getMessageStatus'); // 获取单条消息发送状态 }); //微信分组 diff --git a/Server/application/chukebao/controller/MessageController.php b/Server/application/chukebao/controller/MessageController.php index 9008fb00..3ffeb9fc 100644 --- a/Server/application/chukebao/controller/MessageController.php +++ b/Server/application/chukebao/controller/MessageController.php @@ -196,6 +196,110 @@ class MessageController extends BaseController } + /** + * 获取单条消息发送状态(带轮询功能) + * @return \think\response\Json + */ + public function getMessageStatus() + { + $messageId = $this->request->param('messageId', 0); + $wechatAccountId = $this->request->param('wechatAccountId', ''); + $accountId = $this->getUserInfo('s2_accountId'); + $wechatFriendId = $this->request->param('wechatFriendId', ''); + $wechatChatroomId = $this->request->param('wechatChatroomId', ''); + + if (empty($accountId)) { + return ResponseHelper::error('请先登录'); + } + + if (empty($messageId)) { + return ResponseHelper::error('消息ID不能为空'); + } + + if(empty($wechatFriendId) && empty($wechatChatroomId)) { + return ResponseHelper::error('消息类型不能为空'); + } + + // 查询单条消息的基本信息(只需要发送状态相关字段) + $message = Db::table('s2_wechat_message') + ->where('id', $messageId) + ->field('id,wechatAccountId,wechatFriendId,wechatChatroomId,sendStatus') + ->find(); + + if (empty($message)) { + $message = [ + 'id' => $messageId, + 'wechatAccountId' => $wechatAccountId, + 'wechatFriendId' => $wechatFriendId, + 'wechatChatroomId' => $wechatChatroomId, + 'sendStatus' => 0, + ]; + } + + $sendStatus = isset($message['sendStatus']) ? (int)$message['sendStatus'] : 0; + $isUpdated = false; + $pollCount = 0; + $maxPollCount = 10; // 最多轮询10次 + + // 如果sendStatus不为0,开始轮询 + if ($sendStatus != 0) { + $messageRequest = [ + 'id' => $message['id'], + 'wechatAccountId' => !empty($wechatAccountId) ? $wechatAccountId : $message['wechatAccountId'], + 'wechatFriendId' => !empty($message['wechatFriendId']) ? $message['wechatFriendId'] : '', + 'wechatChatroomId' => !empty($message['wechatChatroomId']) ? $message['wechatChatroomId'] : '', + 'from' => '', + 'to' => '', + ]; + + + // 轮询逻辑:最多10次 + while ($pollCount < $maxPollCount && $sendStatus != 0) { + $pollCount++; + + // 请求线上接口获取最新状态 + $newData = $this->fetchLatestMessageFromApi($messageRequest); + + if (!empty($newData)) { + // 重新查询消息状态(可能已更新) + $updatedMessage = Db::table('s2_wechat_message') + ->where('id', $messageId) + ->field('sendStatus') + ->find(); + + if (!empty($updatedMessage)) { + $newSendStatus = isset($updatedMessage['sendStatus']) ? (int)$updatedMessage['sendStatus'] : 0; + + // 如果状态已更新为0(已发送),停止轮询 + if ($newSendStatus == 0) { + $sendStatus = 0; + $isUpdated = true; + break; + } + + // 如果状态仍然是1,继续轮询(但需要等待一下,避免请求过快) + if ($newSendStatus != 0 && $pollCount < $maxPollCount) { + // 每次轮询间隔500毫秒(0.5秒) + usleep(500000); + } + } + } else { + // 如果请求失败,等待后继续尝试 + if ($pollCount < $maxPollCount) { + usleep(500000); + } + } + } + } + + // 返回发送状态信息 + return ResponseHelper::success([ + 'messageId' => $messageId, + 'sendStatus' => $sendStatus, + 'statusText' => $sendStatus == 0 ? '已发送' : '发送中' + ]); + } + public function details() { $wechatFriendId = $this->request->param('wechatFriendId', ''); @@ -257,6 +361,12 @@ class MessageController extends BaseController return ResponseHelper::success(['total' => $total, 'list' => $list]); } + + + + + + /** * 从线上接口获取最新消息 * @param array $messageRequest 消息项(包含wechatAccountId、wechatFriendId或wechatChatroomId、id等)