From 9193004c54d7ef21248303cb49ca0980324f19dd Mon Sep 17 00:00:00 2001 From: wong <106998207@qq.com> Date: Fri, 11 Jul 2025 16:34:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/controller/AutomaticAssign.php | 43 +++++++++- .../command/SwitchFriendsCommand.php | 79 ++++++++++++++++--- 2 files changed, 109 insertions(+), 13 deletions(-) diff --git a/Server/application/api/controller/AutomaticAssign.php b/Server/application/api/controller/AutomaticAssign.php index 66b0d3d5..5fc04565 100644 --- a/Server/application/api/controller/AutomaticAssign.php +++ b/Server/application/api/controller/AutomaticAssign.php @@ -205,7 +205,7 @@ class AutomaticAssign extends BaseController // 设置请求头 $headerData = ['client:system']; $header = setHeader($headerData, $authorization, 'json'); - + // 发送请求 $url = $this->baseUrl . 'api/WechatFriend/allot?wechatFriendId='.$wechatFriendId.'¬ifyReceiver='.$notifyReceiver.'&comment='.$comment.'&toAccountId='.$toAccountId.'&optFrom='.$optFrom; $result = requestCurl($url, [], 'PUT', $header, 'json'); @@ -231,4 +231,45 @@ class AutomaticAssign extends BaseController } } } + + + + public function multiAllotFriendToAccount($data = []){ + // 获取授权token + $authorization = $this->authorization; + if (empty($authorization)) { + return json_encode(['code'=>500,'msg'=>'缺少授权信息']); + } + + $wechatFriendIds = !empty($data['wechatFriendIds']) ? $data['wechatFriendIds'] : input('wechatFriendIds', []); + $toAccountId = !empty($data['toAccountId']) ? $data['toAccountId'] : input('toAccountId', 0); + $notifyReceiver = !empty($data['notifyReceiver']) ? $data['notifyReceiver'] : input('notifyReceiver', false); + + // 参数验证 + if (empty($wechatFriendId)) { + return errorJson('微信好友ID不能为空'); + } + + if (empty($toAccountId)) { + return errorJson('目标账号ID不能为空'); + } + + + // 设置请求头 + $headerData = ['client:system']; + $header = setHeader($headerData, $authorization, 'json'); + + // 发送请求 + $url = $this->baseUrl . 'api/WechatFriend/multiAllotFriendToAccount?wechatFriendIds='.$wechatFriendIds.'&toAccountId='.$toAccountId.'¬ifyReceiver='.$notifyReceiver; + $result = requestCurl($url, [], 'PUT', $header, 'json'); + + if (empty($result)) { + return json_encode(['code'=>200,'msg'=>'微信好友分配成功']); + } else { + return json_encode(['code'=>500,'msg'=> $result]); + } + + } + + } \ No newline at end of file diff --git a/Server/application/command/SwitchFriendsCommand.php b/Server/application/command/SwitchFriendsCommand.php index c1035cb0..ffcc2098 100644 --- a/Server/application/command/SwitchFriendsCommand.php +++ b/Server/application/command/SwitchFriendsCommand.php @@ -30,7 +30,11 @@ class SwitchFriendsCommand extends Command $maxRetry = 5; $retry = 0; $switchedIds = []; + $totalProcessed = 0; + $totalSuccess = 0; + $totalFailed = 0; + $output->writeln('开始执行好友切换任务...'); do { $friends = Cache::get($cacheKey, []); @@ -47,20 +51,66 @@ class SwitchFriendsCommand extends Command return; } + $output->writeln('找到 ' . count($toSwitch) . ' 个需要切换的好友'); + $automaticAssign = new AutomaticAssign(); + + // 根据accountId对数组进行归类 + $groupedByAccount = []; foreach ($toSwitch as $friend) { - $friendId = !empty($friend['friendId']) ? $friend['friendId'] : $friend['id']; - $res = $automaticAssign->allotWechatFriend([ - 'wechatFriendId' => $friendId, - 'toAccountId' => $friend['accountId'], - ], true); - $res = json_decode($res, true); - if ($res['code'] == 200){ - $output->writeln('切换好友:' . $friendId . ' 到账号:' . $friend['accountId']); - $switchedIds[] = $friendId; - }else{ - $output->writeln('切换好友:' . $friendId . ' 到账号:' . $friend['accountId'] .' 结果:' .$res['msg']); + $accountId = $friend['accountId']; + if (!isset($groupedByAccount[$accountId])) { + $groupedByAccount[$accountId] = []; } + $friendId = !empty($friend['friendId']) ? $friend['friendId'] : $friend['id']; + $groupedByAccount[$accountId][] = $friendId; + } + + // 对每个账号的好友进行20个为一组的分组 + foreach ($groupedByAccount as $accountId => $accountFriends) { + $chunks = array_chunk($accountFriends, 20); + $output->writeln('账号 ' . $accountId . ' 共有 ' . count($accountFriends) . ' 个好友,分为 ' . count($chunks) . ' 组'); + + $accountSuccess = 0; + $accountFailed = 0; + + foreach ($chunks as $chunkIndex => $chunk) { + $output->writeln('处理账号 ' . $accountId . ' 第 ' . ($chunkIndex + 1) . ' 组,共 ' . count($chunk) . ' 个好友'); + + try { + $friendIds = implode(',', $chunk); + $res = $automaticAssign->multiAllotFriendToAccount([ + 'wechatFriendIds' => $friendIds, + 'toAccountId' => $accountId, + ]); + + $res = json_decode($res, true); + if ($res['code'] == 200){ + $output->writeln('✓ 成功切换好友:' . $friendIds . ' 到账号:' . $accountId); + $switchedIds = array_merge($switchedIds, $chunk); + $accountSuccess += count($chunk); + $totalSuccess += count($chunk); + } else { + $output->writeln('✗ 切换失败 - 好友:' . $friendIds . ' 到账号:' . $accountId . ' 结果:' . $res['msg']); + $accountFailed += count($chunk); + $totalFailed += count($chunk); + } + } catch (\Exception $e) { + $output->writeln('✗ 切换异常 - 好友:' . implode(',', $chunk) . ' 到账号:' . $accountId . ' 错误:' . $e->getMessage()); + Log::error('切换好友异常: ' . $e->getMessage() . ' 好友IDs: ' . implode(',', $chunk) . ' 账号ID: ' . $accountId); + $accountFailed += count($chunk); + $totalFailed += count($chunk); + } + + $totalProcessed += count($chunk); + + // 每组处理完后稍作延迟,避免请求过于频繁 + if ($chunkIndex < count($chunks) - 1) { + sleep(1); + } + } + + $output->writeln('账号 ' . $accountId . ' 处理完成 - 成功:' . $accountSuccess . ',失败:' . $accountFailed); } // 过滤掉已切换的,保留未切换和新进来的 @@ -82,7 +132,12 @@ class SwitchFriendsCommand extends Command $retry++; } while (!$success && $retry < $maxRetry); - $output->writeln('切换完成,缓存已更新并排序'); + $output->writeln('=== 切换任务完成 ==='); + $output->writeln('总处理数量:' . $totalProcessed); + $output->writeln('成功切换:' . $totalSuccess); + $output->writeln('切换失败:' . $totalFailed); + $output->writeln('成功率:' . ($totalProcessed > 0 ? round(($totalSuccess / $totalProcessed) * 100, 2) : 0) . '%'); + $output->writeln('缓存已更新并排序'); } } \ No newline at end of file