预防执行点赞、采集朋友圈等切换好友无法切换回来
This commit is contained in:
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('切换完成,缓存已更新并排序');
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user