抄盘手端 - 设置设备任务参数调整

This commit is contained in:
柳清爽
2025-04-16 17:49:17 +08:00
parent ad0d487b6b
commit 9e44fef525
5 changed files with 25 additions and 234 deletions

View File

@@ -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 ? '已启用' : '已禁用'}`)

View File

@@ -127,5 +127,23 @@ export const deviceApi = {
})
return response.json()
},
// 更新设备任务配置
async updateDeviceTaskConfig(params: {
deviceId: string;
autoAddFriend?: number;
autoReply?: number;
momentsSync?: number;
aiChat?: number;
}): Promise<ApiResponse<Device>> {
const response = await fetch(`${API_BASE}/task-config`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(params),
});
return response.json();
},
}

View File

@@ -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

View File

@@ -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'); // 更新设备任务配置
});

View File

@@ -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