149 lines
3.5 KiB
TypeScript
149 lines
3.5 KiB
TypeScript
/**
|
|
* 认证相关API接口
|
|
*/
|
|
|
|
import { post } from '../utils/request'
|
|
import type { ApiResponse } from '../utils/request'
|
|
import { md5 } from '../utils/md5'
|
|
|
|
// ==================== 类型定义 ====================
|
|
|
|
// 用户信息类型
|
|
export type UserInfo = {
|
|
userId: string
|
|
phone: string
|
|
nickname: string
|
|
avatar: string
|
|
token: string
|
|
refreshToken: string
|
|
}
|
|
|
|
// 登录请求参数
|
|
export type LoginParams = {
|
|
phone: string
|
|
password: string
|
|
remember?: boolean
|
|
}
|
|
|
|
// 验证码登录请求参数
|
|
export type LoginByCodeParams = {
|
|
phone: string
|
|
code: string
|
|
}
|
|
|
|
// 发送验证码请求参数
|
|
export type SendCodeParams = {
|
|
phone: string
|
|
type: 'login' | 'register' | 'forgot-password'
|
|
}
|
|
|
|
// 发送验证码响应
|
|
export type SendCodeResponse = {
|
|
expireTime: number
|
|
}
|
|
|
|
// 注册请求参数
|
|
export type RegisterParams = {
|
|
parentName: string
|
|
phone: string
|
|
password: string
|
|
relationship: string
|
|
students: string[] // 学生ID列表
|
|
}
|
|
|
|
// 验证验证码请求参数
|
|
export type VerifyCodeParams = {
|
|
phone: string
|
|
code: string
|
|
}
|
|
|
|
// 验证验证码响应
|
|
export type VerifyCodeResponse = {
|
|
token: string
|
|
}
|
|
|
|
// 重置密码请求参数
|
|
export type ResetPasswordParams = {
|
|
phone: string
|
|
newPassword: string
|
|
resetToken: string
|
|
}
|
|
|
|
// 刷新Token请求参数
|
|
export type RefreshTokenParams = {
|
|
refreshToken: string
|
|
}
|
|
|
|
// 刷新Token响应
|
|
export type RefreshTokenResponse = {
|
|
token: string
|
|
refreshToken: string
|
|
}
|
|
|
|
// ==================== API方法 ====================
|
|
|
|
/**
|
|
* 密码登录
|
|
*/
|
|
export function apiLogin(params: LoginParams): Promise<ApiResponse<UserInfo> | null> {
|
|
return post<any>('/api/v1/Parents/Login', { phone: params.phone, password: md5(params.password), loginType: 0 }).then((res) => {
|
|
if (!res || !res.data) return null
|
|
return { code: res.code, message: res.message, data: { userId: String(res.data.parentId), phone: res.data.phone, nickname: res.data.name, avatar: '', token: res.data.token, refreshToken: '' } }
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 验证码登录
|
|
*/
|
|
export function apiLoginByCode(params: LoginByCodeParams): Promise<ApiResponse<UserInfo> | null> {
|
|
return post<UserInfo>('/api/auth/login-by-code', params)
|
|
}
|
|
|
|
/**
|
|
* 发送验证码
|
|
*/
|
|
export function apiSendCode(params: SendCodeParams): Promise<ApiResponse<SendCodeResponse> | null> {
|
|
return post<SendCodeResponse>('/api/auth/send-code', params)
|
|
}
|
|
|
|
/**
|
|
* 注册
|
|
*/
|
|
export function apiRegister(params: RegisterParams): Promise<ApiResponse<UserInfo> | null> {
|
|
const relations = ['爸爸', '妈妈', '爷爷', '奶奶', '外公', '外婆', '其他亲属']
|
|
return post<any>('/api/v1/Parents/Bind', {
|
|
name: params.parentName,
|
|
phone: params.phone,
|
|
password: params.password,
|
|
students: params.students.map((id) => ({ userId: Number(id), relation: relations.indexOf(params.relationship) + 1 }))
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 验证验证码(用于忘记密码流程)
|
|
*/
|
|
export function apiVerifyCode(params: VerifyCodeParams): Promise<ApiResponse<VerifyCodeResponse> | null> {
|
|
return post<VerifyCodeResponse>('/api/auth/forgot-password/verify', params)
|
|
}
|
|
|
|
/**
|
|
* 重置密码
|
|
*/
|
|
export function apiResetPassword(params: ResetPasswordParams): Promise<ApiResponse<null> | null> {
|
|
return post<null>('/api/auth/forgot-password/reset', params)
|
|
}
|
|
|
|
/**
|
|
* 刷新Token
|
|
*/
|
|
export function apiRefreshToken(params: RefreshTokenParams): Promise<ApiResponse<RefreshTokenResponse> | null> {
|
|
return post<RefreshTokenResponse>('/api/auth/refresh-token', params)
|
|
}
|
|
|
|
/**
|
|
* 退出登录
|
|
*/
|
|
export function apiLogout(): Promise<ApiResponse<null> | null> {
|
|
return post<null>('/api/v1/Parents/Logout')
|
|
}
|