router.js文件
import Vue from 'vue'
import Router from 'vue-router'
import vm from '../main'
import { path } from './path'
Vue.use(Router)
const routes = []
path.forEach((item, i) => {
routes.push({ path: item.path, component: resolve => require(['../components/' + item.path.substring(1, 2).toUpperCase() + item.path.substring(2) + '/' + item.path.substring(1, 2).toUpperCase() + item.path.substring(2) + '.vue'], resolve), children: [] })
if (item.children) {
item.children.forEach((item1, j) => {
routes[i].children.push({ path: item1.path, component: resolve => require(['../components/' + item1.path.substring(0, 1).toUpperCase() + item1.path.substring(1) + '/' + item1.path.substring(0, 1).toUpperCase() + item1.path.substring(1) + '.vue'], resolve), children: [] })
if (item1.children) {
item1.children.forEach((item2, k) => {
routes[i].children[j].children.push({ path: item2.path, component: resolve => require(['../views/' + item2.path.substring(0, 1).toUpperCase() + item2.path.substring(1) + '.vue'], resolve), children: [] })
if (item2.children) {
item2.children.forEach((item3, l) => {
routes[i].children[j].children[k].children.push({ path: item3.path, component: resolve => require(['../views/' + item3.path.substring(0, 1).toUpperCase() + item3.path.substring(1) + '/' + item3.path.substring(0, 1).toUpperCase() + item3.path.substring(1) + '.vue'], resolve), children: [] })
if (item3.children) {
item3.children.forEach((item4) => {
routes[i].children[j].children[k].children[l].children.push({ path: item4.path, component: resolve => require(['../views/' + item4.path.substring(0, 1).toUpperCase() + item4.path.substring(1) + '/' + item4.path.substring(0, 1).toUpperCase() + item4.path.substring(1) + '.vue'], resolve) })
})
}
})
}
})
}
})
}
});
const router = new Router({
routes,
// routes: [
// {
// path: '/index',
// component: Index,
// children:[{path:'tab',component: resolve => require(['../components/Tab/Tab.vue'], resolve),children: [{ path: 'test', component: resolve => require(['../views/Test.vue'], resolve) },{ path: 'test2', component: resolve => require(['../views/Test2.vue'], resolve) },{ path: 'test3', component: resolve => require(['../views/Test3.vue'], resolve)},{ path: 'test4', component: resolve => require(['../views/Test4.vue'], resolve) }]}]
// },
// {
// }
// ]
})
// // 全局路由守卫
router.beforeEach((to, from, next) => {
let isLogin = JSON.parse(sessionStorage.getItem('userInformation'))
// next()
; // 是否登录
console.log(isLogin)
if (to.path.indexOf('index') != -1) {
if (!isLogin) {
next({ path: '/login' })
}
else {
next()
console.log(vm)
}
}
else {
next()
console.log(vm)
}
});
export default router
//拦截器文件js
import axios from 'axios'
import qs from 'qs'
import store from '../vuex'
import router from '../router/index'
console.log(store.state.a.routerPath)
//封装axios
const instanceJson = axios.create({
baseURL:'http://192.168.12.15:9112',//查询单位
withCredentials: true,//是否携带cookie信息
timeout: 150000,
headers: { 'Content-Type': 'application/json,charset=utf-8' }
});
instanceJson.defaults.withCredentials=true
//设置token,token存储在vuex的moduleA中
instanceJson.defaults.headers.common['Authorization'] = sessionStorage.getItem('Authorization')
//headers的另一种格式 'application/x-www-form-urlencoded'
/****** request拦截器==>对请求参数做处理qs.stringify() ******/
instanceJson.interceptors.request.use(config => {
config.method === 'post'
? config.data = { ...config.data }
: config.params = { ...config.params };
config.headers['Content-Type'] = 'application/json';
return config;
}, error => { //请求错误处理
Promise.reject(error)
});
/****** respone拦截器==>对响应做处理 ******/
instanceJson.interceptors.response.use(
response => { //成功请求到数据
//这里根据后端提供的数据进行对应的处理
if (response.data.message === 'success' || response.data.success == true || response.data.success == false) {
return response;
} else {
//常规错误处理
console.log('超时了')
router.push('/login')
sessionStorage.removeItem('userInformation')
sessionStorage.removeItem('Authorization')
}
},
error => { //响应错误处理
console.log('error');
console.log(error);
console.log(JSON.stringify(error));
return Promise.reject(error)
}
)
export default instanceJson