From 9e44fef5250176fca38c8335a8aa17a1615ef47f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=B3=E6=B8=85=E7=88=BD?= Date: Wed, 16 Apr 2025 17:49:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=84=E7=9B=98=E6=89=8B=E7=AB=AF=20-=20?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E8=AE=BE=E5=A4=87=E4=BB=BB=E5=8A=A1=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cunkebao/app/devices/[id]/page.tsx | 9 +- Cunkebao/lib/api/devices.ts | 18 ++ Server/application/common/model/User.php | 18 -- Server/application/cunkebao/config/route.php | 2 +- .../cunkebao/controller/Device.php | 212 ------------------ 5 files changed, 25 insertions(+), 234 deletions(-) diff --git a/Cunkebao/app/devices/[id]/page.tsx b/Cunkebao/app/devices/[id]/page.tsx index d93a02aa..f62bb40b 100644 --- a/Cunkebao/app/devices/[id]/page.tsx +++ b/Cunkebao/app/devices/[id]/page.tsx @@ -353,8 +353,11 @@ export default function DeviceDetailPage() { // 准备更新后的功能状态 const updatedFeatures = { ...device.features, [feature]: checked } - // 创建API请求参数 - const configUpdate = { [feature]: checked } + // 创建API请求参数,将布尔值转换为0/1 + const configUpdate = { + deviceId: device.id, + [feature]: checked ? 1 : 0 + } // 立即更新UI状态,提供即时反馈 setDevice(prev => prev ? { @@ -363,7 +366,7 @@ export default function DeviceDetailPage() { } : null) // 调用API更新服务器配置 - const response = await updateDeviceTaskConfig(device.id, configUpdate) + const response = await updateDeviceTaskConfig(configUpdate) if (response && response.code === 200) { toast.success(`${getFeatureName(feature)}${checked ? '已启用' : '已禁用'}`) diff --git a/Cunkebao/lib/api/devices.ts b/Cunkebao/lib/api/devices.ts index 69fafe0d..9facd1fc 100644 --- a/Cunkebao/lib/api/devices.ts +++ b/Cunkebao/lib/api/devices.ts @@ -127,5 +127,23 @@ export const deviceApi = { }) return response.json() }, + + // 更新设备任务配置 + async updateDeviceTaskConfig(params: { + deviceId: string; + autoAddFriend?: number; + autoReply?: number; + momentsSync?: number; + aiChat?: number; + }): Promise> { + const response = await fetch(`${API_BASE}/task-config`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(params), + }); + return response.json(); + }, } diff --git a/Server/application/common/model/User.php b/Server/application/common/model/User.php index 1e4681d8..b237def2 100644 --- a/Server/application/common/model/User.php +++ b/Server/application/common/model/User.php @@ -25,24 +25,6 @@ class User extends Model */ protected $pk = 'id'; - /** - * 自动写入时间戳 - * @var bool - */ - protected $autoWriteTimestamp = true; - - /** - * 创建时间字段 - * @var string - */ - protected $createTime = 'createTime'; - - /** - * 更新时间字段 - * @var string - */ - protected $updateTime = 'updateTime'; - /** * 软删除字段 * @var string diff --git a/Server/application/cunkebao/config/route.php b/Server/application/cunkebao/config/route.php index 24a47dec..59ef114d 100644 --- a/Server/application/cunkebao/config/route.php +++ b/Server/application/cunkebao/config/route.php @@ -16,7 +16,7 @@ Route::group('v1/', function () { Route::get('count', 'app\\cunkebao\\controller\\Device@count'); // 获取设备总数 Route::get(':id', 'app\\cunkebao\\controller\\device\\GetDeviceDetailV1Controller@index'); // 获取设备详情 Route::post('', 'app\\cunkebao\\controller\\Device@save'); // 添加设备 - Route::put('refresh', 'app\\cunkebao\\controller\\Device@refresh'); // 刷新设备状态 + Route::put('refresh', 'app\\cunkebao\\controller\\device\\RefreshDeviceDetailV1Controller@index'); // 刷新设备状态 Route::delete(':id', 'app\\cunkebao\\controller\\Device@delete'); // 删除设备 Route::post('task-config', 'app\\cunkebao\\controller\\device\\UpdateDeviceTaskConfigV1Controller@index'); // 更新设备任务配置 }); diff --git a/Server/application/cunkebao/controller/Device.php b/Server/application/cunkebao/controller/Device.php index 7d99aed4..b9fdbf69 100644 --- a/Server/application/cunkebao/controller/Device.php +++ b/Server/application/cunkebao/controller/Device.php @@ -30,108 +30,7 @@ class Device extends Controller // 设置时区 date_default_timezone_set('Asia/Shanghai'); } - - /** - * 获取设备总数 - * @return \think\response\Json - */ - public function count() - { - try { - // 获取登录用户信息 - $userInfo = request()->userInfo; - if (empty($userInfo)) { - return json([ - 'code' => 401, - 'msg' => '未登录或登录已过期' - ]); - } - - // 获取查询条件 - $where = []; - - // 租户ID / 公司ID - $where['companyId'] = $userInfo['companyId']; - // 设备在线状态 - $alive = Request::param('alive'); - if (is_numeric($alive)) { - $where['alive'] = $alive; - } - - // 根据用户管理员状态调整查询条件 - if ($userInfo['isAdmin'] == 1) { - // 管理员直接查询所有设备 - $count = DeviceModel::getDeviceCount($where); - } else { - // 非管理员需要查询关联表 - $deviceIds = DeviceUserModel::getUserDeviceIds( - $userInfo['id'], - $userInfo['companyId'] - ); - - if (empty($deviceIds)) { - return json([ - 'code' => 403, - 'msg' => '请联系管理员绑定设备', - 'data' => [ - 'count' => 0 - ] - ]); - } - - // 添加设备ID过滤条件 - $where['id'] = ['in', $deviceIds]; - $count = DeviceModel::getDeviceCount($where); - } - - return json([ - 'code' => 200, - 'msg' => '获取成功', - 'data' => [ - 'count' => $count - ] - ]); - } catch (\Exception $e) { - return json([ - 'code' => 500, - 'msg' => '获取失败:' . $e->getMessage() - ]); - } - } - - /** - * 刷新设备 - * @return \think\response\Json - */ - public function refresh() - { - try { - // 获取登录用户信息 - $userInfo = request()->userInfo; - if (empty($userInfo)) { - return json([ - 'code' => 401, - 'msg' => '未登录或登录已过期' - ]); - } - - // 执行刷新逻辑 - // TODO: 实现实际刷新设备状态的功能 - - return json([ - 'code' => 200, - 'msg' => '刷新成功', - 'data' => [] - ]); - } catch (\Exception $e) { - return json([ - 'code' => 500, - 'msg' => '获取失败:' . $e->getMessage() - ]); - } - } - /** * 添加设备 * @return \think\response\Json @@ -274,117 +173,6 @@ class Device extends Controller } } - /** - * 更新设备任务配置 - * @return \think\response\Json - */ - public function updateTaskConfig() - { - // 获取请求参数 - $data = $this->request->post(); - - // 获取登录用户信息 - $userInfo = request()->userInfo; - - // 验证参数 - if (empty($data['id'])) { - return json(['code' => 400, 'msg' => '设备ID不能为空']); - } - - // 转换为整型,确保ID格式正确 - $deviceId = intval($data['id']); - - // 先获取设备信息,确认设备存在且未删除 - $device = DeviceModel::where('id', $deviceId) - ->where('isDeleted', 0) - ->find(); - - if (!$device) { - return json(['code' => 404, 'msg' => '设备不存在或已删除']); - } - - // 读取原taskConfig,如果存在则解析 - $taskConfig = []; - if (!empty($device['taskConfig'])) { - $taskConfig = json_decode($device['taskConfig'], true) ?: []; - } - - // 更新需要修改的配置项 - $updateFields = ['autoAddFriend', 'autoReply', 'momentsSync', 'aiChat']; - $hasUpdate = false; - - foreach ($updateFields as $field) { - if (isset($data[$field])) { - // 将值转换为布尔类型存储 - $taskConfig[$field] = (bool)$data[$field]; - $hasUpdate = true; - } - } - - // 如果没有需要更新的字段,直接返回成功 - if (!$hasUpdate) { - return json(['code' => 200, 'msg' => '更新成功', 'data' => ['taskConfig' => $taskConfig]]); - } - - try { - Db::startTrans(); - - // 更新设备taskConfig字段 - $result = DeviceModel::where('id', $deviceId) - ->update([ - 'taskConfig' => json_encode($taskConfig), - 'updateTime' => time() - ]); - - if (isset($data['autoAddFriend'])) { - $content = $data['autoAddFriend'] ? '开启自动添加好友' : '关闭自动添加好友'; - } - - if (isset($data['autoReply'])) { - $content = $data['autoReply'] ? '开启自动回复' : '关闭自动回复'; - } - - if (isset($data['momentsSync'])) { - $content = $data['momentsSync'] ? '开启朋友圈同步' : '关闭朋友圈同步'; - } - - if (isset($data['aiChat'])) { - $content = $data['aiChat'] ? '开启AI会话' : '关闭AI会话'; - } - - // 添加设备操作记录 - DeviceHandleLog::addLog( - [ - 'imei' => $device['imei'], - 'deviceId' => $deviceId, - 'userId' => $userInfo['id'], - 'content' => $content, - 'companyId' => $userInfo['companyId'], - ] - ); - Db::commit(); - } catch (\Exception $e) { - Db::rollback(); - - return json([ - 'code' => 500, - 'msg' => '更新任务配置失败' - ]); - } - - if ($result) { - return json([ - 'code' => 200, - 'msg' => '更新任务配置成功', - 'data' => [ - 'taskConfig' => $taskConfig - ] - ]); - } else { - return json(['code' => 500, 'msg' => '更新任务配置失败']); - } - } - /** * 获取设备关联的微信账号 * @return \think\response\Json