yangyin
2024-07-19 611a76d81950b33224b7fa4e69b2c3a74b85ceb6
src/utils/request.ts
@@ -5,6 +5,8 @@
import { debounce } from './util';
import { AUTH_URL, MAIN_URL, SECONDARY_URL } from '/@/constants';
import { Local, LoginInfo, Session } from '/@/utils/storage';
import { LOGIN_URL } from '../api/ai/user';
import { NO_AUTH_API_LIST } from '../api/ai/chat';
// import JSONbig from 'json-bigint';
//#region ====================== 后端 res.err_code ======================
@@ -18,28 +20,41 @@
}
//#endregion
const handleNoAuth = debounce(() => {
const emitNoAuth = () => {
   emitter.emit('logout');
   emitter.emit('openLoginDlg');
};
export const handleNormalAuth = () => {
   const accessSession = Local.get(accessSessionKey);
   if (!accessSession) {
      emitter.emit('logout');
      emitter.emit('openLoginDlg');
   }
   return !!accessSession;
};
const handleNoAuth = debounce(() => {
   emitNoAuth();
});
const loginUrl = '/login';
const initRequestInterceptor = (request: AxiosInstance) => {
   // 添加请求拦截器
   request.interceptors.request.use(
      (config) => {
         // 获取本地的 token
         const accessSession = Local.get(accessSessionKey);
         if (accessSession) {
            // 将 token 添加到请求报文头中
            config.headers['hswatersession'] = accessSession;
         } else {
            if (config.url !== loginUrl) {
               handleNoAuth(config.url);
               throw '权限验证失败';
         if (!NO_AUTH_API_LIST.includes(config.url)) {
            if (accessSession) {
               // 将 token 添加到请求报文头中
               config.headers['hswatersession'] = accessSession;
            } else {
               if (config.url !== LOGIN_URL) {
                  handleNoAuth(config.url);
                  throw '权限验证失败';
               }
            }
         }
         return config;
      },
      (error) => {
@@ -73,7 +88,7 @@
         if (!serveData.json_ok) {
            switch (serveData?.err_code) {
               case ErrorCode.Auth:
                  if (res.config.url !== loginUrl) {
                  if (res.config.url !== LOGIN_URL) {
                     handleNoAuth();
                     throw '权限验证失败';
                  }
@@ -87,6 +102,10 @@
         return res.data;
      },
      (error) => {
         if (typeof error === 'string') {
            // ElMessage.error(error);
            return Promise.reject(error);
         }
         // 处理响应错误
         if (error.response) {
            if (error.response.status === 401) {