feat: new project
This commit is contained in:
90
src/services/userService.ts
Normal file
90
src/services/userService.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* 用户服务
|
||||
* 处理用户信息相关的 API 请求
|
||||
*/
|
||||
|
||||
import { request } from '@/src/utils/api';
|
||||
import type { User, UpdateProfileFormData } from '@/src/schemas/user';
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
const response = await request.post<ApiResponse<{ url: string }>>(
|
||||
'/user/avatar',
|
||||
formData,
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
},
|
||||
}
|
||||
);
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user