| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import axios from 'axios';
- import { kuky_Authorization } from './storage';
- import replaceProxyUrl from './ajax/replace-proxy-url';
- import { backToLogin } from './ajax/back-to-login';
- const ajax = axios.create({
- withCredentials: true, // 跨域发送 cookie
- timeout: 300000, // 请求超时
- headers: {
- 'Content-Type': 'application/json'
- }
- });
- ajax.interceptors.request.use(
- (config) => {
- replaceProxyUrl(config);
- const token = kuky_Authorization.get();
- if (token) {
- config.headers['Authorization'] = `Bearer ${token}`;
- }
- return config;
- },
- (error) => {
- // 请求出错
- return Promise.reject(error);
- }
- );
- ajax.interceptors.response.use(
- (response) => {
- const res_data = response.data;
- const { code, data, msg } = res_data;
- if (res_data.code == '401' || res_data.retcode == '401') {
- // 回到登录页面
- backToLogin();
- }
- return res_data;
- },
- (error) => {
- if (error.message.includes('401')) {
- backToLogin();
- }
- if (error.status === 401) {
- backToLogin();
- }
- return Promise.reject(error);
- }
- );
- export default ajax;
|