import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios' import { ElMessage, ElMessageBox } from 'element-plus' import router from './router' export interface RequestInterceptors { requestInterceptors?: (config: AxiosRequestConfig) => AxiosRequestConfig requestInterceptorsCatch?: (err: T) => T responseInterceptors?: (config: T) => T responseInterceptorsCatch?: (err: T) => T } const noop = (_: T) => _ const rejectNoop = (_: T) => Promise.reject(_) export interface RequestConfig extends AxiosRequestConfig { interceptors?: RequestInterceptors } class AxiosRquest { instance: AxiosInstance interceptorsObj?: RequestInterceptors constructor (config: RequestConfig) { this.instance = axios.create({ baseURL: '/', method: 'POST', timeout: 30000, responseType: 'json', withCredentials: true, ...config }) this.interceptorsObj = config.interceptors AxiosRquest.globalInterceptors.request.forEach(({ onFulfilled, onRejected }) => { this.instance.interceptors.request.use( onFulfilled ?? noop, onRejected ?? noop ) }) this.instance.interceptors.request.use( this.interceptorsObj?.requestInterceptors, this.interceptorsObj?.requestInterceptorsCatch ) this.instance.interceptors.response.use( this.interceptorsObj?.responseInterceptors, this.interceptorsObj?.responseInterceptorsCatch ) AxiosRquest.globalInterceptors.response.forEach(({ onFulfilled, onRejected }) => { this.instance.interceptors.response.use( onFulfilled ?? noop, onRejected ?? rejectNoop ) }) } static globalInterceptors: { request: Array<{ onFulfilled?: (config: AxiosRequestConfig) => AxiosRequestConfig, onRejected?: (err: T) => T }>, response: Array<{ onFulfilled?: (config: AxiosResponse) => AxiosResponse|Promise, onRejected?: (err: T) => T }> } = { request: [], response: [] } // eslint-disable-next-line @typescript-eslint/no-explicit-any static registerGlobalRequestInterceptors (onFulfilled?: (config: AxiosRequestConfig) => AxiosRequestConfig, onRejected?: (error: any) => any) { AxiosRquest.globalInterceptors.request.push({ onFulfilled, onRejected }) } // eslint-disable-next-line @typescript-eslint/no-explicit-any static registerGlobalResponseInterceptors (onFulfilled?: (config: AxiosResponse) => AxiosResponse|Promise>, onRejected?: (error: any) => any) { AxiosRquest.globalInterceptors.response.push({ onFulfilled, onRejected }) } request (config: AxiosRequestConfig) { return this.instance.request<{ body: T }>(config).then(res => { return res.data.body }) } } AxiosRquest.registerGlobalRequestInterceptors((config) => { // 处理报文结构 config.data = { header: { // 服务版本号 version: '1.0.0', // 通讯类型 commType: '', // 交易编码 transCode: config.url, // 源系统IP srcIP: '', // 请求方渠道分类 channelType: '', // 请求方渠道标识 channelNo: '', // 源系统ID srcSystemId: '', // 原系统设备号 srcSystemDevId: '', // 响应报文编码格式 charset: 'json', // 响应数据格式 respFormat: 'UTF-8', // 请求流水号 reqNo: '', // 安全标识 securityFlag: '', // mac macCode: '', // mac值 macValue: '', // 交易时间 transTime: new Date().getTime(), // 全局流水号 globalFlowNo: '' }, body: { ...config.data } } return config }) interface ResponseData { header: { errorCode: string, errorMsg: string, }, body: { errorCode?: string, errorMsg?: string, [key: string]: unknown } } AxiosRquest.registerGlobalResponseInterceptors(response => { return new Promise>((resolve, reject) => { const { header, body } = response.data ?? {} let { errorCode = '', errorMsg = '' } = header ?? {} if (!errorCode) { errorCode = body?.errorCode ?? '' errorMsg = body?.errorMsg ?? '' } if (errorCode !== '0') { const needLinkLoginErrorMsg = { EBLN1000: '登陆超时,请重新登陆!', EBLN1001: '该用户已在其他终端登录,系统自动退出!', 20100: '长时间无操作,系统自动退出登录!' }[errorCode] if (needLinkLoginErrorMsg) { ElMessageBox.alert(needLinkLoginErrorMsg) .finally(() => { router.push('/login') }) } // eslint-disable-next-line prefer-promise-reject-errors reject({ errorCode, errorMsg }) } resolve(response) }) }, error => { const errorCode = `${error.response.status}` const errorMsg = error.message // 异常错误默认处理 ElMessage.error(errorMsg) // eslint-disable-next-line prefer-promise-reject-errors return Promise.reject({ errorCode, errorMsg }) }) export default AxiosRquest