From 6ec4be71d935cc086de22423c12b816a3f410cfa Mon Sep 17 00:00:00 2001 From: wong <106998207@qq.com> Date: Wed, 9 Jul 2025 14:56:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/application/common.php | 28 ++++++++++++++ .../controller/ContentLibraryController.php | 28 ++++---------- Server/application/job/WechatMomentsJob.php | 35 ++++++------------ .../application/job/WorkbenchAutoLikeJob.php | 37 ++++++++----------- 4 files changed, 63 insertions(+), 65 deletions(-) diff --git a/Server/application/common.php b/Server/application/common.php index d412b53a..389a9fad 100644 --- a/Server/application/common.php +++ b/Server/application/common.php @@ -518,6 +518,34 @@ if (!function_exists('exit_data')) { } } +if (!function_exists('artificialAllotWechatFriend')) { + function artificialAllotWechatFriend($friend = []) + { + if (empty($friend)) { + return false; + } + + //记录切换好友 + $cacheKey = 'allotWechatFriend'; + $cacheFriend = $friend; + $cacheFriend['time'] = time() + 120; + $maxRetry = 5; + $retry = 0; + do { + $cacheFriendData = Cache::get($cacheKey, []); + // 去重:移除同friendId的旧数据 + $cacheFriendData = array_filter($cacheFriendData, function($item) use ($cacheFriend) { + return $item['friendId'] !== $cacheFriend['friendId']; + }); + $cacheFriendData[] = $cacheFriend; + $success = Cache::set($cacheKey, $cacheFriendData); + $retry++; + } while (!$success && $retry < $maxRetry); + } +} + + + /** * 调试打印变量但不终止程序 * @return void diff --git a/Server/application/cunkebao/controller/ContentLibraryController.php b/Server/application/cunkebao/controller/ContentLibraryController.php index 91043612..9b048c3f 100644 --- a/Server/application/cunkebao/controller/ContentLibraryController.php +++ b/Server/application/cunkebao/controller/ContentLibraryController.php @@ -894,31 +894,17 @@ class ContentLibraryController extends Controller //执行切换好友命令 $automaticAssign = new AutomaticAssign(); $automaticAssign->allotWechatFriend(['wechatFriendId' => $friend['id'],'toAccountId' => $toAccountId],true); - - //记录切换好友 - $cacheKey = 'allotWechatFriend'; - $cacheFriend = $friend; - $cacheFriend['friendId'] = $friend['id']; - $cacheFriend['time'] = time() + 120; - $maxRetry = 5; - $retry = 0; - do { - $cacheFriendData = Cache::get($cacheKey, []); - // 去重:移除同friendId的旧数据 - $cacheFriendData = array_filter($cacheFriendData, function($item) use ($cacheFriend) { - return $item['friendId'] !== $cacheFriend['friendId']; - }); - $cacheFriendData[] = $cacheFriend; - $success = Cache::set($cacheKey, $cacheFriendData); - $retry++; - } while (!$success && $retry < $maxRetry); - - //执行采集朋友圈命令 $webSocket = new WebSocketController(['userName' => $username,'password' => $password,'accountId' => $toAccountId]); $webSocket->getMoments(['wechatFriendId' => $friend['id'],'wechatAccountId' => $friend['wechatAccountId']]); //采集完毕切换 - $automaticAssign->allotWechatFriend(['wechatFriendId' => $friend['id'],'toAccountId' => $friend['accountId']],true); + $res = $automaticAssign->allotWechatFriend(['wechatFriendId' => $friend['id'],'toAccountId' => $friend['accountId']],true); + $res = json_decode($res, true); + if($res == '无效路径或登录状态失效'){ + $cacheFriend = $friend; + $cacheFriend['friendId'] = $friend['id']; + artificialAllotWechatFriend($friend); + } } diff --git a/Server/application/job/WechatMomentsJob.php b/Server/application/job/WechatMomentsJob.php index 04cedafb..88d380d2 100644 --- a/Server/application/job/WechatMomentsJob.php +++ b/Server/application/job/WechatMomentsJob.php @@ -47,36 +47,25 @@ class WechatMomentsJob $automaticAssign = new AutomaticAssign(); $automaticAssign->allotWechatFriend(['wechatFriendId' => $friend['friendId'], 'toAccountId' => $toAccountId], true); - //记录切换好友 - $cacheKey = 'allotWechatFriend'; - $cacheFriend = $friend; - $cacheFriend['time'] = time() + 120; - $maxRetry = 5; - $retry = 0; - do { - $cacheFriendData = Cache::get($cacheKey, []); - // 去重:移除同friendId的旧数据 - $cacheFriendData = array_filter($cacheFriendData, function($item) use ($cacheFriend) { - return $item['friendId'] !== $cacheFriend['friendId']; - }); - $cacheFriendData[] = $cacheFriend; - $success = Cache::set($cacheKey, $cacheFriendData); - $retry++; - } while (!$success && $retry < $maxRetry); - - - - - // 执行采集朋友圈命令 $webSocket = new WebSocketController(['userName' => $username, 'password' => $password, 'accountId' => $toAccountId]); $webSocket->getMoments(['wechatFriendId' => $friend['friendId'], 'wechatAccountId' => $friend['wechatAccountId']]); // 处理完毕切换回原账号 - $automaticAssign->allotWechatFriend(['wechatFriendId' => $friend['friendId'], 'toAccountId' => $friend['accountId']], true); + $res = $automaticAssign->allotWechatFriend(['wechatFriendId' => $friend['friendId'], 'toAccountId' => $friend['accountId']], true); + $res = json_decode($res, true); + if ($res == '无效路径或登录状态失效'){ + artificialAllotWechatFriend($friend); + } + + } catch (\Exception $e) { // 发生异常时也要切换回原账号 - $automaticAssign->allotWechatFriend(['wechatFriendId' => $friend['friendId'], 'toAccountId' => $friend['accountId']], true); + $res = $automaticAssign->allotWechatFriend(['wechatFriendId' => $friend['friendId'], 'toAccountId' => $friend['accountId']], true); + $res = json_decode($res, true); + if ($res == '无效路径或登录状态失效'){ + artificialAllotWechatFriend($friend); + } Log::error("采集好友 {$friend['id']} 的朋友圈失败:" . $e->getMessage()); continue; } diff --git a/Server/application/job/WorkbenchAutoLikeJob.php b/Server/application/job/WorkbenchAutoLikeJob.php index 7520a46f..b0f489de 100644 --- a/Server/application/job/WorkbenchAutoLikeJob.php +++ b/Server/application/job/WorkbenchAutoLikeJob.php @@ -220,24 +220,6 @@ class WorkbenchAutoLikeJob $automaticAssign = new AutomaticAssign(); $automaticAssign->allotWechatFriend(['wechatFriendId' => $friend['friendId'], 'toAccountId' => $toAccountId], true); - //记录切换好友 - $cacheKey = 'allotWechatFriend'; - $cacheFriend = $friend; - $cacheFriend['time'] = time() + 120; - $maxRetry = 5; - $retry = 0; - do { - $cacheFriendData = Cache::get($cacheKey, []); - // 去重:移除同friendId的旧数据 - $cacheFriendData = array_filter($cacheFriendData, function($item) use ($cacheFriend) { - return $item['friendId'] !== $cacheFriend['friendId']; - }); - $cacheFriendData[] = $cacheFriend; - $success = Cache::set($cacheKey, $cacheFriendData); - $retry++; - } while (!$success && $retry < $maxRetry); - - // 创建WebSocket链接 $webSocket = new WebSocketController(['userName' => $username, 'password' => $password, 'accountId' => $toAccountId]); @@ -252,7 +234,12 @@ class WorkbenchAutoLikeJob if (empty($moments) || count($moments) == 0) { // 处理完毕切换回原账号 - $automaticAssign->allotWechatFriend(['wechatFriendId' => $friend['friendId'], 'toAccountId' => $friend['accountId']], true); + $res = $automaticAssign->allotWechatFriend(['wechatFriendId' => $friend['friendId'], 'toAccountId' => $friend['accountId']], true); + $res = json_decode($res, true); + if($res == '无效路径或登录状态失效'){ + artificialAllotWechatFriend($friend); + } + Log::info("好友 {$friend['friendId']} 没有需要点赞的朋友圈"); return; } @@ -276,11 +263,19 @@ class WorkbenchAutoLikeJob } // 处理完毕切换回原账号 - $automaticAssign->allotWechatFriend(['wechatFriendId' => $friend['friendId'], 'toAccountId' => $friend['accountId']], true); + $res = $automaticAssign->allotWechatFriend(['wechatFriendId' => $friend['friendId'], 'toAccountId' => $friend['accountId']], true); + $res = json_decode($res, true); + if($res == '无效路径或登录状态失效'){ + artificialAllotWechatFriend($friend); + } } catch (\Exception $e) { // 异常情况下也要确保切换回原账号 $automaticAssign = new AutomaticAssign(); - $automaticAssign->allotWechatFriend(['wechatFriendId' => $friend['friendId'], 'toAccountId' => $friend['accountId']], true); + $res = $automaticAssign->allotWechatFriend(['wechatFriendId' => $friend['friendId'], 'toAccountId' => $friend['accountId']], true); + $res = json_decode($res, true); + if($res == '无效路径或登录状态失效'){ + artificialAllotWechatFriend($friend); + } Log::error("处理好友 {$friend['friendId']} 朋友圈失败异常: " . $e->getMessage()); } }