-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathstartup.js
188 lines (168 loc) · 5.93 KB
/
startup.js
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// import getPageTitle from '@/utils/get-page-title'
import store from '@/store'
import router, { resetRouter } from '@/router'
import Vue from 'vue'
import { message } from '@/utils/message'
import orgUtil from '@/utils/org'
import orgs from '@/api/orgs'
import { getPropView, isViewHasOrgs } from '@/utils/jms'
const whiteList = ['/login', process.env.VUE_APP_LOGIN_PATH] // no redirect whitelist
const autoEnterOrgs = ['00000000-0000-0000-0000-000000000001', '00000000-0000-0000-0000-000000000000']
function reject(msg) {
return new Promise((resolve, reject) => reject(msg))
}
async function checkLogin({ to, from, next }) {
if (whiteList.indexOf(to.path) !== -1) {
next()
}
try {
return await store.dispatch('users/getProfile')
} catch (e) {
Vue.$log.error(e)
const status = e.response.status
if (store.getters.currentOrg.autoEnter) {
await store.dispatch('users/setCurrentOrg', store.getters.preOrg)
}
if (status === 401 || status === 403) {
setTimeout(() => {
window.location = process.env.VUE_APP_LOGIN_PATH
}, 100)
}
return reject('No profile get: ' + e)
}
}
async function getPublicSetting({ to, from, next }, isOpen) {
// 获取Public settings
const publicSettings = store.getters.publicSettings
if (!publicSettings || Object.keys(publicSettings).length === 0 || !isOpen) {
await store.dispatch('settings/getPublicSettings', isOpen)
}
}
async function refreshCurrentOrg() {
return orgs.getCurrentOrg().then(org => {
// Root 就不刷新本地的了, 会影响 autoEnter
if (autoEnterOrgs.indexOf(org.id) !== -1) {
return
}
store.dispatch('users/setCurrentOrg', org)
})
}
async function changeCurrentOrgIfNeed({ to, from, next }) {
await store.dispatch('users/getProfile')
const usingOrgs = store.getters.usingOrgs
if (!usingOrgs || usingOrgs.length === 0) {
Vue.$log.debug('No using orgs, return: ', usingOrgs)
return
}
await refreshCurrentOrg()
const currentOrg = store.getters.currentOrg
if (!currentOrg || typeof currentOrg !== 'object') {
Vue.$log.error('Current org is null or not a object: ', currentOrg)
await orgUtil.change2PropOrg({ to, from, next })
}
const globalOrgPath = [
'/console/perms/login-acls/', '/console/users/roles/',
'/console/perms/connect-method-acls/', '/settings/'
]
if (autoEnterOrgs.indexOf(currentOrg.id) !== -1 && currentOrg.autoEnter) {
const delta = new Date().getTime() - currentOrg.autoEnter
const notNeedChange = globalOrgPath.find(path => to.path.indexOf(path) === 0)
if (!notNeedChange && delta > 3000) {
await orgUtil.change2PropOrg({ to, from, next })
}
return
}
if (!orgUtil.hasCurrentOrgPermission()) {
Vue.$log.error('Not has current org permission: ', currentOrg)
await orgUtil.change2PropOrg({ to, from, next })
}
}
export async function generatePageRoutes({ to, from, next }) {
// determine whether the user has obtained his permission roles through getProfile
resetRouter()
try {
// try get user profile
// generate accessible routes map based on roles
let accessRoutes = await store.dispatch('permission/generateRoutes', { to, from })
// Incorrect route, jump to 404
accessRoutes = [...accessRoutes, {
path: '*',
redirect: '/404',
hidden: true
}]
// dynamically add accessible routes
Vue.$log.debug('All routes:', accessRoutes.reduce((acc, cur) => {
acc[cur.name] = cur
return acc
}, {}))
router.addRoutes(accessRoutes)
await store.dispatch('permission/generateViewRoutes', { to, from })
// hack method to ensure that addRoutes is complete
// set the replace: true, so the navigation will not leave a history record
// Vue.$log.debug('Next to: ', to)
next({ ...to, replace: true })
} catch (error) {
// remove token and go to login page to re-login
// await store.dispatch('user/resetToken')
message.error(error || 'Has Error')
Vue.$log.error('Error occur: ', error)
}
}
export async function checkUserFirstLogin({ to, from, next }) {
if (store.state.users.profile.is_first_login) {
next('/profile/improvement')
}
}
export async function changeCurrentViewIfNeed({ to, from, next }) {
let viewName = to.path.split('/')[1]
// 这几个是需要检测的, 切换视图组织时,避免 404, 这里不能加 settings, 因为 默认没有返回 setting 组织(System) 的管理权限
if (['console', 'audit', 'workbench', 'tickets', 'pam', ''].indexOf(viewName) === -1) {
Vue.$log.debug('Current view no need check', viewName)
return
}
const has = isViewHasOrgs(viewName)
Vue.$log.debug('Change has current view, has perm: ', viewName, '=>', has)
if (has) {
await store.dispatch('users/changeToView', viewName)
return
}
viewName = getPropView()
// Next 之前要重置 init 状态,否则这些路由守卫就不走了
await store.dispatch('app/reset')
next(`/${viewName}/`)
return new Promise((resolve, reject) => reject(''))
}
function onI18nLoaded() {
return new Promise(resolve => {
const load = store.state.app.i18nLoaded
if (load) {
resolve()
}
const itv = setInterval(() => {
const load = store.state.app.i18nLoaded
if (load) {
clearInterval(itv)
resolve()
}
}, 100)
})
}
export async function startup({ to, from, next }) {
// if (store.getters.inited) { return true }
if (store.getters.inited) {
return true
}
await store.dispatch('app/init')
// set page title
// await getOpenPublicSetting({ to, from, next })
await getPublicSetting({ to, from, next }, true)
await checkLogin({ to, from, next })
await onI18nLoaded()
await getPublicSetting({ to, from, next }, false)
await changeCurrentViewIfNeed({ to, from, next })
await changeCurrentOrgIfNeed({ to, from, next })
await generatePageRoutes({ to, from, next })
await checkUserFirstLogin({ to, from, next })
await store.dispatch('assets/getAssetCategories')
return true
}