auth.ts 3.0 KB

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