53 lines
1.4 KiB
TypeScript
53 lines
1.4 KiB
TypeScript
import { http } from './http'
|
|
|
|
// 与老版保持相同的接口路径与参数
|
|
const base = '/api/device'
|
|
|
|
export interface DeviceQuota {
|
|
limit: number
|
|
used: number
|
|
}
|
|
|
|
export interface DeviceItem {
|
|
deviceId: string
|
|
name?: string
|
|
status?: 'online' | 'offline'
|
|
lastActiveAt?: string
|
|
}
|
|
|
|
export const deviceApi = {
|
|
getQuota(username: string) {
|
|
return http.get<DeviceQuota | any>(`${base}/quota`, { username }).then((res: any) => {
|
|
if (res && typeof res.limit !== 'undefined') return res as DeviceQuota
|
|
if (res && typeof res.code === 'number') return (res.data as DeviceQuota) || { limit: 0, used: 0 }
|
|
return (res?.data as DeviceQuota) || { limit: 0, used: 0 }
|
|
})
|
|
},
|
|
|
|
list(username: string) {
|
|
return http.get<DeviceItem[] | any>(`${base}/list`, { username }).then((res: any) => {
|
|
if (Array.isArray(res)) return res as DeviceItem[]
|
|
if (res && typeof res.code === 'number') return (res.data as DeviceItem[]) || []
|
|
return (res?.data as DeviceItem[]) || []
|
|
})
|
|
},
|
|
|
|
register(payload: { username: string }) {
|
|
return http.post(`${base}/register`, payload)
|
|
},
|
|
|
|
remove(payload: { deviceId: string }) {
|
|
return http.postVoid(`${base}/remove`, payload)
|
|
},
|
|
|
|
heartbeat(payload: { username: string; deviceId: string; version?: string }) {
|
|
return http.postVoid(`${base}/heartbeat`, payload)
|
|
},
|
|
|
|
offline(payload: { deviceId: string }) {
|
|
return http.postVoid(`${base}/offline`, payload)
|
|
},
|
|
}
|
|
|
|
|