¶Ô±ÈÐÂÎļþ |
| | |
| | | // axiosé
ç½® å¯èªè¡æ ¹æ®é¡¹ç®è¿è¡æ´æ¹ï¼åªéæ´æ¹è¯¥æä»¶å³å¯ï¼å
¶ä»æä»¶å¯ä»¥ä¸å¨ |
| | | // 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 { VAxios } from './Axios' |
| | | import type { AxiosTransform, CreateAxiosOptions } from './axiosTransform' |
| | | import { checkStatus } from './checkStatus' |
| | | import { formatRequestDate, joinTimestamp } from './helper' |
| | | import type { RequestOptions, Result } from '/#/axios' |
| | | import { ConfigEnum, ContentTypeEnum, RequestEnum, ResultEnum } from '/@/enums/httpEnum' |
| | | import { useGlobSetting } from '/@/hooks/setting' |
| | | import { useI18n } from '/@/hooks/web/useI18n' |
| | | import { useMessage } from '/@/hooks/web/useMessage' |
| | | import { router } from '/@/router' |
| | | import { useErrorLogStoreWithOut } from '/@/store/modules/errorLog' |
| | | import { useUserStoreWithOut } from '/@/store/modules/user' |
| | | import { deepMerge, setObjToUrlParams } from '/@/utils' |
| | | import { getTenantId, getToken } from '/@/utils/auth' |
| | | import signMd5Utils from '/@/utils/encryption/signMd5Utils' |
| | | import { isString } from '/@/utils/is' |
| | | const globSetting = useGlobSetting() |
| | | const urlPrefix = globSetting.urlPrefix |
| | | const { createMessage, createErrorModal } = useMessage() |
| | | |
| | | /** |
| | | * @description: æ°æ®å¤çï¼æ¹ä¾¿åºåå¤ç§å¤çæ¹å¼ |
| | | */ |
| | | const transform: AxiosTransform = { |
| | | /** |
| | | * @description: å¤çè¯·æ±æ°æ®ãå¦ææ°æ®ä¸æ¯é¢ææ ¼å¼ï¼å¯ç´æ¥æåºé误 |
| | | */ |
| | | transformRequestHook: (res: AxiosResponse<Result>, options: RequestOptions) => { |
| | | //console.log(`output->res`, res) |
| | | //console.log(`output->options`, options) |
| | | const { t } = useI18n() |
| | | const { isTransformResponse, isReturnNativeResponse } = options |
| | | // æ¯å¦è¿ååçååºå¤´ æ¯å¦ï¼éè¦è·åååºå¤´æ¶ä½¿ç¨è¯¥å±æ§ |
| | | if (isReturnNativeResponse) { |
| | | return res |
| | | } |
| | | // ä¸è¿è¡ä»»ä½å¤çï¼ç´æ¥è¿å |
| | | // ç¨äºé¡µé¢ä»£ç å¯è½éè¦ç´æ¥è·åcodeï¼dataï¼messageè¿äºä¿¡æ¯æ¶å¼å¯ |
| | | if (!isTransformResponse) { |
| | | return res.data |
| | | } |
| | | // éè¯¯çæ¶åè¿å |
| | | |
| | | const { data } = res |
| | | if (!data) { |
| | | // return '[HTTP] Request has no return value'; |
| | | throw new Error(t('sys.api.apiRequestFailed')) |
| | | } |
| | | // è¿é codeï¼resultï¼message为 åå°ç»ä¸çåæ®µï¼éè¦å¨ types.tså
ä¿®æ¹ä¸ºé¡¹ç®èªå·±çæ¥å£è¿åæ ¼å¼ |
| | | const { code, result, message, success } = data |
| | | // è¿éé»è¾å¯ä»¥æ ¹æ®é¡¹ç®è¿è¡ä¿®æ¹ |
| | | const hasSuccess = data && Reflect.has(data, 'code') && (code === ResultEnum.SUCCESS || code === 200) |
| | | if (hasSuccess) { |
| | | if (success && message && options.successMessageMode === 'success') { |
| | | //ä¿¡æ¯æåæç¤º |
| | | createMessage.success(message) |
| | | } |
| | | return result |
| | | } |
| | | |
| | | // 卿¤å¤æ ¹æ®èªå·±é¡¹ç®çå®é
æ
åµå¯¹ä¸åçcodeæ§è¡ä¸åçæä½ |
| | | // 妿ä¸å¸æä¸æå½å请æ±ï¼è¯·returnæ°æ®ï¼å¦åç´æ¥æåºå¼å¸¸å³å¯ |
| | | let timeoutMsg = '' |
| | | 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 |
| | | } |
| | | } |
| | | |
| | | // errorMessageMode=âmodalâçæ¶å伿¾ç¤ºmodalé误弹çªï¼è䏿¯æ¶æ¯æç¤ºï¼ç¨äºä¸äºæ¯è¾éè¦çé误 |
| | | // errorMessageMode='none' ä¸è¬æ¯è°ç¨æ¶æç¡®è¡¨ç¤ºä¸å¸æèªå¨å¼¹åºé误æç¤º |
| | | if (options.errorMessageMode === 'modal') { |
| | | createErrorModal({ title: t('sys.api.errorTip'), content: timeoutMsg }) |
| | | } else if (options.errorMessageMode === 'message') { |
| | | createMessage.error(timeoutMsg) |
| | | } |
| | | |
| | | throw new Error(timeoutMsg || t('sys.api.apiRequestFailed')) |
| | | }, |
| | | |
| | | // 请æ±ä¹åå¤çconfig |
| | | beforeRequestHook: (config, 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}` |
| | | } |
| | | 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 请æ±å 䏿¶é´æ³åæ°ï¼é¿å
ä»ç¼å䏿¿æ°æ®ã |
| | | config.params = Object.assign(params || {}, joinTimestamp(joinTime, false)) |
| | | } else { |
| | | // å
¼å®¹restful飿 ¼ |
| | | config.url = config.url + params + `${joinTimestamp(joinTime, true)}` |
| | | config.params = undefined |
| | | } |
| | | } else { |
| | | if (!isString(params)) { |
| | | formatDate && formatRequestDate(params) |
| | | 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, Object.assign({}, config.params, config.data)) |
| | | } |
| | | } else { |
| | | // å
¼å®¹restful飿 ¼ |
| | | config.url = config.url + params |
| | | config.params = undefined |
| | | } |
| | | } |
| | | return config |
| | | }, |
| | | |
| | | /** |
| | | * @description: è¯·æ±æ¦æªå¨å¤ç |
| | | */ |
| | | requestInterceptors: (config: Recordable, options) => { |
| | | // console.log(`output->config`, config); |
| | | // 请æ±ä¹åå¤çconfig |
| | | const token = getToken() |
| | | let tenantid = getTenantId() |
| | | //--update-begin--author:liusq---date:20220325---for: å¢å vue3æ è®° |
| | | config.headers[ConfigEnum.VERSION] = 'v3' |
| | | //--update-end--author:liusq---date:20220325---for:å¢å vue3æ è®° |
| | | if (token && (config as Recordable)?.requestOptions?.withToken !== false) { |
| | | // jwt token |
| | | config.headers.Authorization = options.authenticationScheme ? `${options.authenticationScheme} ${token}` : token |
| | | config.headers[ConfigEnum.TOKEN] = token |
| | | //--update-begin--author:liusq---date:20210831---for:å°ç¾ååæ¶é´æ³ï¼æ·»å å¨è¯·æ±æ¥å£ Header |
| | | |
| | | // update-begin--author:taoyan---date:20220421--for: VUEN-410ãç¾åæ¹é ã X-TIMESTAMPçµæ¯ |
| | | config.headers[ConfigEnum.TIMESTAMP] = signMd5Utils.getTimestamp() |
| | | // update-end--author:taoyan---date:20220421--for: VUEN-410ãç¾åæ¹é ã X-TIMESTAMPçµæ¯ |
| | | |
| | | config.headers[ConfigEnum.Sign] = signMd5Utils.getSign(config.url, config.params) |
| | | //--update-end--author:liusq---date:20210831---for:å°ç¾ååæ¶é´æ³ï¼æ·»å å¨è¯·æ±æ¥å£ Header |
| | | //--update-begin--author:liusq---date:20211105---for: for:å°å¤ç§æ·idï¼æ·»å å¨è¯·æ±æ¥å£ Header |
| | | if (!tenantid) { |
| | | tenantid = '0' |
| | | } |
| | | config.headers[ConfigEnum.TENANT_ID] = tenantid |
| | | |
| | | //--update-end--author:liusq---date:20211105---for:å°å¤ç§æ·idï¼æ·»å å¨è¯·æ±æ¥å£ Header |
| | | |
| | | // ======================================================================================== |
| | | // update-begin--author:sunjianlei---date:20220624--for: æ·»å ä½ä»£ç åºç¨ID |
| | | const routeParams = router.currentRoute.value.params |
| | | if (routeParams.appId) { |
| | | config.headers[ConfigEnum.X_LOW_APP_ID] = routeParams.appId |
| | | // lowAppèªå®ä¹ç鿡件 |
| | | if (routeParams.lowAppFilter) { |
| | | config.params = { ...config.params, ...JSON.parse(routeParams.lowAppFilter as string) } |
| | | delete routeParams.lowAppFilter |
| | | } |
| | | } |
| | | // update-end--author:sunjianlei---date:20220624--for: æ·»å ä½ä»£ç åºç¨ID |
| | | // ======================================================================================== |
| | | } |
| | | return config |
| | | }, |
| | | |
| | | /** |
| | | * @description: ååºæ¦æªå¨å¤ç |
| | | */ |
| | | responseInterceptors: (res: AxiosResponse<any>) => { |
| | | return res |
| | | }, |
| | | |
| | | /** |
| | | * @description: ååºé误å¤ç |
| | | */ |
| | | responseInterceptorsCatch: (error: any) => { |
| | | const { t } = useI18n() |
| | | const errorLogStore = useErrorLogStoreWithOut() |
| | | errorLogStore.addAjaxErrorInfo(error) |
| | | const { response, code, message, config } = error || {} |
| | | const errorMessageMode = config?.requestOptions?.errorMessageMode || 'none' |
| | | //scott 20211022 token失ææç¤ºä¿¡æ¯ |
| | | //const msg: string = response?.data?.error?.message ?? ''; |
| | | const msg: string = response?.data?.message ?? '' |
| | | const err: string = error?.toString?.() ?? '' |
| | | let errMessage = '' |
| | | |
| | | try { |
| | | if (code === 'ECONNABORTED' && message.indexOf('timeout') !== -1) { |
| | | errMessage = t('sys.api.apiTimeoutMessage') |
| | | } |
| | | if (err?.includes('Network Error')) { |
| | | errMessage = t('sys.api.networkExceptionMsg') |
| | | } |
| | | |
| | | if (errMessage) { |
| | | if (errorMessageMode === 'modal') { |
| | | createErrorModal({ title: t('sys.api.errorTip'), content: errMessage }) |
| | | } else if (errorMessageMode === 'message') { |
| | | createMessage.error(errMessage) |
| | | } |
| | | return Promise.reject(error) |
| | | } |
| | | } catch (error) { |
| | | throw new Error(error) |
| | | } |
| | | |
| | | checkStatus(error?.response?.status, msg, errorMessageMode) |
| | | return Promise.reject(error) |
| | | }, |
| | | } |
| | | |
| | | function createAxios(opt?: Partial<CreateAxiosOptions>) { |
| | | return new VAxios( |
| | | deepMerge( |
| | | { |
| | | // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#authentication_schemes |
| | | // authentication schemesï¼e.g: Bearer |
| | | // authenticationScheme: 'Bearer', |
| | | authenticationScheme: '', |
| | | timeout: 10 * 1000, |
| | | // åºç¡æ¥å£å°å |
| | | // baseURL: globSetting.apiUrl, |
| | | headers: { 'Content-Type': ContentTypeEnum.JSON }, |
| | | // 妿æ¯form-dataæ ¼å¼ |
| | | // headers: { 'Content-Type': ContentTypeEnum.FORM_URLENCODED }, |
| | | // æ°æ®å¤çæ¹å¼ |
| | | transform, |
| | | // é
置项ï¼ä¸é¢çé项é½å¯ä»¥å¨ç¬ç«çæ¥å£è¯·æ±ä¸è¦ç |
| | | requestOptions: { |
| | | // é»è®¤å°prefix æ·»å å°url |
| | | joinPrefix: true, |
| | | // æ¯å¦è¿ååçååºå¤´ æ¯å¦ï¼éè¦è·åååºå¤´æ¶ä½¿ç¨è¯¥å±æ§ |
| | | isReturnNativeResponse: false, |
| | | // éè¦å¯¹è¿åæ°æ®è¿è¡å¤ç |
| | | isTransformResponse: true, |
| | | // post请æ±çæ¶åæ·»å åæ°å°url |
| | | joinParamsToUrl: false, |
| | | // æ ¼å¼åæäº¤åæ°æ¶é´ |
| | | formatDate: true, |
| | | // å¼å¸¸æ¶æ¯æç¤ºç±»å |
| | | errorMessageMode: 'message', |
| | | // æåæ¶æ¯æç¤ºç±»å |
| | | successMessageMode: 'success', |
| | | // æ¥å£å°å |
| | | apiUrl: globSetting.apiUrl, |
| | | // æ¥å£æ¼æ¥å°å |
| | | urlPrefix: urlPrefix, |
| | | // æ¯å¦å å
¥æ¶é´æ³ |
| | | joinTime: true, |
| | | // 忽ç¥éå¤è¯·æ± |
| | | ignoreCancelToken: true, |
| | | // æ¯å¦æºå¸¦token |
| | | withToken: true, |
| | | }, |
| | | }, |
| | | opt || {} |
| | | ) |
| | | ) |
| | | } |
| | | export const defHttp = createAxios() |
| | | |
| | | // other api url |
| | | // export const otherHttp = createAxios({ |
| | | // requestOptions: { |
| | | // apiUrl: 'xxx', |
| | | // }, |
| | | // }); |