1. import axios from 'axios';
    2. // cancelToken.js
    3. // 声明一个 Map 用于存储每个请求的标识 和 取消函数
    4. const pending = new Map();
    5. /**
    6. * 添加请求
    7. * @param {Object} config
    8. */
    9. export const addPending = (config: any) => {
    10. const url = [config.method, config.url].join('&');
    11. config.cancelToken =
    12. config.cancelToken ||
    13. new axios.CancelToken((cancel: any) => {
    14. if (!pending.has(url)) {
    15. // 如果 pending 中不存在当前请求,则添加进去
    16. pending.set(url, cancel);
    17. }
    18. });
    19. };
    20. /**
    21. * 移除请求
    22. * @param {Object} config
    23. */
    24. export const removePending = (config: any) => {
    25. const url = [config.method, config.url].join('&');
    26. if (pending.has(url)) {
    27. // 如果在 pending 中存在当前请求标识,需要取消当前请求,并且移除
    28. const cancel = pending.get(url);
    29. cancel(url);
    30. pending.delete(url);
    31. }
    32. };
    33. /**
    34. * 清空 pending 中的请求(在路由跳转时调用)
    35. */
    36. export const clearPending = () => {
    37. // eslint-disable-next-line no-restricted-syntax
    38. for (const [url, cancel] of pending) {
    39. cancel(url);
    40. }
    41. pending.clear();
    42. };
    1. /* eslint-disable */
    2. import axios from 'axios';
    3. import {h} from 'vue'
    4. // import qs from 'qs';
    5. import { ElMessageBox } from 'element-plus';
    6. import globalData from './contant';
    7. import warningIcon from '@/assets/imgs/status_warning.png';
    8. import {addPending,removePending} from "./cancelToken";
    9. let showedMsgBox = false; // 默认messagebox未打开
    10. axios.defaults.withCredentials = true;
    11. // const isDemoPage = window.location.href.includes('/resource-center')
    12. // 请求拦截(配置发送请求的信息)
    13. axios.interceptors.request.use(
    14. config => {
    15. const {appName,versionName,versionCode, cuid, channel, version} = globalData.appInfo;
    16. // if(isDemoPage){
    17. // config.headers['X-SMART-DLGIN'] = '!ab@testemo'
    18. // }
    19. if (
    20. config.method === 'post' &&
    21. config.headers['content-type'] !== 'multipart/form-data'
    22. ) {
    23. config.data = config.data;
    24. }
    25. removePending(config) // 在请求开始前,对之前的请求做检查取消操作
    26. addPending(config) // 将当前请求添加到 pending 中
    27. return config;
    28. },
    29. error => {
    30. return Promise.reject(error);
    31. }
    32. );
    33. const showMessageBox = (message: string, fn?: Function) => {
    34. return (
    35. ElMessageBox({
    36. customClass: 'globalMsg',
    37. showClose: false,
    38. message: h('div',{ class: 'message-body-wrapper' }, [
    39. h('div', { class: 'message-title'}, [
    40. h('img', { class: 'icon', src: warningIcon }),
    41. h('span',{ class:'title' }, '提示')
    42. ]),
    43. h('div', { class: 'message-content' }, message),
    44. ]),
    45. confirmButtonText: '确定',
    46. closeOnClickModal: true,
    47. callback:(action:any) =>{
    48. fn && fn();
    49. }
    50. })
    51. )
    52. }
    53. /**
    54. * define error msg
    55. */
    56. const msgConst = new Map([
    57. [5002, '用户未登录'],
    58. [5006, '密码已修改,请重新登录'],
    59. [4000, '服务器异常, 请稍后重试'],
    60. ])
    61. // 响应拦截(配置请求回来的信息)
    62. axios.interceptors.response.use(
    63. res => {
    64. removePending(res) // 在请求结束后,移除本次请求
    65. if (res.headers['content-type'] === 'text/html') {
    66. return Promise.reject({
    67. errMsg: "Not Found",
    68. errNo: 404,
    69. data: null
    70. });
    71. }
    72. const resData: any = res.data;
    73. let errCode: number | undefined = Number(resData.errNo);
    74. if (errCode === 0) {
    75. return Promise.resolve(resData.data);
    76. }
    77. if (msgConst.has(errCode) && !showedMsgBox) {
    78. showedMsgBox = true;
    79. showMessageBox(msgConst.get(errCode) || '系统发现未知异常', () => showedMsgBox = false);
    80. }
    81. return Promise.reject(resData);
    82. },
    83. err => {
    84. console.log(err)
    85. if (axios.isCancel(err)) {//处理手动cancel
    86. console.log('这是手动cancel的')
    87. }
    88. return Promise.reject(err);
    89. }
    90. );
    91. export default axios;