完善验证页面和后端接口的链接
> 1. 配置了开发环境的端口转发 -> localhost:8080\
> 2. 完成了注册,登录,忘记密码页的功能
> 3. 为项目配置了vitest测试框架
> 4. 对这三个页面进行了测试
> 重写了/test/setup.ts
Change-Id: I46c600ce06d698dae6953b2e1e3ff4a98b0f3de4
diff --git a/src/api/authApi.ts b/src/api/authApi.ts
index 3a8eb17..4a08c2d 100644
--- a/src/api/authApi.ts
+++ b/src/api/authApi.ts
@@ -1,78 +1,42 @@
-import axios from 'axios';
+import axios, { type AxiosResponse } from 'axios';
+import type { RejisterRequest , CommonResponse, ResetPasswordRequest} from './type';
+import type{ LoginRequest } from './type';
+class authAPI {
- class authAPI {
-
- // static getUserById(userId) {
- // // 例如 GET http://localhost:8080/123
- // return axios.get(`/${userId}`);
- // }
-
-
- // static updateUser(userId, data) {
- // // 例如 PUT http://localhost:8080/123 Body: { username: 'xxx', ... }
- // return axios.put(`/${userId}`, data);
- // }
- //
- //
- // static deleteUser(userId:string) {
- // // 例如 DELETE http://localhost:8080/123
- // return axios.delete(`/${userId}`);
- // }
-
-
- static sendVerificationCode(email: string) {
- // Body: { email: 'xxx@yyy.com'}
+ static sendVerificationCode(email: string): Promise<AxiosResponse<CommonResponse>> {
return axios.post('/api/sendVerification', { email });
}
+ static register(request: RejisterRequest): Promise<AxiosResponse<CommonResponse>> {
+ return axios.post('/api/register', request);
+ }
- static sendResetCode(email: string) {
- // Body: { email: 'xxx@yyy.com' }
+ static sendResetCode(email: string):Promise<AxiosResponse<CommonResponse>> {
return axios.post('/api/sendResetCode', { email });
}
- //
- // static resetPassword({ email, code, newPassword }) {
- // // Body: { email, code, newPassword }
- // return axios.post('/resetPassword', { email, code, newPassword });
- // }
- //
- //
- // static register({ username, email, verificationCode, password }) {
- // // Body: { username, email, verificationCode, password, identificationNumber? }
- // const body = {
- // username,
- // email,
- // verificationCode,
- // password,
- // };
- // return axios.post('/register', body);
- // }
- //
- // /**
- // * 刷新 JWT Token(POST /refreshToken)
- // * @param {string} oldToken 旧的 JWT(放在 header: token)
- // * @returns {Promise<AxiosResponse>}
- // */
- // static refreshToken(oldToken : string) {
- // // 因为后端是从 header 中读取旧 token,这里直接把 token 放进 headers
- // return axios.post(
- // '/refreshToken',
- // {}, // 请求体空
- // {
- // headers: {
- // token: oldToken,
- // },
- // }
- // );
- // }
- //
- //
- // static login({ email, password } : {email: string, password:string}) {
- // // Body: { email, password }
- // return axios.post('/login', { email, password });
- // }
+ static resetPassword( request: ResetPasswordRequest ):Promise<AxiosResponse<CommonResponse>> {
+ return axios.post('/api/resetPassword', request);
+ }
+
+
+ static refreshToken(oldToken : string): Promise<AxiosResponse<CommonResponse<string>>> {
+ return axios.post(
+ '/api/refreshToken',
+ {}, // 请求体空
+ {
+ headers: {
+ token: oldToken,
+ },
+ }
+ );
+ }
+
+
+ static login(loginRequest: LoginRequest): Promise<AxiosResponse<CommonResponse<string>>> {
+ return axios.post('/api/login', loginRequest);
+ }
}
diff --git a/src/api/interceptors.ts b/src/api/interceptors.ts
index f56a8d4..3945bc3 100644
--- a/src/api/interceptors.ts
+++ b/src/api/interceptors.ts
@@ -1,4 +1,4 @@
-import axios from "axios";
+import axios, { type AxiosResponse } from "axios";
// 为所有auth外请求添加token头
axios.interceptors.request.use((config) => {
@@ -14,4 +14,33 @@
return error;
} );
+
+// 统一响应拦截器
+axios.interceptors.response.use(
+ (response: AxiosResponse) => {
+ const { code, msg, data } = response.data;
+
+ return {
+ ...response, // 保留原本的响应信息
+ data: {
+ code,
+ message: msg,
+ data,
+ success: code === 0, // 根据 code 判断请求是否成功
+ },
+ };
+ },
+ (error) => {
+ return {
+ ...error.response, // 保留原本的错误响应信息
+ data: {
+ code: -1,
+ message: error.message || '请求失败',
+ data: null,
+ success: false,
+ },
+ };
+ }
+);
+
export default axios
\ No newline at end of file
diff --git a/src/api/type.ts b/src/api/type.ts
index a5dd1ca..c1acc22 100644
--- a/src/api/type.ts
+++ b/src/api/type.ts
@@ -2,9 +2,22 @@
email: string;
password: string;
}
-
-export interface LoginResponse {
- user: string;
- token: string;
- refreshToken: string;
-}
\ No newline at end of file
+
+export interface RejisterRequest {
+ username: string,
+ email: string,
+ verificationCode: string,
+ password: string,
+}
+
+export interface ResetPasswordRequest {
+ email: string,
+ code: string,
+ newPassword: string,
+}
+
+export interface CommonResponse<T= null> {
+ code: number;
+ message: string;
+ data: T;
+ }