From f577dbdd704201bdc9c0619d979c666f86b60a14 Mon Sep 17 00:00:00 2001 From: wong <106998207@qq.com> Date: Tue, 16 Sep 2025 09:57:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=90=8C=E6=AD=A5=E6=8F=90?= =?UTF-8?q?=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/controller/WebSocketController.php | 15 ++-- .../api/controller/WechatController.php | 39 ++++++++++ Server/application/chukebao/config/route.php | 14 +++- .../controller/CustomerServiceController.php | 41 +++++++++++ .../chukebao/controller/LoginController.php | 41 ++++++----- .../chukebao/controller/MessageController.php | 72 +++++++++++++++++++ .../controller/WechatChatroomController.php | 16 ++++- .../controller/WechatFriendController.php | 17 ++++- .../controller/PasswordLoginController.php | 6 +- .../job/WorkbenchGroupCreateJob.php | 6 +- Server/crontab_tasks.md | 5 ++ 11 files changed, 236 insertions(+), 36 deletions(-) create mode 100644 Server/application/chukebao/controller/CustomerServiceController.php create mode 100644 Server/application/chukebao/controller/MessageController.php diff --git a/Server/application/api/controller/WebSocketController.php b/Server/application/api/controller/WebSocketController.php index e0662a79..63a93ca1 100644 --- a/Server/application/api/controller/WebSocketController.php +++ b/Server/application/api/controller/WebSocketController.php @@ -33,7 +33,11 @@ class WebSocketController extends BaseController */ public function __construct($userData = []) { - parent::__construct(); + //parent::__construct(); + if (empty($userData)){ + return; + } + $this->initConnection($userData); } @@ -51,7 +55,6 @@ class WebSocketController extends BaseController // 检查缓存中是否存在有效的token $cacheKey = 'websocket_token_' . $userData['userName']; $cachedToken = Cache::get($cacheKey); - if ($cachedToken) { $this->authorized = $cachedToken; $this->accountId = $userData['accountId']; @@ -61,9 +64,11 @@ class WebSocketController extends BaseController 'username' => $userData['userName'], 'password' => $userData['password'] ]; - // 调用登录接口获取token $headerData = ['client:kefu-client']; + + $headerData[] = 'verifysessionid:3f21df29-6d8a-4980-ae8a-bf15ef17955f'; + $headerData[] = 'verifycode:0k3g'; $header = setHeader($headerData, '', 'plain'); $result = requestCurl('https://kf.quwanzhi.com:9991/token', $params, 'POST', $header); $result_array = handleApiResponse($result); @@ -877,8 +882,10 @@ class WebSocketController extends BaseController "seq" => time(), "wechatAccountId" => $data['wechatAccountId'], "chatroomName" => $data['chatroomName'], - "wechatFriendIds" => $data['wechatFriendIds'] +// "wechatFriendIds" => $data['wechatFriendIds'] + "wechatFriendIds" => [17453051,17453058] ]; + // 记录请求日志 Log::info('创建群聊请求:' . json_encode($params, 256)); diff --git a/Server/application/api/controller/WechatController.php b/Server/application/api/controller/WechatController.php index 0643d6d1..947b0e1d 100644 --- a/Server/application/api/controller/WechatController.php +++ b/Server/application/api/controller/WechatController.php @@ -249,4 +249,43 @@ class WechatController extends BaseController WechatAccountModel::create($data); } } + + + public function chatroomCreate($data = []) + { + + $authorization = $this->authorization; + + if (empty($authorization)) { + return json_encode(['code' => 500, 'msg' => '缺少授权信息']); + } + + try { + // 设置请求头 + $headerData = ['Client:system']; + $header = setHeader($headerData, $authorization,'json'); + $params = [ + "chatroomOperateType" => 7, + "extra" => "{chatroomName:{$data['chatroomName']}}", + "wechatAccountId" => $data['wechatAccountId'], + "wechatChatroomId" => 0, + "wechatFriendIds" => $data['wechatFriendIds'] + ]; + + // 发送请求获取状态信息 + $result = requestCurl($this->baseUrl . 'api/WechatChatroom/chatroomOperate', $params, 'POST', $header,'json'); + $response = handleApiResponse($result); + if (!empty($response)) { + return json_encode(['code' => 500, 'msg' =>$response]); + }else{ + return json_encode(['code' => 200, 'msg' =>'成功']); + } + } catch (\Exception $e) { + if (empty($authorization)) { // 只有作为独立API调用时才返回 + return json_encode(['code' => 500, 'msg' => '获取失败:' . $e->getMessage()]); + } + } + } + + } \ No newline at end of file diff --git a/Server/application/chukebao/config/route.php b/Server/application/chukebao/config/route.php index 8e57834c..1384ac63 100644 --- a/Server/application/chukebao/config/route.php +++ b/Server/application/chukebao/config/route.php @@ -13,10 +13,22 @@ Route::group('v1/', function () { Route::group('wechatFriend/', function () { Route::get('list', 'app\chukebao\controller\WechatFriendController@getList'); // 获取好友列表 }); - //好友相关 + //群相关 Route::group('wechatChatroom/', function () { Route::get('list', 'app\chukebao\controller\WechatChatroomController@getList'); // 获取好友列表 }); + + //客服相关 + Route::group('customerService/', function () { + Route::get('list', 'app\chukebao\controller\CustomerServiceController@getList'); // 获取好友列表 + }); + + //客服相关 + Route::group('message/', function () { + Route::get('list', 'app\chukebao\controller\MessageController@getList'); // 获取好友列表 + }); + + }); diff --git a/Server/application/chukebao/controller/CustomerServiceController.php b/Server/application/chukebao/controller/CustomerServiceController.php new file mode 100644 index 00000000..93892fe5 --- /dev/null +++ b/Server/application/chukebao/controller/CustomerServiceController.php @@ -0,0 +1,41 @@ +getUserInfo('s2_accountId'); + if (empty($accountId)){ + return ResponseHelper::error('请先登录'); + } + + $accountIds1= Db::table('s2_wechat_friend')->where(['accountId' => $accountId,'isDeleted' => 0])->group('wechatAccountId')->column('wechatAccountId'); + $accountIds2 = Db::table('s2_wechat_chatroom')->where(['accountId' => $accountId,'isDeleted' => 0])->group('wechatAccountId')->column('wechatAccountId'); + // 确保即使有空数组也不会报错,并且去除重复值 + $accountIds = array_unique(array_merge($accountIds1 ?: [], $accountIds2 ?: [])); + + $list = Db::table('s2_wechat_account') + ->whereIn('id',$accountIds) + ->order('id desc') + ->group('id') + ->select(); + foreach ($list as $k=>&$v){ + $v['createTime'] = !empty($v['createTime']) ? date('Y-m-d H:i:s',$v['createTime']) : ''; + $v['updateTime'] = !empty($v['updateTime']) ? date('Y-m-d H:i:s',$v['updateTime']) : ''; + $v['labels'] = json_decode($v['labels'],true); + unset( + $v['accountUserName'], + $v['accountRealName'], + $v['accountNickname'], + ); + } + unset($v); + + return ResponseHelper::success($list); + } +} \ No newline at end of file diff --git a/Server/application/chukebao/controller/LoginController.php b/Server/application/chukebao/controller/LoginController.php index 519b744a..44a03338 100644 --- a/Server/application/chukebao/controller/LoginController.php +++ b/Server/application/chukebao/controller/LoginController.php @@ -7,6 +7,8 @@ use Exception; use library\ResponseHelper; use think\Db; use think\Controller; +use think\facade\Cache; + /** * 认证控制器 * 处理用户登录和身份验证 @@ -27,18 +29,27 @@ class LoginController extends Controller $password = !empty($password) ? $password : $this->request->param('password', ''); $verifySessionId =!empty($verifySessionId) ? $verifySessionId : $this->request->param('verifySessionId', ''); $verifyCode = !empty($verifyCode) ? $verifyCode : $this->request->param('verifyCode', ''); + $token = JwtUtil::getRequestToken(); + $payload = ''; + if (!empty($token)){ + $payload = JwtUtil::verifyToken($token); + } - if (empty($username) || empty($password)) { + if ((empty($username) || empty($password)) && empty($payload)){ return ResponseHelper::error('请输入账号密码'); } // 验证账号是否存在(支持账号或手机号登录) - $user = Db::name('users') - ->where(function ($query) use ($username) { - $query->where('account', $username)->whereOr('phone', $username); - }) - ->where('passwordMd5', md5($password)) - ->find(); + if (empty($payload11)){ + $user = Db::name('users') + ->where(function ($query) use ($username) { + $query->where('account', $username)->whereOr('phone', $username); + }) + ->where('passwordMd5', md5($password)) + ->find(); + }else{ + $user = $payload; + } if (empty($user)) { return ResponseHelper::error('账号不存在或密码错误'); @@ -48,15 +59,12 @@ class LoginController extends Controller return ResponseHelper::error('账号已禁用'); } - //登录参数 $params = [ 'grant_type' => 'password', 'username' => $user['account'], 'password' => !empty($user['passwordLocal']) ? localDecrypt($user['passwordLocal']) : $password ]; - - try { // 调用登录接口获取token $headerData = ['client:kefu-client']; @@ -68,20 +76,18 @@ class LoginController extends Controller $result = requestCurl('https://s2.siyuguanli.com:9991/token', $params, 'POST', $header); $result = handleApiResponse($result); if (isset($result['access_token']) && !empty($result['access_token'])) { - $userData['kefuData']['token'] = $result; + $kefuData['token'] = $result; $headerData = ['client:kefu-client']; $header = setHeader($headerData, $result['access_token']); $result2 = requestCurl('https://s2.siyuguanli.com:9991/api/account/self', [], 'GET', $header, 'json'); $self = handleApiResponse($result2); - $userData['kefuData']['self'] = $self; - - Db::name('users')->where('id', $user['id'])->update(['passwordLocal' => localEncrypt($password),'updateTime' => time()]); - + $kefuData['self'] = $self; + Db::name('users')->where('id', $user['id'])->update(['passwordLocal' => localEncrypt($params['password']),'updateTime' => time()]); }else{ return ResponseHelper::error($result['error_description']); } - unset($user['passwordMd5'],$user['deleteTime'],$user['passwordLocal']); + unset($user['passwordMd5'],$user['deleteTime']); $userData['member'] = $user; // 生成JWT令牌 @@ -89,10 +95,9 @@ class LoginController extends Controller $token = JwtUtil::createToken($user, $expired); $token_expired = time() + $expired; - $userData['token'] = $token; $userData['token_expired'] = $token_expired; - + $userData['kefuData'] = $kefuData; return ResponseHelper::success($userData, '登录成功'); } catch (Exception $e) { diff --git a/Server/application/chukebao/controller/MessageController.php b/Server/application/chukebao/controller/MessageController.php new file mode 100644 index 00000000..c213b7e8 --- /dev/null +++ b/Server/application/chukebao/controller/MessageController.php @@ -0,0 +1,72 @@ +request->param('page', 1); + $limit = $this->request->param('limit', 10); + $accountId = $this->getUserInfo('s2_accountId'); + if (empty($accountId)){ + return ResponseHelper::error('请先登录'); + } + + $chatroomList = Db::table('s2_wechat_chatroom')->alias('wc') + ->join(['s2_wechat_message' => 'm'], 'wc.id = m.wechatChatroomId', 'LEFT') + ->where(['wc.accountId' => $accountId,'m.type' => 2,' wc.isDeleted' => 0]) + ->order('m.id desc') + ->group('m.wechatChatroomId') + ->page($page, $limit) + ->select(); + $friendIds = Db::table('s2_wechat_friend') + ->where(['accountId' => $accountId,' isDeleted' => 0]) + ->group('id') + ->column('id'); + $friendList = Db::table('s2_wechat_message') + ->where(['type' => 1]) + ->whereIn('wechatFriendId',$friendIds) + ->order('id desc') + ->group('wechatFriendId') + ->page($page, $limit) + ->select(); + + $list = array_merge($chatroomList,$friendList); + + // 按createTime字段从大到小排序 + usort($list, function($a, $b) { + return $b['createTime'] <=> $a['createTime']; + }); + + + foreach ($list as $k=>&$v){ + $v['createTime'] = !empty($v['createTime']) ? date('Y-m-d H:i:s',$v['createTime']) : ''; + $v['wechatTime'] = !empty($v['wechatTime']) ? date('Y-m-d H:i:s',$v['wechatTime']) : ''; + + if (!empty($v['wechatFriendId'])){ + $friend = Db::table('s2_wechat_friend') + ->where(['id'=>$v['wechatFriendId']]) + ->field('id,nickname,avatar') + ->find(); + $v['msgInfo'] = $friend; + } + + if (!empty($v['wechatChatroomId'])){ + $chatroom = Db::table('s2_wechat_chatroom') + ->where(['id'=>$v['wechatChatroomId']]) + ->field('id,nickname,chatroomAvatar as avatar') + ->find(); + $v['msgInfo'] = $chatroom; + } + + + } + unset($v); + + return ResponseHelper::success($list); + } +} \ No newline at end of file diff --git a/Server/application/chukebao/controller/WechatChatroomController.php b/Server/application/chukebao/controller/WechatChatroomController.php index cf03fd41..e6c64f5c 100644 --- a/Server/application/chukebao/controller/WechatChatroomController.php +++ b/Server/application/chukebao/controller/WechatChatroomController.php @@ -15,7 +15,19 @@ class WechatChatroomController extends BaseController if (empty($accountId)){ return ResponseHelper::error('请先登录'); } - $list = Db::table('s2_wechat_chatroom')->where('accountId',$accountId)->order('id desc')->page($page, $limit)->select(); - return ResponseHelper::success($list); + $query = Db::table('s2_wechat_chatroom') + ->where(['accountId' => $accountId,'isDeleted' => 0]) + ->order('id desc'); + $list = $query->page($page, $limit)->select(); + $total = $query->count(); + + + foreach ($list as $k=>&$v){ + $v['createTime'] = !empty($v['createTime']) ? date('Y-m-d H:i:s',$v['createTime']) : ''; + $v['updateTime'] = !empty($v['updateTime']) ? date('Y-m-d H:i:s',$v['updateTime']) : ''; + } + unset($v); + + return ResponseHelper::success(['list'=>$list,'total'=>$total]); } } \ No newline at end of file diff --git a/Server/application/chukebao/controller/WechatFriendController.php b/Server/application/chukebao/controller/WechatFriendController.php index aa9f0248..bd8f0095 100644 --- a/Server/application/chukebao/controller/WechatFriendController.php +++ b/Server/application/chukebao/controller/WechatFriendController.php @@ -15,7 +15,20 @@ class WechatFriendController extends BaseController if (empty($accountId)){ return ResponseHelper::error('请先登录'); } - $list = Db::table('s2_wechat_friend')->where('accountId',$accountId)->order('id desc')->page($page, $limit)->select(); - return ResponseHelper::success($list); + $query = Db::table('s2_wechat_friend') + ->where(['accountId' => $accountId,'isDeleted' => 0]) + ->order('id desc'); + $list = $query->page($page, $limit)->select(); + $total = $query->count(); + + + foreach ($list as $k=>&$v){ + $v['createTime'] = !empty($v['createTime']) ? date('Y-m-d H:i:s',$v['createTime']) : ''; + $v['updateTime'] = !empty($v['updateTime']) ? date('Y-m-d H:i:s',$v['updateTime']) : ''; + $v['passTime'] = !empty($v['passTime']) ? date('Y-m-d H:i:s',$v['passTime']) : ''; + } + unset($v); + + return ResponseHelper::success(['list'=>$list,'total'=>$total]); } } \ No newline at end of file diff --git a/Server/application/common/controller/PasswordLoginController.php b/Server/application/common/controller/PasswordLoginController.php index 30eba2ae..a3748a06 100644 --- a/Server/application/common/controller/PasswordLoginController.php +++ b/Server/application/common/controller/PasswordLoginController.php @@ -116,11 +116,7 @@ class PasswordLoginController extends BaseController // 生成JWT令牌 $token = JwtUtil::createToken($member, 86400 * 30); $token_expired = time() + 86400 * 30; - $kefuData = [ - 'token' => [], - 'self' => [], - ]; - return compact('member', 'token', 'token_expired','deviceTotal','kefuData'); + return compact('member', 'token', 'token_expired','deviceTotal'); } /** diff --git a/Server/application/job/WorkbenchGroupCreateJob.php b/Server/application/job/WorkbenchGroupCreateJob.php index b5e3af0f..197134c2 100644 --- a/Server/application/job/WorkbenchGroupCreateJob.php +++ b/Server/application/job/WorkbenchGroupCreateJob.php @@ -15,6 +15,7 @@ use think\facade\Cache; use think\facade\Config; use app\api\controller\MomentsController as Moments; use Workerman\Lib\Timer; +use app\api\controller\WechatController; /** * 工作台群创建任务 @@ -127,8 +128,6 @@ class WorkbenchGroupCreateJob $addGroupUser[] = $user; } } - - foreach ($addGroupUser as $key => $val) { //判断第一组用户是否满足创建群的条件 $friendIds = Db::name('wechat_friendship')->alias('f') @@ -162,12 +161,11 @@ class WorkbenchGroupCreateJob $toAccountId = Db::name('users')->where('account', $username)->value('s2_accountId'); } $webSocket = new WebSocketController(['userName' => $username, 'password' => $password, 'accountId' => $toAccountId]); + //$webSocket = new WebSocketController(['userName' => 'wz_03', 'password' => 'key123456', 'accountId' => 5015]); //拉人进群 $webSocket->CmdChatroomInvite(['wechatChatroomId' => 830794, 'wechatFriendIds' => [21168549]]); //修改群名称 $webSocket->CmdChatroomModifyInfo(['wechatChatroomId' => 830794, 'wechatAccountId' => 300745,'chatroomName' => 'test111']); //修改群公告 $webSocket->CmdChatroomModifyInfo(['wechatChatroomId' => 830794, 'wechatAccountId' => 300745,'announce' => 'test111']); //建群 $webSocket->CmdChatroomCreate(['chatroomName' => '聊天测试群', 'wechatFriendIds' => [17453051,17453058],'wechatAccountId' => 300745]); - - foreach ($groupedFriends as $wechatAccountId => $friendId) { //列出所有群 $group = ''; diff --git a/Server/crontab_tasks.md b/Server/crontab_tasks.md index 8aab7567..8dff05ad 100644 --- a/Server/crontab_tasks.md +++ b/Server/crontab_tasks.md @@ -64,9 +64,14 @@ # 工作台群发消息 */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 +# 工作台建群 +*/5 * * * * cd /www/wwwroot/mckb_quwanzhi_com/Server && php think workbench:groupCreate >> /www/wwwroot/mckb_quwanzhi_com/Server/runtime/log/workbench_groupCreate.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 + + # 预防性切换好友 */2 * * * * cd /www/wwwroot/mckb_quwanzhi_com/Server && php think switch:friends >> /www/wwwroot/mckb_quwanzhi_com/Server/runtime/log/switch_friends.log 2>&1