From dbc94e4823f71173171d99d2b9ba3c0741b079eb Mon Sep 17 00:00:00 2001 From: wong <106998207@qq.com> Date: Fri, 25 Apr 2025 09:31:11 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E8=A7=A6=E5=AE=A2=E5=AE=9D=E3=80=91?= =?UTF-8?q?=20=E5=A5=BD=E5=8F=8B=E3=80=81=E7=BE=A4=E3=80=81=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E9=98=9F=E5=88=97=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/controller/WechatFriendController.php | 16 +++++++++++++--- Server/application/command.php | 3 ++- Server/application/command/DeviceListCommand.php | 2 +- .../command/WechatChatroomCommand.php | 2 +- Server/application/job/DeviceListJob.php | 6 +++--- Server/application/job/WechatChatroomJob.php | 6 +++--- Server/application/job/WechatFriendJob.php | 8 ++++---- 7 files changed, 27 insertions(+), 16 deletions(-) diff --git a/Server/application/api/controller/WechatFriendController.php b/Server/application/api/controller/WechatFriendController.php index 298fc69b..ae070fa3 100644 --- a/Server/application/api/controller/WechatFriendController.php +++ b/Server/application/api/controller/WechatFriendController.php @@ -29,6 +29,9 @@ class WechatFriendController extends BaseController } try { + // 初始化isUpdate标志为false + $isUpdate = false; + // 根据isDel设置对应的isDeleted值 $isDeleted = null; // 默认值 if ($isDel === '0' || $isDel === 0) { @@ -67,13 +70,17 @@ class WechatFriendController extends BaseController // 保存数据到数据库 if (is_array($response)) { + $isUpdate = false; foreach ($response as $item) { - $this->saveFriend($item); + $updated = $this->saveFriend($item); + if($updated){ + $isUpdate = true; + } } } if ($isJob) { - return json_encode(['code' => 200, 'msg' => 'success', 'data' => $response]); + return json_encode(['code' => 200, 'msg' => 'success', 'data' => $response, 'isUpdate' => $isUpdate]); } else { return successJson($response); } @@ -90,6 +97,7 @@ class WechatFriendController extends BaseController /** * 保存微信好友数据到数据库 * @param array $item 微信好友数据 + * @return bool 是否创建或更新了记录 */ private function saveFriend($item) { @@ -137,9 +145,11 @@ class WechatFriendController extends BaseController $friend = WechatFriendModel::where('id', $item['id'])->find(); if ($friend) { - $friend->save($data); + $result = $friend->save($data); + return false; } else { WechatFriendModel::create($data); + return true; } } } \ No newline at end of file diff --git a/Server/application/command.php b/Server/application/command.php index 7d742436..f68f6471 100644 --- a/Server/application/command.php +++ b/Server/application/command.php @@ -21,5 +21,6 @@ return [ 'department:list' => 'app\command\DepartmentListCommand', // 部门列表 √ 'content:sync' => 'app\command\SyncContentCommand', // 同步内容库 √ 'groupFriends:list' => 'app\command\GroupFriendsCommand', // 微信群好友列表 - + 'allotFriends:run' => 'app\command\AllotFriendCommand', // 自动分配微信好友 + 'allotChatroom:run' => 'app\command\AllotChatroomCommand', // 自动分配微信群聊 ]; diff --git a/Server/application/command/DeviceListCommand.php b/Server/application/command/DeviceListCommand.php index 4fda4931..ec836826 100644 --- a/Server/application/command/DeviceListCommand.php +++ b/Server/application/command/DeviceListCommand.php @@ -81,7 +81,7 @@ class DeviceListCommand extends Command * @param string $cacheKey 缓存键名 * @param string $queueLockKey 队列锁键名 */ - protected function addToQueue($pageIndex, $pageSize, $isDel = '', $jobId = '', $cacheKey = '', $queueLockKey = '') + public function addToQueue($pageIndex, $pageSize, $isDel = '', $jobId = '', $cacheKey = '', $queueLockKey = '') { $data = [ 'pageIndex' => $pageIndex, diff --git a/Server/application/command/WechatChatroomCommand.php b/Server/application/command/WechatChatroomCommand.php index ba7a6547..b751435b 100644 --- a/Server/application/command/WechatChatroomCommand.php +++ b/Server/application/command/WechatChatroomCommand.php @@ -81,7 +81,7 @@ class WechatChatroomCommand extends Command * @param string $cacheKey 缓存键名 * @param string $queueLockKey 队列锁键名 */ - protected function addToQueue($pageIndex, $pageSize, $isDel = '', $jobId = '', $cacheKey = '', $queueLockKey = '') + public function addToQueue($pageIndex, $pageSize, $isDel = '', $jobId = '', $cacheKey = '', $queueLockKey = '') { $data = [ 'pageIndex' => $pageIndex, diff --git a/Server/application/job/DeviceListJob.php b/Server/application/job/DeviceListJob.php index b887af07..fbf5964b 100644 --- a/Server/application/job/DeviceListJob.php +++ b/Server/application/job/DeviceListJob.php @@ -88,7 +88,7 @@ class DeviceListJob } else { // 处理完所有页面,重置页码并释放队列锁 Cache::set($cacheKey, 0, 86400); - Cache::delete($queueLockKey); + Cache::rm($queueLockKey); Log::info("所有设备列表页面处理完毕,重置页码为0,释放队列锁: {$queueLockKey}"); } @@ -101,7 +101,7 @@ class DeviceListJob if ($job->attempts() > 3) { // 超过重试次数,删除任务并释放队列锁 - Cache::delete($queueLockKey); + Cache::rm($queueLockKey); Log::info("由于错误释放队列锁: {$queueLockKey}"); $job->delete(); } else { @@ -117,7 +117,7 @@ class DeviceListJob Log::error('设备列表任务处理异常: ' . $e->getMessage()); if (!empty($queueLockKey)) { - Cache::delete($queueLockKey); + Cache::rm($queueLockKey); Log::info("由于异常释放队列锁: {$queueLockKey}"); } diff --git a/Server/application/job/WechatChatroomJob.php b/Server/application/job/WechatChatroomJob.php index 50c78614..0977530c 100644 --- a/Server/application/job/WechatChatroomJob.php +++ b/Server/application/job/WechatChatroomJob.php @@ -74,13 +74,13 @@ class WechatChatroomJob } else { // 处理完所有页面,重置页码并释放队列锁 Cache::set($cacheKey, 0, 86400); - Cache::delete($queueLockKey); + Cache::rm($queueLockKey); Log::info("所有微信聊天室列表页面处理完毕,重置页码为0,释放队列锁: {$queueLockKey}"); } } else { // API调用出错,记录错误并释放队列锁 Log::error("微信聊天室列表获取失败: " . $result['msg']); - Cache::delete($queueLockKey); + Cache::rm($queueLockKey); Log::info("由于错误释放队列锁: {$queueLockKey}"); } @@ -90,7 +90,7 @@ class WechatChatroomJob // 出现异常,记录错误并释放队列锁 Log::error('微信聊天室列表任务处理异常: ' . $e->getMessage()); if (!empty($queueLockKey)) { - Cache::delete($queueLockKey); + Cache::rm($queueLockKey); Log::info("由于异常释放队列锁: {$queueLockKey}"); } diff --git a/Server/application/job/WechatFriendJob.php b/Server/application/job/WechatFriendJob.php index 26189162..902e2c10 100644 --- a/Server/application/job/WechatFriendJob.php +++ b/Server/application/job/WechatFriendJob.php @@ -81,7 +81,7 @@ class WechatFriendJob $data = $response['data']; // 判断是否有下一页 - if (!empty($data) && count($data) > 0) { + if (!empty($data) && count($data) > 0 && empty($response['isUpdate'])) { // 获取最后一条记录的ID $lastFriendId = $data[count($data)-1]['id']; @@ -100,7 +100,7 @@ class WechatFriendJob // 没有下一页,重置缓存并释放队列锁 Cache::set($pageIndexCacheKey, 0, 86400); Cache::set($preFriendIdCacheKey, '', 86400); - Cache::delete($queueLockKey); + Cache::rm($queueLockKey); Log::info("所有微信好友列表页面处理完毕,重置页码为0,释放队列锁: {$queueLockKey}"); } @@ -114,7 +114,7 @@ class WechatFriendJob if ($job->attempts() > 3) { // 超过重试次数,删除任务并释放队列锁 - Cache::delete($queueLockKey); + Cache::rm($queueLockKey); Log::info("由于错误释放队列锁: {$queueLockKey}"); $job->delete(); } else { @@ -130,7 +130,7 @@ class WechatFriendJob Log::error('微信好友列表任务异常:' . $e->getMessage()); if (!empty($queueLockKey)) { - Cache::delete($queueLockKey); + Cache::rm($queueLockKey); Log::info("由于异常释放队列锁: {$queueLockKey}"); }