/* --- === Axios packaging === --- */
import axios from 'axios'
import qs from 'qs'
import { Message } from 'element-ui'
import router from '.. /router'
const Axios = axios.create({
baseURL: 'https://*********.com',
timeout: 9000,
responseType: 'json'Said, / * - = = = cross-domain request whether need to use the voucher cookie = = = -- -- - * / / / withCredentials:'true',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'}}) / * - = = = Axios request interceptor = = = -- -- - * / Axios. Interceptors. Request. Use (config = > {if (config.method === 'post') {config.data = qs.stringify(config.data)} /* -- === authorisation === -- */if (localStorage.token) {
config.headers.Authorzation = localStorage.token
}
return config
},
error => {
Message({
showClose: true,
message: error && error.data.error.message,
type: 'error'
})
returnPromise. Reject (the error data. The error. The message)}) / * - interceptor = = = = = = Axios response - * / Axios. Interceptors. Response. Use (res = > {if (res.status === 200) {
switch (res.data.code) {
caseZero:return res.data.data && Promise.resolve(res)
case 100:
return hint('Wrong Operation ! '.'warning')
case 401:
return hint('Login Failure, Please Login Again ! '.'error'.true)
case 403:
return hint('No Operation Permission ! '.'warning')
case 404:
return hint('Access address does not exist ! '.'error')
case 500:
return hint('Request Failed ! '.'warning')
default:
hint('Unknown Error ! '.'error')}}else {
return Promise.reject(res)
}
},
error => {
if (window.localStorage.getItem('loginUserBaseInfo')) {
hint('Login failure, Please login again'.'error'.true)}else {
switch (error.response.status) {
case 404:
hint('Request Address Error ! '.'error')
break
case 503:
hint('Service Unavailable ! '.'error')
break
default:
hint('Unknown Error ! '.'error')}}return Promise.reject(error.response)
}
)
/* --- === Export axios alias $http= = = -- -- - * /export default {
install: function (Vue, Option) {
Object.defineProperty(Vue.prototype, '$http', {value: Axios})
}
}
/* --- === Hint info === --- */
function hint ($message.$type.$error = null) {
Message({
showClose: true,
message: $message.type: $type
})
if ($error) {
setTimeout(() => {
router.push({
path: '/login'}, 3000)}}Copy the code