预防执行点赞、采集朋友圈等切换好友无法切换回来
This commit is contained in:
@@ -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', // 切换好友
|
||||
];
|
||||
|
||||
82
Server/application/command/SwitchFriendsCommand.php
Normal file
82
Server/application/command/SwitchFriendsCommand.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace app\command;
|
||||
|
||||
use app\job\WorkbenchAutoLikeJob;
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\input\Option;
|
||||
use think\console\Output;
|
||||
use think\facade\Cache;
|
||||
use think\facade\Log;
|
||||
use think\Queue;
|
||||
use app\api\controller\AutomaticAssign;
|
||||
|
||||
class SwitchFriendsCommand extends Command
|
||||
{
|
||||
// 队列名称
|
||||
protected $queueName = 'switch_friends';
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->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('切换完成,缓存已更新并排序');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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']]);
|
||||
|
||||
@@ -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']]);
|
||||
|
||||
@@ -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]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user