refactor: http interceptors
This commit is contained in:
parent
8806a6cb43
commit
2d879d0592
@ -1,5 +1,4 @@
|
|||||||
import { useUserStore } from '@/store'
|
import { useUserStore } from '@/store'
|
||||||
import { isNullOrUndef } from '@/utils'
|
|
||||||
|
|
||||||
export function addBaseParams(params) {
|
export function addBaseParams(params) {
|
||||||
if (!params.userId) {
|
if (!params.userId) {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import { repReject, repResolve, reqReject, reqResolve } from './interceptors'
|
import { resReject, resResolve, reqReject, reqResolve } from './interceptors'
|
||||||
|
|
||||||
export function createAxios(options = {}) {
|
export function createAxios(options = {}) {
|
||||||
const defaultOptions = {
|
const defaultOptions = {
|
||||||
@ -10,7 +10,7 @@ export function createAxios(options = {}) {
|
|||||||
...options,
|
...options,
|
||||||
})
|
})
|
||||||
service.interceptors.request.use(reqResolve, reqReject)
|
service.interceptors.request.use(reqResolve, reqReject)
|
||||||
service.interceptors.response.use(repResolve, repReject)
|
service.interceptors.response.use(resResolve, resReject)
|
||||||
return service
|
return service
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,10 +25,10 @@ export function reqReject(error) {
|
|||||||
return Promise.reject(error)
|
return Promise.reject(error)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function repResolve(response) {
|
export function resResolve(response) {
|
||||||
// TODO: 处理不同的 response.headers
|
// TODO: 处理不同的 response.headers
|
||||||
const { data, status, config, statusText } = response
|
const { data, status, config, statusText } = response
|
||||||
if (response.data?.code !== 0) {
|
if (data?.code !== 0) {
|
||||||
const code = data?.code ?? status
|
const code = data?.code ?? status
|
||||||
|
|
||||||
/** 根据code处理对应的操作,并返回处理后的message */
|
/** 根据code处理对应的操作,并返回处理后的message */
|
||||||
@ -36,12 +36,12 @@ export function repResolve(response) {
|
|||||||
|
|
||||||
/** 需要错误提醒 */
|
/** 需要错误提醒 */
|
||||||
!config.noNeedTip && $message.error(message)
|
!config.noNeedTip && $message.error(message)
|
||||||
return Promise.reject({ code, message, error: response?.data })
|
return Promise.reject({ code, message, error: data || response })
|
||||||
}
|
}
|
||||||
return Promise.resolve(data)
|
return Promise.resolve(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function repReject(error) {
|
export function resReject(error) {
|
||||||
if (!error || !error.response) {
|
if (!error || !error.response) {
|
||||||
const code = error?.code
|
const code = error?.code
|
||||||
/** 根据code处理对应的操作,并返回处理后的message */
|
/** 根据code处理对应的操作,并返回处理后的message */
|
||||||
@ -54,5 +54,5 @@ export function repReject(error) {
|
|||||||
const message = resolveResError(code, data?.message ?? error.message)
|
const message = resolveResError(code, data?.message ?? error.message)
|
||||||
/** 需要错误提醒 */
|
/** 需要错误提醒 */
|
||||||
!config?.noNeedTip && $message.error(message)
|
!config?.noNeedTip && $message.error(message)
|
||||||
return Promise.reject({ code, message, error: error.response?.data })
|
return Promise.reject({ code, message, error: error.response?.data || error.response })
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user