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.

91 lines
1.9 KiB

/**
*
* 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;