You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
1.8 KiB
86 lines
1.8 KiB
|
1 month ago
|
/**
|
||
|
|
* 用户服务
|
||
|
|
* 处理用户信息相关的 API 请求
|
||
|
|
*/
|
||
|
|
|
||
|
1 month ago
|
import { request } from '@/utils/network/api';
|
||
|
|
import type { User, UpdateProfileFormData } from '@/schemas/user';
|
||
|
1 month ago
|
|
||
|
|
/**
|
||
|
|
* API 响应接口
|
||
|
|
*/
|
||
|
|
interface ApiResponse<T = any> {
|
||
|
|
code: number;
|
||
|
|
message: string;
|
||
|
|
data: T;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 用户服务类
|
||
|
|
*/
|
||
|
|
class UserService {
|
||
|
|
/**
|
||
|
|
* 获取当前用户信息
|
||
|
|
*/
|
||
|
|
async getCurrentUser(): Promise<User> {
|
||
|
|
const response = await request.get<ApiResponse<User>>('/user/me');
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取用户信息(通过 ID)
|
||
|
|
*/
|
||
|
|
async getUserById(userId: string): Promise<User> {
|
||
|
|
const response = await request.get<ApiResponse<User>>(`/user/${userId}`);
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 更新用户资料
|
||
|
|
*/
|
||
|
|
async updateProfile(data: UpdateProfileFormData): Promise<User> {
|
||
|
|
const response = await request.put<ApiResponse<User>>('/user/profile', data);
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 上传头像
|
||
|
|
*/
|
||
|
|
async uploadAvatar(file: File | Blob): Promise<{ url: string }> {
|
||
|
|
const formData = new FormData();
|
||
|
|
formData.append('avatar', file);
|
||
|
|
|
||
|
1 month ago
|
const response = await request.post<ApiResponse<{ url: string }>>('/user/avatar', formData, {
|
||
|
|
headers: {
|
||
|
|
'Content-Type': 'multipart/form-data',
|
||
|
|
},
|
||
|
|
});
|
||
|
1 month ago
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 绑定手机号
|
||
|
|
*/
|
||
|
|
async bindPhone(phone: string, code: string): Promise<void> {
|
||
|
|
await request.post('/user/bind-phone', { phone, code });
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 绑定邮箱
|
||
|
|
*/
|
||
|
|
async bindEmail(email: string, code: string): Promise<void> {
|
||
|
|
await request.post('/user/bind-email', { email, code });
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 注销账号
|
||
|
|
*/
|
||
|
|
async deleteAccount(): Promise<void> {
|
||
|
|
await request.delete('/user/account');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 导出单例
|
||
|
|
export const userService = new UserService();
|
||
|
|
export default userService;
|