gerson
2024-07-01 87bce04d7cdf5ca427757071e7cc3b0847b64dfc
src/utils/request.ts
@@ -1,8 +1,8 @@
import type { AxiosInstance, AxiosRequestConfig } from 'axios';
import axios from 'axios';
import { ElMessage } from 'element-plus';
import { Local, Session } from '/@/utils/storage';
import { AUTH_URL, MAIN_URL, SECONDARY_URL } from '/@/constants';
import { Local, Session } from '/@/utils/storage';
// import JSONbig from 'json-bigint';
//#region ====================== 后端 res.Code ======================
@@ -30,7 +30,7 @@
// //     超时(暂不使用)
// TimeOut = -5
//#endregion
const initRequestInterceptor = (request: AxiosInstance) => {
const initRequestInterceptor = (request: AxiosInstance,isAuth=false) => {
   // 添加请求拦截器
   request.interceptors.request.use(
      (config) => {
@@ -42,8 +42,15 @@
         // 获取本地的 token
         const accessToken = Local.get(accessTokenKey);
         if (accessToken) {
            // 将 token 添加到请求报文头中
            config.headers!['Authorization'] = `Bearer ${accessToken}`;
            // 将 token 添加到请求报文头中‘
            if(isAuth){
               config.headers!['Authorization'] = `Bearer ${accessToken}`;
            }else{
               config.headers['Referrer-Policy'] = undefined;
            }
            config.headers['hswatersession'] = Local.get('hswatersession');
            // 判断 accessToken 是否过期
            const jwt: any = decryptJWT(accessToken);
@@ -117,6 +124,12 @@
            Local.set(accessTokenKey, accessToken);
            Local.set(refreshAccessTokenKey, refreshAccessToken);
         }
         if (!serve.json_ok && !isAuth) {
            // ElMessage.warning(serve.json_msg)
            throw new Error('响应错误');
         }
         // 响应拦截及自定义处理
         if (serve.data === 401) {
            clearAccessTokens();
@@ -158,16 +171,17 @@
   baseURL: MAIN_URL,
   timeout: 50000,
   headers: { 'Content-Type': 'application/json;charset=utf-8 ' },
   // transformResponse: [
   //    function (data) {
   //       const JSONbigToString = JSONbig({ storeAsString: true });
   //       // 将Long类型数据转换为字符串
   //       return JSONbigToString.parse(data);
   //    },
   // ],
});
const authService = axios.create({
   // baseURL: MAIN_URL,
   timeout: 50000,
   headers: { 'Content-Type': 'application/json;charset=utf-8 ' },
});
initRequestInterceptor(service);
initRequestInterceptor(authService,true)
export function secondaryRequest(config: AxiosRequestConfig<any>) {
   return service({
@@ -180,7 +194,7 @@
 * 用于访问登录接口
 */
export function authRequest(config: AxiosRequestConfig<any>) {
   return service({
   return authService({
      ...config,
      baseURL: AUTH_URL,
   });