auth.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import axios from 'axios';
  2. import type { User } from '../../share/types.ts';
  3. // 从原api.ts导入基础配置
  4. const API_BASE_URL = '/api';
  5. // 定义API返回数据类型
  6. interface AuthLoginResponse {
  7. message: string;
  8. token: string;
  9. refreshToken?: string;
  10. user: User;
  11. }
  12. interface AuthResponse {
  13. message: string;
  14. [key: string]: any;
  15. }
  16. // 定义Auth API接口类型
  17. interface AuthAPIType {
  18. login: (username: string, password: string, latitude?: number, longitude?: number) => Promise<AuthLoginResponse>;
  19. register: (username: string, email: string, password: string) => Promise<AuthResponse>;
  20. logout: () => Promise<AuthResponse>;
  21. getCurrentUser: () => Promise<User>;
  22. updateUser: (userId: number, userData: Partial<User>) => Promise<User>;
  23. changePassword: (oldPassword: string, newPassword: string) => Promise<AuthResponse>;
  24. requestPasswordReset: (email: string) => Promise<AuthResponse>;
  25. resetPassword: (token: string, newPassword: string) => Promise<AuthResponse>;
  26. }
  27. // Auth相关API
  28. export const AuthAPI: AuthAPIType = {
  29. // 登录API
  30. login: async (username: string, password: string, latitude?: number, longitude?: number) => {
  31. try {
  32. const response = await axios.post(`${API_BASE_URL}/auth/login`, {
  33. username,
  34. password,
  35. latitude,
  36. longitude
  37. });
  38. return response.data;
  39. } catch (error) {
  40. throw error;
  41. }
  42. },
  43. // 注册API
  44. register: async (username: string, email: string, password: string) => {
  45. try {
  46. const response = await axios.post(`${API_BASE_URL}/auth/register`, { username, email, password });
  47. return response.data;
  48. } catch (error) {
  49. throw error;
  50. }
  51. },
  52. // 登出API
  53. logout: async () => {
  54. try {
  55. const response = await axios.post(`${API_BASE_URL}/auth/logout`);
  56. return response.data;
  57. } catch (error) {
  58. throw error;
  59. }
  60. },
  61. // 获取当前用户信息
  62. getCurrentUser: async () => {
  63. try {
  64. const response = await axios.get(`${API_BASE_URL}/auth/me`);
  65. return response.data;
  66. } catch (error) {
  67. throw error;
  68. }
  69. },
  70. // 更新用户信息
  71. updateUser: async (userId: number, userData: Partial<User>) => {
  72. try {
  73. const response = await axios.put(`${API_BASE_URL}/auth/users/${userId}`, userData);
  74. return response.data;
  75. } catch (error) {
  76. throw error;
  77. }
  78. },
  79. // 修改密码
  80. changePassword: async (oldPassword: string, newPassword: string) => {
  81. try {
  82. const response = await axios.post(`${API_BASE_URL}/auth/change-password`, { oldPassword, newPassword });
  83. return response.data;
  84. } catch (error) {
  85. throw error;
  86. }
  87. },
  88. // 请求重置密码
  89. requestPasswordReset: async (email: string) => {
  90. try {
  91. const response = await axios.post(`${API_BASE_URL}/auth/request-password-reset`, { email });
  92. return response.data;
  93. } catch (error) {
  94. throw error;
  95. }
  96. },
  97. // 重置密码
  98. resetPassword: async (token: string, newPassword: string) => {
  99. try {
  100. const response = await axios.post(`${API_BASE_URL}/auth/reset-password`, { token, newPassword });
  101. return response.data;
  102. } catch (error) {
  103. throw error;
  104. }
  105. }
  106. };