162 lines
4.6 KiB
JavaScript
162 lines
4.6 KiB
JavaScript
/*
|
||
* @Author: hisense.wuhongjian
|
||
* @Date: 2022-05-11 20:03:41
|
||
* @LastEditors: hisense.wuhongjian
|
||
* @LastEditTime: 2022-06-06 13:08:06
|
||
* @Description: 告诉大家这是什么
|
||
*/
|
||
import axios from 'axios'
|
||
import Cookies from 'js-cookie'
|
||
import router from '@/router'
|
||
import qs from 'qs'
|
||
import { clearLoginInfo } from '@/utils'
|
||
import isPlainObject from 'lodash/isPlainObject'
|
||
|
||
const http = axios.create({
|
||
baseURL: window.SITE_CONFIG.apiURL,
|
||
// baseURL: 'http://15.2.21.238:8888/renren-admin/',
|
||
timeout: 1000 * 180,
|
||
withCredentials: true
|
||
})
|
||
const handleCode = (code, msg, redirect) => {
|
||
switch (code) {
|
||
case 401:
|
||
// message.error(msg || '登录失效')
|
||
// store.dispatch('user/resetAll').catch(() => {})
|
||
// // location.reload()
|
||
if (redirect) {
|
||
window.location.href = redirect
|
||
}
|
||
break
|
||
case 403:
|
||
router.push({ path: '/401' }).catch(() => {})
|
||
break
|
||
case 500:
|
||
// message.error(msg || '接口异常')
|
||
break
|
||
case 302:
|
||
window.location.href = redirect
|
||
break
|
||
}
|
||
}
|
||
/**
|
||
* 请求拦截
|
||
*/
|
||
http.interceptors.request.use(config => {
|
||
config.headers['Accept-Language'] = Cookies.get('language') || 'zh-CN'
|
||
config.headers.token = Cookies.get('ucsToken') || ''
|
||
// 默认参数
|
||
var defaults = {}
|
||
// 防止缓存,GET请求默认带_t参数
|
||
if (config.method === 'get') {
|
||
config.params = {
|
||
...config.params,
|
||
...{ _t: new Date().getTime() }
|
||
}
|
||
}
|
||
if (isPlainObject(config.params)) {
|
||
config.params = {
|
||
...defaults,
|
||
...config.params
|
||
}
|
||
}
|
||
if (isPlainObject(config.data)) {
|
||
config.data = {
|
||
...defaults,
|
||
...config.data
|
||
}
|
||
if (/^application\/x-www-form-urlencoded/.test(config.headers['content-type'])) {
|
||
config.data = qs.stringify(config.data)
|
||
}
|
||
}
|
||
|
||
// get序列化
|
||
config.paramsSerializer = function (params) {
|
||
return qs.stringify(params, { arrayFormat: 'repeat' })
|
||
}
|
||
config.headers.REQUESTURI = window.location.href
|
||
return config
|
||
}, error => {
|
||
return Promise.reject(error)
|
||
})
|
||
|
||
/**
|
||
* 响应拦截
|
||
*/
|
||
http.interceptors.response.use(response => {
|
||
if (response.data.code === 401 || response.data.code === 10001) {
|
||
clearLoginInfo()
|
||
router.replace({ name: 'login' })
|
||
return Promise.reject(response.data.msg)
|
||
}
|
||
return response
|
||
}, error => {
|
||
console.error(error)
|
||
return Promise.reject(error)
|
||
})
|
||
http.interceptors.response.use(
|
||
(response) => {
|
||
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
|
||
if (response.headers.token) {
|
||
Cookies.set('ucsToken', response.headers.token)
|
||
}
|
||
if (response.headers.redirect) {
|
||
window.location.href = response.headers.redirect
|
||
}
|
||
handleCode(code, message, response.headers.redirect)
|
||
// 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) => {
|
||
console.log('接口error', error)
|
||
const { response, message } = error
|
||
if (error.response && error.response.data) {
|
||
console.log('接口返回', response)
|
||
console.log('接口返回headers', response.headers)
|
||
console.log('接口返回REDIRECT', response.headers.redirect)
|
||
if (response.headers.redirect) {
|
||
window.location.href = response.headers.redirect
|
||
return Promise.reject(error)
|
||
}
|
||
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 http
|