vue2-client 1.10.32-alpha → 1.10.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,266 +1,263 @@
1
- import { hasAuthority } from '@vue2-client/utils/authority-utils'
2
- import { loginIgnore } from '@vue2-client/router/index'
3
- import { checkAuthorization, METHOD, request } from '@vue2-client/utils/request'
4
- import NProgress from 'nprogress'
5
- import { logout } from '@vue2-client/services/user'
6
- import {
7
- startLogin,
8
- checkSingleAuthorization,
9
- } from '@vue2-client/utils/login'
10
- import AES from '@vue2-client/utils/EncryptUtil'
11
- import Vue from 'vue'
12
- import { funcToRouter, loadRoutes } from '@vue2-client/utils/routerUtil'
13
- import { indexedDB } from '@vue2-client/utils/indexedDB'
14
-
15
- NProgress.configure({ showSpinner: false })
16
-
17
- /**
18
- * 进度条开始
19
- * @param to
20
- * @param form
21
- * @param next
22
- */
23
- const progressStart = (to, from, next) => {
24
- // start progress bar
25
- if (!NProgress.isStarted()) {
26
- NProgress.start()
27
- }
28
- next()
29
- }
30
-
31
- /**
32
- * 登录守卫
33
- * @param to
34
- * @param form
35
- * @param next
36
- * @param options
37
- */
38
- const loginGuard = (to, from, next, options) => {
39
- const urlParams = new URLSearchParams(window.location.search)
40
- const LoginTicket = urlParams.get('LoginTicket')
41
- // 如果是需要我们自己登陆的项目
42
- const token = urlParams.get('token')
43
- // 如果是v2 的项目 资源管理和营收分开库的那种项目 传递用户信息进来
44
- const tokenV2 = urlParams.get('tokenV2')
45
- // 如果是 v4 的父项目,通过localStorage传递信息
46
- const V4_LOGIN_DATA_KEY = localStorage.getItem('v4LoginData')
47
- if (LoginTicket) {
48
- const loginTicket = decodeURIComponent(LoginTicket)
49
- const { AESDecrypt } = AES
50
- const info = AESDecrypt(loginTicket, '3KMKqvgwR8ULbR8Z')
51
- const { store, message, router } = options
52
- // 单页面通常是嵌入到v3的项目中,没有单独的清除按钮,所以需要清除indexedDB
53
- indexedDB.clear()
54
- if (
55
- (!loginIgnore.includes(to) || to.name === '404') &&
56
- !checkSingleAuthorization('SinglePage', loginTicket)
57
- ) {
58
- localStorage.setItem('SinglePage_TOKEN', loginTicket)
59
- startLogin({
60
- ...info,
61
- pathname: window.location.pathname.replace(process.env.VUE_APP_PUBLIC_PATH, ''),
62
- store,
63
- router,
64
- })
65
- } else {
66
- const roles = store.getters['account/roles']
67
- let user = {}
68
- try {
69
- user = JSON.parse(localStorage.getItem(process.env.VUE_APP_USER_KEY))
70
- } catch (e) {
71
- user = localStorage.getItem(process.env.VUE_APP_USER_KEY)
72
- }
73
- const setUser = store._mutations['account/setUser'][0]
74
- setUser(user)
75
- if (roles.length === 0 && !loginIgnore.includes(to)) {
76
- message.warning('登录已失效,请重新登录')
77
- logout().finally((res) => {
78
- next({ path: '/login' })
79
- })
80
- } else {
81
- next()
82
- }
83
- }
84
- } else if (token) {
85
- const { AESDecrypt } = AES
86
- const info = AESDecrypt(token, '3KMKqvgwR8ULbR8Z')
87
- const { store, message, router } = options
88
- // 单页面通常是嵌入到v3的项目中,没有单独的清除按钮,所以需要清除indexedDB
89
- indexedDB.clear()
90
- if (
91
- (!loginIgnore.includes(to) || to.name === '404') &&
92
- !checkSingleAuthorization('SinglePage', token)
93
- ) {
94
- localStorage.setItem('SinglePage_TOKEN', token)
95
- startLogin({
96
- ...info,
97
- pathname: window.location.pathname.replace(process.env.VUE_APP_PUBLIC_PATH, ''),
98
- store,
99
- router,
100
- })
101
- } else {
102
- const roles = store.getters['account/roles']
103
- let user = {}
104
- try {
105
- user = JSON.parse(localStorage.getItem(process.env.VUE_APP_USER_KEY))
106
- } catch (e) {
107
- user = localStorage.getItem(process.env.VUE_APP_USER_KEY)
108
- }
109
- const setUser = store._mutations['account/setUser'][0]
110
- setUser(user)
111
- if (roles.length === 0 && !loginIgnore.includes(to)) {
112
- message.warning('登录已失效,请重新登录')
113
- logout().finally((res) => {
114
- next({ path: '/login' })
115
- })
116
- } else {
117
- next()
118
- }
119
- }
120
- } else if (tokenV2) {
121
- const { AESDecrypt } = AES
122
- const info = AESDecrypt(tokenV2, '3KMKqvgwR8ULbR8Z')
123
- request(`/rs/user/${info.name}/${info.password}/智慧燃气`, METHOD.GET).then(
124
- res => {
125
- const curUser = {
126
- name: res.name,
127
- username: res.name,
128
- orgid: res.name,
129
- avatar: '',
130
- ename: res.ename,
131
- id: res.id,
132
- orgName: res.f_fengongsi,
133
- contact: '',
134
- phone: '',
135
- orgs: res.orgs
136
- }
137
- Vue.$store.commit('account/setUser', Object.assign(curUser, res))
138
- const login = {
139
- f: res,
140
- jwt: res.id,
141
- r: res.roles
142
- }
143
- Vue.$store.commit('account/setLogin', login)
144
- loadRoutes(funcToRouter(res.functions))
145
- next()
146
- }
147
- )
148
- } else if (V4_LOGIN_DATA_KEY) {
149
- const v4LoginData = JSON.parse(localStorage.getItem('userInfo'))
150
- const curUser = {
151
- name: v4LoginData.name,
152
- username: v4LoginData.name,
153
- orgid: v4LoginData.name,
154
- avatar: '',
155
- ename: v4LoginData.ename,
156
- id: v4LoginData.id,
157
- orgName: v4LoginData.orgs,
158
- contact: '',
159
- phone: '',
160
- orgs: v4LoginData.orgs
161
- }
162
- Vue.$store.commit('account/setUser', Object.assign(curUser, v4LoginData))
163
- const login = {
164
- f: v4LoginData,
165
- jwt: v4LoginData.id,
166
- r: v4LoginData.r
167
- }
168
- Vue.$store.commit('account/setLogin', login)
169
- if (!Vue.$store.state?.setting?.menuData || Vue.$store.state?.setting?.menuData?.length === 0) {
170
- loadRoutes(funcToRouter(v4LoginData.functions))
171
- }
172
-
173
- if (window.__MICRO_APP_ENVIRONMENT__) {
174
- window.microApp.forceDispatch({ type: 'microMounted', appName: window.__MICRO_APP_NAME__ })
175
- }
176
-
177
- console.warn(to.fullPath, from.fullPath)
178
- next()
179
- } else {
180
- const { store, message } = options
181
- if (
182
- (!loginIgnore.includes(to) || to.name === '404') &&
183
- !checkAuthorization()
184
- ) {
185
- message.warning('登录已失效,请重新登录')
186
- next({ path: '/login' })
187
- } else {
188
- const roles = store.getters['account/roles']
189
- if (roles.length === 0 && !loginIgnore.includes(to)) {
190
- message.warning('登录已失效,请重新登录')
191
- logout().finally((res) => {
192
- next({ path: '/login' })
193
- })
194
- } else {
195
- next()
196
- }
197
- }
198
- }
199
- }
200
-
201
- /**
202
- * 权限守卫
203
- * @param to
204
- * @param form
205
- * @param next
206
- * @param options
207
- */
208
- const authorityGuard = (to, from, next, options) => {
209
- const { store, message } = options
210
- const permissions = store.getters['account/permissions']
211
- const roles = store.getters['account/roles']
212
- if (!hasAuthority(to, permissions, roles)) {
213
- message.warning(`对不起,您无权访问页面: ${to.fullPath},请联系管理员`)
214
- next({ path: '/403' })
215
- // NProgress.done()
216
- } else {
217
- next()
218
- }
219
- }
220
-
221
- /**
222
- * 混合导航模式下一级菜单跳转重定向
223
- * @param to
224
- * @param from
225
- * @param next
226
- * @param options
227
- * @returns {*}
228
- */
229
- const redirectGuard = (to, from, next, options) => {
230
- const { store } = options
231
- const getFirstChild = (routes) => {
232
- const route = routes[0]
233
- if (!route.children || route.children.length === 0) {
234
- return route
235
- }
236
- return getFirstChild(route.children)
237
- }
238
- if (store.state.setting.layout === 'mix') {
239
- const firstMenu = store.getters['setting/firstMenu']
240
- if (firstMenu.find((item) => item.fullPath === to.fullPath)) {
241
- store.commit('setting/setActivatedFirst', to.fullPath)
242
- const subMenu = store.getters['setting/subMenu']
243
- if (subMenu.length > 0) {
244
- const redirect = getFirstChild(subMenu)
245
- return next({ path: redirect.fullPath })
246
- }
247
- }
248
- }
249
- next()
250
- }
251
-
252
- /**
253
- * 进度条结束
254
- * @param to
255
- * @param form
256
- * @param options
257
- */
258
- const progressDone = () => {
259
- // finish progress bar
260
- NProgress.done()
261
- }
262
-
263
- export default {
264
- beforeEach: [progressStart, loginGuard, authorityGuard, redirectGuard],
265
- afterEach: [progressDone],
266
- }
1
+ import { hasAuthority } from '@vue2-client/utils/authority-utils'
2
+ import { loginIgnore } from '@vue2-client/router/index'
3
+ import { checkAuthorization, METHOD, request } from '@vue2-client/utils/request'
4
+ import NProgress from 'nprogress'
5
+ import { logout } from '@vue2-client/services/user'
6
+ import {
7
+ startLogin,
8
+ checkSingleAuthorization,
9
+ } from '@vue2-client/utils/login'
10
+ import AES from '@vue2-client/utils/EncryptUtil'
11
+ import Vue from 'vue'
12
+ import { funcToRouter, loadRoutes } from '@vue2-client/utils/routerUtil'
13
+ import { indexedDB } from '@vue2-client/utils/indexedDB'
14
+
15
+ NProgress.configure({ showSpinner: false })
16
+
17
+ /**
18
+ * 进度条开始
19
+ * @param to
20
+ * @param form
21
+ * @param next
22
+ */
23
+ const progressStart = (to, from, next) => {
24
+ // start progress bar
25
+ if (!NProgress.isStarted()) {
26
+ NProgress.start()
27
+ }
28
+ next()
29
+ }
30
+
31
+ /**
32
+ * 登录守卫
33
+ * @param to
34
+ * @param form
35
+ * @param next
36
+ * @param options
37
+ */
38
+ const loginGuard = (to, from, next, options) => {
39
+ if (window.__MICRO_APP_ENVIRONMENT__) {
40
+ window.microApp.dispatch({ type: '子应用发送给主应用的数据', to: to.path })
41
+ }
42
+ const urlParams = new URLSearchParams(window.location.search)
43
+ const LoginTicket = urlParams.get('LoginTicket')
44
+ // 如果是需要我们自己登陆的项目
45
+ const token = urlParams.get('token')
46
+ // 如果是v2 的项目 资源管理和营收分开库的那种项目 传递用户信息进来
47
+ const tokenV2 = urlParams.get('tokenV2')
48
+ // 如果是 v4 的父项目,通过localStorage传递信息
49
+ const V4_LOGIN_DATA_KEY = localStorage.getItem('v4LoginData')
50
+ if (LoginTicket) {
51
+ const loginTicket = decodeURIComponent(LoginTicket)
52
+ const { AESDecrypt } = AES
53
+ const info = AESDecrypt(loginTicket, '3KMKqvgwR8ULbR8Z')
54
+ const { store, message, router } = options
55
+ // 单页面通常是嵌入到v3的项目中,没有单独的清除按钮,所以需要清除indexedDB
56
+ indexedDB.clear()
57
+ if (
58
+ (!loginIgnore.includes(to) || to.name === '404') &&
59
+ !checkSingleAuthorization('SinglePage', loginTicket)
60
+ ) {
61
+ localStorage.setItem('SinglePage_TOKEN', loginTicket)
62
+ startLogin({
63
+ ...info,
64
+ pathname: window.location.pathname.replace(process.env.VUE_APP_PUBLIC_PATH, ''),
65
+ store,
66
+ router,
67
+ })
68
+ } else {
69
+ const roles = store.getters['account/roles']
70
+ let user = {}
71
+ try {
72
+ user = JSON.parse(localStorage.getItem(process.env.VUE_APP_USER_KEY))
73
+ } catch (e) {
74
+ user = localStorage.getItem(process.env.VUE_APP_USER_KEY)
75
+ }
76
+ const setUser = store._mutations['account/setUser'][0]
77
+ setUser(user)
78
+ if (roles.length === 0 && !loginIgnore.includes(to)) {
79
+ message.warning('登录已失效,请重新登录')
80
+ logout().finally((res) => {
81
+ next({ path: '/login' })
82
+ })
83
+ } else {
84
+ next()
85
+ }
86
+ }
87
+ } else if (token) {
88
+ const { AESDecrypt } = AES
89
+ const info = AESDecrypt(token, '3KMKqvgwR8ULbR8Z')
90
+ const { store, message, router } = options
91
+ // 单页面通常是嵌入到v3的项目中,没有单独的清除按钮,所以需要清除indexedDB
92
+ indexedDB.clear()
93
+ if (
94
+ (!loginIgnore.includes(to) || to.name === '404') &&
95
+ !checkSingleAuthorization('SinglePage', token)
96
+ ) {
97
+ localStorage.setItem('SinglePage_TOKEN', token)
98
+ startLogin({
99
+ ...info,
100
+ pathname: window.location.pathname.replace(process.env.VUE_APP_PUBLIC_PATH, ''),
101
+ store,
102
+ router,
103
+ })
104
+ } else {
105
+ const roles = store.getters['account/roles']
106
+ let user = {}
107
+ try {
108
+ user = JSON.parse(localStorage.getItem(process.env.VUE_APP_USER_KEY))
109
+ } catch (e) {
110
+ user = localStorage.getItem(process.env.VUE_APP_USER_KEY)
111
+ }
112
+ const setUser = store._mutations['account/setUser'][0]
113
+ setUser(user)
114
+ if (roles.length === 0 && !loginIgnore.includes(to)) {
115
+ message.warning('登录已失效,请重新登录')
116
+ logout().finally((res) => {
117
+ next({ path: '/login' })
118
+ })
119
+ } else {
120
+ next()
121
+ }
122
+ }
123
+ } else if (tokenV2) {
124
+ const { AESDecrypt } = AES
125
+ const info = AESDecrypt(tokenV2, '3KMKqvgwR8ULbR8Z')
126
+ request(`/rs/user/${info.name}/${info.password}/智慧燃气`, METHOD.GET).then(
127
+ res => {
128
+ const curUser = {
129
+ name: res.name,
130
+ username: res.name,
131
+ orgid: res.name,
132
+ avatar: '',
133
+ ename: res.ename,
134
+ id: res.id,
135
+ orgName: res.f_fengongsi,
136
+ contact: '',
137
+ phone: '',
138
+ orgs: res.orgs
139
+ }
140
+ Vue.$store.commit('account/setUser', Object.assign(curUser, res))
141
+ const login = {
142
+ f: res,
143
+ jwt: res.id,
144
+ r: res.roles
145
+ }
146
+ Vue.$store.commit('account/setLogin', login)
147
+ loadRoutes(funcToRouter(res.functions))
148
+ next()
149
+ }
150
+ )
151
+ } else if (V4_LOGIN_DATA_KEY) {
152
+ const v4LoginData = JSON.parse(localStorage.getItem('userInfo'))
153
+ const curUser = {
154
+ name: v4LoginData.name,
155
+ username: v4LoginData.name,
156
+ orgid: v4LoginData.name,
157
+ avatar: '',
158
+ ename: v4LoginData.ename,
159
+ id: v4LoginData.id,
160
+ orgName: v4LoginData.orgs,
161
+ contact: '',
162
+ phone: '',
163
+ orgs: v4LoginData.orgs
164
+ }
165
+ Vue.$store.commit('account/setUser', Object.assign(curUser, v4LoginData))
166
+ const login = {
167
+ f: v4LoginData,
168
+ jwt: v4LoginData.id,
169
+ r: v4LoginData.r
170
+ }
171
+ Vue.$store.commit('account/setLogin', login)
172
+ if (!Vue.$store.state?.setting?.menuData || Vue.$store.state?.setting?.menuData?.length === 0) {
173
+ loadRoutes(funcToRouter(v4LoginData.functions))
174
+ }
175
+ next()
176
+ } else {
177
+ const { store, message } = options
178
+ if (
179
+ (!loginIgnore.includes(to) || to.name === '404') &&
180
+ !checkAuthorization()
181
+ ) {
182
+ message.warning('登录已失效,请重新登录')
183
+ next({ path: '/login' })
184
+ } else {
185
+ const roles = store.getters['account/roles']
186
+ if (roles.length === 0 && !loginIgnore.includes(to)) {
187
+ message.warning('登录已失效,请重新登录')
188
+ logout().finally((res) => {
189
+ next({ path: '/login' })
190
+ })
191
+ } else {
192
+ next()
193
+ }
194
+ }
195
+ }
196
+ }
197
+
198
+ /**
199
+ * 权限守卫
200
+ * @param to
201
+ * @param form
202
+ * @param next
203
+ * @param options
204
+ */
205
+ const authorityGuard = (to, from, next, options) => {
206
+ const { store, message } = options
207
+ const permissions = store.getters['account/permissions']
208
+ const roles = store.getters['account/roles']
209
+ if (!hasAuthority(to, permissions, roles)) {
210
+ message.warning(`对不起,您无权访问页面: ${to.fullPath},请联系管理员`)
211
+ next({ path: '/403' })
212
+ // NProgress.done()
213
+ } else {
214
+ next()
215
+ }
216
+ }
217
+
218
+ /**
219
+ * 混合导航模式下一级菜单跳转重定向
220
+ * @param to
221
+ * @param from
222
+ * @param next
223
+ * @param options
224
+ * @returns {*}
225
+ */
226
+ const redirectGuard = (to, from, next, options) => {
227
+ const { store } = options
228
+ const getFirstChild = (routes) => {
229
+ const route = routes[0]
230
+ if (!route.children || route.children.length === 0) {
231
+ return route
232
+ }
233
+ return getFirstChild(route.children)
234
+ }
235
+ if (store.state.setting.layout === 'mix') {
236
+ const firstMenu = store.getters['setting/firstMenu']
237
+ if (firstMenu.find((item) => item.fullPath === to.fullPath)) {
238
+ store.commit('setting/setActivatedFirst', to.fullPath)
239
+ const subMenu = store.getters['setting/subMenu']
240
+ if (subMenu.length > 0) {
241
+ const redirect = getFirstChild(subMenu)
242
+ return next({ path: redirect.fullPath })
243
+ }
244
+ }
245
+ }
246
+ next()
247
+ }
248
+
249
+ /**
250
+ * 进度条结束
251
+ * @param to
252
+ * @param form
253
+ * @param options
254
+ */
255
+ const progressDone = () => {
256
+ // finish progress bar
257
+ NProgress.done()
258
+ }
259
+
260
+ export default {
261
+ beforeEach: [progressStart, loginGuard, authorityGuard, redirectGuard],
262
+ afterEach: [progressDone],
263
+ }