diff --git a/Server/application/command.php b/Server/application/command.php index 2dcd3178..b8fb90e2 100644 --- a/Server/application/command.php +++ b/Server/application/command.php @@ -32,4 +32,5 @@ return [ 'sync:wechatData' => 'app\command\SyncWechatDataToCkbTask', // 同步微信数据到存客宝 'sync:allFriends' => 'app\command\SyncAllFriendsCommand', // 同步所有在线好友 'workbench:trafficDistribute' => 'app\command\WorkbenchTrafficDistributeCommand', // 工作台流量分发任务 + 'switch:friends' => 'app\command\SwitchFriendsCommand', // 切换好友 ]; diff --git a/Server/application/command/SwitchFriendsCommand.php b/Server/application/command/SwitchFriendsCommand.php new file mode 100644 index 00000000..38575b9b --- /dev/null +++ b/Server/application/command/SwitchFriendsCommand.php @@ -0,0 +1,82 @@ +setName('switch:friends') + ->setDescription('切换好友命令'); + } + + protected function execute(Input $input, Output $output) + { + $cacheKey = 'allotWechatFriend'; + $now = time(); + $maxRetry = 5; + $retry = 0; + $switchedIds = []; + + + do { + $friends = Cache::get($cacheKey, []); + $original = $friends; + + $toSwitch = []; + foreach ($friends as $friend) { + if (isset($friend['time']) && $friend['time'] > $now) { + $toSwitch[] = $friend; + } + } + + if (empty($toSwitch)) { + $output->writeln('没有需要切换的好友'); + return; + } + + $automaticAssign = new AutomaticAssign(); + foreach ($toSwitch as $friend) { + $automaticAssign->allotWechatFriend([ + 'wechatFriendId' => $friend['friendId'], + 'toAccountId' => $friend['accountId'], + ], true); + $output->writeln('切换好友:' . $friend['friendId'] . ' 到账号:' . $friend['accountId']); + $switchedIds[] = $friend['friendId']; + } + + // 过滤掉已切换的,保留未切换和新进来的 + $newFriends = Cache::get($cacheKey, []); + $updated = []; + foreach ($newFriends as $friend) { + if (!in_array($friend['friendId'], $switchedIds)) { + $updated[] = $friend; + } + } + + // 按time升序排序 + usort($updated, function($a, $b) { + return ($a['time'] ?? 0) <=> ($b['time'] ?? 0); + }); + + $success = Cache::set($cacheKey, $updated); + $retry++; + } while (!$success && $retry < $maxRetry); + + $output->writeln('切换完成,缓存已更新并排序'); + } + +} \ No newline at end of file diff --git a/Server/application/cunkebao/controller/ContentLibraryController.php b/Server/application/cunkebao/controller/ContentLibraryController.php index a18327bf..8fdf96c9 100644 --- a/Server/application/cunkebao/controller/ContentLibraryController.php +++ b/Server/application/cunkebao/controller/ContentLibraryController.php @@ -7,6 +7,7 @@ use app\cunkebao\model\ContentItem; use think\Controller; use think\Db; use app\api\controller\WebSocketController; +use think\facade\Cache; use think\facade\Env; use app\api\controller\AutomaticAssign; @@ -893,6 +894,25 @@ class ContentLibraryController extends Controller //执行切换好友命令 $automaticAssign = new AutomaticAssign(); $automaticAssign->allotWechatFriend(['wechatFriendId' => $friend['id'],'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['id'],'wechatAccountId' => $friend['wechatAccountId']]); diff --git a/Server/application/job/WechatMomentsJob.php b/Server/application/job/WechatMomentsJob.php index 9042783b..04cedafb 100644 --- a/Server/application/job/WechatMomentsJob.php +++ b/Server/application/job/WechatMomentsJob.php @@ -24,7 +24,6 @@ class WechatMomentsJob $toAccountId = Db::name('users')->where('account',$username)->value('s2_accountId'); }else{ Log::error("没有账号配置"); - Cache::rm($queueLockKey); return; } @@ -48,6 +47,27 @@ 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']]); diff --git a/Server/application/job/WorkbenchAutoLikeJob.php b/Server/application/job/WorkbenchAutoLikeJob.php index 351b6b6b..7520a46f 100644 --- a/Server/application/job/WorkbenchAutoLikeJob.php +++ b/Server/application/job/WorkbenchAutoLikeJob.php @@ -219,7 +219,25 @@ 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]);