代码优化

This commit is contained in:
wong
2025-04-30 11:15:19 +08:00
parent 43a27ef35f
commit 209cc96dce
8 changed files with 281 additions and 154 deletions

View File

@@ -20,16 +20,16 @@ class AccountController extends BaseController
* 获取公司账号列表
* @param string $pageIndex 页码
* @param string $pageSize 每页数量
* @param bool $isJob 是否为定时任务调用
* @param bool $isInner 是否为定时任务调用
* @return \think\response\Json
*/
public function getlist($pageIndex = '', $pageSize = '', $isJob = false)
public function getlist($pageIndex = '', $pageSize = '', $isInner = false)
{
// 获取授权token
$authorization = trim($this->request->header('authorization', $this->authorization));
if (empty($authorization)) {
if ($isJob) {
if ($isInner) {
return json_encode(['code' => 500, 'msg' => '缺少授权信息']);
} else {
return errorJson('缺少授权信息');
@@ -61,14 +61,14 @@ class AccountController extends BaseController
}
}
if ($isJob) {
if ($isInner) {
return json_encode(['code' => 200, 'msg' => '获取公司账号列表成功', 'data' => $response]);
} else {
return successJson($response);
}
} catch (\Exception $e) {
if ($isJob) {
if ($isInner) {
return json_encode(['code' => 500, 'msg' => '获取公司账号列表失败:' . $e->getMessage()]);
} else {
return errorJson('获取公司账号列表失败:' . $e->getMessage());
@@ -296,12 +296,12 @@ class AccountController extends BaseController
* 获取部门列表
* @return \think\response\Json
*/
public function getDepartmentList($isJob = false)
public function getDepartmentList($isInner = false)
{
// 获取授权token
$authorization = trim($this->request->header('authorization', $this->authorization));
if (empty($authorization)) {
if ($isJob) {
if ($isInner) {
return json_encode(['code' => 500, 'msg' => '缺少授权信息']);
} else {
return errorJson('缺少授权信息');
@@ -326,13 +326,13 @@ class AccountController extends BaseController
$this->processDepartments($response);
}
if ($isJob) {
if ($isInner) {
return json_encode(['code' => 200, 'msg' => '获取部门列表成功', 'data' => $response]);
} else {
return successJson($response, '获取部门列表成功');
}
} catch (\Exception $e) {
if ($isJob) {
if ($isInner) {
return json_encode(['code' => 500, 'msg' => '获取部门列表失败:' . $e->getMessage()]);
} else {
return errorJson('获取部门列表失败:' . $e->getMessage());

View File

@@ -13,15 +13,15 @@ class FriendTaskController extends BaseController
* 获取添加好友记录列表
* @param int $pageIndex 页码
* @param int $pageSize 每页数量
* @param bool $isJob 是否为定时任务调用
* @param bool $isInner 是否为定时任务调用
* @return \think\response\Json
*/
public function getlist($pageIndex, $pageSize, $isJob = false)
public function getlist($pageIndex, $pageSize, $isInner = false)
{
// 获取授权token
$authorization = trim($this->request->header('authorization', $this->authorization));
if (empty($authorization)) {
if($isJob){
if($isInner){
return json_encode(['code'=>500,'msg'=>'缺少授权信息']);
}else{
return errorJson('缺少授权信息');
@@ -52,13 +52,13 @@ class FriendTaskController extends BaseController
$this->saveFriendTask($item);
}
}
if($isJob){
if($isInner){
return json_encode(['code'=>200,'msg'=>'获取添加好友记录列表成功','data'=>$response]);
}else{
return successJson($response);
}
} catch (\Exception $e) {
if($isJob){
if($isInner){
return json_encode(['code'=>500,'msg'=>'获取添加好友记录列表失败:' . $e->getMessage()]);
}else{
return errorJson('获取添加好友记录列表失败:' . $e->getMessage());

View File

@@ -13,12 +13,12 @@ class MessageController extends BaseController
* 获取微信好友列表
* @return \think\response\Json
*/
public function getFriendsList($pageIndex = '',$pageSize = '',$isJob = false)
public function getFriendsList($pageIndex = '',$pageSize = '',$isInner = false)
{
// 获取授权token
$authorization = trim($this->request->header('authorization', $this->authorization));
if (empty($authorization)) {
if($isJob){
if($isInner){
return json_encode(['code'=>500,'msg'=>'缺少授权信息']);
}else{
return errorJson('缺少授权信息');
@@ -102,13 +102,13 @@ class MessageController extends BaseController
}
unset($friend);
}
if($isJob){
if($isInner){
return json_encode(['code'=>200,'msg'=>'获取好友列表成功','data'=>$response]);
}else{
return successJson($response);
}
} catch (\Exception $e) {
if($isJob){
if($isInner){
return json_encode(['code'=>500,'msg'=>'获取好友列表失败:' . $e->getMessage()]);
}else{
return errorJson('获取好友列表失败:' . $e->getMessage());
@@ -179,12 +179,12 @@ class MessageController extends BaseController
* 获取微信群聊列表
* @return \think\response\Json
*/
public function getChatroomList($pageIndex = '',$pageSize = '',$isJob = false)
public function getChatroomList($pageIndex = '',$pageSize = '',$isInner = false)
{
// 获取授权token
$authorization = trim($this->request->header('authorization', $this->authorization));
if (empty($authorization)) {
if($isJob){
if($isInner){
return json_encode(['code'=>500,'msg'=>'缺少授权信息']);
}else{
return errorJson('缺少授权信息');
@@ -267,13 +267,13 @@ class MessageController extends BaseController
}
unset($chatroom);
}
if($isJob){
if($isInner){
return json_encode(['code'=>200,'msg'=>'获取群聊列表成功','data'=>$response]);
}else{
return successJson($response);
}
} catch (\Exception $e) {
if($isJob){
if($isInner){
return json_encode(['code'=>500,'msg'=>'获取群聊列表失败:' . $e->getMessage()]);
}else{
return errorJson('获取群聊列表失败:' . $e->getMessage());

View File

@@ -15,6 +15,9 @@ class WebSocketController extends BaseController
protected $authorized;
protected $accountId;
protected $client;
protected $isConnected = false;
protected $lastHeartbeatTime = 0;
protected $heartbeatInterval = 30; // 心跳间隔,单位秒
/************************************
* 初始化相关功能
@@ -27,9 +30,16 @@ class WebSocketController extends BaseController
public function __construct($userData = [])
{
parent::__construct();
$this->initConnection($userData);
}
/**
* 初始化WebSocket连接
* @param array $userData 用户数据
*/
protected function initConnection($userData = [])
{
if(!empty($userData) && count($userData)){
if (empty($userData['userName']) || empty($userData['password'])) {
return json_encode(['code'=>400,'msg'=>'参数缺失']);
}
@@ -40,59 +50,152 @@ class WebSocketController extends BaseController
];
// 调用登录接口获取token
// 设置请求头
$headerData = ['client:kefu-client'];
$header = setHeader($headerData, '', 'plain');
$result = requestCurl('https://kf.quwanzhi.com:9991/token', $params, 'POST',$header);
$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'])) {
$authorization = $result_array['access_token'];
$this->authorized = $authorization;
$this->authorized = $result_array['access_token'];
$this->accountId = $userData['accountId'];
} else {
return json_encode(['code'=>400,'msg'=>'获取系统授权信息失败']);
}
}else{
} else {
$this->authorized = $this->request->header('authorization', '');
$this->accountId = $this->request->param('accountId', '');
}
if (empty($this->authorized) || empty($this->accountId)) {
$data['authorized'] = $this->authorized;
$data['accountId'] = $this->accountId;
return json_encode(['code'=>400,'msg'=>'缺失关键参数']);
}
//证书
$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,
];
$this->connect();
}
/**
* 建立WebSocket连接
*/
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,
];
$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);
$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;
}
}
/**
* 启动心跳检测
*/
protected function startHeartbeat()
{
// 使用定时器发送心跳
\Swoole\Timer::tick($this->heartbeatInterval * 1000, function() {
if ($this->isConnected) {
$this->sendHeartbeat();
}
});
}
/**
* 发送心跳包
*/
protected function sendHeartbeat()
{
try {
$heartbeat = [
"cmdType" => "CmdHeartbeat",
"seq" => time()
];
$this->client->send(json_encode($heartbeat));
$this->lastHeartbeatTime = time();
} catch (\Exception $e) {
Log::error("发送心跳包失败:" . $e->getMessage());
$this->reconnect();
}
}
/**
* 重连机制
*/
protected function reconnect()
{
try {
if ($this->client) {
$this->client->close();
}
$this->isConnected = false;
$this->connect();
} catch (\Exception $e) {
Log::error("WebSocket重连失败" . $e->getMessage());
}
}
/**
* 检查连接状态
*/
protected function checkConnection()
{
if (!$this->isConnected || (time() - $this->lastHeartbeatTime) > $this->heartbeatInterval * 2) {
$this->reconnect();
}
}
/**
* 发送消息
* @param array $data 消息数据
* @return array
*/
protected function sendMessage($data)
{
$this->checkConnection();
try {
$this->client->send(json_encode($data));
$response = $this->client->receive();
return json_decode($response, true);
} catch (\Exception $e) {
Log::error("发送消息失败:" . $e->getMessage());
$this->reconnect();
return ['code' => 500, 'msg' => '发送消息失败'];
}
}
/************************************
@@ -106,10 +209,13 @@ class WebSocketController extends BaseController
*/
public function getMoments($data = [])
{
$count = !empty($data['count']) ? $data['count'] : 10;
$wechatAccountId = !empty($data['wechatAccountId']) ? $data['wechatAccountId'] : '';
$wechatFriendId = !empty($data['id']) ? $data['id'] : '';
$wechatFriendId = !empty($data['wechatFriendId']) ? $data['wechatFriendId'] : '';
$prevSnsId = !empty($data['prevSnsId']) ? $data['prevSnsId'] : 0;
$maxPages = 10; // 最大页数
$currentPage = 1; // 当前页码
$allMoments = []; // 存储所有朋友圈数据
//过滤消息
if (empty($wechatAccountId)) {
@@ -118,38 +224,71 @@ class WebSocketController extends BaseController
if (empty($wechatFriendId)) {
return json_encode(['code'=>400,'msg'=>'指定好友不能为空']);
}
$msg = '获取朋友圈信息成功';
$message = [];
try {
$params = [
"cmdType" => "CmdFetchMoment",
"count" => $count,
"createTimeSec" => time(),
"isTimeline" => false,
"prevSnsId" => 0,
"wechatAccountId" => $wechatAccountId,
"wechatFriendId" => $wechatFriendId,
"seq" => time(),
];
$params = json_encode($params);
//Log::write('WS获取朋友圈信息参数' . json_encode($params, 256));
$this->client->send($params);
$message = $this->client->receive();
//Log::write('WS获取朋友圈信息成功结果' . $message);
$message = json_decode($message, 1);
do {
$params = [
"cmdType" => "CmdFetchMoment",
"count" => $count,
"createTimeSec" => time(),
"isTimeline" => false,
"prevSnsId" => $prevSnsId,
"wechatAccountId" => $wechatAccountId,
"wechatFriendId" => $wechatFriendId,
"seq" => time(),
];
// 存储朋友圈数据到数据库
if (isset($message['result']) && !empty($message['result'])) {
$message = $this->sendMessage($params);
// 检查返回结果
if (!isset($message['result']) || empty($message['result'])) {
break;
}
// 合并朋友圈数据
$allMoments = array_merge($allMoments, $message['result']);
// 存储当前页的朋友圈数据到数据库
$this->saveMomentsToDatabase($message['result'], $wechatAccountId, $wechatFriendId);
}
//关闭WS链接
$this->client->close();
} catch (\Exception $e) {
$msg = $e->getMessage();
}
return json_encode(['code'=>200,'msg'=>$msg,'data'=>$message]);
// 获取最后一条数据的snsId用于下次查询
$lastMoment = end($message['result']);
if (!$lastMoment || !isset($lastMoment['snsId'])) {
break;
}
$prevSnsId = $lastMoment['snsId'];
$currentPage++;
// 如果已经达到最大页数,退出循环
if ($currentPage > $maxPages) {
break;
}
// 如果返回的数据少于请求的数量,说明没有更多数据了
if (count($message['result']) < $count) {
break;
}
} while (true);
// 构建返回数据
$result = [
'code' => 200,
'msg' => '获取朋友圈信息成功',
'data' => [
'list' => $allMoments,
'total' => count($allMoments),
'currentPage' => $currentPage - 1,
'hasMore' => $currentPage > $maxPages || count($message['result']) >= $count,
'nextPrevSnsId' => $prevSnsId
]
];
return json_encode($result);
} catch (\Exception $e) {
return json_encode(['code'=>500,'msg'=>$e->getMessage()]);
}
}
/**
@@ -164,37 +303,30 @@ class WebSocketController extends BaseController
if (empty($data)) {
return json_encode(['code'=>400,'msg'=>'参数缺失']);
}
$dataArray = $data;
if (!is_array($dataArray)) {
return json_encode(['code'=>400,'msg'=>'数据格式错误']);
}
//过滤消息
if (empty($dataArray['snsId'])) {
if (empty($data['snsId'])) {
return json_encode(['code'=>400,'msg'=>'snsId不能为空']);
}
if (empty($dataArray['wechatAccountId'])) {
if (empty($data['wechatAccountId'])) {
return json_encode(['code'=>400,'msg'=>'微信id不能为空']);
}
$result = [
"cmdType" => "CmdMomentInteract",
"momentInteractType" => 1,
"seq" => time(),
"snsId" => $dataArray['snsId'],
"wechatAccountId" => $dataArray['wechatAccountId'],
"wechatFriendId" => 0,
];
try {
$result = [
"cmdType" => "CmdMomentInteract",
"momentInteractType" => 1,
"seq" => time(),
"snsId" => $data['snsId'],
"wechatAccountId" => $data['wechatAccountId'],
"wechatFriendId" => 0,
];
$result = json_encode($result);
$this->client->send($result);
$message = $this->client->receive();
$message = json_decode($message, 1);
//关闭WS链接
$this->client->close();
//Log::write('WS个人消息发送');
return json_encode(['code'=>200,'msg'=>'点赞成功','data'=>$message]);
$message = $this->sendMessage($result);
return json_encode(['code'=>200,'msg'=>'点赞成功','data'=>$message]);
} catch (\Exception $e) {
return json_encode(['code'=>500,'msg'=>$e->getMessage()]);
}
} else {
return json_encode(['code'=>400,'msg'=>'非法请求']);
}
@@ -212,39 +344,32 @@ class WebSocketController extends BaseController
if (empty($data)) {
return json_encode(['code'=>400,'msg'=>'参数缺失']);
}
$dataArray = $data;
if (!is_array($dataArray)) {
return json_encode(['code'=>400,'msg'=>'数据格式错误']);
}
//过滤消息
if (empty($dataArray['snsId'])) {
if (empty($data['snsId'])) {
return json_encode(['code'=>400,'msg'=>'snsId不能为空']);
}
if (empty($dataArray['wechatAccountId'])) {
if (empty($data['wechatAccountId'])) {
return json_encode(['code'=>400,'msg'=>'微信id不能为空']);
}
$result = [
"CommentId2" => '',
"CommentTime" => 0,
"cmdType" => "CmdMomentCancelInteract",
"optType" => 1,
"seq" => time(),
"snsId" => $dataArray['snsId'],
"wechatAccountId" => $dataArray['wechatAccountId'],
"wechatFriendId" => 0,
];
try {
$result = [
"CommentId2" => '',
"CommentTime" => 0,
"cmdType" => "CmdMomentCancelInteract",
"optType" => 1,
"seq" => time(),
"snsId" => $data['snsId'],
"wechatAccountId" => $data['wechatAccountId'],
"wechatFriendId" => 0,
];
$result = json_encode($result);
$this->client->send($result);
$message = $this->client->receive();
$message = json_decode($message, 1);
//关闭WS链接
$this->client->close();
//Log::write('WS个人消息发送');
return json_encode(['code'=>200,'msg'=>'取消点赞成功','data'=>$message]);
$message = $this->sendMessage($result);
return json_encode(['code'=>200,'msg'=>'取消点赞成功','data'=>$message]);
} catch (\Exception $e) {
return json_encode(['code'=>500,'msg'=>$e->getMessage()]);
}
} else {
return json_encode(['code'=>400,'msg'=>'非法请求']);
}

View File

@@ -13,12 +13,12 @@ class WechatChatroomController extends BaseController
* 获取微信群聊列表
* @return \think\response\Json
*/
public function getlist($pageIndex = '',$pageSize = '',$isJob = false, $isDel = '')
public function getlist($pageIndex = '',$pageSize = '',$isInner = false, $isDel = '')
{
// 获取授权token
$authorization = trim($this->request->header('authorization', $this->authorization));
if (empty($authorization)) {
if($isJob){
if($isInner){
return json_encode(['code'=>500,'msg'=>'缺少授权信息']);
}else{
return errorJson('缺少授权信息');
@@ -66,7 +66,7 @@ class WechatChatroomController extends BaseController
}
}
if($isJob){
if($isInner){
return json_encode(['code'=>200,'msg'=>'success','data'=>$response,'isUpdate'=>$isUpdate]);
}else{
return successJson($response);
@@ -134,7 +134,7 @@ class WechatChatroomController extends BaseController
* @param string $wechatChatroomId 微信群ID
* @return \think\response\Json
*/
public function listChatroomMember($wechatChatroomId = '',$chatroomId = '',$isJob = false)
public function listChatroomMember($wechatChatroomId = '',$chatroomId = '',$isInner = false)
{
// 获取授权token
$authorization = trim($this->request->header('authorization', $this->authorization));
@@ -143,7 +143,7 @@ class WechatChatroomController extends BaseController
if (empty($authorization)) {
if($isJob){
if($isInner){
return json_encode(['code'=>500,'msg'=>'缺少授权信息']);
}else{
return errorJson('缺少授权信息');
@@ -151,7 +151,7 @@ class WechatChatroomController extends BaseController
}
if (empty($wechatChatroomId)) {
if($isJob){
if($isInner){
return json_encode(['code'=>500,'msg'=>'群ID不能为空']);
}else{
return errorJson('群ID不能为空');
@@ -179,13 +179,13 @@ class WechatChatroomController extends BaseController
}
}
if($isJob){
if($isInner){
return json_encode(['code'=>200,'msg'=>'success','data'=>$response]);
}else{
return successJson($response);
}
} catch (\Exception $e) {
if($isJob){
if($isInner){
return json_encode(['code'=>500,'msg'=>'获取群成员列表失败:' . $e->getMessage()]);
}else{
return errorJson('获取群成员列表失败:' . $e->getMessage());

View File

@@ -15,15 +15,15 @@ class WechatController extends BaseController
*
* @param string $pageIndex 页码
* @param string $pageSize 每页大小
* @param bool $isJob 是否为任务调用
* @param bool $isInner 是否为任务调用
* @return \think\response\Json
*/
public function getList($pageIndex = '', $pageSize = '', $isJob = false)
public function getList($pageIndex = '', $pageSize = '', $isInner = false)
{
// 获取授权token
$authorization = trim($this->request->header('authorization', $this->authorization));
if (empty($authorization)) {
if ($isJob) {
if ($isInner) {
return json_encode(['code' => 500, 'msg' => '缺少授权信息']);
} else {
return errorJson('缺少授权信息');
@@ -62,13 +62,13 @@ class WechatController extends BaseController
// 获取并更新微信账号状态信息
$this->getListTenantWechatPartial($authorization);
if ($isJob) {
if ($isInner) {
return json_encode(['code' => 200, 'msg' => '获取微信账号列表成功', 'data' => $response]);
} else {
return successJson($response);
}
} catch (\Exception $e) {
if ($isJob) {
if ($isInner) {
return json_encode(['code' => 500, 'msg' => '获取微信账号列表失败:' . $e->getMessage()]);
} else {
return errorJson('获取微信账号列表失败:' . $e->getMessage());

View File

@@ -13,15 +13,15 @@ class WechatFriendController extends BaseController
* @param string $pageIndex 页码
* @param string $pageSize 每页大小
* @param string $preFriendId 上一个好友ID
* @param bool $isJob 是否为任务调用
* @param bool $isInner 是否为任务调用
* @return \think\response\Json
*/
public function getlist($pageIndex = '', $pageSize = '', $preFriendId = '', $isJob = false,$isDel = '')
public function getlist($pageIndex = '', $pageSize = '', $preFriendId = '', $isInner = false,$isDel = '')
{
// 获取授权token
$authorization = trim($this->request->header('authorization', $this->authorization));
if (empty($authorization)) {
if ($isJob) {
if ($isInner) {
return json_encode(['code' => 500, 'msg' => '缺少授权信息']);
} else {
return errorJson('缺少授权信息');
@@ -79,14 +79,14 @@ class WechatFriendController extends BaseController
}
}
if ($isJob) {
if ($isInner) {
return json_encode(['code' => 200, 'msg' => 'success', 'data' => $response, 'isUpdate' => $isUpdate]);
} else {
return successJson($response);
}
} catch (\Exception $e) {
if ($isJob) {
if ($isInner) {
return json_encode(['code' => 500, 'msg' => '获取微信好友列表失败:' . $e->getMessage()]);
} else {
return errorJson('获取微信好友列表失败:' . $e->getMessage());

View File

@@ -11,6 +11,7 @@
// 应用公共文件
use app\common\service\AuthService;
use think\facade\Cache;
if (!function_exists('requestCurl')) {
/**
@@ -376,7 +377,8 @@ if (!function_exists('handleApiResponse')) {
// 不是JSON格式直接返回原始数据
if($response == '无效路径或登录状态失效'){
\think\facade\Cache::rm('system_refresh_token');
Cache::rm('system_refresh_token');
Cache::rm('system_authorization_token');
AuthService::getSystemAuthorization();
}