新增小程序后端代码,包括数据库、路由、服务层等。
小程序前后端打通,包括登录、注册、绑定设备、查询绑定信息,修改小朋友名称等功能。
This commit is contained in:
75
banban-mini/src/services/auth.ts
Normal file
75
banban-mini/src/services/auth.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import { request, BASE_URL } from './api'
|
||||
|
||||
export interface Parent {
|
||||
user_id: number
|
||||
openid: string
|
||||
unionid?: string
|
||||
nickname?: string
|
||||
avatar_url?: string
|
||||
phone?: string
|
||||
status: number
|
||||
}
|
||||
|
||||
// 微信登录
|
||||
export async function wechatLogin(code: string): Promise<{ token: string; user_id: number }> {
|
||||
// TODO: 实现微信登录 - 通过 code 换取 openid,然后调用后端
|
||||
// 这里先返回 mock 数据
|
||||
const mockParent: Parent = {
|
||||
user_id: 1,
|
||||
openid: 'mock_openid_' + code,
|
||||
nickname: '微信用户',
|
||||
status: 1,
|
||||
}
|
||||
|
||||
// 保存模拟 token
|
||||
const token = 'mock_token_' + Date.now()
|
||||
Taro.setStorageSync('token', token)
|
||||
Taro.setStorageSync('user_id', mockParent.user_id)
|
||||
|
||||
return { token, user_id: mockParent.user_id }
|
||||
}
|
||||
|
||||
// 创建家长
|
||||
export async function createParent(data: {
|
||||
openid: string
|
||||
unionid?: string
|
||||
nickname?: string
|
||||
avatar_url?: string
|
||||
}): Promise<Parent> {
|
||||
return request<Parent>('/parents', {
|
||||
method: 'POST',
|
||||
data,
|
||||
})
|
||||
}
|
||||
|
||||
// 获取家长信息
|
||||
export async function getParent(userId: number): Promise<Parent> {
|
||||
return request<Parent>(`/parents/${userId}`)
|
||||
}
|
||||
|
||||
// 更新家长信息
|
||||
export async function updateParent(
|
||||
userId: number,
|
||||
data: { nickname?: string; avatar_url?: string; phone?: string }
|
||||
): Promise<Parent> {
|
||||
return request<Parent>(`/parents/${userId}`, {
|
||||
method: 'PATCH',
|
||||
data,
|
||||
})
|
||||
}
|
||||
|
||||
// 获取 token
|
||||
export function getToken(): string {
|
||||
return Taro.getStorageSync('token')
|
||||
}
|
||||
|
||||
// 清除 token
|
||||
export function clearToken(): void {
|
||||
Taro.removeStorageSync('token')
|
||||
Taro.removeStorageSync('user_id')
|
||||
}
|
||||
|
||||
// 获取当前用户ID
|
||||
export function getCurrentUserId(): number {
|
||||
return Taro.getStorageSync('user_id') || 0
|
||||
}
|
||||
Reference in New Issue
Block a user