队列优化

This commit is contained in:
Ghost
2025-04-01 09:26:06 +08:00
parent dfe73f9a22
commit 23fcc47d55
10 changed files with 274 additions and 55 deletions

View File

@@ -14,12 +14,16 @@ class DeviceController extends BaseController
* 获取设备列表
* @return \think\response\Json
*/
public function getlist($pageIndex = '',$pageSize = '',$authorization = '')
public function getlist($pageIndex = '',$pageSize = '',$authorization = '',$isJob = false)
{
// 获取授权token
$authorization = !empty($authorization) ? $authorization : trim($this->request->header('authorization', ''));
if (empty($authorization)) {
return errorJson('缺少授权信息');
if($isJob){
return json_encode(['code'=>500,'msg'=>'缺少授权信息']);
}else{
return errorJson('缺少授权信息');
}
}
try {
@@ -61,9 +65,17 @@ class DeviceController extends BaseController
}
}
return successJson($response);
if($isJob){
return json_encode(['code'=>200,'msg'=>'success','data'=>$response]);
}else{
return successJson($response);
}
} catch (\Exception $e) {
return errorJson('获取设备列表失败:' . $e->getMessage());
if($isJob){
return json_encode(['code'=>500,'msg'=>'获取设备列表失败:' . $e->getMessage()]);
}else{
return errorJson('获取设备列表失败:' . $e->getMessage());
}
}
}

View File

@@ -13,13 +13,17 @@ class WechatChatroomController extends BaseController
* 获取微信群聊列表
* @return \think\response\Json
*/
public function getlist()
public function getlist($pageIndex = '',$pageSize = '',$authorization = '',$isJob = false)
{
// 获取授权token
$authorization = trim($this->request->header('authorization', ''));
if (empty($authorization)) {
$authorization = !empty($authorization) ? $authorization : trim($this->request->header('authorization', ''));
if (empty($authorization)) {
if($isJob){
return json_encode(['code'=>500,'msg'=>'缺少授权信息']);
}else{
return errorJson('缺少授权信息');
}
}
try {
// 构建请求参数
@@ -31,16 +35,16 @@ class WechatChatroomController extends BaseController
'groupId' => $this->request->param('groupId', ''),
'wechatChatroomId' => $this->request->param('wechatChatroomId', 0),
'memberKeyword' => $this->request->param('memberKeyword', ''),
'pageIndex' => $this->request->param('pageIndex', 0),
'pageSize' => $this->request->param('pageSize', 20)
'pageIndex' => !empty($pageIndex) ? $pageIndex : input('pageIndex', 0),
'pageSize' => !empty($pageSize) ? $pageSize : input('pageSize', 20)
];
// 设置请求头
$headerData = ['client:system'];
$header = setHeader($headerData, $authorization, 'plain');
$header = setHeader($headerData, $authorization, 'json');
// 发送请求获取群聊列表
$result = requestCurl($this->baseUrl . 'api/WechatChatroom/pagelist', $params, 'GET', $header);
$result = requestCurl($this->baseUrl . 'api/WechatChatroom/pagelist', $params, 'GET', $header,'json');
$response = handleApiResponse($result);
// 保存数据到数据库
@@ -50,7 +54,11 @@ class WechatChatroomController extends BaseController
}
}
return successJson($response);
if($isJob){
return json_encode(['code'=>200,'msg'=>'success','data'=>$response]);
}else{
return successJson($response);
}
} catch (\Exception $e) {
return errorJson('获取微信群聊列表失败:' . $e->getMessage());
}
@@ -63,6 +71,7 @@ class WechatChatroomController extends BaseController
private function saveChatroom($item)
{
$data = [
'id' => $item['id'],
'wechatAccountId' => $item['wechatAccountId'],
'wechatAccountAlias' => $item['wechatAccountAlias'],
'wechatAccountWechatId' => $item['wechatAccountWechatId'],
@@ -70,29 +79,27 @@ class WechatChatroomController extends BaseController
'wechatAccountNickname' => $item['wechatAccountNickname'],
'chatroomId' => $item['chatroomId'],
'hasMe' => $item['hasMe'],
'chatroomOwnerNickname' => $item['chatroomOwnerNickname'],
'chatroomOwnerAvatar' => $item['chatroomOwnerAvatar'],
'chatroomOwnerNickname' => isset($item['chatroomOwnerNickname']) ? $item['chatroomOwnerNickname'] : '',
'chatroomOwnerAvatar' => isset($item['chatroomOwnerAvatar']) ? $item['chatroomOwnerAvatar'] : '',
'conRemark' => isset($item['conRemark']) ? $item['conRemark'] : '',
'nickname' => $item['nickname'],
'pyInitial' => $item['pyInitial'],
'quanPin' => $item['quanPin'],
'chatroomAvatar' => $item['chatroomAvatar'],
'nickname' => isset($item['nickname']) ? $item['nickname'] : '',
'pyInitial' => isset($item['pyInitial']) ? $item['pyInitial'] : '',
'quanPin' => isset($item['quanPin']) ? $item['quanPin'] : '',
'chatroomAvatar' => isset($item['chatroomAvatar']) ? $item['chatroomAvatar'] : '',
'members' => is_array($item['members']) ? json_encode($item['members']) : json_encode([]),
'isDeleted' => $item['isDeleted'],
'deleteTime' => $item['deleteTime'],
'createTime' => $item['createTime'],
'accountId' => $item['accountId'],
'accountUserName' => $item['accountUserName'],
'accountRealName' => $item['accountRealName'],
'accountNickname' => $item['accountNickname'],
'groupId' => $item['groupId']
'isDeleted' => isset($item['isDeleted']) ? $item['isDeleted'] : 0,
'deleteTime' => isset($item['deleteTime']) ? $item['deleteTime'] : 0,
'createTime' => isset($item['createTime']) ? $item['createTime'] : time(),
'accountId' => isset($item['accountId']) ? $item['accountId'] : 0,
'accountUserName' => isset($item['accountUserName']) ? $item['accountUserName'] : '',
'accountRealName' => isset($item['accountRealName']) ? $item['accountRealName'] : '',
'accountNickname' => isset($item['accountNickname']) ? $item['accountNickname'] : '',
'groupId' => isset($item['groupId']) ? $item['groupId'] : 0,
'updateTime' => time()
];
// 使用chatroomId和wechatAccountId的组合作为唯一性判断
$chatroom = WechatChatroomModel::where([
['chatroomId', '=', $item['chatroomId']],
['wechatAccountId', '=', $item['wechatAccountId']]
])->find();
$chatroom = WechatChatroomModel::where('id',$item['id'])->find();
if ($chatroom) {
$chatroom->save($data);
@@ -161,12 +168,13 @@ class WechatChatroomController extends BaseController
{
$data = [
'chatroomId' => $wechatChatroomId,
'wechatId' => $item['wechatId'],
'nickname' => $item['nickname'],
'avatar' => $item['avatar'],
'wechatId' => isset($item['wechatId']) ? $item['wechatId'] : '',
'nickname' => isset($item['nickname']) ? $item['nickname'] : '',
'avatar' => isset($item['avatar']) ? $item['avatar'] : '',
'conRemark' => isset($item['conRemark']) ? $item['conRemark'] : '',
'alias' => isset($item['alias']) ? $item['alias'] : '',
'friendType' => isset($item['friendType']) ? $item['friendType'] : false
'friendType' => isset($item['friendType']) ? $item['friendType'] : false,
'updateTime' => time()
];
// 使用chatroomId和wechatId的组合作为唯一性判断
@@ -178,6 +186,7 @@ class WechatChatroomController extends BaseController
if ($member) {
$member->save($data);
} else {
$data['createTime'] = time();
WechatChatroomMemberModel::create($data);
}
}

View File

@@ -4,6 +4,7 @@ namespace app\api\controller;
use app\api\model\WechatFriendModel;
use think\facade\Request;
use think\facade\Log;
class WechatFriendController extends BaseController
{
@@ -11,13 +12,17 @@ class WechatFriendController extends BaseController
* 获取微信好友列表数据
* @return \think\response\Json
*/
public function getlist()
public function getlist($pageIndex = '',$pageSize = '',$preFriendId = '',$authorization = '',$isJob = false)
{
// 获取授权token
$authorization = trim($this->request->header('authorization', ''));
if (empty($authorization)) {
// 获取授权token
$authorization = !empty($authorization) ? $authorization : trim($this->request->header('authorization', ''));
if (empty($authorization)) {
if($isJob){
return json_encode(['code'=>500,'msg'=>'缺少授权信息']);
}else{
return errorJson('缺少授权信息');
}
}
try {
// 构建请求参数
@@ -34,12 +39,11 @@ class WechatFriendController extends BaseController
'isPass' => null,
'keyword' => input('keyword', ''),
'labels' => '[]',
'pageIndex' => input('pageIndex', 0),
'pageSize' => input('pageSize', 20),
'preFriendId' => input('preFriendId', ''),
'pageIndex' => !empty($pageIndex) ? $pageIndex : input('pageIndex', 0),
'pageSize' => !empty($pageSize) ? $pageSize : input('pageSize', 20),
'preFriendId' => !empty($preFriendId) ? $preFriendId : input('preFriendId', ''),
'wechatAccountKeyword' => input('wechatAccountKeyword', '')
];
// 设置请求头
$headerData = ['client:system'];
$header = setHeader($headerData, $authorization);
@@ -55,9 +59,19 @@ class WechatFriendController extends BaseController
}
}
return successJson($response);
if($isJob){
return json_encode(['code'=>200,'msg'=>'success','data'=>$response]);
}else{
return successJson($response);
}
} catch (\Exception $e) {
return errorJson('获取微信好友列表失败:' . $e->getMessage());
if($isJob){
return json_encode(['code'=>500,'msg'=>'获取微信好友列表失败:' . $e->getMessage()]);
}else{
return errorJson('获取微信好友列表失败:' . $e->getMessage());
}
}
}
@@ -104,6 +118,7 @@ class WechatFriendController extends BaseController
'province' => isset($item['province']) ? $item['province'] : '',
'city' => isset($item['city']) ? $item['city'] : '',
'createTime' =>isset($item['createTime']) ? $item['createTime'] : '',
'updateTime' => time()
];
// 使用三个字段的组合作为唯一性判断

View File

@@ -24,7 +24,7 @@ class WechatFriendCommand extends Command
try {
// 初始页码
$pageIndex = 0;
$pageSize = 100; // 每页获取100条记录
$pageSize = 1000; // 每页获取1000条记录
// 将第一页任务添加到队列
$this->addToQueue($pageIndex, $pageSize);

View File

@@ -81,7 +81,7 @@ class DeviceListJob
}
// 调用设备列表获取方法
$result = $deviceController->getlist($pageIndex,$pageSize,$authorization);
$result = $deviceController->getlist($pageIndex,$pageSize,$authorization,true);
$response = json_decode($result,true);

View File

@@ -81,9 +81,9 @@ class WechatChatroomJob
}
// 调用设备列表获取方法
$result = $wechatChatroomController->getlist($pageIndex,$pageSize,$authorization);
$result = $wechatChatroomController->getlist($pageIndex,$pageSize,$authorization,true);
$response = json_decode($result,true);
// 判断是否成功
if ($response['code'] == 200) {

View File

@@ -56,7 +56,8 @@ class WechatFriendJob
{
// 获取参数
$pageIndex = isset($data['pageIndex']) ? $data['pageIndex'] : 0;
$pageSize = isset($data['pageSize']) ? $data['pageSize'] : 100;
$pageSize = isset($data['pageSize']) ? $data['pageSize'] : 1000;
$preFriendId = isset($data['preFriendId']) ? $data['preFriendId'] : '';
Log::info('开始获取微信列表,页码:' . $pageIndex . ',页大小:' . $pageSize);
@@ -66,7 +67,8 @@ class WechatFriendJob
// 构建请求参数
$params = [
'pageIndex' => $pageIndex,
'pageSize' => $pageSize
'pageSize' => $pageSize,
'preFriendId' => $preFriendId
];
// 设置请求信息
@@ -81,7 +83,7 @@ class WechatFriendJob
}
// 调用设备列表获取方法
$result = $wechatFriendController->getlist($pageIndex,$pageSize,$authorization);
$result = $wechatFriendController->getlist($pageIndex,$pageSize,$preFriendId,$authorization,true);
$response = json_decode($result,true);
@@ -90,10 +92,10 @@ class WechatFriendJob
$data = $response['data'];
// 判断是否有下一页
if (!empty($data) && count($data['results']) > 0) {
if (!empty($data) && count($data) > 0) {
// 有下一页,将下一页任务添加到队列
$nextPageIndex = $pageIndex + 1;
$this->addNextPageToQueue($nextPageIndex, $pageSize);
$this->addNextPageToQueue($nextPageIndex, $pageSize,$data[count($data)-1]['id']);
Log::info('添加下一页任务到队列,页码:' . $nextPageIndex);
}
@@ -110,11 +112,12 @@ class WechatFriendJob
* @param int $pageIndex 页码
* @param int $pageSize 每页大小
*/
protected function addNextPageToQueue($pageIndex, $pageSize)
protected function addNextPageToQueue($pageIndex, $pageSize,$preFriendId)
{
$data = [
'pageIndex' => $pageIndex,
'pageSize' => $pageSize
'pageSize' => $pageSize,
'preFriendId' => $preFriendId
];
// 添加到队列,设置任务名为 wechat_friends

View File

@@ -23,4 +23,11 @@ Route::group('v1/store', function () {
Route::group('customers', function () {
Route::get('list', 'app\\store\\controller\\CustomerController@getList'); // 获取客户列表
});
// 系统配置相关路由
Route::group('system-config', function () {
Route::get('switch-status', 'app\\store\\controller\\SystemConfigController@getSwitchStatus'); // 获取系统开关状态
Route::post('update-switch-status', 'app\\store\\controller\\SystemConfigController@updateSwitchStatus'); // 更新系统开关状态
});
})->middleware(['jwt']);

View File

@@ -0,0 +1,66 @@
<?php
namespace app\store\controller;
use think\Controller;
use think\facade\Config;
use think\facade\Request;
use think\facade\Response;
use think\facade\Log;
use app\common\controller\Api;
use think\Db;
use think\facade\Cache;
/**
* 基础控制器
*/
class BaseController extends Api
{
protected $device = [];
protected $userInfo = [];
protected $cacheExpire = 3600; // 缓存过期时间1小时
/**
* 构造方法
*/
public function __construct()
{
parent::__construct();
$this->userInfo = request()->userInfo;
// 生成缓存key
$cacheKey = 'device_info_' . $this->userInfo['id'] . '_' . $this->userInfo['companyId'];
// 尝试从缓存获取设备信息
$device = Cache::get($cacheKey);
$device = '';
// 如果缓存不存在,则从数据库获取
if (!$device) {
$device = Db::name('device_user')
->alias('du')
->join('device d', 'd.id = du.deviceId','left')
->where([
'du.userId' => $this->userInfo['id'],
'du.companyId' => $this->userInfo['companyId']
])
->field('d.*')
->find();
// 将设备信息存入缓存
if ($device) {
Cache::set($cacheKey, $device, $this->cacheExpire);
}
}
$this->device = $device;
}
/**
* 清除设备信息缓存
*/
protected function clearDeviceCache()
{
$cacheKey = 'device_info_' . $this->userInfo['id'] . '_' . $this->userInfo['companyId'];
Cache::delete($cacheKey);
}
}

View File

@@ -0,0 +1,107 @@
<?php
namespace app\store\controller;
use think\Db;
use think\facade\Log;
use app\store\controller\BaseController;
/**
* 系统设置控制器
*/
class SystemConfigController extends BaseController
{
protected $noNeedLogin = [];
protected $noNeedRight = ['*'];
/**
* 获取系统开关状态
*
* @return \think\Response
*/
public function getSwitchStatus()
{
try {
// 获取设备ID
$deviceId = $this->device['id'] ?? 0;
if (!$deviceId) {
return $this->error('设备不存在');
}
// 获取已解析的配置
$config = $this->device['taskConfig'] ?? [];
// 返回开关状态
return $this->success('获取成功', [
'autoLike' => $config['autoLike'] ?? false,
'momentsSync' => $config['momentsSync'] ?? false,
'autoCustomerDev' => $config['autoCustomerDev'] ?? false,
'groupMessageDeliver' => $config['groupMessageDeliver'] ?? false,
'autoGroup' => $config['autoGroup'] ?? false
]);
} catch (\Exception $e) {
Log::error('获取开关状态异常:' . $e->getMessage());
return $this->error('获取开关状态失败');
}
}
/**
* 更新系统开关状态
*
* @return \think\Response
*/
public function updateSwitchStatus()
{
try {
// 获取参数
if (empty($this->device)) {
return errorJson('设备不存在');
}
$switchName = $this->request->param('switchName');
$deviceId = $this->device['id'];
if (empty($switchName)) {
return errorJson('开关名称不能为空');
}
// 验证开关名称是否有效
$validSwitches = ['autoLike', 'momentsSync', 'autoCustomerDev', 'groupMessageDeliver', 'autoGroup'];
if (!in_array($switchName, $validSwitches)) {
return errorJson('无效的开关名称');
}
// 获取当前配置并确保是数组
$taskConfig = json_decode($this->device['taskConfig'], true);
// 更新指定开关状态
$taskConfig[$switchName] = !$taskConfig[$switchName];
$taskConfig = json_encode($taskConfig);
// 更新数据库
$result = Db::name('device')
->where('id', $deviceId)
->update([
'taskConfig' => $taskConfig,
'updateTime' => time()
]);
if ($result === false) {
Log::error("更新设备{$switchName}开关状态失败设备ID{$deviceId}");
return errorJson('更新失败');
}
// 清除缓存
// $this->clearDeviceCache();
return successJson([], '更新成功');
} catch (\Exception $e) {
return errorJson('系统错误'. $e->getMessage());
}
}
}