import { Message } from 'element-ui'; import config from '~/config/app' function checkParams(params) { if (typeof params != 'object') return params for (let key in params) { const value = params[key]; if (value === null || value === undefined || value === "") { delete params[key]; } } return params; } export default function({ $axios, redirect, store }, inject) { $axios.setBaseURL(config.baseUrl + '/api/v1') $axios.onRequest(config => { config.data = checkParams(config.data) config.params = checkParams(config.params) if (config.method == 'GET') { config.url += paramsToStr(config.params) } config.headers.token = store.state.token // config.headers['Content-Type'] = 'application/x-www-form-urlencoded' }) $axios.onResponse((response) => { const { code, show, msg } = response.data; if (code == 0 && show && msg) { Message({ message: msg, type: 'error' }) } else if (code == -1) { console.log(response.data) store.commit('logout') //todo 测试关闭登录 redirect('/account/login') } }) $axios.onError(error => { Message({ message: '系统错误', type: 'error' }) }) inject('get', $axios.$get) inject('post', $axios.$post) }