wujingjing
2024-10-31 bb4170bb6bf4af9cd76f976b1460ea76d33b3bb9
src/utils/request.ts
@@ -1,15 +1,17 @@
import type { AxiosInstance, AxiosRequestConfig } from 'axios';
import type { AxiosInstance, AxiosRequestConfig, CancelToken } from 'axios';
import axios from 'axios';
import { ElLoadingService, ElMessage } from 'element-plus';
import router from '../router';
import { debounce } from './util';
import { debounce, decodeFormData } from './util';
import { AUTH_URL, MAIN_URL, SECONDARY_URL } from '/@/constants';
import { Local, Session } from '/@/utils/storage';
import { Logger } from '../model/logger/Logger';
// import JSONbig from 'json-bigint';
export type ExtraConfig = {
   loading?: boolean;
   noAuth?: boolean;
   handleFail?: boolean;
   cancelToken?: CancelToken | undefined;
};
export const checkAuth = () => {
   const session = Local.get(accessSessionKey);
@@ -37,14 +39,15 @@
   clearAccessTokens();
   window.location.reload();
});
window.handleNoAuth = handleNoAuth;
let requestNum = 0;
let loadingInstance: ReturnType<typeof ElLoadingService>;
const addLoading = () => {
   // 增加loading 如果pending请求数量等于1,弹出loading, 防止重复弹出
   // 增加loading 如果pending请求数量等于1,弹出loading, 防止重复弹出F
   requestNum++;
   if (requestNum == 1) {
       loadingInstance = ElLoadingService({
      loadingInstance = ElLoadingService({
         text: '加载中...',
         target: '.layout-parent',
      });
@@ -72,7 +75,7 @@
            config.headers['hswatersession'] = accessSession;
         }
         if (!noAuth) {
            if (!accessSession ) {
            if (!accessSession) {
               handleNoAuth();
            }
         }
@@ -111,18 +114,23 @@
            switch (serveData?.err_code) {
               case ErrorCode.Auth:
                  handleNoAuth();
                     throw '权限验证失败';
                  break;
                  throw '权限验证失败';
               case ErrorCode.Exception:
                  const param = res.config.data ? `\n    请求参数:${JSON.stringify(decodeFormData(res.config.data))}\n` : '';
                  ElMessage.error('内部错误!');
                  throw '内部错误';
                  Logger.error(`${res.config.url} 响应失败${param}`, serveData?.json_msg && new Error(serveData?.json_msg));
                  throw '';
                  return res.data;
            }
            // 非 message error,且 handleFail 为 true
            // message error 不需要处理
            if (handleFail && serveData?.err_code !== ErrorCode.Message) {
               const errorText = serveData?.json_msg || '响应失败!';
               const errorText = serveData?.json_msg;
               ElMessage.error(errorText);
               throw errorText;
               const param = res.config.data ? `\n    请求参数:${JSON.stringify(decodeFormData(res.config.data))}\n` : '';
               Logger.error(`${res.config.url} 响应失败${param}`, errorText && new Error(errorText));
               throw '';
            }
         }
         return res.data;