permission.js
3.95 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* 权限模块
* 1. 获取菜单权限
* 2.
*/
import router from './router'
import store from './store'
import { Message } from 'element-ui'
import Cookies from 'js-cookie'
import { getAtoken } from '@/utils/auth'
let menuName = []
function dealMenu(menu, parent) {
menu.children.forEach((item, index) => {
let parentTemp = {}
parentTemp.unid = item.unid
parentTemp.id = item.perm_unid
parentTemp.label = item.note
parentTemp.path = item.name
parentTemp.parentId = menu.unid
parentTemp.parentPermId = menu.perm_unid
parent.children.push(parentTemp)
menuName.push(item.name)
if (item.children && item.children.length > 0) {
dealChildren(item, parent.children, index)
}
})
window.localStorage.setItem('menuName', JSON.stringify(menuName))
window.localStorage.setItem('menu', JSON.stringify(parent))
window.sessionStorage.setItem('loginVal', true)
}
function dealChildren(item, parent, index) {
item.children.forEach((child, childIndex) => {
let childTemp = {}
childTemp.unid = child.unid
childTemp.id = child.perm_unid
childTemp.label = child.note
childTemp.path = child.name
childTemp.parentId = item.unid
childTemp.parentPermId = item.perm_unid
if (!parent[index]['children']) {
parent[index]['children'] = []
parent[index]['children'].push(childTemp)
} else {
parent[index]['children'].push(childTemp)
}
menuName.push(child.name)
if (child.children && child.children.length > 0) {
dealChildren(child, parent[index]['children'], childIndex)
}
})
}
const { platform } = window._vionConfig
const whiteList = ['login']
router.beforeEach(async(to, from, next) => {
// loading....
// determine whether the user has logged in
const hasAtoken = getAtoken()
if (hasAtoken) {
if (to.path === '/') {
// if is logged in, redirect to the home page
next({ path: '/home' })
} else {
try {
// determine whether the user has his/her permission roles through get some info
if (!store.getters.accountVal) {
let accountInfo
const { userType, accountId, loginName } = store.getters.userInfo
if (userType === 'super' || !accountId) {
accountInfo = await store.dispatch('app/getAccounts', userType)
} else if (accountId) {
accountInfo = await store.dispatch('app/getAccounts', userType, { id: accountId })
}
// 2333333
Cookies.set('username', loginName)
Cookies.set('accountId', accountInfo.id)
Cookies.set('accountName', accountInfo.name)
// if the first route is mall, you can use mall api
// store.dispatch('app/getMallList', Object.assign({}, accountInfo, { status: 1 }))
}
if (!store.getters.appUnid) {
const {
data: {
menu_tree
}
} = await store.dispatch('users/getMenu', platform)
let menuArray = []
if (menu_tree.length >= 2) {
menuArray.push(menu_tree[1])
} else {
menuArray.push(menu_tree[0])
}
if (menuArray.length) {
menuArray.forEach((menu, index) => {
let root = {}
root.unid = menu.unid
root.id = menu.perm_unid
root.children = []
if (platform === 'mall') {
root.label = '智慧商业客流分析平台'
} else {
root.label = '智慧连锁客流分析平台'
}
menuName = []
dealMenu(menu, root)
})
next({ path: '/home' })
}
}
} catch (error) {
store.dispatch('users/logout')
Message.error(error || 'Error!!')
next('/')
console.error(error)
}
}
next()
} else {
// hasn't atoken
if (whiteList.includes(to.name)) {
next()
} else {
next({
path: '/'
})
}
}
})