Files
banban/banban-mini/src/services/binding.ts
T
ChengCan 1b4c47f77a feat: 支持先绑定设备后关联儿童档案
- 绑定模型新增 owner_user_id,支持 child_id 为空\n- 新增绑定后设置儿童接口 /bindings/{device_id}/child\n- 前端绑定页改为先绑设备后补充儿童\n- 补充数据库迁移脚本与 DAO 测试用例
2026-04-14 12:47:52 +08:00

117 lines
2.6 KiB
TypeScript

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<Binding | null> {
try {
return await request<Binding>('/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<BindStartResponse> {
return request<BindStartResponse>('/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<Binding | null> {
// 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<Binding>(`/bindings/${deviceId}`)
}
// 解绑设备
export async function unbindDevice(deviceId: string): Promise<void> {
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<BindHistoryResponse> {
const params = new URLSearchParams()
if (cursor) params.append('cursor', cursor)
params.append('limit', String(limit))
return request<BindHistoryResponse>(`/bindings/history/${deviceId}?${params.toString()}`)
}