feat: new project

This commit is contained in:
2025-11-04 13:27:19 +08:00
commit 7526a9b827
49 changed files with 12520 additions and 0 deletions

134
src/services/authService.ts Normal file
View File

@@ -0,0 +1,134 @@
/**
* 认证服务
* 处理登录、注册等认证相关的 API 请求
*/
import { request } from '@/src/utils/api';
import type {
LoginFormData,
RegisterFormData,
ForgotPasswordFormData,
ResetPasswordFormData,
ChangePasswordFormData,
PhoneLoginFormData,
} from '@/src/schemas/auth';
import type { User } from '@/src/schemas/user';
/**
* API 响应接口
*/
interface ApiResponse<T = any> {
code: number;
message: string;
data: T;
}
/**
* 登录响应
*/
interface LoginResponse {
user: User;
token: string;
refreshToken?: string;
}
/**
* 认证服务类
*/
class AuthService {
/**
* 邮箱登录
*/
async login(data: LoginFormData): Promise<LoginResponse> {
const response = await request.post<ApiResponse<LoginResponse>>(
'/auth/login',
data
);
return response.data;
}
/**
* 手机号登录
*/
async phoneLogin(data: PhoneLoginFormData): Promise<LoginResponse> {
const response = await request.post<ApiResponse<LoginResponse>>(
'/auth/phone-login',
data
);
return response.data;
}
/**
* 注册
*/
async register(data: RegisterFormData): Promise<LoginResponse> {
const response = await request.post<ApiResponse<LoginResponse>>(
'/auth/register',
data
);
return response.data;
}
/**
* 登出
*/
async logout(): Promise<void> {
await request.post('/auth/logout');
}
/**
* 发送忘记密码邮件
*/
async forgotPassword(data: ForgotPasswordFormData): Promise<void> {
await request.post('/auth/forgot-password', data);
}
/**
* 重置密码
*/
async resetPassword(data: ResetPasswordFormData): Promise<void> {
await request.post('/auth/reset-password', data);
}
/**
* 修改密码
*/
async changePassword(data: ChangePasswordFormData): Promise<void> {
await request.post('/auth/change-password', data);
}
/**
* 发送验证码
*/
async sendVerificationCode(phone: string): Promise<void> {
await request.post('/auth/send-code', { phone });
}
/**
* 刷新 token
*/
async refreshToken(refreshToken: string): Promise<{ token: string }> {
const response = await request.post<ApiResponse<{ token: string }>>(
'/auth/refresh-token',
{ refreshToken }
);
return response.data;
}
/**
* 验证 token 是否有效
*/
async verifyToken(): Promise<boolean> {
try {
await request.get('/auth/verify-token');
return true;
} catch {
return false;
}
}
}
// 导出单例
export const authService = new AuthService();
export default authService;

View 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;