feat(小程序): 支持设备定位与轨迹展示
This commit is contained in:
67
banban-mini/src/services/location.ts
Normal file
67
banban-mini/src/services/location.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import { request } from './api'
|
||||
import { resolveActiveBinding } from './binding'
|
||||
|
||||
export interface DeviceLocation {
|
||||
child_id: number
|
||||
child_name?: string | null
|
||||
device_id: string
|
||||
coord_type: string
|
||||
lat: number
|
||||
lng: number
|
||||
accuracy_m?: number | null
|
||||
altitude_m?: number | null
|
||||
speed_mps?: number | null
|
||||
heading_deg?: number | null
|
||||
source: number
|
||||
battery_pct?: number | null
|
||||
device_time: string
|
||||
server_time: string
|
||||
updated_at: string
|
||||
}
|
||||
|
||||
export interface DeviceTrajectoryPoint extends DeviceLocation {
|
||||
id: number
|
||||
created_at: string
|
||||
}
|
||||
|
||||
export interface DeviceTrajectoryResponse {
|
||||
items: DeviceTrajectoryPoint[]
|
||||
total: number
|
||||
start_at?: string | null
|
||||
end_at?: string | null
|
||||
}
|
||||
|
||||
export async function getCurrentDeviceLocation(deviceId?: string): Promise<DeviceLocation | null> {
|
||||
const resolvedDeviceId = String(deviceId || '').trim() || (await resolveActiveBinding())?.device_id || ''
|
||||
if (!resolvedDeviceId) return null
|
||||
|
||||
try {
|
||||
return await request<DeviceLocation>(`/devices/${resolvedDeviceId}/location`)
|
||||
} catch (error: any) {
|
||||
if (error?.status === 404) return null
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export async function getDeviceTrajectory(params?: {
|
||||
deviceId?: string
|
||||
startAt?: string
|
||||
endAt?: string
|
||||
limit?: number
|
||||
}): Promise<DeviceTrajectoryResponse | null> {
|
||||
const resolvedDeviceId = String(params?.deviceId || '').trim() || (await resolveActiveBinding())?.device_id || ''
|
||||
if (!resolvedDeviceId) return null
|
||||
|
||||
const query = new URLSearchParams()
|
||||
if (params?.startAt) query.append('start_at', params.startAt)
|
||||
if (params?.endAt) query.append('end_at', params.endAt)
|
||||
if (params?.limit) query.append('limit', String(params.limit))
|
||||
|
||||
try {
|
||||
const suffix = query.toString() ? `?${query.toString()}` : ''
|
||||
return await request<DeviceTrajectoryResponse>(`/devices/${resolvedDeviceId}/trajectory${suffix}`)
|
||||
} catch (error: any) {
|
||||
if (error?.status === 404) return null
|
||||
throw error
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user