队列优化
This commit is contained in:
66
Server/application/store/controller/BaseController.php
Normal file
66
Server/application/store/controller/BaseController.php
Normal 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);
|
||||
}
|
||||
}
|
||||
107
Server/application/store/controller/SystemConfigController.php
Normal file
107
Server/application/store/controller/SystemConfigController.php
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user