2019-02-14 11:38:09 +08:00
|
|
|
|
/**
|
|
|
|
|
* 全站http配置
|
|
|
|
|
*
|
|
|
|
|
* axios参数说明
|
|
|
|
|
* isSerialize是否开启form表单提交
|
|
|
|
|
* isToken是否需要token
|
|
|
|
|
*/
|
|
|
|
|
import axios from 'axios'
|
|
|
|
|
import store from '@/store/';
|
|
|
|
|
import router from '@/router/router'
|
2019-03-31 22:32:30 +08:00
|
|
|
|
import {serialize} from '@/util/util'
|
|
|
|
|
import {getToken} from '@/util/auth'
|
|
|
|
|
import {Message} from 'element-ui'
|
2019-02-14 11:38:09 +08:00
|
|
|
|
import website from '@/config/website';
|
|
|
|
|
import NProgress from 'nprogress' // progress bar
|
|
|
|
|
import 'nprogress/nprogress.css' // progress bar style
|
2019-03-31 22:32:30 +08:00
|
|
|
|
import {Base64} from 'js-base64';
|
|
|
|
|
|
2019-02-14 11:38:09 +08:00
|
|
|
|
axios.defaults.timeout = 10000;
|
|
|
|
|
//返回其他状态吗
|
|
|
|
|
axios.defaults.validateStatus = function (status) {
|
2019-03-31 22:32:30 +08:00
|
|
|
|
return status >= 200 && status <= 500; // 默认的
|
2019-02-14 11:38:09 +08:00
|
|
|
|
};
|
|
|
|
|
//跨域请求,允许保存cookie
|
|
|
|
|
axios.defaults.withCredentials = true;
|
|
|
|
|
// NProgress Configuration
|
|
|
|
|
NProgress.configure({
|
2019-03-31 22:32:30 +08:00
|
|
|
|
showSpinner: false
|
2019-02-14 11:38:09 +08:00
|
|
|
|
});
|
|
|
|
|
//HTTPrequest拦截
|
|
|
|
|
axios.interceptors.request.use(config => {
|
2019-03-31 22:32:30 +08:00
|
|
|
|
NProgress.start() // start progress bar
|
|
|
|
|
const meta = (config.meta || {});
|
|
|
|
|
const isToken = meta.isToken === false;
|
|
|
|
|
config.headers['Authorization'] = `Basic ${Base64.encode(`${website.clientId}:${website.clientSecret}`)}`;
|
|
|
|
|
if (getToken() && !isToken) {
|
|
|
|
|
config.headers['Blade-Auth'] = 'bearer ' + getToken() // 让每个请求携带token--['Authorization']为自定义key 请根据实际情况自行修改
|
|
|
|
|
}
|
|
|
|
|
//headers中配置serialize为true开启序列化
|
|
|
|
|
if (config.method === 'post' && meta.isSerialize === true) {
|
|
|
|
|
config.data = serialize(config.data);
|
|
|
|
|
}
|
|
|
|
|
return config
|
2019-02-14 11:38:09 +08:00
|
|
|
|
}, error => {
|
2019-03-31 22:32:30 +08:00
|
|
|
|
return Promise.reject(error)
|
2019-02-14 11:38:09 +08:00
|
|
|
|
});
|
|
|
|
|
//HTTPresponse拦截
|
|
|
|
|
axios.interceptors.response.use(res => {
|
2019-03-31 22:32:30 +08:00
|
|
|
|
NProgress.done();
|
|
|
|
|
const status = res.data.code || 200
|
|
|
|
|
const statusWhiteList = website.statusWhiteList || [];
|
|
|
|
|
const message = res.data.msg || '未知错误';
|
|
|
|
|
//如果在白名单里则自行catch逻辑处理
|
|
|
|
|
if (statusWhiteList.includes(status)) return Promise.reject(res);
|
|
|
|
|
//如果是401则跳转到登录页面
|
|
|
|
|
if (status === 401) store.dispatch('FedLogOut').then(() => router.push({path: '/login'}));
|
|
|
|
|
// 如果请求为非200否者默认统一处理
|
|
|
|
|
if (status !== 200) {
|
|
|
|
|
Message({
|
|
|
|
|
message: message,
|
|
|
|
|
type: 'error'
|
|
|
|
|
})
|
|
|
|
|
return Promise.reject(new Error(message))
|
|
|
|
|
}
|
|
|
|
|
return res;
|
2019-02-14 11:38:09 +08:00
|
|
|
|
}, error => {
|
2019-03-31 22:32:30 +08:00
|
|
|
|
NProgress.done();
|
|
|
|
|
return Promise.reject(new Error(error));
|
2019-02-14 11:38:09 +08:00
|
|
|
|
})
|
|
|
|
|
|
2019-02-14 17:36:47 +08:00
|
|
|
|
export default axios;
|