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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import Vue from 'vue'
import store from "@/store"
import Router from 'vue-router'
import Cookie from '../util/cookie'
Vue.use(Router);
const router =new Router({
routes: [
{
path: '/',
name: 'index',
component: e=>require(['@/components/framework'],e),
redirect:{name:'first'},
children:[
{
path: '',
name:'first',
component: e=>require(['@/components/main'],e),
},
]
},
{
path:'/login',
name:'login',
component: e=>require(['@/components/login'],e),
meta:{
skip_auth:true,
}
},
{
path:'/outManager',
name:'outManager',
component: e=>require(['@/components/promotion'],e),
meta:{
skip_auth:true,
}
},
{
path:'/preview/:id',
name:'preview',
component: e=>require(['@/components/preview'],e),
meta:{
}
},
{
path:'/upload',
name:'upload',
component: e=>require(['@/components/framework/upload'],e),
meta:{
skip_auth:true,
}
}
]
});
router.beforeEach((to,from,next)=> {
store.commit('mainCanShow');
if(Cookie.get('cc_token')){
store.dispatch('setToken',Cookie.get('cc_token'));
store.dispatch('setPermission',JSON.parse(localStorage.getItem('permission')));
if(Cookie.get('cc_user_name') !== null){
store.dispatch('setUserName',Cookie.get('cc_user_name'))
}else{
}
}
store.state.nowTab = to.name;
if(to.matched.length > 1 && to.name !== 'first'){
let thisMenu;
for (let i = 0 ; i < store.state.menuList.length ; i ++ ){
let _this = store.state.menuList[i];
for (let j = 0 ; j < _this.list.length ; j++){
let _that = _this.list[j];
if (_that.path === to.path){
thisMenu = _that
}
}
}
if(store.state.openedTab.indexOf(thisMenu) < 0){
store.state.openedTab.push(thisMenu);
}
}
if(to.meta.readonly){
store.dispatch('readonly',true)
}else{
store.dispatch('readonly',false)
}
if(to.name==='userDetail'){
let list = JSON.parse(localStorage.getItem('permission'))
let readonly = list.find(i=>{
return i.cover === '3-2'
}).readonly;
store.dispatch('readonly',readonly)
}
if(to.meta.delete){
store.dispatch('deletePermission',true)
}else {
store.dispatch('deletePermission',false)
}
// 登录拦截
if (to.matched.some(record => record.meta.skip_auth !== true) && !store.state.token) {
next({
path: '/login'
})
} else {
next()
}
});
router.onError((error) => {
const pattern = /Loading chunk (\d)+ failed/g;
const isChunkLoadFailed = error.message.match(pattern);
const targetPath = router.history.pending.fullPath;
if (isChunkLoadFailed) {
router.replace(targetPath);
}
});
export default router;