代码优化
This commit is contained in:
@@ -226,11 +226,21 @@ class MessageController extends BaseController
|
||||
$result = requestCurl($this->baseUrl . 'api/WechatChatroom/listWechatChatroomForMsgPagination', $params, 'POST', $header, 'json');
|
||||
$response = handleApiResponse($result);
|
||||
|
||||
// 确保 response 是数组格式
|
||||
if (!is_array($response)) {
|
||||
$response = [];
|
||||
}
|
||||
|
||||
// 确保 results 字段存在且是数组
|
||||
if (!isset($response['results']) || !is_array($response['results'])) {
|
||||
$response['results'] = [];
|
||||
}
|
||||
|
||||
// 获取同步消息标志
|
||||
$syncMessages = $this->request->param('syncMessages', true);
|
||||
|
||||
// 如果需要同步消息,则获取每个群的消息
|
||||
if ($syncMessages && !empty($response)) {
|
||||
if ($syncMessages && !empty($response['results'])) {
|
||||
$from = strtotime($fromTime) * 1000;
|
||||
$to = strtotime($toTime) * 1000;
|
||||
foreach ($response['results'] as &$chatroom) {
|
||||
@@ -255,10 +265,17 @@ class MessageController extends BaseController
|
||||
$messageResult = requestCurl($this->baseUrl . 'api/ChatroomMessage/searchMessage', $messageParams, 'GET', $header, 'json');
|
||||
$messageResponse = handleApiResponse($messageResult);
|
||||
|
||||
// 确保 messageResponse 是数组格式
|
||||
if (!is_array($messageResponse)) {
|
||||
$messageResponse = [];
|
||||
}
|
||||
|
||||
// 保存消息到数据库
|
||||
if (!empty($messageResponse)) {
|
||||
foreach ($messageResponse as $item) {
|
||||
$this->saveChatroomMessage($item);
|
||||
if (is_array($item)) {
|
||||
$this->saveChatroomMessage($item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user