|
|
|
@ -5,36 +5,39 @@ |
|
|
|
* isSerialize是否开启form表单提交 |
|
|
|
* isSerialize是否开启form表单提交 |
|
|
|
* isToken是否需要token |
|
|
|
* isToken是否需要token |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
import axios from 'axios' |
|
|
|
import axios from 'axios'; |
|
|
|
import store from '@/store/'; |
|
|
|
import store from '@/store/'; |
|
|
|
import router from '@/router/router' |
|
|
|
import router from '@/router/router'; |
|
|
|
import {serialize} from '@/util/util' |
|
|
|
import {serialize} from '@/util/util'; |
|
|
|
import {getToken} from '@/util/auth' |
|
|
|
import {getToken} from '@/util/auth'; |
|
|
|
import {Message} from 'element-ui' |
|
|
|
import {Message} from 'element-ui'; |
|
|
|
import website from '@/config/website'; |
|
|
|
import website from '@/config/website'; |
|
|
|
import NProgress from 'nprogress' // progress bar
|
|
|
|
import NProgress from 'nprogress'; |
|
|
|
import 'nprogress/nprogress.css' // progress bar style
|
|
|
|
import 'nprogress/nprogress.css'; |
|
|
|
import {Base64} from 'js-base64'; |
|
|
|
import {Base64} from 'js-base64'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//默认超时时间
|
|
|
|
axios.defaults.timeout = 10000; |
|
|
|
axios.defaults.timeout = 10000; |
|
|
|
//返回其他状态吗
|
|
|
|
//返回其他状态码
|
|
|
|
axios.defaults.validateStatus = function (status) { |
|
|
|
axios.defaults.validateStatus = function (status) { |
|
|
|
return status >= 200 && status <= 500; // 默认的
|
|
|
|
return status >= 200 && status <= 500; |
|
|
|
}; |
|
|
|
}; |
|
|
|
//跨域请求,允许保存cookie
|
|
|
|
//跨域请求,允许保存cookie
|
|
|
|
axios.defaults.withCredentials = true; |
|
|
|
axios.defaults.withCredentials = true; |
|
|
|
// NProgress Configuration
|
|
|
|
// NProgress 配置
|
|
|
|
NProgress.configure({ |
|
|
|
NProgress.configure({ |
|
|
|
showSpinner: false |
|
|
|
showSpinner: false |
|
|
|
}); |
|
|
|
}); |
|
|
|
//http request拦截
|
|
|
|
//http request拦截
|
|
|
|
axios.interceptors.request.use(config => { |
|
|
|
axios.interceptors.request.use(config => { |
|
|
|
NProgress.start() // start progress bar
|
|
|
|
//开启 progress bar
|
|
|
|
|
|
|
|
NProgress.start(); |
|
|
|
const meta = (config.meta || {}); |
|
|
|
const meta = (config.meta || {}); |
|
|
|
const isToken = meta.isToken === false; |
|
|
|
const isToken = meta.isToken === false; |
|
|
|
config.headers['Authorization'] = `Basic ${Base64.encode(`${website.clientId}:${website.clientSecret}`)}`; |
|
|
|
config.headers['Authorization'] = `Basic ${Base64.encode(`${website.clientId}:${website.clientSecret}`)}`; |
|
|
|
if (getToken() && !isToken) { |
|
|
|
if (getToken() && !isToken) { |
|
|
|
config.headers['Blade-Auth'] = 'bearer ' + getToken() // 让每个请求携带token--['Authorization']为自定义key 请根据实际情况自行修改
|
|
|
|
//让每个请求携带token--['Authorization']为自定义key 请根据实际情况自行修改
|
|
|
|
|
|
|
|
config.headers['Blade-Auth'] = 'bearer ' + getToken() |
|
|
|
} |
|
|
|
} |
|
|
|
//headers中配置serialize为true开启序列化
|
|
|
|
//headers中配置serialize为true开启序列化
|
|
|
|
if (config.method === 'post' && meta.isSerialize === true) { |
|
|
|
if (config.method === 'post' && meta.isSerialize === true) { |
|
|
|
@ -46,6 +49,7 @@ axios.interceptors.request.use(config => { |
|
|
|
}); |
|
|
|
}); |
|
|
|
//http response 拦截
|
|
|
|
//http response 拦截
|
|
|
|
axios.interceptors.response.use(res => { |
|
|
|
axios.interceptors.response.use(res => { |
|
|
|
|
|
|
|
//关闭 progress bar
|
|
|
|
NProgress.done(); |
|
|
|
NProgress.done(); |
|
|
|
//获取状态码
|
|
|
|
//获取状态码
|
|
|
|
const status = res.data.code || res.status; |
|
|
|
const status = res.data.code || res.status; |
|
|
|
|