全部request.js代码
import Vue from 'vue'
import axios from 'axios'
import store from '@/store'
import { VueAxios } from './axios'
import { Modal, notification } from 'ant-design-vue'
import { ACCESS_TOKEN , TENANT_ID } from '@/store/mutation-types'
let apiBaseUrl = window. _CONFIG[ 'domianURL' ] || '/jeecg-boot'
let reqList = [ ]
const stopRepeatRequest = function ( reqList, url, cancel, errorMessage) {
const errorMsg = errorMessage || ''
for ( let i = 0 ; i < reqList. length; i++ ) {
if ( reqList[ i] === url) {
cancel ( errorMsg)
return
}
}
reqList. push ( url)
}
const allowRequest = function ( reqList, url) {
for ( let i = 0 ; i < reqList. length; i++ ) {
if ( reqList[ i] === url) {
reqList. splice ( i, 1 )
break
}
}
}
const service = axios. create ( {
baseURL: apiBaseUrl,
timeout: 30000
} )
const err = ( error) => {
if ( error. response) {
let that = this
let data = error. response. data
const token = Vue. ls. get ( ACCESS_TOKEN )
console . log ( '------异常响应------' , token)
console . log ( '------异常响应------' , error. response. status)
switch ( error. response. status) {
case 403 :
notification. error ( { message: '系统提示' , description: '拒绝访问' , duration: 4 } )
break
case 500 :
if ( token && data. message. includes ( 'Token失效' ) ) {
Modal. error ( {
title: '登录已过期' ,
content: '很抱歉,登录已过期,请重新登录' ,
okText: '重新登录' ,
mask: false ,
onOk : ( ) => {
store. dispatch ( 'Logout' ) . then ( ( ) => {
Vue. ls. remove ( ACCESS_TOKEN )
try {
let path = window. location. pathname
if ( path. indexOf ( '/user/login' ) < 0 ) {
window. location. reload ( )
}
} catch ( e) {
window. location. reload ( )
}
} )
}
} )
}
break
case 404 :
notification. error ( { message: '系统提示' , description: '很抱歉,资源未找到!' , duration: 4 } )
break
case 504 :
notification. error ( { message: '系统提示' , description: '网络超时' } )
break
case 401 :
notification. error ( { message: '系统提示' , description: '未授权,请重新登录' , duration: 4 } )
if ( token) {
store. dispatch ( 'Logout' ) . then ( ( ) => {
setTimeout ( ( ) => {
window. location. reload ( )
} , 1500 )
} )
}
break
default :
notification. error ( {
message: '系统提示' ,
description: data. message,
duration: 4
} )
break
}
}
return Promise . reject ( error)
}
service. interceptors. request. use ( config => {
const token = Vue. ls. get ( ACCESS_TOKEN )
if ( token) {
config. headers[ 'X-Access-Token' ] = token
}
let tenantid = Vue. ls. get ( TENANT_ID )
if ( ! tenantid) {
tenantid = 0
}
config. headers[ 'tenant_id' ] = tenantid
if ( config. method == 'get' ) {
if ( config. url. indexOf ( 'sys/dict/getDictItems' ) < 0 ) {
config. params = {
_t: Date. parse ( new Date ( ) ) / 1000 ,
... config. params
}
}
}
if ( config. method !== 'get' ) {
let cancel
config. cancelToken = new axios . CancelToken ( function ( c) {
cancel = c
} )
stopRepeatRequest ( reqList, config. baseURL + config. url, cancel, ` ${ config. url} ,重复请求被中断! ` )
}
return config
} , ( error) => {
return Promise . reject ( error)
} )
service. interceptors. response. use ( ( response) => {
setTimeout ( ( ) => {
allowRequest ( reqList, response. config. url)
} , 1000 )
return response. data
} , error => {
if ( axios. isCancel ( error) ) {
} else {
setTimeout ( ( ) => {
allowRequest ( reqList, error. config. url)
} , 1000 )
}
return err ( error)
} )
const installer = {
vm: { } ,
install ( Vue, router = { } ) {
Vue. use ( VueAxios, router, service)
}
}
export {
installer as VueAxios,
service as axios
}