79 lines
1.9 KiB
TypeScript
79 lines
1.9 KiB
TypeScript
import request from '@/utils/request'
|
|
import type { ApiResponse, PageResult } from './types'
|
|
|
|
// 定义接口的请求参数类型
|
|
export interface LoginParams {
|
|
phone: string
|
|
password: string
|
|
verifyCode?: string
|
|
}
|
|
|
|
// 定义后端返回的用户信息类型
|
|
export interface UserInfo {
|
|
id: number
|
|
phone: string
|
|
nickname: string
|
|
avatar: string
|
|
gender: 0 | 1 // 0-女 1-男
|
|
age: number
|
|
address: string
|
|
emergencyContact: {
|
|
name: string
|
|
phone: string
|
|
relation: string
|
|
}
|
|
}
|
|
|
|
// 定义用户列表查询参数
|
|
export interface UserQueryParams {
|
|
page: number
|
|
pageSize: number
|
|
nickname?: string
|
|
phone?: string
|
|
gender?: 0 | 1
|
|
ageRange?: [number, number]
|
|
}
|
|
|
|
// 用户API接口
|
|
export const userApi = {
|
|
// 手机号密码登录
|
|
login(data: LoginParams) {
|
|
return request.post<ApiResponse<{ token: string }>>('/auth/login', data)
|
|
},
|
|
|
|
// 发送验证码
|
|
sendVerifyCode(phone: string) {
|
|
return request.post<ApiResponse<void>>('/auth/send-code', { phone })
|
|
},
|
|
|
|
// 获取当前登录用户信息
|
|
getCurrentUser() {
|
|
return request.get<ApiResponse<UserInfo>>('/user/current')
|
|
},
|
|
|
|
// 更新用户信息
|
|
updateUserInfo(data: Partial<UserInfo>) {
|
|
return request.put<ApiResponse<UserInfo>>('/user/info', data)
|
|
},
|
|
|
|
// 更新用户头像
|
|
updateAvatar(file: File) {
|
|
const formData = new FormData()
|
|
formData.append('avatar', file)
|
|
return request.post<ApiResponse<{ avatarUrl: string }>>('/user/avatar', formData, {
|
|
headers: {
|
|
'Content-Type': 'multipart/form-data'
|
|
}
|
|
})
|
|
},
|
|
|
|
// 分页获取用户列表(管理员接口)
|
|
getUserList(params: UserQueryParams) {
|
|
return request.get<ApiResponse<PageResult<UserInfo>>>('/admin/users', { params })
|
|
},
|
|
|
|
// 修改密码
|
|
changePassword(data: { oldPassword: string; newPassword: string }) {
|
|
return request.post<ApiResponse<void>>('/user/change-password', data)
|
|
}
|
|
}
|