优化代码

This commit is contained in:
wong
2025-07-09 14:56:58 +08:00
parent 40e0938e2c
commit 6ec4be71d9
4 changed files with 63 additions and 65 deletions

View File

@@ -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);
}
}