| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- import axios from 'axios';
- import { notification } from 'ant-design-vue';
- import Common from '../common/Common.js';
- // 创建 axios 实例
- const request = axios.create({
- // API 请求的默认前缀
- baseURL: '/',
- timeout: 60000, // 请求超时时间
- });
- // 异常拦截处理器
- const errorHandler = error => {
- console.log(error);
- if (error.response) {
- const data = error.response.data;
- // 从 localstorage 获取 token
- const token = localStorage.getItem('#token');
- if (error.response.status === 403) {
- notification.error({
- message: 'Forbidden',
- description: data.message,
- });
- }
- if (error.response.status === 401 && !(data.result && data.result.isLogin)) {
- console.log('401');
- notification.error({
- message: 'Unauthorized',
- description: 'Authorization verification failed',
- });
- if (token) {
- // store.dispatch('Logout').then(() => {
- // setTimeout(() => {
- // window.location.reload();
- // }, 1500);
- // });
- }
- }
- }
- return Promise.reject(error);
- };
- // request interceptor
- request.interceptors.request.use(config => {
- const token = localStorage.getItem('#token');
- // 如果 token 存在
- // 让每个请求携带自定义 token 请根据实际情况自行修改
- if (token) {
- config.headers['token'] = token;
- }
- return config;
- }, errorHandler);
- // 异常拦截处理器
- const responseErrorHandler = error => {
- console.log(error);
- if (error.response.status === 401) {
- const currentUrl = window.location.href;
- if (currentUrl.indexOf('login') < 0 && currentUrl.indexOf('redirectUrl=') < 0) {
- // window.location = Common.getRedirectUrl('#/login?redirectUrl=' + encodeURIComponent(currentUrl));
- const isOut = localStorage.getItem('isOut');
- console.log(isOut, '9999999999999');
- if (isOut == 'true') {
- window.location = Common.getRedirectUrl('#/login?isOut=true&redirectUrl=' + encodeURIComponent(currentUrl));
- } else {
- window.location = Common.getRedirectUrl('#/login?isOut=false&redirectUrl=' + encodeURIComponent(currentUrl));
- }
- }
- }
- if (error.response.status === 504) {
- notification.error({
- message: '504',
- description: error.response.data,
- });
- }
- return Promise.reject(error);
- };
- // response interceptor
- request.interceptors.response.use(response => {
- return response.data;
- }, responseErrorHandler);
- export default request;
|