From ebff15fbed9d7cb8899d2b615c1cbb46e4cca088 Mon Sep 17 00:00:00 2001 From: wong <106998207@qq.com> Date: Tue, 5 Aug 2025 10:26:55 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E7=BC=96=E8=BE=91=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E5=8F=8A=E4=BF=AE=E6=94=B9=E5=AF=86=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/controller/AccountController.php | 70 ++++++++++++++ Server/application/cunkebao/config/route.php | 8 ++ .../cunkebao/controller/BaseController.php | 95 +++++++++++++++++++ 3 files changed, 173 insertions(+) diff --git a/Server/application/api/controller/AccountController.php b/Server/application/api/controller/AccountController.php index 30345292..f239be00 100644 --- a/Server/application/api/controller/AccountController.php +++ b/Server/application/api/controller/AccountController.php @@ -576,6 +576,76 @@ class AccountController extends BaseController + public function accountModify($data = []) + { + // 获取授权token + $authorization = $this->authorization; + if (empty($authorization)) { + return errorJson('缺少授权信息'); + } + + + $id = !empty($data['id']) ? $data['id'] : ''; + if (empty($id)) { + return errorJson('账号ID不能为空'); + } + + $account = CompanyAccountModel::where('id', $id)->find(); + + + + if (empty($account)) { + return errorJson('账号不存在'); + } + $privilegeIds = json_decode($account->privilegeIds,true); + $privilegeIds = !empty($privilegeIds) ? $privilegeIds : [1001,1002,1004,1023,1406,20003,20021,20022,20023,20032,20041,20049,20054,20055,20060,20100,20102,20107,20055]; + + // 构建请求参数 + $params = [ + 'accountType' => !empty($data['accountType']) ? $data['accountType'] : $account->accountType, + 'alive' => !empty($data['alive']) ? $data['alive'] : $account->alive, + 'avatar' => !empty($data['avatar']) ? $data['avatar'] : $account->avatar, + 'createTime' => !empty($data['createTime']) ? $data['createTime'] : $account->createTime, + 'creator' => !empty($data['creator']) ? $data['creator'] : $account->creator, + 'creatorRealName' => !empty($data['creatorRealName']) ? $data['creatorRealName'] : $account->creatorRealName, + 'creatorUserName' => !empty($data['creatorUserName']) ? $data['creatorUserName'] : $account->creatorUserName, + 'departmentId' => !empty($data['departmentId']) ? $data['departmentId'] : $account->departmentId, + 'departmentIdArr' => !empty($data['departmentIdArr']) ? $data['departmentIdArr'] : [914,$account->departmentId], + 'departmentName' => !empty($data['departmentName']) ? $data['departmentName'] : $account->departmentName, + 'hasXiakeAccount' => !empty($data['hasXiakeAccount']) ? $data['hasXiakeAccount'] : false, + 'id' => !empty($data['id']) ? $data['id'] : $account->id, + 'memo' => !empty($data['memo']) ? $data['memo'] : $account->memo, + 'nickname' => !empty($data['nickname']) ? $data['nickname'] : $account->nickname, + 'privilegeIds' => !empty($data['privilegeIds']) ? $data['privilegeIds'] : $privilegeIds, + 'realName' => !empty($data['realName']) ? $data['realName'] : $account->realName, + 'status' => !empty($data['status']) ? $data['status'] : $account->status, + 'tenantId' => !empty($data['tenantId']) ? $data['tenantId'] : $account->tenantId, + 'userName' => !empty($data['userName']) ? $data['userName'] : $account->userName, + ]; + // 设置请求头 + $headerData = ['client:system']; + $header = setHeader($headerData, $authorization, 'json'); + + // 发送请求修改部门 + $result = requestCurl($this->baseUrl . 'api/account/modify', $params, 'PUT', $header, 'json'); + $response = handleApiResponse($result); + + + if(empty($response)){ + $newData = [ + 'nickname' => $params['nickname'], + 'avatar' => $params['avatar'], + ]; + CompanyAccountModel::where('id', $id)->update($newData); + return json_encode(['code' => 200, 'msg' => '账号修改成功']); + }else{ + return json_encode(['code' => 500, 'msg' => $response]); + } + } + + + + /************************ 私有辅助方法 ************************/ diff --git a/Server/application/cunkebao/config/route.php b/Server/application/cunkebao/config/route.php index 8f7777f8..392d73e6 100644 --- a/Server/application/cunkebao/config/route.php +++ b/Server/application/cunkebao/config/route.php @@ -7,6 +7,14 @@ use think\facade\Route; // 定义RESTful风格的API路由 Route::group('v1/', function () { + + Route::group('user', function () { + Route::put('editUserInfo', 'app\cunkebao\controller\BaseController@editUserInfo'); + Route::put('editPassWord', 'app\cunkebao\controller\BaseController@editPassWord'); + }); + + + // 设备管理相关 Route::group('devices', function () { Route::put('refresh', 'app\cunkebao\controller\device\RefreshDeviceDetailV1Controller@index'); diff --git a/Server/application/cunkebao/controller/BaseController.php b/Server/application/cunkebao/controller/BaseController.php index 961497ef..26bd79db 100644 --- a/Server/application/cunkebao/controller/BaseController.php +++ b/Server/application/cunkebao/controller/BaseController.php @@ -2,8 +2,11 @@ namespace app\cunkebao\controller; +use app\api\controller\AccountController; use app\common\service\ClassTableService; +use library\ResponseHelper; use think\Controller; +use think\Db; /** * 设备管理控制器 @@ -58,4 +61,96 @@ class BaseController extends Controller return $column ? $user[$column] : $user; } + + + public function editUserInfo() + { + $userId = $this->request->param('userId', ''); + $nickname = $this->request->param('nickname', ''); + $avatar = $this->request->param('avatar', ''); + $phone = $this->request->param('phone', ''); + $companyId = $this->getUserInfo('companyId'); + if (empty($userId)) { + return ResponseHelper::error('用户id不能为空'); + } + + if (empty($nickname) && empty($avatar) && empty($phone)) { + return ResponseHelper::error('修改的用户信息不能为空'); + } + + $user = Db::name('users')->where(['id' => $userId, 'companyId' => $companyId])->find(); + if (empty($user)) { + return ResponseHelper::error('用户不存在'); + } + + $user2 = Db::name('users')->where(['phone' => $phone])->find(); + if (!empty($user2) && $user2['id'] != $userId) { + return ResponseHelper::error('修改的手机号已存在'); + } + + $data = [ + 'id' => $user['s2_accountId'], + ]; + + if (!empty($nickname)) { + $data['nickname'] = $nickname; + } + if (!empty($avatar)) { + $data['avatar'] = $avatar; + } + if (!empty($phone)) { + $data['phone'] = $phone; + } + + $AccountControllel = new AccountController(); + $res = $AccountControllel->accountModify($data); + $res = json_decode($res, true); + if ($res['code'] == 200) { + unset($data['id']); + if (!empty($nickname)) { + $data['username'] = $nickname; + unset($data['nickname']); + } + Db::name('users')->where(['id' => $userId, 'companyId' => $companyId])->update($data); + return ResponseHelper::success('更新成功'); + } else { + return ResponseHelper::error($res['msg']); + } + } + + + public function editPassWord() + { + $userId = $this->request->param('userId', ''); + $passWord = $this->request->param('passWord', ''); + $companyId = $this->getUserInfo('companyId'); + if (empty($userId)) { + return ResponseHelper::error('用户id不能为空'); + } + + if (empty($passWord)) { + return ResponseHelper::error('密码不能为空'); + } + + $user = Db::name('users')->where(['id' => $userId, 'companyId' => $companyId])->find(); + if (empty($user)) { + return ResponseHelper::error('用户不存在'); + } + if ($user['passwordMd5'] == md5($passWord)) { + return ResponseHelper::error('新密码与旧密码一致'); + } + + $data = [ + 'passwordMd5' => md5($passWord), + 'passwordLocal' => localEncrypt($passWord), + 'updateTime' => time() + ]; + + $res = Db::name('users')->where(['id' => $userId, 'companyId' => $companyId])->update($data); + if (!empty($res)) { + return ResponseHelper::success('密码修改成功'); + } else { + return ResponseHelper::error('密码修改失败'); + } + } } \ No newline at end of file From 982b2c024bbb4e5b074cc4c8f1a7ce08c3180933 Mon Sep 17 00:00:00 2001 From: wong <106998207@qq.com> Date: Tue, 5 Aug 2025 10:28:28 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E7=BE=A4=E5=88=97?= =?UTF-8?q?=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../command/SyncWechatDataToCkbTask.php | 6 +++ .../Adapters/ChuKeBao/Adapter.php | 39 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/Server/application/command/SyncWechatDataToCkbTask.php b/Server/application/command/SyncWechatDataToCkbTask.php index 4e1bc482..fe95c1c0 100644 --- a/Server/application/command/SyncWechatDataToCkbTask.php +++ b/Server/application/command/SyncWechatDataToCkbTask.php @@ -51,6 +51,7 @@ class SyncWechatDataToCkbTask extends Command $this->syncWechatDeviceLoginLog($ChuKeBaoAdapter); $this->syncWechatDevice($ChuKeBaoAdapter); $this->syncWechatCustomer($ChuKeBaoAdapter); + $this->syncWechatGroup($ChuKeBaoAdapter); $this->syncWechatFriendToTrafficPoolBatch($ChuKeBaoAdapter); $this->syncTrafficSourceUser($ChuKeBaoAdapter); $this->syncTrafficSourceGroup($ChuKeBaoAdapter); @@ -108,5 +109,10 @@ class SyncWechatDataToCkbTask extends Command return $ChuKeBaoAdapter->syncTrafficSourceGroup(); } + protected function syncWechatGroup(ChuKeBaoAdapter $ChuKeBaoAdapter) + { + return $ChuKeBaoAdapter->syncWechatGroup(); + } + } \ No newline at end of file diff --git a/Server/extend/WeChatDeviceApi/Adapters/ChuKeBao/Adapter.php b/Server/extend/WeChatDeviceApi/Adapters/ChuKeBao/Adapter.php index 2dbded4d..95358eeb 100644 --- a/Server/extend/WeChatDeviceApi/Adapters/ChuKeBao/Adapter.php +++ b/Server/extend/WeChatDeviceApi/Adapters/ChuKeBao/Adapter.php @@ -1318,4 +1318,43 @@ class Adapter implements WeChatServiceInterface } } while ($affected > 0); } + + public function syncWechatGroup() + { + $sql = "insert into ck_wechat_group(`id`,`chatroomId`,`name`,`avatar`,`companyId`,`ownerWechatId`,`createTime`,`updateTime`,`deleteTime`) + SELECT + g.id id, + g.chatroomId chatroomId, + g.nickname name, + g.chatroomAvatar avatar, + c.departmentId companyId, + g.wechatAccountWechatId ownerWechatId, + g.createTime createTime, + g.updateTime updateTime, + g.deleteTime deleteTime + FROM + s2_wechat_chatroom g + LEFT JOIN s2_company_account c ON g.accountId = c.id + ORDER BY g.id DESC + LIMIT ?, ? + ON DUPLICATE KEY UPDATE + chatroomId=VALUES(chatroomId), + companyId=VALUES(companyId), + ownerWechatId=VALUES(ownerWechatId)"; + + + $offset = 0; + $limit = 2000; + $usleepTime = 50000; + do { + $affected = Db::execute($sql, [$offset, $limit]); + $offset += $limit; + if ($affected > 0) { + usleep($usleepTime); + } + } while ($affected > 0); + } + + + } From d4c10de30a8d3c3d85b399249f6507485fcfced4 Mon Sep 17 00:00:00 2001 From: wong <106998207@qq.com> Date: Wed, 6 Aug 2025 17:57:24 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E7=BE=A4=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/extend/WeChatDeviceApi/Adapters/ChuKeBao/Adapter.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Server/extend/WeChatDeviceApi/Adapters/ChuKeBao/Adapter.php b/Server/extend/WeChatDeviceApi/Adapters/ChuKeBao/Adapter.php index 95358eeb..a3751975 100644 --- a/Server/extend/WeChatDeviceApi/Adapters/ChuKeBao/Adapter.php +++ b/Server/extend/WeChatDeviceApi/Adapters/ChuKeBao/Adapter.php @@ -1321,9 +1321,10 @@ class Adapter implements WeChatServiceInterface public function syncWechatGroup() { - $sql = "insert into ck_wechat_group(`id`,`chatroomId`,`name`,`avatar`,`companyId`,`ownerWechatId`,`createTime`,`updateTime`,`deleteTime`) + $sql = "insert into ck_wechat_group(`id`,`wechatAccountId`,`chatroomId`,`name`,`avatar`,`companyId`,`ownerWechatId`,`createTime`,`updateTime`,`deleteTime`) SELECT g.id id, + g.wechatAccountId wechatAccountId, g.chatroomId chatroomId, g.nickname name, g.chatroomAvatar avatar, From 7a2553940c4fc07c6f7eee5b1d1e6e069587dc90 Mon Sep 17 00:00:00 2001 From: wong <106998207@qq.com> Date: Wed, 6 Aug 2025 18:00:50 +0800 Subject: [PATCH 4/6] =?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 --- ...PotentialListWithInCompanyV1Controller.php | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/Server/application/cunkebao/controller/traffic/GetPotentialListWithInCompanyV1Controller.php b/Server/application/cunkebao/controller/traffic/GetPotentialListWithInCompanyV1Controller.php index 9a68d3c7..ec35f503 100644 --- a/Server/application/cunkebao/controller/traffic/GetPotentialListWithInCompanyV1Controller.php +++ b/Server/application/cunkebao/controller/traffic/GetPotentialListWithInCompanyV1Controller.php @@ -270,7 +270,7 @@ class GetPotentialListWithInCompanyV1Controller extends BaseController $percentage = number_format(($taskNum / $passNum) * 100, 2); $total['percentage'] = $percentage; } - + $data['total'] = $total; @@ -322,14 +322,20 @@ class GetPotentialListWithInCompanyV1Controller extends BaseController public function getUserTags() { $userId = $this->request->param('userId', ''); + $companyId = $this->getUserInfo('companyId'); if (empty($userId)) { return json_encode(['code' => 500, 'msg' => '用户id不能为空']); } $data = Db::name('traffic_pool')->alias('tp') - ->join(['s2_wechat_friend' => 'wf'], 'tp.wechatId=wf.wechatId', 'left') + ->join('wechat_friendship f', 'tp.wechatId=f.wechatId AND f.companyId='.$companyId, 'left') + ->join(['s2_wechat_friend' => 'wf'], 'f.wechatId=wf.wechatId', 'left') ->where(['tp.id' => $userId]) ->order('tp.createTime desc') ->column('wf.id,wf.labels,wf.siteLabels'); + if (empty($data)) { + return ResponseHelper::success(['wechat' => [], 'siteLabels' => []]); + } + $tags = []; $siteLabels = []; @@ -350,5 +356,31 @@ class GetPotentialListWithInCompanyV1Controller extends BaseController return ResponseHelper::success(['wechat' => $tags, 'siteLabels' => $siteLabels]); } + /* public function editUserTags() + { + $userId = $this->request->param('userId', ''); + if (empty($userId)) { + return json_encode(['code' => 500, 'msg' => '用户id不能为空']); + } + $tags = $this->request->param('tags', []); + $tags = $this->request->param('tags', []); + $isWechat = $this->request->param('isWechat', false); + $companyId = $this->getUserInfo('companyId'); + + $friend = Db::name('traffic_pool')->alias('tp') + ->join('wechat_friendship f', 'tp.wechatId=f.wechatId AND f.companyId='.$companyId, 'left') + ->join(['s2_wechat_friend' => 'wf'], 'f.wechatId=wf.wechatId', 'left') + ->where(['tp.id' => $userId]) + ->order('tp.createTime desc') + ->column('wf.id,wf.accountId,wf.labels,wf.siteLabels'); + if (empty($data)) { + return ResponseHelper::error('该用户不存在'); + } + + + }*/ + + + } \ No newline at end of file From 94c484dbe474ed2ba3822a46350dbff51eb6171c Mon Sep 17 00:00:00 2001 From: wong <106998207@qq.com> Date: Wed, 6 Aug 2025 18:02:03 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E5=B7=A5=E4=BD=9C=E5=8F=B0-=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E7=BE=A4=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/controller/WebSocketController.php | 362 +++++++++--------- Server/application/command.php | 1 + Server/application/cunkebao/config/route.php | 3 + .../controller/WorkbenchController.php | 34 ++ Server/crontab_tasks.md | 3 + 5 files changed, 215 insertions(+), 188 deletions(-) diff --git a/Server/application/api/controller/WebSocketController.php b/Server/application/api/controller/WebSocketController.php index 5090a578..24d67fa6 100644 --- a/Server/application/api/controller/WebSocketController.php +++ b/Server/application/api/controller/WebSocketController.php @@ -9,12 +9,11 @@ use think\Db; use think\facade\Log; use WebSocket\Client; use think\facade\Env; -use app\api\model\WechatFriendModel as WechatFriend; +use app\api\model\WechatFriendModel as WechatFriend; use app\api\model\WechatMomentsModel as WechatMoments; use think\facade\Cache; - class WebSocketController extends BaseController { protected $authorized; @@ -27,7 +26,7 @@ class WebSocketController extends BaseController /************************************ * 初始化相关功能 ************************************/ - + /** * 构造函数 - 初始化WebSocket连接 * @param array $userData 用户数据 @@ -44,39 +43,39 @@ class WebSocketController extends BaseController */ protected function initConnection($userData = []) { - if(!empty($userData) && count($userData)){ + if (!empty($userData) && count($userData)) { if (empty($userData['userName']) || empty($userData['password'])) { - return json_encode(['code'=>400,'msg'=>'参数缺失']); + return json_encode(['code' => 400, 'msg' => '参数缺失']); } // 检查缓存中是否存在有效的token $cacheKey = 'websocket_token_' . $userData['userName']; - $cachedToken = Cache::get($cacheKey); - + $cachedToken = Cache::get($cacheKey); + if ($cachedToken) { $this->authorized = $cachedToken; $this->accountId = $userData['accountId']; } else { - $params = [ - 'grant_type' => 'password', - 'username' => $userData['userName'], - 'password' => $userData['password'] - ]; + $params = [ + 'grant_type' => 'password', + 'username' => $userData['userName'], + 'password' => $userData['password'] + ]; - // 调用登录接口获取token - $headerData = ['client:kefu-client']; - $header = setHeader($headerData, '', 'plain'); - $result = requestCurl('https://kf.quwanzhi.com:9991/token', $params, 'POST', $header); - $result_array = handleApiResponse($result); + // 调用登录接口获取token + $headerData = ['client:kefu-client']; + $header = setHeader($headerData, '', 'plain'); + $result = requestCurl('https://kf.quwanzhi.com:9991/token', $params, 'POST', $header); + $result_array = handleApiResponse($result); - if (isset($result_array['access_token']) && !empty($result_array['access_token'])) { - $this->authorized = $result_array['access_token']; - $this->accountId = $userData['accountId']; - - // 将token存入缓存,有效期5分钟 - Cache::set($cacheKey, $this->authorized, 300); - } else { - return json_encode(['code'=>400,'msg'=>'获取系统授权信息失败']); + if (isset($result_array['access_token']) && !empty($result_array['access_token'])) { + $this->authorized = $result_array['access_token']; + $this->accountId = $userData['accountId']; + + // 将token存入缓存,有效期5分钟 + Cache::set($cacheKey, $this->authorized, 300); + } else { + return json_encode(['code' => 400, 'msg' => '获取系统授权信息失败']); } } } else { @@ -85,7 +84,7 @@ class WebSocketController extends BaseController } if (empty($this->authorized) || empty($this->accountId)) { - return json_encode(['code'=>400,'msg'=>'缺失关键参数']); + return json_encode(['code' => 400, 'msg' => '缺失关键参数']); } $this->connect(); @@ -97,40 +96,40 @@ class WebSocketController extends BaseController protected function connect() { try { - //证书 - $context = stream_context_create(); - stream_context_set_option($context, 'ssl', 'verify_peer', false); - stream_context_set_option($context, 'ssl', 'verify_peer_name', false); - - //开启WS链接 - $result = [ - "accessToken" => $this->authorized, - "accountId" => $this->accountId, - "client" => "kefu-client", - "cmdType" => "CmdSignIn", - "seq" => 1, - ]; + //证书 + $context = stream_context_create(); + stream_context_set_option($context, 'ssl', 'verify_peer', false); + stream_context_set_option($context, 'ssl', 'verify_peer_name', false); - $content = json_encode($result); - $this->client = new Client("wss://kf.quwanzhi.com:9993", - [ - 'filter' => ['text', 'binary', 'ping', 'pong', 'close','receive', 'send'], - 'context' => $context, - 'headers' => [ - 'Sec-WebSocket-Protocol' => 'soap', - 'origin' => 'localhost', - ], - 'timeout' => 86400, - ] - ); - - $this->client->send($content); + //开启WS链接 + $result = [ + "accessToken" => $this->authorized, + "accountId" => $this->accountId, + "client" => "kefu-client", + "cmdType" => "CmdSignIn", + "seq" => 1, + ]; + + $content = json_encode($result); + $this->client = new Client("wss://kf.quwanzhi.com:9993", + [ + 'filter' => ['text', 'binary', 'ping', 'pong', 'close', 'receive', 'send'], + 'context' => $context, + 'headers' => [ + 'Sec-WebSocket-Protocol' => 'soap', + 'origin' => 'localhost', + ], + 'timeout' => 86400, + ] + ); + + $this->client->send($content); $this->isConnected = true; $this->lastHeartbeatTime = time(); - + // 启动心跳检测 //$this->startHeartbeat(); - + } catch (\Exception $e) { Log::error("WebSocket连接失败:" . $e->getMessage()); $this->isConnected = false; @@ -143,7 +142,7 @@ class WebSocketController extends BaseController protected function startHeartbeat() { // 使用定时器发送心跳 - \Swoole\Timer::tick($this->heartbeatInterval * 1000, function() { + \Swoole\Timer::tick($this->heartbeatInterval * 1000, function () { if ($this->isConnected) { $this->sendHeartbeat(); } @@ -160,10 +159,10 @@ class WebSocketController extends BaseController "cmdType" => "CmdHeartbeat", "seq" => time() ]; - + $this->client->send(json_encode($heartbeat)); $this->lastHeartbeatTime = time(); - + } catch (\Exception $e) { Log::error("发送心跳包失败:" . $e->getMessage()); $this->reconnect(); @@ -204,7 +203,7 @@ class WebSocketController extends BaseController protected function sendMessage($data) { $this->checkConnection(); - + try { $this->client->send(json_encode($data)); $response = $this->client->receive(); @@ -235,9 +234,9 @@ class WebSocketController extends BaseController $currentPage = 1; // 当前页码 $allMoments = []; // 存储所有朋友圈数据 - //过滤消息 + //过滤消息 if (empty($wechatAccountId)) { - return json_encode(['code'=>400,'msg'=>'指定账号不能为空']); + return json_encode(['code' => 400, 'msg' => '指定账号不能为空']); } try { @@ -263,21 +262,21 @@ class WebSocketController extends BaseController sleep(10); continue; } - + // 检查返回结果 if (!isset($message['result']) || empty($message['result']) || !is_array($message['result'])) { break; } - + // 检查是否遇到旧数据 $hasOldData = false; foreach ($message['result'] as $moment) { $momentId = WechatMoments::where('snsId', $moment['snsId']) ->where('wechatAccountId', $wechatAccountId) ->value('id'); - + if (!empty($momentId)) { $hasOldData = true; break; @@ -330,31 +329,31 @@ class WebSocketController extends BaseController return json_encode($result); } catch (\Exception $e) { - return json_encode(['code'=>500,'msg'=>$e->getMessage()]); + return json_encode(['code' => 500, 'msg' => $e->getMessage()]); } } - /** + /** * 朋友圈点赞 * @return \think\response\Json */ public function momentInteract($data = []) { - + $snsId = !empty($data['snsId']) ? $data['snsId'] : ''; $wechatAccountId = !empty($data['wechatAccountId']) ? $data['wechatAccountId'] : ''; $wechatFriendId = !empty($data['wechatFriendId']) ? $data['wechatFriendId'] : 0; //过滤消息 - if (empty($snsId)) { - return json_encode(['code'=>400,'msg'=>'snsId不能为空']); + if (empty($snsId)) { + return json_encode(['code' => 400, 'msg' => 'snsId不能为空']); } - if (empty($wechatAccountId)) { - return json_encode(['code'=>400,'msg'=>'微信id不能为空']); + if (empty($wechatAccountId)) { + return json_encode(['code' => 400, 'msg' => '微信id不能为空']); } - - try { + + try { $result = [ "cmdType" => "CmdMomentInteract", "momentInteractType" => 1, @@ -362,16 +361,16 @@ class WebSocketController extends BaseController "snsId" => $snsId, "wechatAccountId" => $wechatAccountId, "wechatFriendId" => $wechatFriendId, - ]; + ]; $message = $this->sendMessage($result); - return json_encode(['code'=>200,'msg'=>'点赞成功','data'=>$message]); - } catch (\Exception $e) { - return json_encode(['code'=>500,'msg'=>$e->getMessage()]); + return json_encode(['code' => 200, 'msg' => '点赞成功', 'data' => $message]); + } catch (\Exception $e) { + return json_encode(['code' => 500, 'msg' => $e->getMessage()]); } } - /** + /** * 朋友圈取消点赞 * @return \think\response\Json */ @@ -381,36 +380,36 @@ class WebSocketController extends BaseController $data = $this->request->param(); if (empty($data)) { - return json_encode(['code'=>400,'msg'=>'参数缺失']); + return json_encode(['code' => 400, 'msg' => '参数缺失']); } //过滤消息 if (empty($data['snsId'])) { - return json_encode(['code'=>400,'msg'=>'snsId不能为空']); + return json_encode(['code' => 400, 'msg' => 'snsId不能为空']); } if (empty($data['wechatAccountId'])) { - return json_encode(['code'=>400,'msg'=>'微信id不能为空']); + return json_encode(['code' => 400, 'msg' => '微信id不能为空']); } - + try { - $result = [ - "CommentId2" => '', - "CommentTime" => 0, - "cmdType" => "CmdMomentCancelInteract", - "optType" => 1, - "seq" => time(), - "snsId" => $data['snsId'], - "wechatAccountId" => $data['wechatAccountId'], - "wechatFriendId" => 0, - ]; + $result = [ + "CommentId2" => '', + "CommentTime" => 0, + "cmdType" => "CmdMomentCancelInteract", + "optType" => 1, + "seq" => time(), + "snsId" => $data['snsId'], + "wechatAccountId" => $data['wechatAccountId'], + "wechatFriendId" => 0, + ]; $message = $this->sendMessage($result); - return json_encode(['code'=>200,'msg'=>'取消点赞成功','data'=>$message]); + return json_encode(['code' => 200, 'msg' => '取消点赞成功', 'data' => $message]); } catch (\Exception $e) { - return json_encode(['code'=>500,'msg'=>$e->getMessage()]); + return json_encode(['code' => 500, 'msg' => $e->getMessage()]); } } else { - return json_encode(['code'=>400,'msg'=>'非法请求']); + return json_encode(['code' => 400, 'msg' => '非法请求']); } } @@ -462,19 +461,19 @@ class WebSocketController extends BaseController // 发送请求 $this->client->send(json_encode($params)); - + // 接收响应 $response = $this->client->receive(); $message = json_decode($response, true); - if(empty($message)){ - return json_encode(['code'=>500,'msg'=>'获取朋友圈资源链接失败']); + if (empty($message)) { + return json_encode(['code' => 500, 'msg' => '获取朋友圈资源链接失败']); } - if($message['cmdType'] == 'CmdDownloadMomentImagesResult' && is_array($message['urls']) && count($message['urls']) > 0){ - $urls = json_encode($message['urls'],256); - Db::table('s2_wechat_moments')->where('snsId',$data['snsId'])->update(['resUrls'=>$urls]); + if ($message['cmdType'] == 'CmdDownloadMomentImagesResult' && is_array($message['urls']) && count($message['urls']) > 0) { + $urls = json_encode($message['urls'], 256); + Db::table('s2_wechat_moments')->where('snsId', $data['snsId'])->update(['resUrls' => $urls]); } - return json_encode(['code'=>200,'msg'=>'获取朋友圈资源链接成功','data'=>$message]); + return json_encode(['code' => 200, 'msg' => '获取朋友圈资源链接成功', 'data' => $message]); } catch (\Exception $e) { // 记录错误日志 Log::error('获取朋友圈资源链接异常:' . $e->getMessage()); @@ -507,19 +506,18 @@ class WebSocketController extends BaseController return false; } - + try { foreach ($momentList as $moment) { // 提取momentEntity中的数据 $momentEntity = $moment['momentEntity'] ?? []; - + // 检查朋友圈数据是否已存在 $momentId = WechatMoments::where('snsId', $moment['snsId']) ->where('wechatAccountId', $wechatAccountId) ->value('id'); - $dataToSave = [ 'commentList' => json_encode($moment['commentList'] ?? [], 256), 'createTime' => $moment['createTime'] ?? 0, @@ -543,7 +541,7 @@ class WebSocketController extends BaseController // 如果已存在,则更新数据 Db::table('s2_wechat_moments')->where('id', $momentId)->update($dataToSave); } else { - if(empty($wechatFriendId)){ + if (empty($wechatFriendId)) { $wechatFriendId = WechatFriend::where('wechatAccountId', $wechatAccountId)->where('wechatId', $momentEntity['userName'])->value('id'); } // 如果不存在,则插入新数据 @@ -588,7 +586,7 @@ class WebSocketController extends BaseController if (empty($wechatFriendId)) { return json_encode(['code' => 400, 'msg' => '好友ID不能为空']); } - + if (empty($wechatAccountId)) { return json_encode(['code' => 400, 'msg' => '微信账号ID不能为空']); } @@ -609,7 +607,7 @@ class WebSocketController extends BaseController // 发送请求并获取响应 $message = $this->sendMessage($params); - + // 记录日志 Log::info('修改好友标签:' . json_encode($params, 256)); Log::info('修改好友标签结果:' . json_encode($message, 256)); @@ -619,7 +617,7 @@ class WebSocketController extends BaseController } catch (\Exception $e) { // 记录错误日志 Log::error('修改好友标签失败:' . $e->getMessage()); - + // 返回错误响应 return json_encode(['code' => 500, 'msg' => '修改标签失败:' . $e->getMessage()]); } @@ -653,24 +651,23 @@ class WebSocketController extends BaseController // 消息拼接 msgType(1:文本 3:图片 43:视频 47:动图表情包(gif、其他表情包) 49:小程序/其他:图文、文件) // 当前,type 为文本、图片、动图表情包的时候,content为string, 其他情况为对象 {type: 'file/link/...', url: '', title: '', thunmbPath: '', desc: ''} $params = [ - "cmdType" => "CmdSendMessage", - "content" => $dataArray['content'], - "msgSubType" => 0, - "msgType" => $dataArray['msgType'], - "seq" => time(), - "wechatAccountId" => $dataArray['wechatAccountId'], - "wechatChatroomId" => 0, - "wechatFriendId" => $dataArray['wechatFriendId'], - ]; + "cmdType" => "CmdSendMessage", + "content" => $dataArray['content'], + "msgSubType" => 0, + "msgType" => $dataArray['msgType'], + "seq" => time(), + "wechatAccountId" => $dataArray['wechatAccountId'], + "wechatChatroomId" => 0, + "wechatFriendId" => $dataArray['wechatFriendId'], + ]; // 发送请求 $this->client->send(json_encode($params)); // 接收响应 $response = $this->client->receive(); $message = json_decode($response, true); - - if(!empty($message)){ - return json_encode(['code'=>500,'msg'=>'信息发送成功','data'=>$message]); + if (!empty($message)) { + return json_encode(['code' => 200, 'msg' => '信息发送成功', 'data' => $message]); } } @@ -678,65 +675,55 @@ class WebSocketController extends BaseController * 发送群消息 * @return \think\response\Json */ - public function sendCommunity() + public function sendCommunity($dataArray = []) { - if ($this->request->isPost()) { - $data = $this->request->post(); - if (empty($data)) { - return json_encode(['code'=>400,'msg'=>'参数缺失']); - } - $dataArray = $data; - if (!is_array($dataArray)) { - return json_encode(['code'=>400,'msg'=>'数据格式错误']); - } - - //过滤消息 - if (empty($dataArray['content'])) { - return json_encode(['code'=>400,'msg'=>'内容缺失']); - } - if (empty($dataArray['wechatAccountId'])) { - return json_encode(['code'=>400,'msg'=>'微信id不能为空']); - } - - if (empty($dataArray['msgType'])) { - return json_encode(['code'=>400,'msg'=>'类型缺失']); - } - if (empty($dataArray['wechatChatroomId'])) { - return json_encode(['code'=>400,'msg'=>'群id不能为空']); - } - - $msg = '消息成功发送'; - $message = []; - try { - //消息拼接 msgType(1:文本 3:图片 43:视频 47:动图表情包 49:小程序) - $result = [ - "cmdType" => "CmdSendMessage", - "content" => htmlspecialchars_decode($dataArray['content']), - "msgSubType" => 0, - "msgType" => $dataArray['msgType'], - "seq" => time(), - "wechatAccountId" => $dataArray['wechatAccountId'], - "wechatChatroomId" => $dataArray['wechatChatroomId'], - "wechatFriendId" => 0, - ]; - - $result = json_encode($result); - $this->client->send($result); - $message = $this->client->receive(); - //关闭WS链接 - $this->client->close(); - //Log::write('WS群消息发送'); - //Log::write($message); - $message = json_decode($message, 1); - } catch (\Exception $e) { - $msg = $e->getMessage(); - } - return json_encode(['code'=>200,'msg'=>$msg,'data'=>$message]); - - } else { - return json_encode(['code'=>400,'msg'=>'非法请求']); - //return errorJson('非法请求'); + if (!is_array($dataArray)) { + return json_encode(['code' => 400, 'msg' => '数据格式错误']); } + + //过滤消息 + if (empty($dataArray['content'])) { + return json_encode(['code' => 400, 'msg' => '内容缺失']); + } + if (empty($dataArray['wechatAccountId'])) { + return json_encode(['code' => 400, 'msg' => '微信id不能为空']); + } + + if (empty($dataArray['msgType'])) { + return json_encode(['code' => 400, 'msg' => '类型缺失']); + } + if (empty($dataArray['wechatChatroomId'])) { + return json_encode(['code' => 400, 'msg' => '群id不能为空']); + } + + $message = []; + try { + //消息拼接 msgType(1:文本 3:图片 43:视频 47:动图表情包 49:小程序) + $params = [ + "cmdType" => "CmdSendMessage", + "content" => htmlspecialchars_decode($dataArray['content']), + "msgSubType" => 0, + "msgType" => $dataArray['msgType'], + "seq" => time(), + "wechatAccountId" => $dataArray['wechatAccountId'], + "wechatChatroomId" => $dataArray['wechatChatroomId'], + "wechatFriendId" => 0, + ]; + + // 发送请求 + $this->client->send(json_encode($params)); + // 接收响应 + $response = $this->client->receive(); + $message = json_decode($response, true); + if (!empty($message)) { + return json_encode(['code' => 200, 'msg' => '信息发送成功', 'data' => $message]); + } + } catch (\Exception $e) { + $msg = $e->getMessage(); + return json_encode(['code' => 400, 'msg' => $msg, 'data' => $message]); + } + + } /** @@ -747,26 +734,26 @@ class WebSocketController extends BaseController public function sendCommunitys($data = []) { if (empty($data)) { - return json_encode(['code'=>400,'msg'=>'参数缺失']); + return json_encode(['code' => 400, 'msg' => '参数缺失']); } $dataArray = $data; if (!is_array($dataArray)) { - return json_encode(['code'=>400,'msg'=>'数据格式错误']); + return json_encode(['code' => 400, 'msg' => '数据格式错误']); } //过滤消息 if (empty($dataArray['content'])) { - return json_encode(['code'=>400,'msg'=>'内容缺失']); + return json_encode(['code' => 400, 'msg' => '内容缺失']); } if (empty($dataArray['wechatAccountId'])) { - return json_encode(['code'=>400,'msg'=>'微信id不能为空']); + return json_encode(['code' => 400, 'msg' => '微信id不能为空']); } if (empty($dataArray['msgType'])) { - return json_encode(['code'=>400,'msg'=>'类型缺失']); + return json_encode(['code' => 400, 'msg' => '类型缺失']); } if (empty($dataArray['wechatChatroomId'])) { - return json_encode(['code'=>400,'msg'=>'群id不能为空']); + return json_encode(['code' => 400, 'msg' => '群id不能为空']); } $msg = '消息成功发送'; @@ -796,11 +783,10 @@ class WebSocketController extends BaseController $msg = $e->getMessage(); } - return json_encode(['code'=>200,'msg'=>$msg,'data'=>$message]); + return json_encode(['code' => 200, 'msg' => $msg, 'data' => $message]); } - /** * 邀请好友入群 * @param array $data 请求参数 @@ -843,11 +829,11 @@ class WebSocketController extends BaseController Log::info('邀请好友入群请求:' . json_encode($params, 256)); $message = $this->sendMessage($params); - return json_encode(['code'=>200,'msg'=>'邀请成功','data'=>$message]); + return json_encode(['code' => 200, 'msg' => '邀请成功', 'data' => $message]); } catch (\Exception $e) { // 记录错误日志 Log::error('邀请好友入群异常:' . $e->getMessage()); - // 返回错误响应 + // 返回错误响应 return json_encode(['code' => 500, 'msg' => '邀请好友入群异常:' . $e->getMessage()]); } } diff --git a/Server/application/command.php b/Server/application/command.php index 570c76a4..17904a8a 100644 --- a/Server/application/command.php +++ b/Server/application/command.php @@ -32,5 +32,6 @@ return [ 'sync:wechatData' => 'app\command\SyncWechatDataToCkbTask', // 同步微信数据到存客宝 'sync:allFriends' => 'app\command\SyncAllFriendsCommand', // 同步所有在线好友 'workbench:trafficDistribute' => 'app\command\WorkbenchTrafficDistributeCommand', // 工作台流量分发任务 + 'workbench:groupPush' => 'app\command\WorkbenchGroupPushCommand', // 工作台群组同步任务 'switch:friends' => 'app\command\SwitchFriendsCommand', ]; diff --git a/Server/application/cunkebao/config/route.php b/Server/application/cunkebao/config/route.php index 392d73e6..2a45e9f6 100644 --- a/Server/application/cunkebao/config/route.php +++ b/Server/application/cunkebao/config/route.php @@ -94,6 +94,9 @@ Route::group('v1/', function () { Route::get('device-labels', 'app\cunkebao\controller\WorkbenchController@getDeviceLabels'); // 获取设备微信好友标签统计 Route::get('group-list', 'app\cunkebao\controller\WorkbenchController@getGroupList'); // 获取群列表 Route::get('account-list', 'app\cunkebao\controller\WorkbenchController@getAccountList'); // 获取账号列表 + + Route::get('getJdSocialMedia', 'app\cunkebao\controller\WorkbenchController@getJdSocialMedia'); // 获取京东联盟导购媒体 + Route::get('getJdPromotionSite', 'app\cunkebao\controller\WorkbenchController@getJdPromotionSite'); // 获取京东联盟广告位 }); // 内容库相关 diff --git a/Server/application/cunkebao/controller/WorkbenchController.php b/Server/application/cunkebao/controller/WorkbenchController.php index 5bf682a6..95ec91a9 100644 --- a/Server/application/cunkebao/controller/WorkbenchController.php +++ b/Server/application/cunkebao/controller/WorkbenchController.php @@ -1500,4 +1500,38 @@ class WorkbenchController extends Controller } + /** + * 获取京东联盟导购媒体 + * @return \think\response\Json + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public function getJdSocialMedia() + { + $data = Db::name('jd_social_media')->order('id DESC')->select(); + return json(['code' => 200, 'msg' => '获取成功', 'data' => $data]); + } + + /** + * 获取京东联盟广告位 + * @return \think\response\Json + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public function getJdPromotionSite() + { + $id = $this->request->param('id', ''); + if (empty($id)) { + return json(['code' => 500, 'msg' => '参数缺失']); + } + + $data = Db::name('jd_promotion_site')->where('jdSocialMediaId',$id)->order('id DESC')->select(); + return json(['code' => 200, 'msg' => '获取成功', 'data' => $data]); + } + + + + } \ No newline at end of file diff --git a/Server/crontab_tasks.md b/Server/crontab_tasks.md index 00aadff3..1442f0c6 100644 --- a/Server/crontab_tasks.md +++ b/Server/crontab_tasks.md @@ -57,6 +57,9 @@ # 同步微信数据到存客宝 0 9 * * * cd /www/wwwroot/mckb_quwanzhi_com/Server && php think sync:wechatData >> /www/wwwroot/mckb_quwanzhi_com/Server/runtime/log/sync_wechat_data.log 2>&1 +# 工作台群发消息 +0 2 * * * cd /www/wwwroot/mckb_quwanzhi_com/Server && php think workbench:groupPush >> /www/wwwroot/mckb_quwanzhi_com/Server/runtime/log/workbench_groupPush.log 2>&1 + # 工作台流量分发 0 9 * * * cd /www/wwwroot/mckb_quwanzhi_com/Server && php think workbench:trafficDistribute >> /www/wwwroot/mckb_quwanzhi_com/Server/runtime/log/traffic_distribute.log 2>&1 From f78cac645cc0c188efbbf249971e79c5f07a8210 Mon Sep 17 00:00:00 2001 From: wong <106998207@qq.com> Date: Thu, 7 Aug 2025 09:22:39 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E7=BE=A4=E6=8E=A8=E9=80=81=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../command/WorkbenchGroupPushCommand.php | 76 +++++++++++++++++++ Server/crontab_tasks.md | 2 +- 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 Server/application/command/WorkbenchGroupPushCommand.php diff --git a/Server/application/command/WorkbenchGroupPushCommand.php b/Server/application/command/WorkbenchGroupPushCommand.php new file mode 100644 index 00000000..4fa7dfd7 --- /dev/null +++ b/Server/application/command/WorkbenchGroupPushCommand.php @@ -0,0 +1,76 @@ +setName('workbench:groupPush') + ->setDescription('工作台群组同步任务队列') + ->addOption('jobId', null, Option::VALUE_OPTIONAL, '任务ID,用于区分不同实例', date('YmdHis') . rand(1000, 9999)); + } + + protected function execute(Input $input, Output $output) + { + $output->writeln('开始处理工作台群组同步任务...'); + + try { + // 获取任务ID + $jobId = $input->getOption('jobId'); + + $output->writeln('任务ID: ' . $jobId); + + // 检查队列是否已经在运行 + $queueLockKey = "queue_lock:{$this->queueName}"; + Cache::rm($queueLockKey); + if (Cache::get($queueLockKey)) { + $output->writeln("队列 {$this->queueName} 已经在运行中,跳过执行"); + Log::warning("队列 {$this->queueName} 已经在运行中,跳过执行"); + return false; + } + + // 设置队列运行锁,有效期1小时 + Cache::set($queueLockKey, $jobId, 3600); + $output->writeln("已设置队列运行锁,键名:{$queueLockKey},值:{$jobId},有效期:1小时"); + + // 将任务添加到队列 + $this->addToQueue($jobId, $queueLockKey); + + $output->writeln('工作台群组同步任务已添加到队列'); + } catch (\Exception $e) { + Log::error('工作台群组同步任务添加失败:' . $e->getMessage()); + $output->writeln('工作台群组同步任务添加失败:' . $e->getMessage()); + return false; + } + + return true; + } + + /** + * 添加任务到队列 + * @param string $jobId 任务ID + * @param string $queueLockKey 队列锁键名 + */ + public function addToQueue($jobId = '', $queueLockKey = '') + { + $data = [ + 'jobId' => $jobId, + 'queueLockKey' => $queueLockKey + ]; + + // 添加到队列,设置任务名为 workbench_groupPush + Queue::push(WorkbenchGroupPushJob::class, $data, $this->queueName); + } +} \ No newline at end of file diff --git a/Server/crontab_tasks.md b/Server/crontab_tasks.md index 1442f0c6..f6852e58 100644 --- a/Server/crontab_tasks.md +++ b/Server/crontab_tasks.md @@ -58,7 +58,7 @@ 0 9 * * * cd /www/wwwroot/mckb_quwanzhi_com/Server && php think sync:wechatData >> /www/wwwroot/mckb_quwanzhi_com/Server/runtime/log/sync_wechat_data.log 2>&1 # 工作台群发消息 -0 2 * * * cd /www/wwwroot/mckb_quwanzhi_com/Server && php think workbench:groupPush >> /www/wwwroot/mckb_quwanzhi_com/Server/runtime/log/workbench_groupPush.log 2>&1 +*/2 * * * * cd /www/wwwroot/mckb_quwanzhi_com/Server && php think workbench:groupPush >> /www/wwwroot/mckb_quwanzhi_com/Server/runtime/log/workbench_groupPush.log 2>&1 # 工作台流量分发 0 9 * * * cd /www/wwwroot/mckb_quwanzhi_com/Server && php think workbench:trafficDistribute >> /www/wwwroot/mckb_quwanzhi_com/Server/runtime/log/traffic_distribute.log 2>&1