fix: bug#10667

This commit is contained in:
xieyonghong 2023-03-24 15:52:37 +08:00
parent 58e01fe28e
commit 12e3bf620b
1 changed files with 124 additions and 125 deletions

View File

@ -1,9 +1,9 @@
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 router from '@/router'
import { LoginPath } from '@/router/menu'
import {LocalStore} from "@/utils/comm";
import { cleanToken, getToken, LocalStore } from '@/utils/comm'
import type { AxiosInstance, AxiosResponse } from 'axios'
interface AxiosResponseRewrite<T = any[]> extends AxiosResponse<T, any> {
@ -30,7 +30,7 @@ export const request = axios.create({
}
* @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
return request<any, AxiosResponseRewrite<T>>({
...ext,
@ -47,7 +47,7 @@ export const post = function<T>(url: string, data = {}, params = {}, ext={}) {
* @param {Object} [data]
* @returns {AxiosInstance}
*/
export const put = function<T>(url: string, data = {},) {
export const put = function <T>(url: string, data = {}) {
return request<any, AxiosResponseRewrite<T>>({
method: 'PUT',
url,
@ -61,7 +61,7 @@ export const put = function<T>(url: string, data = {},) {
* @param {Object} [data]
* @returns {AxiosInstance}
*/
export const patch = function<T>(url: string, data = {}) {
export const patch = function <T>(url: string, data = {}) {
return request<any, AxiosResponseRewrite<T>>({
method: 'PATCH',
url,
@ -75,7 +75,7 @@ export const patch = function<T>(url: string, data = {}) {
* @param {Object} [ext]
* @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>>({
method: 'GET',
url,
@ -91,7 +91,7 @@ export const get = function<T>(url: string, params = {}, ext?: any) {
* @param {Object} [ext]
* @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>>({
method: 'DELETE',
url,
@ -112,13 +112,13 @@ export const getStream = function(url: string, params = {}) {
})
}
export const postStream = function(url: string, data={}, params = {}) {
export const postStream = function(url: string, data = {}, params = {}) {
return post<any>(url, data, params, {
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) {
Notification.error({
key,
@ -138,20 +138,22 @@ const errorHandler = (error: any) => {
const data = error.response.data
const status = error.response.status
if (status === 403) {
showNotification( 'Forbidden', (data.message + '').substr(0, 90), '403')
showNotification('Forbidden', (data.message + '').substr(0, 90), '403')
} 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) {
showNotification( 'Request Error', (data.message + '').substr(0, 90), '400')
showNotification('Request Error', (data.message + '').substr(0, 90), '400')
} else if (status === 401) {
showNotification( 'Unauthorized', '用户未登录', '401')
console.log('showNotification')
showNotification('Unauthorized', '用户未登录', '401')
setTimeout(() => {
location.href = `/#${LoginPath}`
cleanToken()
router.replace({
path: LoginPath
})
}, 0)
}
} else if (error.response === undefined) {
showNotification( error.message, (error.stack + '').substr(0, 90), undefined)
showNotification(error.message, (error.stack + '').substr(0, 90), undefined)
}
return Promise.reject(error)
}
@ -160,16 +162,13 @@ const errorHandler = (error: any) => {
request.interceptors.request.use(config => {
// 如果 token 存在
// 让每个请求携带自定义 token 请根据实际情况自行修改
const token = LocalStore.get(TOKEN_KEY)
// const token = store.$state.tokenAlias
const token = getToken()
if (!token) {
// setTimeout(() => {
// router.replace({
// path: LoginPath
// })
// }, 0)
setTimeout(() => {
location.href = `/#${LoginPath}`
cleanToken()
router.replace({
path: LoginPath
})
}, 0)
return config
}