新增小程序后端代码,包括数据库、路由、服务层等。
小程序前后端打通,包括登录、注册、绑定设备、查询绑定信息,修改小朋友名称等功能。
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
import { request } from './api'
|
||||
|
||||
export interface Binding {
|
||||
device_id: string
|
||||
child_id: number
|
||||
status: number
|
||||
bound_at: string
|
||||
}
|
||||
|
||||
export interface BindStartResponse {
|
||||
bind_token: string
|
||||
expires_at: string
|
||||
}
|
||||
|
||||
export interface BindHistoryItem {
|
||||
device_id: string
|
||||
child_id: number
|
||||
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 }> {
|
||||
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 }> {
|
||||
return request('/bindings/direct', {
|
||||
method: 'POST',
|
||||
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()}`)
|
||||
}
|
||||
Reference in New Issue
Block a user