|
|
@ -50,7 +50,7 @@ export const token = {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 请求拦截
|
|
|
|
// 请求拦截
|
|
|
|
const beforeRequest = (config: any) => {
|
|
|
|
const beforeRequest = (config: any) => {
|
|
|
|
config.retryTimes = 3
|
|
|
|
config.retryTimes = config.retryTimes || 3
|
|
|
|
// NOTE 添加自定义头部
|
|
|
|
// NOTE 添加自定义头部
|
|
|
|
token.value && (config.headers.Authorization = `Bearer ${token.value}`)
|
|
|
|
token.value && (config.headers.Authorization = `Bearer ${token.value}`)
|
|
|
|
return config
|
|
|
|
return config
|
|
|
@ -76,24 +76,25 @@ const responseSuccess = (client: AxiosInstance) => {
|
|
|
|
const responseFailed = (client: AxiosInstance) => {
|
|
|
|
const responseFailed = (client: AxiosInstance) => {
|
|
|
|
return (error: AxiosError) => {
|
|
|
|
return (error: AxiosError) => {
|
|
|
|
const { response } = error
|
|
|
|
const { response } = error
|
|
|
|
|
|
|
|
const config = response?.config
|
|
|
|
const data = response?.data || {} as any
|
|
|
|
const data = response?.data || {} as any
|
|
|
|
if (!window.navigator.onLine) {
|
|
|
|
if (!window.navigator.onLine) {
|
|
|
|
alert('没有网络')
|
|
|
|
alert('没有网络')
|
|
|
|
return Promise.reject(new Error('请检查网络连接'))
|
|
|
|
return Promise.reject(new Error('请检查网络连接'))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let needRetry = true
|
|
|
|
// @ts-ignore
|
|
|
|
|
|
|
|
let needRetry = config?.needRetry !== false
|
|
|
|
if (response?.status == 404) {
|
|
|
|
if (response?.status == 404) {
|
|
|
|
needRetry = false
|
|
|
|
needRetry = false
|
|
|
|
} else if (response?.status == 401) {
|
|
|
|
} else if (response?.status == 401 && needRetry) {
|
|
|
|
needRetry = false
|
|
|
|
needRetry = false
|
|
|
|
// AuthNotFound = New(40100, "auth not found")
|
|
|
|
// AuthNotFound = New(40100, "auth not found")
|
|
|
|
// AuthExpired = New(40102, "auth expired")
|
|
|
|
// AuthExpired = New(40102, "auth expired")
|
|
|
|
if (data.code === 40102 || data.code === 40100) {
|
|
|
|
if (data.code === 40102 || data.code === 40100) {
|
|
|
|
token.value = ''
|
|
|
|
token.value = ''
|
|
|
|
return token.update().then((e) => {
|
|
|
|
return token.update().then(() => {
|
|
|
|
console.log('token updated: ' + e)
|
|
|
|
return requestRetry(client)(200, response!)
|
|
|
|
return requestRetry(client)(500, response!)
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (response?.status == 500) {
|
|
|
|
} else if (response?.status == 500) {
|
|
|
@ -143,6 +144,7 @@ interface data {
|
|
|
|
query?: any
|
|
|
|
query?: any
|
|
|
|
form?: any
|
|
|
|
form?: any
|
|
|
|
header?: any
|
|
|
|
header?: any
|
|
|
|
|
|
|
|
config?: Object
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -159,6 +161,9 @@ function transData(d: data) {
|
|
|
|
if (d.header) {
|
|
|
|
if (d.header) {
|
|
|
|
opts.headers = Object.assign(opts.headers, d.header)
|
|
|
|
opts.headers = Object.assign(opts.headers, d.header)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d.config) {
|
|
|
|
|
|
|
|
opts = Object.assign(opts, d.config)
|
|
|
|
|
|
|
|
}
|
|
|
|
return opts
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|