Files
cunkebao_v3/Server/application/store/controller/SystemConfigController.php

151 lines
4.8 KiB
PHP
Raw Normal View History

2025-04-01 09:26:06 +08:00
<?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('设备不存在');
}
2025-04-17 17:18:35 +08:00
// 从新表中获取配置
$config = Db::name('device_taskconf')
->where('deviceId', $deviceId)
->field('id,autoLike,autoCustomerDev,groupMessageDeliver,autoGroup,contentSync,aiChat,autoReply,momentsSync')
->find();
2025-04-02 10:19:03 +08:00
2025-04-17 17:18:35 +08:00
// 如果没有找到配置,创建默认配置
if (empty($config)) {
$taskConfig = [
'deviceId' => $deviceId,
'autoLike' => 0,
'autoCustomerDev' => 0,
'groupMessageDeliver' => 0,
'autoGroup' => 0,
'contentSync' => 0,
'aiChat' => 0,
'autoReply' => 0,
'momentsSync' => 0,
'companyId' => $this->device['companyId'] ?? 0,
'createTime' => time(),
'updateTime' => time()
];
// 添加到数据库
Db::name('device_taskconf')->insert($taskConfig);
// 返回默认配置
return successJson($taskConfig);
}
2025-04-01 09:26:06 +08:00
// 返回开关状态
2025-04-02 10:19:03 +08:00
return successJson($config);
2025-04-01 09:26:06 +08:00
} 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('开关名称不能为空');
}
// 验证开关名称是否有效
2025-04-17 17:18:35 +08:00
$validSwitches = ['autoLike', 'autoCustomerDev', 'groupMessageDeliver', 'autoGroup', 'contentSync', 'aiChat', 'autoReply', 'momentsSync'];
2025-04-01 09:26:06 +08:00
if (!in_array($switchName, $validSwitches)) {
return errorJson('无效的开关名称');
}
2025-04-17 17:18:35 +08:00
// 获取当前配置
$taskConfig = Db::name('device_taskconf')
->where('deviceId', $deviceId)
->find();
2025-04-01 09:26:06 +08:00
2025-04-17 17:18:35 +08:00
// 如果没有找到配置,创建默认配置
if (empty($taskConfig)) {
$taskConfig = [
'deviceId' => $deviceId,
'autoLike' => 0,
'autoCustomerDev' => 0,
'groupMessageDeliver' => 0,
'autoGroup' => 0,
'contentSync' => 0,
'aiChat' => 0,
'autoReply' => 0,
'momentsSync' => 0,
'companyId' => $this->device['companyId'] ?? 0,
'createTime' => time(),
'updateTime' => time()
];
// 设置要更新的开关
$taskConfig[$switchName] = 1;
// 添加到数据库
Db::name('device_taskconf')->insert($taskConfig);
} else {
// 更新指定开关状态
$updateData = [
$switchName => !$taskConfig[$switchName],
2025-04-01 09:26:06 +08:00
'updateTime' => time()
2025-04-17 17:18:35 +08:00
];
2025-04-01 09:26:06 +08:00
2025-04-17 17:18:35 +08:00
// 更新数据库
$result = Db::name('device_taskconf')
->where('deviceId', $deviceId)
->update($updateData);
if ($result === false) {
Log::error("更新设备{$switchName}开关状态失败设备ID{$deviceId}");
return errorJson('更新失败');
}
2025-04-01 09:26:06 +08:00
}
// 清除缓存
2025-04-17 17:18:35 +08:00
$this->clearDeviceCache();
2025-04-01 09:26:06 +08:00
return successJson([], '更新成功');
} catch (\Exception $e) {
return errorJson('系统错误'. $e->getMessage());
}
}
2025-04-17 17:18:35 +08:00
}