1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| import Vue from 'vue'; import axios from 'axios'; import { Message } from 'element-ui'; Vue.component(Message.name, Message); let sessionLoginId = sessionStorage.getItem('LoginId') || ''; axios.interceptors.request.use((res) => { res.headers['TIME_TOKEN'] = new Date().getTime(); res.headers['Cache-Control'] = 'no-cache'; res.headers['Token'] = sessionLoginId; try { if (!res) return; res.headers['content-type'] = 'application/json; charset=UTF-8'; if (res.method === 'get') { res.data = {unused: 0}; } } catch (e) { console.error(e); } return res; }, (err) => { return Promise.reject(err); });
axios.interceptors.response.use(res => { return res; }, err => { if (err && err.response && err.response.data.code === 40005) { location.href = err.response.data.extraInfo; } else if (err && err.response && err.response.data.code === 40013) { Vue.prototype.$message({ message: err.response.data.msg || '系统异常,请联系管理员!', type: 'error' }); setTimeout(function() { axios.get('/admin/logout'); sessionStorage.clear(); }, 3000); } else { let errRes = err.response.data.extraInfo || err.response.data.msg || '系统异常,请联系管理员!'; if (errRes.length >= 40) errRes = errRes.slice(0, 40) + '...';
Vue.prototype.$message({ message: errRes, type: 'error' }); } return Promise.reject(err); } );
export default class BaseApi { constructor() { this.axios = axios; } }
|