gerson
2025-02-09 d5846fc94573f1c2156230d9cdf2da45e4e12c46
src/utils/request.ts
@@ -1,41 +1,61 @@
import type { AxiosInstance, AxiosRequestConfig } from 'axios';
import type { AxiosInstance, AxiosRequestConfig, CreateAxiosDefaults } from 'axios';
import axios from 'axios';
import { ElMessage } from 'element-plus';
import { NO_AUTH_API_LIST } from '../api/ai/chat';
import { LOGIN_URL, TEL_LOGIN_URL } from '../api/ai/user';
import { Logger } from '../model/logger/Logger';
import emitter from './mitt';
import { debounce } from './util';
import { debounce, decodeFormData } from './util';
import { AUTH_URL, MAIN_URL, SECONDARY_URL } from '/@/constants';
import { Local, LoginInfo, Session } from '/@/utils/storage';
import { isSharePage } from '../stores/chatRoom';
// import JSONbig from 'json-bigint';
//#region ====================== 后端 res.err_code ======================
export const enum ErrorCode {
   /** @description 权限验证失败 */
   Message = 'MESSAGE',
   /** @description 内部错误 */
   Exception = 'EXCEPTION',
   /** @description 无权使用 */
   Auth = 'AUTH',
}
//#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;
};
export const SESSION_KEY = 'hswatersession';
export 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) {
            config.headers[SESSION_KEY] = accessSession;
         }
         if (!NO_AUTH_API_LIST.includes(config.url) && !isSharePage.value) {
            if (!accessSession && config.url !== LOGIN_URL && config.url !== TEL_LOGIN_URL) {
               handleNoAuth(config.url);
               throw '权限验证失败';
            }
         }
         return config;
      },
      (error) => {
@@ -69,22 +89,25 @@
         if (!serveData.json_ok) {
            switch (serveData?.err_code) {
               case ErrorCode.Auth:
                  if (res.config.url !== loginUrl) {
                  if (res.config.url !== LOGIN_URL && res.config.url !== TEL_LOGIN_URL && !isSharePage.value) {
                     handleNoAuth();
                     throw '权限验证失败';
                  }
                  break;
               case ErrorCode.Exception:
                  const param = res.config.data ? `\n    请求参数:${JSON.stringify(decodeFormData(res.config.data))}\n` : '';
                  ElMessage.error('内部错误!');
                  Logger.error(`${res.config.url} 响应失败${param}`, serveData?.json_msg && new Error(serveData?.json_msg));
                  return res.data;
            }
            const msg = serveData.json_msg ?? '';
            const error = serveData?.err_code ? `${msg ? `【${serveData.err_code}】` : serveData.err_code}` : '';
            const tip = error + msg || '请求失败';
            ElMessage.error(tip);
            const url = res.request.responseURL;
            throw new Error(url + '\n' + tip);
         }
         return res.data;
      },
      (error) => {
         if (typeof error === 'string') {
            // ElMessage.error(error);
            return Promise.reject(error);
         }
         // 处理响应错误
         if (error.response) {
            if (error.response.status === 401) {
@@ -106,18 +129,68 @@
   );
};
// 配置新建一个 axios 实例
const createAxiosInstance = () => {
const createAxiosInstance = (option: Partial<CreateAxiosDefaults<any>> = {}) => {
   return axios.create({
      baseURL: MAIN_URL,
      timeout: 50000,
      headers: { 'Content-Type': 'application/json;charset=utf-8 ' },
      timeout: 1200000,
      headers: {
         'Content-Type': 'application/x-www-form-urlencoded',
      },
      ...option,
   });
};
const service = createAxiosInstance();
const service = createAxiosInstance();
export const mainRequest = service;
//#region ====================== 流响应数据 ======================
const streamInstance = createAxiosInstance({
   adapter: 'fetch',
   responseType: 'stream',
});
const decoder = new TextDecoder();
const readStream = async (stream: ReadableStream, cb: (value) => void): Promise<any> => {
   const reader = stream.getReader();
   let lastValue = '';
   const p = new Promise(async (resolve, reject) => {
      let fullValue = '';
      while (1) {
         const { done, value } = await reader.read();
         if (done) {
            break;
         }
         // const txt = decoder.decode(Uint8Array.from([...lastValue, ...value]));
         const txt = decoder.decode(value);
         const txtArr = txt.split('\n');
         txtArr[0] = lastValue + txtArr[0];
         txtArr.forEach((value, index, array) => {
            // 一般不会出现连续换行,只可能最后一个是换行
            if (index === array.length - 1) {
               lastValue = value;
            } else {
               const decodeValue = decodeURIComponent(value);
               fullValue += decodeValue;
               cb(decodeValue);
            }
         });
      }
      resolve(fullValue);
   });
   return p;
};
export const streamReq = async (config: AxiosRequestConfig<any>, callback: (value) => void) => {
   const response = await streamInstance(config);
   const stream = response as unknown as ReadableStream;
   return readStream(stream, (value) => {
      const jsonValue = JSON.parse(value);
      callback(jsonValue);
   });
};
//#endregion
initRequestInterceptor(service);
initRequestInterceptor(streamInstance);
export function secondaryRequest(config: AxiosRequestConfig<any>) {
   return service({
@@ -139,7 +212,7 @@
 * @description 域名前缀
 * 防止类似于 http://sqi.beng35.com/airp 和 http://sqi.beng35.com/test 公用同一个 token 或 userInfo
 */
export const getDomainPrefix = (win: Window) => {
export const getDomainPrefix = (win: Window = window) => {
   const subDomainName = win.location.pathname
      .split('/')
      .filter((item) => !!item)
@@ -148,15 +221,17 @@
   return domainPrefix;
};
export const domainPrefix = getDomainPrefix(window);
// token 键定义
export const sessionName = 'access-session';
export const userName = 'userName';
export const getSessionKey = (win: Window) => {
   return getDomainPrefix(win) + sessionName;
   return  sessionName;
};
export const getUserNameKey = (win: Window) => {
   return getDomainPrefix(win) + userName;
   return  userName;
};
export const accessSessionKey = getSessionKey(window);
@@ -165,8 +240,7 @@
export const refreshAccessTokenKey = `x-${accessSessionKey}`;
// userInfo键定义
export const userInfoKey = getDomainPrefix(window) + 'userInfo';
export const userInfoKey ='userInfo';
// 获取 token
export const getSession = () => {
   return Local.get(accessSessionKey);