- 修复路由配置:移除 top-level await,改用手动路由配置
- 修复响应拦截器:正确解包 { code, message, data } 格式的 API 响应
- 更新开发日志和变更日志,记录浏览器功能测试结果
- 添加教师端重构设计文档
修复的问题:
1. 登录功能无法正常工作(响应数据解包问题)
2. 页面无法加载(路由配置问题)
测试结果:
- 管理员登录: ✓ 成功
- 教师登录: ✓ 成功
- 主要页面导航: ✓ 正常
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
100 lines
2.6 KiB
TypeScript
100 lines
2.6 KiB
TypeScript
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
||
import { message } from 'ant-design-vue';
|
||
|
||
// 创建axios实例
|
||
const request: AxiosInstance = axios.create({
|
||
baseURL: '/api/v1', // 使用 /api/v1,代理会保留完整路径
|
||
timeout: 30000,
|
||
headers: {
|
||
'Content-Type': 'application/json',
|
||
},
|
||
});
|
||
|
||
// 请求拦截器
|
||
request.interceptors.request.use(
|
||
(config) => {
|
||
const token = localStorage.getItem('token');
|
||
if (token) {
|
||
config.headers.Authorization = `Bearer ${token}`;
|
||
}
|
||
return config;
|
||
},
|
||
(error) => {
|
||
return Promise.reject(error);
|
||
}
|
||
);
|
||
|
||
// 响应拦截器
|
||
request.interceptors.response.use(
|
||
(response: AxiosResponse) => {
|
||
const { data } = response;
|
||
// 如果是标准响应格式 { code, message, data },返回 data 字段
|
||
if (typeof data === 'object' && data !== null && 'code' in data && 'data' in data) {
|
||
return data.data;
|
||
}
|
||
// 否则直接返回响应数据
|
||
return data;
|
||
},
|
||
(error) => {
|
||
const { response } = error;
|
||
|
||
if (response) {
|
||
const { status, data } = response;
|
||
|
||
switch (status) {
|
||
case 401:
|
||
message.error('登录已过期,请重新登录');
|
||
localStorage.removeItem('token');
|
||
localStorage.removeItem('user');
|
||
localStorage.removeItem('role');
|
||
localStorage.removeItem('name');
|
||
window.location.href = '/login';
|
||
break;
|
||
case 403:
|
||
message.error('没有权限访问');
|
||
break;
|
||
case 404:
|
||
message.error('请求的资源不存在');
|
||
break;
|
||
case 500:
|
||
message.error('服务器错误');
|
||
break;
|
||
default:
|
||
message.error(data?.message || '请求失败');
|
||
}
|
||
} else if (error.code === 'ECONNABORTED') {
|
||
message.error('请求超时');
|
||
} else {
|
||
message.error('网络错误');
|
||
}
|
||
|
||
return Promise.reject(error);
|
||
}
|
||
);
|
||
|
||
// 导出请求方法
|
||
export default request;
|
||
|
||
// 通用请求方法
|
||
export const http = {
|
||
get<T = any>(url: string, config?: AxiosRequestConfig): Promise<T> {
|
||
return request.get(url, config);
|
||
},
|
||
|
||
post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> {
|
||
return request.post(url, data, config);
|
||
},
|
||
|
||
put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> {
|
||
return request.put(url, data, config);
|
||
},
|
||
|
||
delete<T = any>(url: string, config?: AxiosRequestConfig): Promise<T> {
|
||
return request.delete(url, config);
|
||
},
|
||
|
||
patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> {
|
||
return request.patch(url, data, config);
|
||
},
|
||
};
|