| | |
| | | // The axios configuration can be changed according to the project, just change the file, other files can be left unchanged |
| | | |
| | | import type { AxiosResponse } from 'axios'; |
| | | import { clone } from 'lodash-es'; |
| | | import type { RequestOptions, Result } from '/#/axios'; |
| | | import type { AxiosTransform, CreateAxiosOptions } from './axiosTransform'; |
| | | import { VAxios } from './Axios'; |
| | |
| | | import { useErrorLogStoreWithOut } from '/@/store/modules/errorLog'; |
| | | import { useI18n } from '/@/hooks/web/useI18n'; |
| | | import { joinTimestamp, formatRequestDate } from './helper'; |
| | | import { useUserStoreWithOut } from '/@/store/modules/user'; |
| | | import { AxiosRetry } from '/@/utils/http/axios/axiosRetry'; |
| | | |
| | | const globSetting = useGlobSetting(); |
| | | const urlPrefix = globSetting.urlPrefix; |
| | |
| | | switch (code) { |
| | | case ResultEnum.TIMEOUT: |
| | | timeoutMsg = t('sys.api.timeoutMessage'); |
| | | const userStore = useUserStoreWithOut(); |
| | | userStore.setToken(undefined); |
| | | userStore.logout(true); |
| | | break; |
| | | default: |
| | | if (message) { |
| | | timeoutMsg = message; |
| | |
| | | |
| | | // 请求之前处理config |
| | | beforeRequestHook: (config, options) => { |
| | | const { baseURL } = config; |
| | | const { apiUrl, joinPrefix, joinParamsToUrl, formatDate, joinTime = true } = options; |
| | | const { apiUrl, joinPrefix, joinParamsToUrl, formatDate, joinTime = true, urlPrefix } = options; |
| | | |
| | | if (joinPrefix) { |
| | | config.url = `${urlPrefix}${config.url}`; |
| | |
| | | if (apiUrl && isString(apiUrl)) { |
| | | config.url = `${apiUrl}${config.url}`; |
| | | } |
| | | if (baseURL && isString(baseURL)) { |
| | | config.url = `${baseURL}${config.url}`; |
| | | } |
| | | const params = config.params || {}; |
| | | const data = config.data || false; |
| | | formatDate && data && !isString(data) && formatRequestDate(data); |
| | | if (config.method?.toUpperCase() === RequestEnum.GET) { |
| | | if (!isString(params)) { |
| | | // 给 get 请求加上时间戳参数,避免从缓存中拿数据。 |
| | |
| | | } else { |
| | | if (!isString(params)) { |
| | | formatDate && formatRequestDate(params); |
| | | config.data = params; |
| | | config.params = undefined; |
| | | if (Reflect.has(config, 'data') && config.data && Object.keys(config.data).length > 0) { |
| | | config.data = data; |
| | | config.params = params; |
| | | } else { |
| | | // 非GET请求如果没有提供data,则将params视为data |
| | | config.data = params; |
| | | config.params = undefined; |
| | | } |
| | | if (joinParamsToUrl) { |
| | | config.url = setObjToUrlParams(config.url as string, config.data); |
| | | config.url = setObjToUrlParams( |
| | | config.url as string, |
| | | Object.assign({}, config.params, config.data), |
| | | ); |
| | | } |
| | | } else { |
| | | // 兼容restful风格 |
| | |
| | | requestInterceptors: (config, options) => { |
| | | // 请求之前处理config |
| | | const token = getToken(); |
| | | if (token) { |
| | | if (token && (config as Recordable)?.requestOptions?.withToken !== false) { |
| | | // jwt token |
| | | config.headers.Authorization = options.authenticationScheme |
| | | (config as Recordable).headers.Authorization = options.authenticationScheme |
| | | ? `${options.authenticationScheme} ${token}` |
| | | : token; |
| | | } |
| | |
| | | /** |
| | | * @description: 响应错误处理 |
| | | */ |
| | | responseInterceptorsCatch: (error: any) => { |
| | | responseInterceptorsCatch: (axiosInstance: AxiosResponse, error: any) => { |
| | | const { t } = useI18n(); |
| | | const errorLogStore = useErrorLogStoreWithOut(); |
| | | errorLogStore.addAjaxErrorInfo(error); |
| | |
| | | return Promise.reject(error); |
| | | } |
| | | } catch (error) { |
| | | throw new Error(error); |
| | | throw new Error(error as unknown as string); |
| | | } |
| | | |
| | | checkStatus(error?.response?.status, msg, errorMessageMode); |
| | | |
| | | // 添加自动重试机制 保险起见 只针对GET请求 |
| | | const retryRequest = new AxiosRetry(); |
| | | const { isOpenRetry } = config.requestOptions.retryRequest; |
| | | config.method?.toUpperCase() === RequestEnum.GET && |
| | | isOpenRetry && |
| | | // @ts-ignore |
| | | retryRequest.retry(axiosInstance, error); |
| | | return Promise.reject(error); |
| | | }, |
| | | }; |
| | |
| | | authenticationScheme: '', |
| | | timeout: 10 * 1000, |
| | | // 基础接口地址 |
| | | baseURL: globSetting.apiUrl, |
| | | // 接口可能会有通用的地址部分,可以统一抽取出来 |
| | | urlPrefix: urlPrefix, |
| | | // baseURL: globSetting.apiUrl, |
| | | |
| | | headers: { 'Content-Type': ContentTypeEnum.JSON }, |
| | | // 如果是form-data格式 |
| | | // headers: { 'Content-Type': ContentTypeEnum.FORM_URLENCODED }, |
| | | // 数据处理方式 |
| | | transform, |
| | | transform: clone(transform), |
| | | // 配置项,下面的选项都可以在独立的接口请求中覆盖 |
| | | requestOptions: { |
| | | // 默认将prefix 添加到url |
| | |
| | | errorMessageMode: 'message', |
| | | // 接口地址 |
| | | apiUrl: globSetting.apiUrl, |
| | | // 接口拼接地址 |
| | | urlPrefix: urlPrefix, |
| | | // 是否加入时间戳 |
| | | joinTime: true, |
| | | // 忽略重复请求 |
| | | ignoreCancelToken: true, |
| | | // 是否携带token |
| | | withToken: true, |
| | | retryRequest: { |
| | | isOpenRetry: true, |
| | | count: 5, |
| | | waitTime: 100, |
| | | }, |
| | | }, |
| | | }, |
| | | opt || {} |
| | | ) |
| | | opt || {}, |
| | | ), |
| | | ); |
| | | } |
| | | export const defHttp = createAxios(); |
| | |
| | | // export const otherHttp = createAxios({ |
| | | // requestOptions: { |
| | | // apiUrl: 'xxx', |
| | | // urlPrefix: 'xxx', |
| | | // }, |
| | | // }); |