All files / src request.ts

51.42% Statements 36/70
100% Branches 0/0
100% Functions 0/0
51.42% Lines 36/70

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 711x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x 1x                                                       1x         1x 1x  
/**
 * @description axios请求封装
 */
import axios from 'axios';
// import { Message, Modal } from '@arco-design/web-react';
import config from '@/config/net.config';
// import { setting } from '@/config/setting';
 
// const { tokenName } = setting;
let tokenLose = true;
 
const { baseURL, successCode, invalidCode, requestTimeout, contentType } = config;
 
const instance = axios.create({
  baseURL,
  timeout: requestTimeout,
  headers: {
    'Content-Type': contentType
  }
});
 
// request interceptor
instance.interceptors.request.use(
  (configItem) => configItem,
  (error) =>
    // do something with request error
    Promise.reject(error)
);
 
// response interceptor
instance.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
   */
  (response) => {
    const res = response.data;
    // 请求出错处理
    // -1 超时、token过期或者没有获得授权
    if (res.code === invalidCode && tokenLose) {
      tokenLose = false;
      // Modal.confirm({
      //   title: '重新登录',
      //   content: '您已掉线,或者访问权限出错,请重新登录!',
      //   okText: '确定',
      //   cancelText: '取消',
      //   onOk: () => {
      //     // 重新登录
      //     tokenLose = true;
      //     window.location.reload();
      //   },
      //   onCancel: () => {
      //     tokenLose = true;
      //   }
      // });
    }

    if (successCode.indexOf(res.code) === -1) {
      // Message.error(res.msg);
      return Promise.reject(res);
    }
    return res;
  },
  (error) => {
    // Message.error('请求出错啦!');
    return Promise.reject(error);
  }
);
 
export default instance;