Compare commits
No commits in common. "2f51cc464114853a839d35bb1f5254c9a41bdcf9" and "8f9a6371c4d60853bbf64ea9298ea4f30ef834e9" have entirely different histories.
2f51cc4641
...
8f9a6371c4
|
@ -1,103 +1,37 @@
|
||||||
import request from '@/utils/request'
|
import request from '@/utils/request'
|
||||||
import type { ApiResponse, PageResult } from './types'
|
import type { ApiResponse } from './types'
|
||||||
|
|
||||||
// 定义接口的请求参数类型
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 关键点说明:
|
|
||||||
* 类型定义:
|
|
||||||
* 使用 interface 定义接口的请求参数和响应数据类型
|
|
||||||
* 使用 Partial<T> 表示部分可选的类型
|
|
||||||
* 使用联合类型定义枚举值,如 0 | 1
|
|
||||||
* 请求方法:
|
|
||||||
* request.get<T>() - GET请求
|
|
||||||
* request.post<T>() - POST请求
|
|
||||||
* request.put<T>() - PUT请求
|
|
||||||
* 泛型参数 T 用于定义响应数据的类型
|
|
||||||
* 错误处理:
|
|
||||||
* 使用 try/catch 捕获请求错误
|
|
||||||
* 统一的错误提示
|
|
||||||
* 文件上传:
|
|
||||||
* 使用 FormData 处理文件上传
|
|
||||||
* 设置正确的 Content-Type
|
|
||||||
* 类型推导:
|
|
||||||
* TypeScript 会自动推导 API 响应的类型
|
|
||||||
* 编辑器会提供代码提示和类型检查
|
|
||||||
*/
|
|
||||||
|
|
||||||
export interface LoginParams {
|
export interface LoginParams {
|
||||||
phone: string
|
username: string
|
||||||
password: string
|
password: string
|
||||||
verifyCode?: string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 定义后端返回的用户信息类型
|
|
||||||
export interface UserInfo {
|
export interface UserInfo {
|
||||||
id: number
|
id: number
|
||||||
phone: string
|
username: string
|
||||||
nickname: string
|
nickname: string
|
||||||
avatar: string
|
avatar: string
|
||||||
gender: 0 | 1 // 0-女 1-男
|
roles: string[]
|
||||||
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 = {
|
export const userApi = {
|
||||||
// 手机号密码登录
|
// 登录
|
||||||
login(data: LoginParams) {
|
login(data: LoginParams) {
|
||||||
return request.post<ApiResponse<{ token: string }>>('/auth/login', data)
|
return request.post<ApiResponse<{ token: string }>>('/auth/login', data)
|
||||||
},
|
},
|
||||||
|
|
||||||
// 发送验证码
|
// 获取用户信息
|
||||||
sendVerifyCode(phone: string) {
|
getUserInfo() {
|
||||||
return request.post<ApiResponse<void>>('/auth/send-code', { phone })
|
return request.get<ApiResponse<UserInfo>>('/user/info')
|
||||||
},
|
|
||||||
|
|
||||||
// 获取当前登录用户信息
|
|
||||||
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 }) {
|
changePassword(data: { oldPassword: string; newPassword: string }) {
|
||||||
return request.post<ApiResponse<void>>('/user/change-password', data)
|
return request.post<ApiResponse>('/user/change-password', data)
|
||||||
|
},
|
||||||
|
|
||||||
|
// 更新用户信息
|
||||||
|
updateUserInfo(data: Partial<UserInfo>) {
|
||||||
|
return request.put<ApiResponse>('/user/info', data)
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue