创建了api目录并拆分为10个功能模块文件
建立了index.ts统一导出 精简了原api.ts文件
This commit is contained in:
118
client/mobile/api/auth.ts
Normal file
118
client/mobile/api/auth.ts
Normal file
@@ -0,0 +1,118 @@
|
||||
import axios from 'axios';
|
||||
import type { User } from '../../share/types.ts';
|
||||
|
||||
// 从原api.ts导入基础配置
|
||||
const API_BASE_URL = '/api';
|
||||
|
||||
// 定义API返回数据类型
|
||||
interface AuthLoginResponse {
|
||||
message: string;
|
||||
token: string;
|
||||
refreshToken?: string;
|
||||
user: User;
|
||||
}
|
||||
|
||||
interface AuthResponse {
|
||||
message: string;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
// 定义Auth API接口类型
|
||||
interface AuthAPIType {
|
||||
login: (username: string, password: string, latitude?: number, longitude?: number) => Promise<AuthLoginResponse>;
|
||||
register: (username: string, email: string, password: string) => Promise<AuthResponse>;
|
||||
logout: () => Promise<AuthResponse>;
|
||||
getCurrentUser: () => Promise<User>;
|
||||
updateUser: (userId: number, userData: Partial<User>) => Promise<User>;
|
||||
changePassword: (oldPassword: string, newPassword: string) => Promise<AuthResponse>;
|
||||
requestPasswordReset: (email: string) => Promise<AuthResponse>;
|
||||
resetPassword: (token: string, newPassword: string) => Promise<AuthResponse>;
|
||||
}
|
||||
|
||||
// Auth相关API
|
||||
export const AuthAPI: AuthAPIType = {
|
||||
// 登录API
|
||||
login: async (username: string, password: string, latitude?: number, longitude?: number) => {
|
||||
try {
|
||||
const response = await axios.post(`${API_BASE_URL}/auth/login`, {
|
||||
username,
|
||||
password,
|
||||
latitude,
|
||||
longitude
|
||||
});
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
// 注册API
|
||||
register: async (username: string, email: string, password: string) => {
|
||||
try {
|
||||
const response = await axios.post(`${API_BASE_URL}/auth/register`, { username, email, password });
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
// 登出API
|
||||
logout: async () => {
|
||||
try {
|
||||
const response = await axios.post(`${API_BASE_URL}/auth/logout`);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
// 获取当前用户信息
|
||||
getCurrentUser: async () => {
|
||||
try {
|
||||
const response = await axios.get(`${API_BASE_URL}/auth/me`);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
// 更新用户信息
|
||||
updateUser: async (userId: number, userData: Partial<User>) => {
|
||||
try {
|
||||
const response = await axios.put(`${API_BASE_URL}/auth/users/${userId}`, userData);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
// 修改密码
|
||||
changePassword: async (oldPassword: string, newPassword: string) => {
|
||||
try {
|
||||
const response = await axios.post(`${API_BASE_URL}/auth/change-password`, { oldPassword, newPassword });
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
// 请求重置密码
|
||||
requestPasswordReset: async (email: string) => {
|
||||
try {
|
||||
const response = await axios.post(`${API_BASE_URL}/auth/request-password-reset`, { email });
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
// 重置密码
|
||||
resetPassword: async (token: string, newPassword: string) => {
|
||||
try {
|
||||
const response = await axios.post(`${API_BASE_URL}/auth/reset-password`, { token, newPassword });
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user