2022-06-14 09:32:49 +08:00
|
|
|
|
import axios from 'axios'
|
|
|
|
|
import {
|
|
|
|
|
// baseURL,
|
|
|
|
|
contentType,
|
|
|
|
|
debounce,
|
|
|
|
|
requestTimeout,
|
|
|
|
|
// successCode,
|
|
|
|
|
tokenName,
|
|
|
|
|
} from '@/config'
|
|
|
|
|
import store from '@/store'
|
|
|
|
|
import qs from 'qs'
|
|
|
|
|
import router from '@/router'
|
|
|
|
|
// import { isArray } from '@/utils/validate'
|
|
|
|
|
import { message } from 'ant-design-vue'
|
2022-07-08 19:02:20 +08:00
|
|
|
|
import { getAccessToken, setAccessToken } from '@/utils/accessToken'
|
2022-06-14 09:32:49 +08:00
|
|
|
|
|
|
|
|
|
let loadingInstance
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author chuzhixin 1204505056@qq.com
|
|
|
|
|
* @description 处理code异常
|
|
|
|
|
* @param {*} code
|
|
|
|
|
* @param {*} msg
|
|
|
|
|
*/
|
2022-07-08 19:02:20 +08:00
|
|
|
|
const handleCode = (code, msg, res) => {
|
2022-07-16 16:16:38 +08:00
|
|
|
|
// debugger
|
2022-06-14 09:32:49 +08:00
|
|
|
|
switch (code) {
|
|
|
|
|
case 401:
|
|
|
|
|
store.dispatch('user/resetAll').catch(() => {})
|
|
|
|
|
break
|
|
|
|
|
case 403:
|
|
|
|
|
router.push({ path: '/401' }).catch(() => {})
|
|
|
|
|
break
|
|
|
|
|
case 500:
|
|
|
|
|
message.error(msg || '接口异常')
|
|
|
|
|
break
|
|
|
|
|
case 302:
|
2022-07-15 21:35:22 +08:00
|
|
|
|
// window.location.href = redirect
|
2022-06-14 09:32:49 +08:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author chuzhixin 1204505056@qq.com
|
|
|
|
|
* @description axios初始化
|
|
|
|
|
*/
|
|
|
|
|
const instance = axios.create({
|
|
|
|
|
baseURL: window.SITE_CONFIG.apiURL,
|
|
|
|
|
timeout: requestTimeout,
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': contentType,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author chuzhixin 1204505056@qq.com
|
|
|
|
|
* @description axios请求拦截器
|
|
|
|
|
*/
|
|
|
|
|
instance.interceptors.request.use(
|
|
|
|
|
(config) => {
|
2022-07-16 16:16:38 +08:00
|
|
|
|
// debugger
|
2022-07-11 19:12:53 +08:00
|
|
|
|
const token = getAccessToken()
|
2022-07-15 21:35:22 +08:00
|
|
|
|
if (token) config.headers[tokenName] = token
|
2022-06-14 09:32:49 +08:00
|
|
|
|
if (
|
|
|
|
|
config.data &&
|
|
|
|
|
config.headers['Content-Type'] ===
|
|
|
|
|
'application/x-www-form-urlencoded;charset=UTF-8'
|
|
|
|
|
)
|
|
|
|
|
config.data = qs.stringify(config.data)
|
|
|
|
|
if (debounce.some((item) => config.url.includes(item))) {
|
|
|
|
|
//这里写加载动画
|
|
|
|
|
}
|
2022-07-16 16:44:00 +08:00
|
|
|
|
if (!config.headers.REQUESTURI) {
|
2022-07-16 16:16:38 +08:00
|
|
|
|
config.headers.REQUESTURI = window.location.href
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-14 09:32:49 +08:00
|
|
|
|
return config
|
|
|
|
|
},
|
|
|
|
|
(error) => {
|
|
|
|
|
return Promise.reject(error)
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author chuzhixin 1204505056@qq.com
|
|
|
|
|
* @description axios响应拦截器
|
|
|
|
|
*/
|
|
|
|
|
instance.interceptors.response.use(
|
|
|
|
|
(response) => {
|
2022-07-08 19:02:20 +08:00
|
|
|
|
console.log('接口返回', response)
|
|
|
|
|
console.log('接口返回headers', response.headers)
|
|
|
|
|
console.log('接口返回REDIRECT', response.headers.redirect)
|
|
|
|
|
response['Access-Control-Expose-Headers'] = 'redirect'
|
|
|
|
|
const { code, message } = response.data
|
2022-07-16 16:44:00 +08:00
|
|
|
|
|
2022-07-16 16:16:38 +08:00
|
|
|
|
if (response.headers.token) {
|
2022-07-08 19:02:20 +08:00
|
|
|
|
setAccessToken(response.headers.token)
|
2022-07-16 16:44:00 +08:00
|
|
|
|
}
|
2022-07-16 16:16:38 +08:00
|
|
|
|
if (response.headers.redirect) {
|
|
|
|
|
window.location.replace(response.headers.redirect)
|
2022-07-16 16:44:00 +08:00
|
|
|
|
return
|
2022-07-08 19:02:20 +08:00
|
|
|
|
}
|
2022-06-23 14:52:50 +08:00
|
|
|
|
if (response.headers.redirect === '/#/login') {
|
|
|
|
|
var keys = document.cookie.match(/[^ =;]+(?=\=)/g)
|
|
|
|
|
if (keys) {
|
|
|
|
|
for (var i = keys.length; i--; ) {
|
|
|
|
|
document.cookie =
|
|
|
|
|
keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString() //清除当前域名下的,例如:m.kevis.com
|
|
|
|
|
document.cookie =
|
|
|
|
|
keys[i] +
|
|
|
|
|
'=0;path=/;domain=' +
|
|
|
|
|
document.domain +
|
|
|
|
|
';expires=' +
|
|
|
|
|
new Date(0).toUTCString() //清除当前域名下的,例如 .m.kevis.com
|
|
|
|
|
document.cookie =
|
|
|
|
|
keys[i] +
|
|
|
|
|
'=0;path=/;domain=kevis.com;expires=' +
|
|
|
|
|
new Date(0).toUTCString() //清除一级域名下的或指定的,例如 .kevis.com
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-08 19:02:20 +08:00
|
|
|
|
console.log('已清除')
|
2022-06-23 17:56:40 +08:00
|
|
|
|
setTimeout(() => {
|
|
|
|
|
location.reload()
|
|
|
|
|
}, 1000)
|
2022-06-23 14:52:50 +08:00
|
|
|
|
}
|
2022-07-08 19:02:20 +08:00
|
|
|
|
handleCode(code, message, response.headers)
|
2022-06-14 09:32:49 +08:00
|
|
|
|
if (loadingInstance) loadingInstance.close()
|
|
|
|
|
// const { data, config } = response
|
|
|
|
|
// const { code, msg } = data
|
|
|
|
|
// 操作正常Code数组
|
|
|
|
|
// const codeVerificationArray = isArray(successCode)
|
|
|
|
|
// ? [...successCode]
|
|
|
|
|
// : [...[successCode]]
|
|
|
|
|
// 是否操作正常
|
|
|
|
|
// if (codeVerificationArray.includes(code)) {
|
|
|
|
|
return response
|
|
|
|
|
// } else {
|
|
|
|
|
// handleCode(code, msg)
|
|
|
|
|
// return Promise.reject(
|
|
|
|
|
// 'vue-admin-beautiful请求异常拦截:' +
|
|
|
|
|
// JSON.stringify({ url: config.url, code, msg }) || 'Error'
|
|
|
|
|
// )
|
|
|
|
|
// }
|
|
|
|
|
},
|
|
|
|
|
(error) => {
|
2022-07-16 16:16:38 +08:00
|
|
|
|
// debugger
|
2022-07-08 19:02:20 +08:00
|
|
|
|
console.log('接口error', error)
|
2022-06-14 09:32:49 +08:00
|
|
|
|
if (loadingInstance) loadingInstance.close()
|
2022-07-11 19:12:53 +08:00
|
|
|
|
|
2022-06-14 09:32:49 +08:00
|
|
|
|
const { response, message } = error
|
2022-07-11 19:12:53 +08:00
|
|
|
|
if (error.response) {
|
2022-07-08 19:02:20 +08:00
|
|
|
|
console.log('接口返回', response)
|
|
|
|
|
console.log('接口返回headers', response.headers)
|
|
|
|
|
console.log('接口返回REDIRECT', response.headers.redirect)
|
2022-07-16 16:16:38 +08:00
|
|
|
|
if (response.headers.token) {
|
|
|
|
|
setAccessToken(response.headers.token)
|
|
|
|
|
}
|
2022-06-14 09:32:49 +08:00
|
|
|
|
if (response.headers.redirect) {
|
2022-07-08 19:02:20 +08:00
|
|
|
|
window.location.replace(response.headers.redirect)
|
2022-07-16 16:16:38 +08:00
|
|
|
|
return Promise.resolve()
|
2022-06-14 09:32:49 +08:00
|
|
|
|
}
|
|
|
|
|
const { status, data } = response
|
|
|
|
|
|
|
|
|
|
handleCode(status, data.msg || message, response.headers.redirect)
|
|
|
|
|
return Promise.reject(error)
|
|
|
|
|
} else {
|
|
|
|
|
let { message } = error
|
|
|
|
|
if (message === 'Network Error') {
|
|
|
|
|
message = '后端接口连接异常'
|
|
|
|
|
}
|
|
|
|
|
if (message.includes('timeout')) {
|
|
|
|
|
message = '后端接口请求超时'
|
|
|
|
|
}
|
|
|
|
|
if (message.includes('Request failed with status code')) {
|
|
|
|
|
const code = message.substr(message.length - 3)
|
|
|
|
|
message = '后端接口' + code + '异常'
|
|
|
|
|
}
|
|
|
|
|
message.error(message || `后端接口未知异常`)
|
|
|
|
|
return Promise.reject(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
export default instance
|