import { http } from './http' import { unwrap } from './envelope' /** 日志文件行(桌面客户端 / 麦象采集机上报)。 */ export interface DeviceLogFileRow { id: number source: string deviceId: string deviceName: string | null username: string | null uid: number | null fileName: string logDate: string uploadedBytes: number partCount: number lastUploadAt: string | null createdAt: string | null } export interface DeviceLogPage { items: DeviceLogFileRow[] total: number page: number pageSize: number /** 云端日志保留天数(页面提示用)。 */ retentionDays: number } export interface DeviceLogContent { fileId: number fileName: string content: string totalBytes: number shownBytes: number truncated: boolean } export interface DeviceLogDevice { source: string deviceId: string deviceName: string | null lastUploadAt: string | null } export interface DeviceLogOverride { id: number source: string deviceId: string deviceName: string | null mode: string updatedAt: string | null } export interface DeviceLogConfigData { globalMode: string overrides: DeviceLogOverride[] } export interface DeviceLogQuery { source?: string /** 指定用户(users.id);不传=全部上报者,含 uid 为空的采集机与未登录客户端。 */ uid?: number startDate?: string endDate?: string page: number pageSize: number /** 超管选中的公司 tab;null/undefined=不发这个参数,后端按「不筛」出全量(非超管恒不发)。 */ companyId?: number | null } /** 分页查询日志文件列表:GET /api/admin/device-logs/files */ export async function fetchDeviceLogFiles(params: DeviceLogQuery): Promise { const query: Record = { page: params.page, pageSize: params.pageSize } if (params.source) query.source = params.source if (params.uid) query.uid = params.uid if (params.startDate) query.startDate = params.startDate if (params.endDate) query.endDate = params.endDate // 公司值由页面组件(companyTab.requestCompanyId)传进来,本模块不读 Pinia store。 // query 是这里逐字段点名的白名单构造 ⇒ 右半边只许取原始 params。 if (params.companyId != null) query.company_id = params.companyId const { data } = await http.get('/api/admin/device-logs/files', { params: query }) return unwrap(data) } /** 查看日志尾部内容:GET /api/admin/device-logs/content */ export async function fetchDeviceLogContent(fileId: number, maxBytes?: number): Promise { const query: Record = { fileId } if (maxBytes) query.maxBytes = maxBytes const { data } = await http.get('/api/admin/device-logs/content', { params: query }) return unwrap(data) } /** 完整日志下载地址(同域 cookie 鉴权,直接给 a[href] 或 window.open 用)。 */ export function deviceLogDownloadUrl(fileId: number): string { return `/api/admin/device-logs/download?fileId=${fileId}` } /** 删除日志文件(片段与元数据,不可恢复):DELETE /api/admin/device-logs/{id} */ export async function deleteDeviceLogFile(id: number): Promise { const { data } = await http.delete(`/api/admin/device-logs/${id}`) unwrap(data) } /** 采集配置(全局默认 + 终端覆盖):GET /api/admin/device-logs/config */ export async function fetchDeviceLogConfig(keyword?: string): Promise { const { data } = await http.get('/api/admin/device-logs/config', { params: keyword ? { keyword } : undefined, }) return unwrap(data) } /** 最近上报过的终端(覆盖选择用):GET /api/admin/device-logs/devices */ export async function fetchDeviceLogDevices(): Promise { const { data } = await http.get('/api/admin/device-logs/devices') return unwrap(data) } /** 设置全局采集模式:PUT /api/admin/device-logs/config/global */ export async function updateDeviceLogGlobalMode(mode: string): Promise { const { data } = await http.put('/api/admin/device-logs/config/global', undefined, { params: { mode } }) unwrap(data) } /** 设置/更新终端覆盖:PUT /api/admin/device-logs/config/device */ export async function updateDeviceLogOverride( source: string, deviceId: string, deviceName: string | null, mode: string, ): Promise { const { data } = await http.put('/api/admin/device-logs/config/device', undefined, { params: { source, deviceId, deviceName: deviceName || undefined, mode }, }) unwrap(data) } /** 删除终端覆盖(回落到全局默认):DELETE /api/admin/device-logs/config/device/{id} */ export async function deleteDeviceLogOverride(id: number): Promise { const { data } = await http.delete(`/api/admin/device-logs/config/device/${id}`) unwrap(data) }