fix: bug#10667
This commit is contained in:
parent
58e01fe28e
commit
12e3bf620b
|
@ -1,22 +1,22 @@
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import {BASE_API_PATH, TOKEN_KEY} from '@/utils/variable'
|
import { BASE_API_PATH, TOKEN_KEY } from '@/utils/variable'
|
||||||
import { notification as Notification } from 'ant-design-vue'
|
import { notification as Notification } from 'ant-design-vue'
|
||||||
import router from '@/router'
|
import router from '@/router'
|
||||||
import { LoginPath } from '@/router/menu'
|
import { LoginPath } from '@/router/menu'
|
||||||
import {LocalStore} from "@/utils/comm";
|
import { cleanToken, getToken, LocalStore } from '@/utils/comm'
|
||||||
import type { AxiosInstance, AxiosResponse } from 'axios'
|
import type { AxiosInstance, AxiosResponse } from 'axios'
|
||||||
|
|
||||||
interface AxiosResponseRewrite<T = any[]> extends AxiosResponse<T, any> {
|
interface AxiosResponseRewrite<T = any[]> extends AxiosResponse<T, any> {
|
||||||
result: T
|
result: T
|
||||||
success: boolean
|
success: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export const SUCCESS_CODE = 200 // 成功代码
|
export const SUCCESS_CODE = 200 // 成功代码
|
||||||
|
|
||||||
export const request = axios.create({
|
export const request = axios.create({
|
||||||
withCredentials: false,
|
withCredentials: false,
|
||||||
baseURL: BASE_API_PATH,
|
baseURL: BASE_API_PATH,
|
||||||
timeout: 1000 * 60 * 5
|
timeout: 1000 * 60 * 5
|
||||||
})
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -30,15 +30,15 @@ export const request = axios.create({
|
||||||
}
|
}
|
||||||
* @returns {AxiosInstance}
|
* @returns {AxiosInstance}
|
||||||
*/
|
*/
|
||||||
export const post = function<T>(url: string, data = {}, params = {}, ext={}) {
|
export const post = function <T>(url: string, data = {}, params = {}, ext = {}) {
|
||||||
ext = typeof ext === 'string' ? { responseType: ext } : ext
|
ext = typeof ext === 'string' ? { responseType: ext } : ext
|
||||||
return request<any, AxiosResponseRewrite<T>>({
|
return request<any, AxiosResponseRewrite<T>>({
|
||||||
...ext,
|
...ext,
|
||||||
params,
|
params,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url,
|
url,
|
||||||
data
|
data
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -47,12 +47,12 @@ export const post = function<T>(url: string, data = {}, params = {}, ext={}) {
|
||||||
* @param {Object} [data]
|
* @param {Object} [data]
|
||||||
* @returns {AxiosInstance}
|
* @returns {AxiosInstance}
|
||||||
*/
|
*/
|
||||||
export const put = function<T>(url: string, data = {},) {
|
export const put = function <T>(url: string, data = {}) {
|
||||||
return request<any, AxiosResponseRewrite<T>>({
|
return request<any, AxiosResponseRewrite<T>>({
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
url,
|
url,
|
||||||
data
|
data
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -61,12 +61,12 @@ export const put = function<T>(url: string, data = {},) {
|
||||||
* @param {Object} [data]
|
* @param {Object} [data]
|
||||||
* @returns {AxiosInstance}
|
* @returns {AxiosInstance}
|
||||||
*/
|
*/
|
||||||
export const patch = function<T>(url: string, data = {}) {
|
export const patch = function <T>(url: string, data = {}) {
|
||||||
return request<any, AxiosResponseRewrite<T>>({
|
return request<any, AxiosResponseRewrite<T>>({
|
||||||
method: 'PATCH',
|
method: 'PATCH',
|
||||||
url,
|
url,
|
||||||
data
|
data
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* GET method request
|
* GET method request
|
||||||
|
@ -75,13 +75,13 @@ export const patch = function<T>(url: string, data = {}) {
|
||||||
* @param {Object} [ext] 扩展参数
|
* @param {Object} [ext] 扩展参数
|
||||||
* @returns {AxiosInstance}
|
* @returns {AxiosInstance}
|
||||||
*/
|
*/
|
||||||
export const get = function<T>(url: string, params = {}, ext?: any) {
|
export const get = function <T>(url: string, params = {}, ext?: any) {
|
||||||
return request<any, AxiosResponseRewrite<T>>({
|
return request<any, AxiosResponseRewrite<T>>({
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
url,
|
url,
|
||||||
params,
|
params,
|
||||||
...ext
|
...ext
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -91,13 +91,13 @@ export const get = function<T>(url: string, params = {}, ext?: any) {
|
||||||
* @param {Object} [ext] 扩展参数
|
* @param {Object} [ext] 扩展参数
|
||||||
* @returns {AxiosInstance}
|
* @returns {AxiosInstance}
|
||||||
*/
|
*/
|
||||||
export const remove = function<T>(url: string, params = {}, ext?: any) {
|
export const remove = function <T>(url: string, params = {}, ext?: any) {
|
||||||
return request<any, AxiosResponseRewrite<T>>({
|
return request<any, AxiosResponseRewrite<T>>({
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
url,
|
url,
|
||||||
params,
|
params,
|
||||||
...ext
|
...ext
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -107,25 +107,25 @@ export const remove = function<T>(url: string, params = {}, ext?: any) {
|
||||||
* @return {*}
|
* @return {*}
|
||||||
*/
|
*/
|
||||||
export const getStream = function(url: string, params = {}) {
|
export const getStream = function(url: string, params = {}) {
|
||||||
return get<any>(url, params, {
|
return get<any>(url, params, {
|
||||||
responseType: 'arraybuffer' // 设置请求数据类型,返回blob可解析类型
|
responseType: 'arraybuffer' // 设置请求数据类型,返回blob可解析类型
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export const postStream = function(url: string, data={}, params = {}) {
|
export const postStream = function(url: string, data = {}, params = {}) {
|
||||||
return post<any>(url, data, params, {
|
return post<any>(url, data, params, {
|
||||||
responseType: 'arraybuffer' // 设置请求数据类型,返回blob可解析类型
|
responseType: 'arraybuffer' // 设置请求数据类型,返回blob可解析类型
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const showNotification = ( message: string, description: string, key?: string, show: boolean = true ) => {
|
const showNotification = (message: string, description: string, key?: string, show: boolean = true) => {
|
||||||
if (show) {
|
if (show) {
|
||||||
Notification.error({
|
Notification.error({
|
||||||
key,
|
key,
|
||||||
message,
|
message,
|
||||||
description
|
description
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -134,87 +134,86 @@ const showNotification = ( message: string, description: string, key?: string, s
|
||||||
* @returns {Promise<never>}
|
* @returns {Promise<never>}
|
||||||
*/
|
*/
|
||||||
const errorHandler = (error: any) => {
|
const errorHandler = (error: any) => {
|
||||||
if (error.response) {
|
if (error.response) {
|
||||||
const data = error.response.data
|
const data = error.response.data
|
||||||
const status = error.response.status
|
const status = error.response.status
|
||||||
if (status === 403) {
|
if (status === 403) {
|
||||||
showNotification( 'Forbidden', (data.message + '').substr(0, 90), '403')
|
showNotification('Forbidden', (data.message + '').substr(0, 90), '403')
|
||||||
} else if (status === 500) {
|
} else if (status === 500) {
|
||||||
showNotification( 'Server Side Error', (data.message + '').substr(0, 90), '500')
|
showNotification('Server Side Error', (data.message + '').substr(0, 90), '500')
|
||||||
} else if (status === 400) {
|
} else if (status === 400) {
|
||||||
showNotification( 'Request Error', (data.message + '').substr(0, 90), '400')
|
showNotification('Request Error', (data.message + '').substr(0, 90), '400')
|
||||||
} else if (status === 401) {
|
} else if (status === 401) {
|
||||||
showNotification( 'Unauthorized', '用户未登录', '401')
|
showNotification('Unauthorized', '用户未登录', '401')
|
||||||
console.log('showNotification')
|
setTimeout(() => {
|
||||||
setTimeout(() => {
|
cleanToken()
|
||||||
location.href = `/#${LoginPath}`
|
router.replace({
|
||||||
}, 0)
|
path: LoginPath
|
||||||
}
|
})
|
||||||
} else if (error.response === undefined) {
|
}, 0)
|
||||||
showNotification( error.message, (error.stack + '').substr(0, 90), undefined)
|
|
||||||
}
|
}
|
||||||
return Promise.reject(error)
|
} else if (error.response === undefined) {
|
||||||
|
showNotification(error.message, (error.stack + '').substr(0, 90), undefined)
|
||||||
|
}
|
||||||
|
return Promise.reject(error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// request interceptor
|
// request interceptor
|
||||||
request.interceptors.request.use(config => {
|
request.interceptors.request.use(config => {
|
||||||
// 如果 token 存在
|
// 如果 token 存在
|
||||||
// 让每个请求携带自定义 token 请根据实际情况自行修改
|
// 让每个请求携带自定义 token 请根据实际情况自行修改
|
||||||
const token = LocalStore.get(TOKEN_KEY)
|
const token = getToken()
|
||||||
// const token = store.$state.tokenAlias
|
if (!token) {
|
||||||
if (!token) {
|
setTimeout(() => {
|
||||||
// setTimeout(() => {
|
cleanToken()
|
||||||
// router.replace({
|
router.replace({
|
||||||
// path: LoginPath
|
path: LoginPath
|
||||||
// })
|
})
|
||||||
// }, 0)
|
}, 0)
|
||||||
setTimeout(() => {
|
|
||||||
location.href = `/#${LoginPath}`
|
|
||||||
}, 0)
|
|
||||||
return config
|
|
||||||
}
|
|
||||||
|
|
||||||
config.headers![TOKEN_KEY] = token
|
|
||||||
|
|
||||||
return config
|
return config
|
||||||
|
}
|
||||||
|
|
||||||
|
config.headers![TOKEN_KEY] = token
|
||||||
|
|
||||||
|
return config
|
||||||
}, errorHandler)
|
}, errorHandler)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* response interceptor
|
* response interceptor
|
||||||
*/
|
*/
|
||||||
request.interceptors.response.use(response => {
|
request.interceptors.response.use(response => {
|
||||||
if (response.data instanceof ArrayBuffer) {
|
if (response.data instanceof ArrayBuffer) {
|
||||||
return response
|
return response
|
||||||
} else {
|
} else {
|
||||||
const { status, message } = response.data
|
const { status, message } = response.data
|
||||||
// 增加业务接口处理成功判断方式,只需要判断返回参数包含:success为true
|
// 增加业务接口处理成功判断方式,只需要判断返回参数包含:success为true
|
||||||
if (typeof response.data === 'object' && typeof response.data.success === 'undefined') {
|
if (typeof response.data === 'object' && typeof response.data.success === 'undefined') {
|
||||||
response.data.success = status === SUCCESS_CODE
|
response.data.success = status === SUCCESS_CODE
|
||||||
}
|
|
||||||
|
|
||||||
// 统一显示异常业务信息
|
|
||||||
if (status !== SUCCESS_CODE && message) {
|
|
||||||
Notification.error({
|
|
||||||
message: 'Server Errors: ' + status,
|
|
||||||
description: message
|
|
||||||
})
|
|
||||||
}
|
|
||||||
// 如果返回的的是文件流,那么return值则为response
|
|
||||||
if (response.headers['content-type'] === 'application/octet-stream; charset=UTF-8' || response.headers['content-type'] === 'application/vnd.ms-excel;charset=UTF-8') {
|
|
||||||
return response.data
|
|
||||||
} else {
|
|
||||||
return response.data
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 统一显示异常业务信息
|
||||||
|
if (status !== SUCCESS_CODE && message) {
|
||||||
|
Notification.error({
|
||||||
|
message: 'Server Errors: ' + status,
|
||||||
|
description: message
|
||||||
|
})
|
||||||
|
}
|
||||||
|
// 如果返回的的是文件流,那么return值则为response
|
||||||
|
if (response.headers['content-type'] === 'application/octet-stream; charset=UTF-8' || response.headers['content-type'] === 'application/vnd.ms-excel;charset=UTF-8') {
|
||||||
|
return response.data
|
||||||
|
} else {
|
||||||
|
return response.data
|
||||||
|
}
|
||||||
|
}
|
||||||
}, errorHandler)
|
}, errorHandler)
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
request: axios,
|
request: axios,
|
||||||
post,
|
post,
|
||||||
get,
|
get,
|
||||||
patch,
|
patch,
|
||||||
put,
|
put,
|
||||||
remove,
|
remove,
|
||||||
getStream,
|
getStream,
|
||||||
postStream
|
postStream
|
||||||
}
|
}
|
Loading…
Reference in New Issue