Merge branch 'develop' of https://gitee.com/Tyssen/yi-shi into develop
This commit is contained in:
@@ -16,6 +16,7 @@ Route::group('v1/', function () {
|
||||
Route::post('', 'app\\devices\\controller\\Device@save'); // 添加设备
|
||||
Route::put('refresh', 'app\\devices\\controller\\Device@refresh'); // 刷新设备状态
|
||||
Route::delete(':id', 'app\\devices\\controller\\Device@delete'); // 删除设备
|
||||
Route::post('task-config', 'app\\devices\\controller\\Device@updateTaskConfig'); // 更新设备任务配置
|
||||
});
|
||||
|
||||
// 设备微信相关
|
||||
|
||||
@@ -222,15 +222,7 @@ class Device extends Controller
|
||||
'msg' => '设备不存在'
|
||||
]);
|
||||
}
|
||||
|
||||
// 检查设备是否属于用户所在公司
|
||||
if ($info['companyId'] != $userInfo['companyId']) {
|
||||
return json([
|
||||
'code' => 403,
|
||||
'msg' => '您没有权限查看该设备'
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '获取成功',
|
||||
@@ -404,4 +396,73 @@ class Device extends Controller
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新设备任务配置
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function updateTaskConfig()
|
||||
{
|
||||
// 获取请求参数
|
||||
$data = $this->request->post();
|
||||
|
||||
// 验证参数
|
||||
if (empty($data['id'])) {
|
||||
return json(['code' => 400, 'msg' => '设备ID不能为空']);
|
||||
}
|
||||
|
||||
// 转换为整型,确保ID格式正确
|
||||
$deviceId = intval($data['id']);
|
||||
|
||||
// 先获取设备信息,确认设备存在且未删除
|
||||
$device = \app\devices\model\Device::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]]);
|
||||
}
|
||||
|
||||
// 更新设备taskConfig字段
|
||||
$result = \app\devices\model\Device::where('id', $deviceId)
|
||||
->update([
|
||||
'taskConfig' => json_encode($taskConfig),
|
||||
'updateTime' => time()
|
||||
]);
|
||||
|
||||
if ($result) {
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '更新任务配置成功',
|
||||
'data' => [
|
||||
'taskConfig' => $taskConfig
|
||||
]
|
||||
]);
|
||||
} else {
|
||||
return json(['code' => 500, 'msg' => '更新任务配置失败']);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -109,9 +109,61 @@ class Device extends Model
|
||||
*/
|
||||
public static function getDeviceInfo($id)
|
||||
{
|
||||
return self::where('id', $id)
|
||||
->where('isDeleted', 0)
|
||||
// 查询设备基础信息与关联的微信账号信息
|
||||
$device = self::alias('d')
|
||||
->field([
|
||||
'd.id', 'd.imei', 'd.memo', 'd.alive', 'd.taskConfig', 'd.lastUpdateTime',
|
||||
'w.id as wechatId', 'w.thirtyDayMsgCount', 'w.totalFriend', 'd.extra'
|
||||
])
|
||||
->leftJoin('tk_wechat_account w', 'd.imei = w.imei')
|
||||
->where('d.id', $id)
|
||||
->where('d.isDeleted', 0)
|
||||
->find();
|
||||
|
||||
// 如果设备存在,处理额外信息
|
||||
if ($device) {
|
||||
// 解析电量信息
|
||||
$battery = 0;
|
||||
if (!empty($device['extra'])) {
|
||||
$extra = json_decode($device['extra'], true);
|
||||
if (is_array($extra) && isset($extra['battery'])) {
|
||||
$battery = intval($extra['battery']);
|
||||
}
|
||||
}
|
||||
$device['battery'] = $battery;
|
||||
|
||||
// 解析taskConfig字段获取功能开关
|
||||
$features = [
|
||||
'autoAddFriend' => false,
|
||||
'autoReply' => false,
|
||||
'contentSync' => false,
|
||||
'aiChat' => false
|
||||
];
|
||||
|
||||
if (!empty($device['taskConfig'])) {
|
||||
$taskConfig = json_decode($device['taskConfig'], true);
|
||||
if (is_array($taskConfig)) {
|
||||
// 映射taskConfig中的字段到前端需要的features
|
||||
$features['autoAddFriend'] = isset($taskConfig['autoAddFriend']) ? (bool)$taskConfig['autoAddFriend'] : false;
|
||||
$features['autoReply'] = isset($taskConfig['autoReply']) ? (bool)$taskConfig['autoReply'] : false;
|
||||
$features['contentSync'] = isset($taskConfig['momentsSync']) ? (bool)$taskConfig['momentsSync'] : false;
|
||||
$features['aiChat'] = isset($taskConfig['aiChat']) ? (bool)$taskConfig['aiChat'] : false;
|
||||
}
|
||||
}
|
||||
|
||||
$device['features'] = $features;
|
||||
unset($device['extra']);
|
||||
unset($device['taskConfig']);
|
||||
|
||||
// 格式化最后活跃时间
|
||||
$device['lastUpdateTime'] = !empty($device['lastUpdateTime']) ? date('Y-m-d H:i:s', strtotime($device['lastUpdateTime'])) : date('Y-m-d H:i:s');
|
||||
|
||||
// 确保totalFriend和thirtyDayMsgCount有值,防止NULL
|
||||
$device['totalFriend'] = intval($device['totalFriend'] ?? 0);
|
||||
$device['thirtyDayMsgCount'] = intval($device['thirtyDayMsgCount'] ?? 0);
|
||||
}
|
||||
|
||||
return $device;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user