139 lines
2.9 KiB
TypeScript
139 lines
2.9 KiB
TypeScript
/**
|
|
* 认证相关API接口
|
|
*/
|
|
|
|
import { post } from '../utils/request'
|
|
import type { ApiResponse } from '../utils/request'
|
|
|
|
// ==================== 类型定义 ====================
|
|
|
|
// 用户信息类型
|
|
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<UserInfo>('/api/auth/login', params)
|
|
}
|
|
|
|
/**
|
|
* 验证码登录
|
|
*/
|
|
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> {
|
|
return post<UserInfo>('/api/auth/register', params)
|
|
}
|
|
|
|
/**
|
|
* 验证验证码(用于忘记密码流程)
|
|
*/
|
|
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/auth/logout')
|
|
}
|