From 9736416fffc6ba4f9fc1679061ce7a452f71fe74 Mon Sep 17 00:00:00 2001 From: wong <106998207@qq.com> Date: Mon, 14 Jul 2025 18:16:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=81=E9=87=8F=E5=88=86=E5=8F=91=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/controller/AccountController.php | 18 ++-- .../api/controller/MessageController.php | 46 ++++++++++ .../command/SwitchFriendsCommand.php | 85 +++++++++++++++++++ Server/application/job/AccountListJob.php | 2 +- .../job/WorkbenchTrafficDistributeJob.php | 6 +- 5 files changed, 149 insertions(+), 8 deletions(-) diff --git a/Server/application/api/controller/AccountController.php b/Server/application/api/controller/AccountController.php index aeadb752..30345292 100644 --- a/Server/application/api/controller/AccountController.php +++ b/Server/application/api/controller/AccountController.php @@ -23,9 +23,15 @@ class AccountController extends BaseController * @param bool $isInner 是否为定时任务调用 * @return \think\response\Json */ - public function getlist($pageIndex = '', $pageSize = '', $isInner = false) + public function getlist($data = [], $isInner = false) { + $pageIndex = !empty($data['pageIndex']) ? $data['pageIndex'] : 0; + $pageSize = !empty($data['pageSize']) ? $data['pageSize'] : 20; + $showNormalAccount = !empty($data['showNormalAccount']) ? $data['showNormalAccount'] : ''; + $keyword = !empty($data['keyword']) ? $data['keyword'] : ''; + $departmentId = !empty($data['departmentId']) ? $data['departmentId'] : ''; + // 获取授权token $authorization = trim($this->request->header('authorization', $this->authorization)); if (empty($authorization)) { @@ -39,11 +45,11 @@ class AccountController extends BaseController try { // 构建请求参数 $params = [ - 'showNormalAccount' => $this->request->param('showNormalAccount', ''), - 'keyword' => $this->request->param('keyword', ''), - 'departmentId' => $this->request->param('departmentId', ''), - 'pageIndex' => !empty($pageIndex) ? $pageIndex : $this->request->param('pageIndex', 0), - 'pageSize' => !empty($pageSize) ? $pageSize : $this->request->param('pageSize', 20) + 'showNormalAccount' => $showNormalAccount, + 'keyword' => $keyword, + 'departmentId' => $departmentId, + 'pageIndex' => $pageIndex, + 'pageSize' => $pageSize ]; // 设置请求头 diff --git a/Server/application/api/controller/MessageController.php b/Server/application/api/controller/MessageController.php index 2cb39682..da033741 100644 --- a/Server/application/api/controller/MessageController.php +++ b/Server/application/api/controller/MessageController.php @@ -3,6 +3,7 @@ namespace app\api\controller; use app\api\model\WechatMessageModel; +use think\Db; use think\facade\Request; class MessageController extends BaseController @@ -386,6 +387,51 @@ class MessageController extends BaseController 'wechatTime' => $wechatTime ]; + //已被删除 + if ($item['msgType'] == 10000 && strpos($item['content'],'开启了朋友验证') !== false) { + Db::table('s2_wechat_friend')->where('id',$item['wechatFriendId'])->update(['isDeleted'=> 1,'deleteTime' => $wechatTime]); + }else{ + //优先分配在线客服 + $friend = Db::table('s2_wechat_friend')->where('id',$item['wechatFriendId'])->find(); + if (!empty($friend)){ + $accountId = $item['accountId']; + $accountData = Db::table('s2_company_account')->where('id',$accountId)->find(); + if (!empty($accountData)){ + $account = new AccountController(); + $account->getlist(['pageIndex' => 0,'pageSize' => 100,'departmentId' => $accountData['departmentId']]); + $accountIds = Db::table('s2_company_account')->where(['id' => $accountId,'alive' => 1])->column('id'); + if (!empty($accountIds)){ + if (!in_array($friend['accountId'],$accountData)){ + // 执行切换好友命令 + $randomKey = array_rand($accountIds, 1); + $toAccountId = $accountIds[$randomKey]; + $toAccountData = Db::table('s2_company_account')->where('id',$toAccountId)->find(); + $automaticAssign = new AutomaticAssign(); + $automaticAssign->allotWechatFriend([ + 'wechatFriendId' => $friend['id'], + 'toAccountId' => $toAccountId + ], true); + Db::table('s2_wechat_friend') + ->where('id',$friend['id']) + ->update([ + 'accountId' => $toAccountId, + 'accountUserName' => $toAccountData['userName'], + 'accountRealName' => $toAccountData['realName'], + 'accountNickname' => $toAccountData['nickname'], + ]); + } + } + + } + } + } + + + + + + + // 创建新记录 WechatMessageModel::create($data); } diff --git a/Server/application/command/SwitchFriendsCommand.php b/Server/application/command/SwitchFriendsCommand.php index d7bf89ce..d153f372 100644 --- a/Server/application/command/SwitchFriendsCommand.php +++ b/Server/application/command/SwitchFriendsCommand.php @@ -7,6 +7,7 @@ use think\console\Command; use think\console\Input; use think\console\input\Option; use think\console\Output; +use think\Db; use think\facade\Cache; use think\facade\Log; use think\Queue; @@ -25,6 +26,90 @@ class SwitchFriendsCommand extends Command protected function execute(Input $input, Output $output) { + //处理流量分过期数据 + $expUserData = Db::name('workbench_traffic_config_item') + ->where('expTime','<=',time()) + ->where('isRecycle',0) + ->select(); + + // 根据accountId对数组进行归类 + $groupedByAccount = []; + foreach ($expUserData as $friend) { + $accountId = $friend['wechatAccountId']; + if (!isset($groupedByAccount[$accountId])) { + $groupedByAccount[$accountId] = []; + } + $friendId = $friend['wechatFriendId']; + $groupedByAccount[$accountId][] = $friendId; + } + + // 对每个账号的好友进行20个为一组的分组 + foreach ($groupedByAccount as $accountId => $accountFriends) { + //检索主账号 + $account = Db::name('users')->where('s2_accountId',$accountId)->find(); + if (empty($account)) { + continue; + } + $account2 = Db::name('users') + ->where('s2_accountId','>',0) + ->where('companyId',$account['companyId']) + ->order('s2_accountId ASC') + ->find(); + if (empty($account2)) { + continue; + } + $newaAccountId = $account2['s2_accountId']; + + $chunks = array_chunk($accountFriends, 20); + $output->writeln('账号 ' . $newaAccountId . ' 共有 ' . count($accountFriends) . ' 个好友,分为 ' . count($chunks) . ' 组'); + + $automaticAssign = new AutomaticAssign(); + foreach ($chunks as $chunkIndex => $chunk) { + $output->writeln('处理账号 ' . $newaAccountId . ' 第 ' . ($chunkIndex + 1) . ' 组,共 ' . count($chunk) . ' 个好友'); + try { + $friendIds = implode(',', $chunk); + $res = $automaticAssign->multiAllotFriendToAccount([ + 'wechatFriendIds' => $friendIds, + 'toAccountId' => $newaAccountId, + ]); + $res = json_decode($res, true); + if ($res['code'] == 200){ + //修改数据库 + Db::table('s2_wechat_friend') + ->where('id',$friendIds) + ->update([ + 'accountId' => $account2['s2_accountId'], + 'accountUserName' => $account2['account'], + 'accountRealName' => $account2['username'], + 'accountNickname' => $account2['username'], + ]); + + Db::name('workbench_traffic_config_item') + ->whereIn('wechatFriendId',$friendIds) + ->where('wechatAccountId',$accountId) + ->update([ + 'isRecycle' => 1, + 'recycleTime' => time(), + ]); + $output->writeln('✓ 成功切换好友:' . $friendIds . ' 到账号:' . $newaAccountId); + } else { + $output->writeln('✗ 切换失败 - 好友:' . $friendIds . ' 到账号:' . $newaAccountId . ' 结果:' . $res['msg']); + } + } catch (\Exception $e) { + $output->writeln('✗ 切换异常 - 好友:' . implode(',', $chunk) . ' 到账号:' . $newaAccountId . ' 错误:' . $e->getMessage()); + } + + // 每组处理完后稍作延迟,避免请求过于频繁 + if ($chunkIndex < count($chunks) - 1) { + sleep(1); + } + } + } + + + + + $cacheKey = 'allotWechatFriend'; $now = time(); $maxRetry = 5; diff --git a/Server/application/job/AccountListJob.php b/Server/application/job/AccountListJob.php index 08f23142..6d76ab3d 100644 --- a/Server/application/job/AccountListJob.php +++ b/Server/application/job/AccountListJob.php @@ -73,7 +73,7 @@ class AccountListJob $request->withGet($params); // 调用公司账号列表获取方法 - $result = $accountController->getlist($pageIndex,$pageSize,true); + $result = $accountController->getlist(['pageIndex' => $pageIndex,'pageSize' => $pageSize],true); $response = json_decode($result,true); diff --git a/Server/application/job/WorkbenchTrafficDistributeJob.php b/Server/application/job/WorkbenchTrafficDistributeJob.php index 674a54a0..de457812 100644 --- a/Server/application/job/WorkbenchTrafficDistributeJob.php +++ b/Server/application/job/WorkbenchTrafficDistributeJob.php @@ -152,7 +152,9 @@ class WorkbenchTrafficDistributeJob 'deviceId' => $friend['deviceId'], 'wechatFriendId' => $friend['id'], 'wechatAccountId' => $account['id'], - 'createTime' => time() + 'createTime' => time(), + 'exp' => $config['exp'], + 'expTime' => time() + 86400 * $config['exp'], ]); Log::info("流量分发工作台 {$workbench->id} 好友[{$friend['id']}]分配给客服[{$account['id']}] 成功"); $i++; @@ -206,9 +208,11 @@ class WorkbenchTrafficDistributeJob ->join(['s2_wechat_account' => 'wa'], 'wa.id = wf.wechatAccountId', 'left') ->join('workbench_traffic_config_item wtci', 'wtci.wechatFriendId = wf.id AND wtci.workbenchId = ' . $config['workbenchId'], 'left') ->where([ + ['wf.isDeleted', '=', 0], ['wf.isDeleted', '=', 0], ['wf.isPassed', '=', 1], //['sa.departmentId', '=', $workbench->companyId], + ['wtci.expTime', '>', time()], ['wtci.id', 'null', null] ]) ->whereIn('wa.currentDeviceId', $devices)