在iview admin中封装axios请求
在libs目录下建立axios.js文件
axios.js:
import Vue from 'vue';
import iView from 'iview';
import {router} from '../router/index';
import {appRouter} from '../router/router';
import store from '../store';
import App from '../app.vue';
import qs from 'qs';
import axios from 'axios';
import { Message } from 'iview';
import utils from './util';
import Cookies from 'js-cookie';
const vm=new Vue({
router: router,
store: store,
render: h => h(App),
data: {
currentPageName: '',
},
mounted () {
this.currentPageName = this.$route.name;
// 显示打开的页面的列表
this.$store.commit('setOpenedList');
this.$store.commit('initCachepage');
// 权限菜单过滤相关
this.$store.commit('updateMenulist');
// iview-admin检查更新
// util.checkUpdate(this);
},
created () {
let tagsList = [];
appRouter.map((item) => {
if (item.children.length <= 1) {
tagsList.push(item.children[0]);
} else {
tagsList.push(...item.children);
}
});
this.$store.commit('setTagsList', tagsList);
}
});
let baseUrl = axios.defaults.baseURL+'admin_api/';
//baseUrl 设置成你自己的请求地址哦
const CancelToken = axios.CancelToken;
const source = CancelToken.source();
//设置token
function setToken() {
if (Cookies.get('admin_access_token')) {
axios.defaults.headers.common['Authorization'] = 'Bearer ' + Cookies.get('admin_access_token');
}
}
//请求验证拦截器
axios.interceptors.request.use(config => {
// store.commit('UPDATE_LOADING', true); //显示loading,这里没有用vuex可以省去
//判断请求不是登陆接口的时候,字段是不是过期了,(根据业务逻辑做对应的处理)
if (config