代码优化提交

This commit is contained in:
wong
2025-04-17 17:18:35 +08:00
parent 4f838149e9
commit 9c33603c04
19 changed files with 1657 additions and 480 deletions

View File

@@ -29,9 +29,35 @@ class SystemConfigController extends BaseController
return $this->error('设备不存在');
}
// 获取已解析的配置
$config = json_decode($this->device['taskConfig'], true);
// 从新表中获取配置
$config = Db::name('device_taskconf')
->where('deviceId', $deviceId)
->field('id,autoLike,autoCustomerDev,groupMessageDeliver,autoGroup,contentSync,aiChat,autoReply,momentsSync')
->find();
// 如果没有找到配置,创建默认配置
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);
}
// 返回开关状态
return successJson($config);
@@ -63,35 +89,58 @@ class SystemConfigController extends BaseController
}
// 验证开关名称是否有效
$validSwitches = ['autoLike', 'momentsSync', 'autoCustomerDev', 'groupMessageDeliver', 'autoGroup'];
$validSwitches = ['autoLike', 'autoCustomerDev', 'groupMessageDeliver', 'autoGroup', 'contentSync', 'aiChat', 'autoReply', 'momentsSync'];
if (!in_array($switchName, $validSwitches)) {
return errorJson('无效的开关名称');
}
// 获取当前配置并确保是数组
$taskConfig = json_decode($this->device['taskConfig'], true);
// 获取当前配置
$taskConfig = Db::name('device_taskconf')
->where('deviceId', $deviceId)
->find();
// 更新指定开关状态
$taskConfig[$switchName] = !$taskConfig[$switchName];
$taskConfig = json_encode($taskConfig);
// 更新数据库
$result = Db::table('s2_device')
->where('id', $deviceId)
->update([
'taskConfig' => $taskConfig,
// 如果没有找到配置,创建默认配置
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()
]);
];
if ($result === false) {
Log::error("更新设备{$switchName}开关状态失败设备ID{$deviceId}");
return errorJson('更新失败');
// 设置要更新的开关
$taskConfig[$switchName] = 1;
// 添加到数据库
Db::name('device_taskconf')->insert($taskConfig);
} else {
// 更新指定开关状态
$updateData = [
$switchName => !$taskConfig[$switchName],
'updateTime' => time()
];
// 更新数据库
$result = Db::name('device_taskconf')
->where('deviceId', $deviceId)
->update($updateData);
if ($result === false) {
Log::error("更新设备{$switchName}开关状态失败设备ID{$deviceId}");
return errorJson('更新失败');
}
}
// 清除缓存
$this->clearDeviceCache();
$this->clearDeviceCache();
return successJson([], '更新成功');
@@ -99,4 +148,4 @@ class SystemConfigController extends BaseController
return errorJson('系统错误'. $e->getMessage());
}
}
}
}