| | |
| | | import axios from 'axios'; |
| | | import store from '@/store/'; |
| | | import router from '@/router/router'; |
| | | import {serialize} from '@/util/util'; |
| | | import {getToken} from '@/util/auth'; |
| | | import {Message} from 'element-ui'; |
| | | import { serialize } from '@/util/util'; |
| | | import { getToken } from '@/util/auth'; |
| | | // import { Message } from 'element-ui'; |
| | | import { MessageBox } from 'element-ui'; |
| | | import website from '@/config/website'; |
| | | import {Base64} from 'js-base64'; |
| | | import { Base64 } from 'js-base64'; |
| | | import NProgress from 'nprogress'; |
| | | import 'nprogress/nprogress.css'; |
| | | |
| | | //默认超时时间 |
| | | axios.defaults.timeout = 10000; |
| | | axios.defaults.timeout = 60000; |
| | | //返回其他状态码 |
| | | axios.defaults.validateStatus = function (status) { |
| | | return status >= 200 && status <= 500; |
| | | axios.defaults.validateStatus = function(status) { |
| | | return status >= 200 && status <= 500; |
| | | }; |
| | | //跨域请求,允许保存cookie |
| | | axios.defaults.withCredentials = true; |
| | | // NProgress 配置 |
| | | NProgress.configure({ |
| | | showSpinner: false |
| | | showSpinner: false |
| | | }); |
| | | //http request拦截 |
| | | axios.interceptors.request.use(config => { |
| | | //开启 progress bar |
| | | NProgress.start(); |
| | | const meta = (config.meta || {}); |
| | | const isToken = meta.isToken === false; |
| | | config.headers['Authorization'] = `Basic ${Base64.encode(`${website.clientId}:${website.clientSecret}`)}`; |
| | | //开启 progress bar |
| | | NProgress.start(); |
| | | const meta = (config.meta || {}); |
| | | const isToken = meta.isToken === false; |
| | | config.headers['Authorization'] = `Basic ${Base64.encode(`${website.clientId}:${website.clientSecret}`)}`; |
| | | //让每个请求携带token |
| | | if (getToken() && !isToken) { |
| | | config.headers[website.tokenHeader] = 'bearer ' + getToken() |
| | |
| | | if (status === 401) store.dispatch('FedLogOut').then(() => router.push({path: '/login'})); |
| | | // 如果请求为非200否者默认统一处理 |
| | | if (status !== 200) { |
| | | Message({ |
| | | message: message, |
| | | type: 'error' |
| | | }); |
| | | // Message({ |
| | | // message: message, |
| | | // type: 'error' |
| | | // }); |
| | | MessageBox.alert(message); |
| | | // alert(message); |
| | | return Promise.reject(new Error(message)) |
| | | } |
| | | return res; |
| | |
| | | return Promise.reject(new Error(error)); |
| | | }); |
| | | |
| | | export default axios; |
| | | export default axios; |