You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
house/fount/utils/http.js

39 lines
1.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import axios from 'axios'; // 引入 axios 库,用于发送 HTTP 请求
import router from '@/router/router-static'; // 引入路由配置文件
import storage from '@/utils/storage'; // 引入存储工具,用于操作本地存储
const http = axios.create({ // 创建一个自定义的 axios 实例
timeout: 1000 * 86400, // 设置请求超时时间为 1 天(单位:毫秒)
withCredentials: true, // 跨域请求时是否携带凭证(如 Cookie
baseURL: '/springboot08hr3', // 设置请求的基础 URL
headers: { // 设置默认请求头
'Content-Type': 'application/json; charset=utf-8' // 指定内容类型为 JSON
}
});
// 请求拦截器:在请求发送前进行处理
http.interceptors.request.use(
config => {
config.headers['Token'] = storage.get('Token'); // 从存储中获取 Token 并添加到请求头中
return config; // 返回配置对象
},
error => {
return Promise.reject(error); // 如果发生错误,返回拒绝的 Promise
}
);
// 响应拦截器:在接收到响应数据后进行处理
http.interceptors.response.use(
response => {
if (response.data && response.data.code === 401) { // 如果响应数据存在且状态码为 401Token 失效)
router.push({ name: 'login' }); // 跳转到登录页面
}
return response; // 返回响应数据
},
error => {
return Promise.reject(error); // 如果发生错误,返回拒绝的 Promise
}
);
export default http; // 导出自定义的 axios 实例,供其他模块使用