import { getToken } from '@/utils/auth' export interface MachineOverview { total: number physical: number virtual: number cmdb: { total: number physical: number virtual: number } aliyun: { total: number label: string } awsQiniu: { total: number aws: number qiniu: number } lastSync: { status: string time: string created: number error?: string } } export interface MachineResource { hostname: string assetNumber: string type: string location: string ip: string spec: string businessLine: string source: string status: string } export interface MachineResourceList { total: number items: MachineResource[] } export interface MachineResourceQuery { page: number size: number businessLineId?: number hostname?: string assetNumber?: string type?: string location?: string ip?: string spec?: string source?: string status?: string } export const emptyMachineOverview: MachineOverview = { total: 0, physical: 0, virtual: 0, cmdb: { total: 0, physical: 0, virtual: 0, }, aliyun: { total: 0, label: 'ECS · 增量同步', }, awsQiniu: { total: 0, aws: 0, qiniu: 0, }, lastSync: { status: 'unknown', time: '—', created: 0, }, } export const machineApi = { async getOverview(businessLineId?: number): Promise { const params = new URLSearchParams() if (businessLineId) params.set('business_line_id', String(businessLineId)) const suffix = params.toString() ? `?${params.toString()}` : '' return authRequest(`/auth/api/v1/machines/overview${suffix}`) }, async listResources(query: MachineResourceQuery): Promise { const params = new URLSearchParams() params.set('page', String(query.page)) params.set('size', String(query.size)) if (query.businessLineId) params.set('business_line_id', String(query.businessLineId)) if (query.hostname) params.set('hostname', query.hostname) if (query.assetNumber) params.set('assetNumber', query.assetNumber) if (query.type) params.set('type', query.type) if (query.location) params.set('location', query.location) if (query.ip) params.set('ip', query.ip) if (query.spec) params.set('spec', query.spec) if (query.source) params.set('source', query.source) if (query.status) params.set('status', query.status) const data = await authRequest(`/auth/api/v1/machines/resources?${params.toString()}`) return { total: Number(data.total || 0), items: Array.isArray(data.items) ? data.items : [], } }, async sync(): Promise { await authRequest('/auth/api/v1/machines/sync', { method: 'POST' }) }, } async function authRequest(path: string, init: RequestInit = {}) { const token = getToken() const response = await fetch(path, { ...init, headers: { Accept: 'application/json', 'Content-Type': 'application/json', ...(token ? { Authorization: `Bearer ${token}` } : {}), ...init.headers, }, }) const text = await response.text() const data = parseResponseBody(text) if (!response.ok) { const message = data?.error || data?.message || text || `HTTP ${response.status}` throw new Error(message) } return data || {} } function parseResponseBody(text: string) { if (!text.trim()) { return {} } try { return JSON.parse(text) } catch { return { error: text } } }