import { request } from './api' export interface Binding { device_id: string child_id: number | null status: number bound_at: string } export interface BindStartResponse { bind_token: string expires_at: string } export interface BindHistoryItem { device_id: string child_id: number | null bound_at: string unbound_at?: string } export interface BindHistoryResponse { items: BindHistoryItem[] total: number next_cursor?: string } // 获取当前绑定信息 export async function getCurrentBinding(): Promise { try { return await request('/bindings/current') } catch (err: any) { if (err.status === 404) { return null } throw err } } // 开始绑定设备 export async function startBind(data: { device_id: string child_id?: number }): Promise { return request('/bindings/start', { method: 'POST', data, }) } // 确认绑定设备 export async function confirmBind(data: { bind_token: string challenge_code: string }): Promise<{ device_id: string; child_id: number | null }> { return request('/bindings/confirm', { method: 'POST', data, }) } // 获取设备绑定信息 export async function getBinding(deviceId: string): Promise { // If no deviceId, get the first binding for this user if (!deviceId) { try { // Try to get binding list - for now return null if no specific device return null } catch { return null } } return request(`/bindings/${deviceId}`) } // 解绑设备 export async function unbindDevice(deviceId: string): Promise { return request(`/bindings/${deviceId}`, { method: 'DELETE', }) } // 直接绑定设备(手动输入设备码) export async function directBind(data: { device_id: string child_id?: number }): Promise<{ device_id: string; child_id: number | null }> { return request('/bindings/direct', { method: 'POST', data, }) } // Bind child profile after device has been bound to parent account. export async function setBindingChild( deviceId: string, data: { child_id: number } ): Promise<{ device_id: string; child_id: number | null }> { return request(`/bindings/${deviceId}/child`, { method: 'PATCH', data, }) } // 获取绑定历史 export async function getBindHistory( deviceId: string, cursor?: string, limit: number = 20 ): Promise { const params = new URLSearchParams() if (cursor) params.append('cursor', cursor) params.append('limit', String(limit)) return request(`/bindings/history/${deviceId}?${params.toString()}`) }