From e0f29c6da76e9bb06ddfd5ff6b4f254195084e82 Mon Sep 17 00:00:00 2001 From: wong <106998207@qq.com> Date: Mon, 12 May 2025 17:40:04 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E8=A7=A6=E5=AE=A2=E5=AE=9D=E3=80=91?= =?UTF-8?q?=E6=9C=8B=E5=8F=8B=E5=9C=88=E9=87=87=E9=9B=86=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E8=A7=84=E5=88=99=EF=BC=88=E5=B0=86=E9=9C=80=E8=A6=81=E9=87=87?= =?UTF-8?q?=E9=9B=86=E7=9A=84=E7=94=A8=E6=88=B7=E5=85=88=E8=BF=81=E7=A7=BB?= =?UTF-8?q?=E5=88=B0=E6=8C=87=E5=AE=9A=E8=B4=A6=E5=8F=B7=E5=90=8E=E5=BC=80?= =?UTF-8?q?=E5=90=AF=E9=87=87=E9=9B=86=EF=BC=8C=E5=AE=8C=E6=AF=95=E5=90=8E?= =?UTF-8?q?=E8=BF=81=E7=A7=BB=E5=9B=9E=E5=8E=BB=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/controller/AutomaticAssign.php | 101 ++++++++++++++++-- .../common/service/AuthService.php | 2 +- .../controller/ContentLibraryController.php | 32 +++++- 3 files changed, 123 insertions(+), 12 deletions(-) diff --git a/Server/application/api/controller/AutomaticAssign.php b/Server/application/api/controller/AutomaticAssign.php index 2a61392e..88e0048f 100644 --- a/Server/application/api/controller/AutomaticAssign.php +++ b/Server/application/api/controller/AutomaticAssign.php @@ -19,12 +19,16 @@ class AutomaticAssign extends BaseController * 自动分配微信好友 * @return \think\response\Json */ - public function autoAllotWechatFriend($data = []) + public function autoAllotWechatFriend($data = [],$isInner = false) { // 获取授权token $authorization = trim($this->request->header('authorization', $this->authorization)); if (empty($authorization)) { - return errorJson('缺少授权信息'); + if($isInner){ + return json_encode(['code'=>500,'msg'=>'缺少授权信息']); + }else{ + return errorJson('缺少授权信息'); + } } try { @@ -34,7 +38,11 @@ class AutomaticAssign extends BaseController $isDeleted = !empty($data['isDeleted']) ? $data['isDeleted'] : input('isDeleted', false); if (empty($toAccountId)) { - return errorJson('目标账号ID不能为空'); + if($isInner){ + return json_encode(['code'=>500,'msg'=>'目标账号ID不能为空']); + }else{ + return errorJson('目标账号ID不能为空'); + } } $params = [ @@ -73,13 +81,25 @@ class AutomaticAssign extends BaseController $response = handleApiResponse($result); if($response){ - return successJson([],'微信好友自动分配成功'); + if($isInner){ + return json_encode(['code'=>200,'msg'=>'微信好友自动分配成功']); + }else{ + return successJson([],'微信好友自动分配成功'); + } }else{ - return errorJson($response); + if($isInner){ + return json_encode(['code'=>500,'msg'=>$response]); + }else{ + return errorJson($response); + } } } catch (\Exception $e) { - return errorJson('微信好友自动分配失败:' . $e->getMessage()); + if($isInner){ + return json_encode(['code'=>500,'msg'=>'微信好友自动分配失败:' . $e->getMessage()]); + }else{ + return errorJson('微信好友自动分配失败:' . $e->getMessage()); + } } } @@ -141,4 +161,73 @@ class AutomaticAssign extends BaseController return errorJson('微信群聊自动分配失败:' . $e->getMessage()); } } + + /** + * 指定微信好友分配到指定账号 + * @param int $wechatFriendId 微信好友ID + * @param int $toAccountId 目标账号ID + * @param string $comment 评论/备注 + * @param bool $notifyReceiver 是否通知接收者 + * @param int $optFrom 操作来源 + * @return \think\response\Json + */ + public function allotWechatFriend($data = [],$isInner = false) + { + // 获取授权token + $authorization = trim($this->request->header('authorization', $this->authorization)); + if (empty($authorization)) { + if($isInner){ + return json_encode(['code'=>500,'msg'=>'缺少授权信息']); + }else{ + return errorJson('缺少授权信息'); + } + } + + try { + // 获取请求参数 + $wechatFriendId = !empty($data['wechatFriendId']) ? $data['wechatFriendId'] : input('wechatFriendId', 0); + $toAccountId = !empty($data['toAccountId']) ? $data['toAccountId'] : input('toAccountId', 0); + $comment = !empty($data['comment']) ? $data['comment'] : input('comment', ''); + $notifyReceiver = !empty($data['notifyReceiver']) ? $data['notifyReceiver'] : input('notifyReceiver', false); + $optFrom = !empty($data['optFrom']) ? $data['optFrom'] : input('optFrom', 4); // 默认操作来源为4 + + // 参数验证 + 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/allot?wechatFriendId='.$wechatFriendId.'¬ifyReceiver='.$notifyReceiver.'&comment='.$comment.'&toAccountId='.$toAccountId.'&optFrom='.$optFrom; + $result = requestCurl($url, [], 'PUT', $header, 'json'); + + if (empty($result)) { + if($isInner){ + return json_encode(['code'=>200,'msg'=>'微信好友分配成功']); + }else{ + return successJson([], '微信好友分配成功'); + } + } else { + if($isInner){ + return json_encode(['code'=>500,'msg'=>$result]); + }else{ + return errorJson($result); + } + } + } catch (\Exception $e) { + if($isInner){ + return json_encode(['code'=>500,'msg'=>'微信好友分配失败:' . $e->getMessage()]); + }else{ + return errorJson('微信好友分配失败:' . $e->getMessage()); + } + } + } } \ No newline at end of file diff --git a/Server/application/common/service/AuthService.php b/Server/application/common/service/AuthService.php index 7f7188f3..f31fc600 100644 --- a/Server/application/common/service/AuthService.php +++ b/Server/application/common/service/AuthService.php @@ -196,7 +196,7 @@ class AuthService // 尝试从缓存获取授权信息 $authorization = Cache::get($cacheKey); - //$authorization = 'mYpVVhPY7PxctvYw1pn1VCTS2ck0yZG8q11gAiJrRN_D3q7KXXBPAfXoAmqs7kKHeaAx-h4GB7DiqVIQJ09HiXVhaQT6PtgLX3w8YV16erThC-lG1fyJB4DJxu-QxA3Q8ogSs1WFOa8aAXD1QQUZ7Kbjkw_VMLL4lrfe0Yjaqy3DnO7aL1xGnNjjX8P5uqCAZgHKlN8NjuDEGyYvXygW1YyoK9pNpwvq-6DYKjLWdmbHvFaAybHf-hU1XyrFavZqcZYxIoVXjfJ5ASp4XxeCWqMCzwtSoz9RAvwLAlNxGweowtuyX9389ZaXI-zbqb2T0S8llg'; + //$authorization = ''; // 如果缓存中没有或已过期,则重新获取 if (empty($authorization)) { try { diff --git a/Server/application/cunkebao/controller/ContentLibraryController.php b/Server/application/cunkebao/controller/ContentLibraryController.php index 19980111..fe2bcbc9 100644 --- a/Server/application/cunkebao/controller/ContentLibraryController.php +++ b/Server/application/cunkebao/controller/ContentLibraryController.php @@ -7,6 +7,8 @@ use app\cunkebao\model\ContentItem; use think\Controller; use think\Db; use app\api\controller\WebSocketController; +use think\facade\Env; +use app\api\controller\AutomaticAssign; /** * 内容库控制器 @@ -732,7 +734,7 @@ class ContentLibraryController extends Controller } // 返回采集结果 - return json([ + return json_encode([ 'code' => 200, 'msg' => '采集任务执行完成', 'data' => [ @@ -760,14 +762,20 @@ class ContentLibraryController extends Controller } try { + $toAccountId = ''; + $username = Env::get('api.username', ''); + $password = Env::get('api.password', ''); + if (!empty($username) || !empty($password)) { + $toAccountId = Db::name('users')->where('account',$username)->value('s2_accountId'); + } + // 查询好友信息 $friends = Db::table('s2_wechat_friend') - ->field('id, wechatAccountId, wechatId') + ->field('id, wechatAccountId, wechatId,accountId') ->whereIn('id', $friendIds) ->where('isDeleted', 0) ->select(); - if (empty($friends)) { return [ 'status' => 'failed', @@ -778,8 +786,22 @@ class ContentLibraryController extends Controller // 从朋友圈采集内容 $collectedData = []; $totalMomentsCount = 0; - + foreach ($friends as $friend) { + if (!empty($username) && !empty($password)) { + //执行切换好友命令 + $automaticAssign = new AutomaticAssign(); + $automaticAssign->allotWechatFriend(['wechatFriendId' => $friend['id'],'toAccountId' => $toAccountId],true); + //执行采集朋友圈命令 + $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); + } + + + + // 从s2_wechat_moments表获取朋友圈数据 $moments = Db::table('s2_wechat_moments') ->where([ @@ -788,7 +810,7 @@ class ContentLibraryController extends Controller ]) ->order('createTime', 'desc') ->select(); - + if (empty($moments)) { continue; }