diff --git a/Server/application/devices/controller/Device.php b/Server/application/devices/controller/Device.php index 4515a492..ad8db19a 100644 --- a/Server/application/devices/controller/Device.php +++ b/Server/application/devices/controller/Device.php @@ -1,8 +1,10 @@ $data['imei'], + 'userId' => $userInfo['id'], + 'content' => '添加设备', + 'companyId' => $userInfo['companyId'], + ] + ); + Db::commit(); + } catch (\Exception $e) { + Db::rollback(); + + return json([ + 'code' => 500, + 'msg' => '添加失败:' . $e->getMessage() + ]); + } // 此处调用底层API return json([ @@ -333,12 +357,6 @@ class Device extends Controller try { // 获取登录用户信息 $userInfo = request()->userInfo; - if (empty($userInfo)) { - return json([ - 'code' => 401, - 'msg' => '未登录或登录已过期' - ]); - } // 检查用户权限,只有管理员可以删除设备 if ($userInfo['isAdmin'] != 1) { @@ -396,6 +414,9 @@ class Device extends Controller { // 获取请求参数 $data = $this->request->post(); + + // 获取登录用户信息 + $userInfo = request()->userInfo; // 验证参数 if (empty($data['id'])) { @@ -436,14 +457,52 @@ class Device extends Controller 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() - ]); + + try { + Db::startTrans(); + + // 更新设备taskConfig字段 + $result = \app\devices\model\Device::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'], + '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, diff --git a/Server/application/devices/model/DeviceHandleLog.php b/Server/application/devices/model/DeviceHandleLog.php new file mode 100644 index 00000000..f1a2520d --- /dev/null +++ b/Server/application/devices/model/DeviceHandleLog.php @@ -0,0 +1,120 @@ + 'integer', + 'userId' => 'integer', + 'companyId' => 'integer', + 'createTime' => 'datetime' + ]; + + /** + * 添加设备操作日志 + * @param array $data 日志数据 + * @return int 新增日志ID + */ + public static function addLog($data) + { + $log = new self(); + $log->allowField(true)->save($data); + return $log->id; + } + + /** + * 获取设备操作日志列表 + * @param array $where 查询条件 + * @param string $order 排序方式 + * @param int $page 页码 + * @param int $limit 每页数量 + * @return \think\Paginator 分页对象 + */ + public static function getLogList($where = [], $order = 'createTime desc', $page = 1, $limit = 10) + { + return self::where($where) + ->order($order) + ->paginate($limit, false, ['page' => $page]); + } + + /** + * 根据IMEI获取设备操作日志 + * @param string $imei 设备IMEI + * @param int $companyId 租户ID + * @param int $limit 获取条数 + * @return array 日志记录 + */ + public static function getLogsByImei($imei, $companyId = null, $limit = 10) + { + $query = self::where('imei', $imei); + + if ($companyId !== null) { + $query->where('companyId', $companyId); + } + + return $query->order('createTime', 'desc') + ->limit($limit) + ->select(); + } + + /** + * 根据用户ID获取操作日志 + * @param int $userId 用户ID + * @param int $companyId 租户ID + * @param int $page 页码 + * @param int $limit 每页数量 + * @return \think\Paginator 分页对象 + */ + public static function getLogsByUser($userId, $companyId = null, $page = 1, $limit = 10) + { + $query = self::where('userId', $userId); + + if ($companyId !== null) { + $query->where('companyId', $companyId); + } + + return $query->order('createTime', 'desc') + ->paginate($limit, false, ['page' => $page]); + } + + /** + * 记录设备操作日志的便捷方法 + * @param string $imei 设备IMEI + * @param int $userId 操作用户ID + * @param string $content 操作内容 + * @param int $companyId 租户ID + * @return int 日志ID + */ + public static function recordLog($imei, $userId, $content, $companyId = null) + { + $data = [ + 'imei' => $imei, + 'userId' => $userId, + 'content' => $content, + 'companyId' => $companyId + ]; + + return self::addLog($data); + } +} \ No newline at end of file