vue2-client 1.8.23 → 1.8.24

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue2-client",
3
- "version": "1.8.23",
3
+ "version": "1.8.24",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "serve": "SET NODE_OPTIONS=--openssl-legacy-provider && vue-cli-service serve --no-eslint",
@@ -1,379 +1,387 @@
1
- <template>
2
- <div id="TabsView">
3
- <admin-layout>
4
- <contextmenu :itemList="menuItemList" :visible.sync="menuVisible" @select="onMenuSelect" />
5
- <tabs-head
6
- v-if="multiPage"
7
- :active="activePage"
8
- :page-list="pageList"
9
- @change="changePage"
10
- @close="remove"
11
- @refresh="refresh"
12
- @contextmenu="onContextmenu"
13
- />
14
- <div :class="['tabs-view-content', layout, pageWidth]" :style="`margin-top: ${multiPage ? -24 : 0}px`">
15
- <page-toggle-transition v-show="!$route.meta.singlePage" :disabled="animate.disabled" :animate="animate.name" :direction="animate.direction">
16
- <a-keep-alive :exclude-keys="excludeKeys" v-if="multiPage && cachePage" v-model="clearCaches">
17
- <router-view v-if="!refreshing" ref="tabContent" :key="$route.fullPath" />
18
- </a-keep-alive>
19
- <router-view ref="tabContent" v-else-if="!refreshing" />
20
- </page-toggle-transition>
21
- <template v-for="page in single">
22
- <single-page-view
23
- v-show="page.meta.singlePage === $route.meta.singlePage"
24
- :single-page-url="page.meta.singlePage"
25
- :full-path="page.fullPath"
26
- :key="page.meta.singlePage"
27
- @load="onSinglePageLoad" />
28
- </template>
29
- </div>
30
- </admin-layout>
31
- </div>
32
- </template>
33
-
34
- <script>
35
- import AdminLayout from '@vue2-client/layouts/AdminLayout'
36
- import Contextmenu from '@vue2-client/components/menu/Contextmenu'
37
- import PageToggleTransition from '@vue2-client/components/transition/PageToggleTransition'
38
- import { mapState, mapMutations } from 'vuex'
39
- import { getI18nKey } from '@vue2-client/utils/routerUtil'
40
- import AKeepAlive from '@vue2-client/components/cache/AKeepAlive'
41
- import TabsHead from '@vue2-client/layouts/tabs/TabsHead'
42
- import SinglePageView from '../SinglePageView'
43
-
44
- export default {
45
- name: 'TabsView',
46
- i18n: require('./i18n'),
47
- components: { SinglePageView, TabsHead, PageToggleTransition, Contextmenu, AdminLayout, AKeepAlive },
48
- data () {
49
- return {
50
- clearCaches: [],
51
- pageList: [],
52
- activePage: '',
53
- menuVisible: false,
54
- refreshing: false,
55
- allSinglePages: [],
56
- excludeKeys: []
57
- }
58
- },
59
- computed: {
60
- ...mapState('setting', ['multiPage', 'cachePage', 'animate', 'layout', 'pageWidth']),
61
- ...mapState('account', ['single']),
62
- menuItemList () {
63
- return [
64
- { key: '1', icon: 'vertical-right', text: this.$t('closeLeft') },
65
- { key: '2', icon: 'vertical-left', text: this.$t('closeRight') },
66
- { key: '3', icon: 'close', text: this.$t('closeOthers') },
67
- { key: '4', icon: 'sync', text: this.$t('refresh') }
68
- ]
69
- },
70
- tabsOffset () {
71
- return this.multiPage ? 24 : 0
72
- }
73
- },
74
- created () {
75
- this.loadCacheConfig(this.$router?.options?.routes)
76
- this.loadCachedTabs()
77
- const route = this.$route
78
- if (this.pageList.findIndex(item => item.path === route.fullPath) === -1) {
79
- this.pageList.push(this.createPage(route))
80
- }
81
- this.activePage = route.fullPath
82
- if (this.multiPage) {
83
- this.$nextTick(() => {
84
- this.setCachedKey(route)
85
- })
86
- this.addListener()
87
- }
88
- },
89
- mounted () {
90
- this.correctPageMinHeight(-this.tabsOffset)
91
- },
92
- beforeDestroy () {
93
- this.removeListener()
94
- this.correctPageMinHeight(this.tabsOffset)
95
- },
96
- watch: {
97
- '$router.options.routes': function (val) {
98
- this.excludeKeys = []
99
- this.loadCacheConfig(val)
100
- },
101
- $route: function (newRoute) {
102
- this.activePage = newRoute.fullPath
103
- const page = this.pageList.find(item => item.path === newRoute.fullPath)
104
- if (!this.multiPage) {
105
- this.pageList = [this.createPage(newRoute)]
106
- } else if (page) {
107
- page.fullPath = newRoute.fullPath
108
- } else if (!page) {
109
- this.pageList.push(this.createPage(newRoute))
110
- }
111
- if (this.multiPage) {
112
- this.$nextTick(() => {
113
- this.setCachedKey(newRoute)
114
- })
115
- }
116
- },
117
- multiPage: function (newVal) {
118
- if (!newVal) {
119
- this.pageList = [this.createPage(this.$route)]
120
- this.removeListener()
121
- } else {
122
- this.addListener()
123
- }
124
- },
125
- tabsOffset (newVal, oldVal) {
126
- this.correctPageMinHeight(oldVal - newVal)
127
- }
128
- },
129
- methods: {
130
- changePage (key) {
131
- this.activePage = key
132
- const page = this.pageList.find(item => item.path === key)
133
- this.$router.push(page.fullPath)
134
- },
135
- remove (key, next) {
136
- if (this.pageList.length === 1) {
137
- return this.$message.warning(this.$t('warn'))
138
- }
139
- this.setSingle({ path: key, delete: true })
140
- // 清除缓存
141
- let index = this.pageList.findIndex(item => item.path === key)
142
- this.clearCaches = this.pageList.splice(index, 1).map(page => page.cachedKey)
143
- if (next) {
144
- this.$router.push(next)
145
- } else if (key === this.activePage) {
146
- index = index >= this.pageList.length ? this.pageList.length - 1 : index
147
- this.activePage = this.pageList[index].path
148
- this.$router.push(this.activePage)
149
- }
150
- },
151
- refresh (key, page) {
152
- page = page || this.pageList.find(item => item.path === key)
153
- page.loading = true
154
- // 单页面刷新
155
- const singlePage = this.allSinglePages.find(item => item.fullPath === key)
156
- if (singlePage) {
157
- window.frames[singlePage.fullPath].location.reload()
158
- return
159
- }
160
- this.clearCache(page)
161
- if (key === this.activePage) {
162
- // eslint-disable-next-line no-return-assign
163
- this.reloadContent(() => page.loading = false)
164
- } else {
165
- // 其实刷新很快,加这个延迟纯粹为了 loading 状态多展示一会儿,让用户感知刷新这一过程
166
- // eslint-disable-next-line no-return-assign
167
- setTimeout(() => page.loading = false, 500)
168
- }
169
- },
170
- onSinglePageLoad (fullPath) {
171
- const page = this.pageList.find(item => item.fullPath === fullPath)
172
- page.loading = false
173
- },
174
- onContextmenu (pageKey, e) {
175
- if (pageKey) {
176
- e.preventDefault()
177
- e.meta = pageKey
178
- this.menuVisible = true
179
- }
180
- },
181
- onMenuSelect (key, target, pageKey) {
182
- switch (key) {
183
- case '1': this.closeLeft(pageKey); break
184
- case '2': this.closeRight(pageKey); break
185
- case '3': this.closeOthers(pageKey); break
186
- case '4': this.refresh(pageKey); break
187
- default: break
188
- }
189
- },
190
- closeOthers (pageKey) {
191
- // 清除缓存
192
- const clearPages = this.pageList.filter(item => item.path !== pageKey && !item.unclose)
193
- this.clearCaches = clearPages.map(item => item.cachedKey)
194
- this.pageList = this.pageList.filter(item => !clearPages.includes(item))
195
- // 判断跳转
196
- if (this.activePage !== pageKey) {
197
- this.activePage = pageKey
198
- this.$router.push(this.activePage)
199
- }
200
- },
201
- closeLeft (pageKey) {
202
- const index = this.pageList.findIndex(item => item.path === pageKey)
203
- // 清除缓存
204
- const clearPages = this.pageList.filter((item, i) => i < index && !item.unclose)
205
- this.clearCaches = clearPages.map(item => item.cachedKey)
206
- this.pageList = this.pageList.filter(item => !clearPages.includes(item))
207
- // 判断跳转
208
- if (!this.pageList.find(item => item.path === this.activePage)) {
209
- this.activePage = pageKey
210
- this.$router.push(this.activePage)
211
- }
212
- },
213
- closeRight (pageKey) {
214
- // 清除缓存
215
- const index = this.pageList.findIndex(item => item.path === pageKey)
216
- const clearPages = this.pageList.filter((item, i) => i > index && !item.unclose)
217
- this.clearCaches = clearPages.map(item => item.cachedKey)
218
- this.pageList = this.pageList.filter(item => !clearPages.includes(item))
219
- // 判断跳转
220
- if (!this.pageList.find(item => item.path === this.activePage)) {
221
- this.activePage = pageKey
222
- this.$router.push(this.activePage)
223
- }
224
- },
225
- clearCache (page) {
226
- page._init_ = false
227
- this.clearCaches = [page.cachedKey]
228
- },
229
- reloadContent (onLoaded) {
230
- this.refreshing = true
231
- setTimeout(() => {
232
- this.refreshing = false
233
- this.$nextTick(() => {
234
- this.setCachedKey(this.$route)
235
- if (typeof onLoaded === 'function') {
236
- onLoaded.apply(this, [])
237
- }
238
- })
239
- }, 200)
240
- },
241
- pageName (page) {
242
- return this.$t(getI18nKey(page.keyPath))
243
- },
244
- /**
245
- * 添加监听器
246
- */
247
- addListener () {
248
- window.addEventListener('page:close', this.closePageListener)
249
- window.addEventListener('page:refresh', this.refreshPageListener)
250
- window.addEventListener('unload', this.unloadListener)
251
- },
252
- /**
253
- * 移出监听器
254
- */
255
- removeListener () {
256
- window.removeEventListener('page:close', this.closePageListener)
257
- window.removeEventListener('page:refresh', this.refreshPageListener)
258
- window.removeEventListener('unload', this.unloadListener)
259
- },
260
- /**
261
- * 页签关闭事件监听
262
- * @param event 页签关闭事件
263
- */
264
- closePageListener (event) {
265
- const { closeRoute, nextRoute } = event.detail
266
- const closePath = typeof closeRoute === 'string' ? closeRoute : closeRoute.path
267
- const path = closePath && closePath.split('?')[0]
268
- this.remove(path, nextRoute)
269
- },
270
- /**
271
- * 页面刷新事件监听
272
- * @param event 页签关闭事件
273
- */
274
- refreshPageListener (event) {
275
- const { pageKey } = event.detail
276
- const path = pageKey && pageKey.split('?')[0]
277
- this.refresh(path)
278
- },
279
- /**
280
- * 页面 unload 事件监听器,添加页签到 session 缓存,用于刷新时保留页签
281
- */
282
- unloadListener () {
283
- const tabs = this.pageList.map(item => ({ ...item, _init_: false }))
284
- sessionStorage.setItem(process.env.VUE_APP_TBAS_KEY, JSON.stringify(tabs))
285
- },
286
- createPage (route) {
287
- return {
288
- keyPath: route.matched[route.matched.length - 1].path,
289
- fullPath: route.fullPath,
290
- loading: false,
291
- path: route.fullPath,
292
- title: route.meta && route.meta.page && route.meta.page.title,
293
- unclose: route.meta && route.meta.page && (route.meta.page.closable === false)
294
- }
295
- },
296
- /**
297
- * 设置页面缓存的key
298
- * @param route 页面对应的路由
299
- */
300
- setCachedKey (route) {
301
- const page = this.pageList.find(item => item.path === route.path)
302
- page.unclose = route.meta && route.meta.page && (route.meta.page.closable === false)
303
- if (!page._init_) {
304
- const vnode = this.$refs.tabContent.$vnode
305
- page.cachedKey = vnode.key + vnode.componentOptions.Ctor.cid
306
- page._init_ = true
307
- }
308
- },
309
- /**
310
- * 加载缓存的 tabs
311
- */
312
- loadCachedTabs () {
313
- const cachedTabsStr = sessionStorage.getItem(process.env.VUE_APP_TBAS_KEY)
314
- if (cachedTabsStr) {
315
- try {
316
- // 加载缓存的单页面
317
- const allCachedPath = JSON.parse(cachedTabsStr).map(str => { return str.fullPath })
318
- this.allSinglePages.forEach(item => {
319
- if (allCachedPath.includes(item.fullPath)) {
320
- // 如果缓存中有单页面 加载到 state.single
321
- this.setSingle(item)
322
- }
323
- })
324
- const cachedTabs = JSON.parse(cachedTabsStr)
325
- if (cachedTabs.length > 0) {
326
- this.pageList = cachedTabs
327
- }
328
- } catch (e) {
329
- console.warn('failed to load cached tabs, got exception:', e)
330
- } finally {
331
- sessionStorage.removeItem(process.env.VUE_APP_TBAS_KEY)
332
- }
333
- }
334
- },
335
- loadCacheConfig (routes, pCache = true) {
336
- routes.forEach(item => {
337
- const cacheAble = item.meta?.page?.cacheAble ?? pCache ?? true
338
- if (item.meta && item.meta.singlePage) {
339
- // 找到所有单页面添加到 allSinglePages 中
340
- this.allSinglePages.push(item)
341
- }
342
- if (!cacheAble) {
343
- this.excludeKeys.push(new RegExp(`${item.path.replace(/:[^/]*/g, '[^/]*')}(\\?.*)?\\d*$`))
344
- }
345
- if (item.children) {
346
- this.loadCacheConfig(item.children, cacheAble)
347
- }
348
- })
349
- },
350
- ...mapMutations('setting', ['correctPageMinHeight']),
351
- ...mapMutations('account', ['setSingle'])
352
- }
353
- }
354
- </script>
355
-
356
- <style lang="less">
357
- #TabsView {
358
- .tabs-view {
359
- margin: -16px auto 8px;
360
- &.head.fixed {
361
- max-width: 100%;
362
- }
363
- }
364
- .tabs-view-content {
365
- position: relative;
366
- &.head.fixed {
367
- width: 100%;
368
- margin: 0 auto;
369
- }
370
- > div > div > .ant-card:first-of-type > .ant-card-body {
371
- max-height: calc(100vh - 64px - 48px - 24px);
372
- overflow-y: auto;
373
- &::-webkit-scrollbar {
374
- display: none;
375
- }
376
- }
377
- }
378
- }
379
- </style>
1
+ <template>
2
+ <div id="TabsView">
3
+ <admin-layout>
4
+ <contextmenu :itemList="menuItemList" :visible.sync="menuVisible" @select="onMenuSelect" />
5
+ <tabs-head
6
+ v-if="multiPage"
7
+ :active="activePage"
8
+ :page-list="pageList"
9
+ @change="changePage"
10
+ @close="remove"
11
+ @refresh="refresh"
12
+ @contextmenu="onContextmenu"
13
+ />
14
+ <div :class="['tabs-view-content', layout, pageWidth]" :style="`margin-top: ${multiPage ? -24 : 0}px`">
15
+ <page-toggle-transition v-show="!$route.meta.singlePage" :disabled="animate.disabled" :animate="animate.name" :direction="animate.direction">
16
+ <a-keep-alive :exclude-keys="excludeKeys" v-if="multiPage && cachePage" v-model="clearCaches">
17
+ <router-view v-if="!refreshing" ref="tabContent" :key="$route.fullPath" />
18
+ </a-keep-alive>
19
+ <router-view ref="tabContent" v-else-if="!refreshing" />
20
+ </page-toggle-transition>
21
+ <template v-for="page in single">
22
+ <single-page-view
23
+ v-show="page.meta.singlePage === $route.meta.singlePage"
24
+ :single-page-url="page.meta.singlePage"
25
+ :full-path="page.fullPath"
26
+ :key="page.meta.singlePage"
27
+ @load="onSinglePageLoad" />
28
+ </template>
29
+ </div>
30
+ </admin-layout>
31
+ </div>
32
+ </template>
33
+
34
+ <script>
35
+ import AdminLayout from '@vue2-client/layouts/AdminLayout'
36
+ import Contextmenu from '@vue2-client/components/menu/Contextmenu'
37
+ import PageToggleTransition from '@vue2-client/components/transition/PageToggleTransition'
38
+ import { mapState, mapMutations } from 'vuex'
39
+ import { getI18nKey } from '@vue2-client/utils/routerUtil'
40
+ import AKeepAlive from '@vue2-client/components/cache/AKeepAlive'
41
+ import TabsHead from '@vue2-client/layouts/tabs/TabsHead'
42
+ import SinglePageView from '../SinglePageView'
43
+
44
+ export default {
45
+ name: 'TabsView',
46
+ i18n: require('./i18n'),
47
+ components: { SinglePageView, TabsHead, PageToggleTransition, Contextmenu, AdminLayout, AKeepAlive },
48
+ data () {
49
+ return {
50
+ clearCaches: [],
51
+ pageList: [],
52
+ activePage: '',
53
+ menuVisible: false,
54
+ refreshing: false,
55
+ allSinglePages: [],
56
+ excludeKeys: []
57
+ }
58
+ },
59
+ computed: {
60
+ ...mapState('setting', ['multiPage', 'cachePage', 'animate', 'layout', 'pageWidth']),
61
+ ...mapState('account', ['single']),
62
+ menuItemList () {
63
+ return [
64
+ { key: '1', icon: 'vertical-right', text: this.$t('closeLeft') },
65
+ { key: '2', icon: 'vertical-left', text: this.$t('closeRight') },
66
+ { key: '3', icon: 'close', text: this.$t('closeOthers') },
67
+ { key: '4', icon: 'sync', text: this.$t('refresh') }
68
+ ]
69
+ },
70
+ tabsOffset () {
71
+ return this.multiPage ? 24 : 0
72
+ }
73
+ },
74
+ created () {
75
+ this.loadCacheConfig(this.$router?.options?.routes)
76
+ this.loadCachedTabs()
77
+ // 解决单页面为首页时首次登录不加载问题
78
+ if (this.$route.meta.singlePage && this.single.length === 0){
79
+ this.allSinglePages.forEach(item => {
80
+ if (this.$route.fullPath.includes(item.fullPath)) {
81
+ this.setSingle(item)
82
+ }
83
+ })
84
+ }
85
+ const route = this.$route
86
+ if (this.pageList.findIndex(item => item.path === route.fullPath) === -1) {
87
+ this.pageList.push(this.createPage(route))
88
+ }
89
+ this.activePage = route.fullPath
90
+ if (this.multiPage) {
91
+ this.$nextTick(() => {
92
+ this.setCachedKey(route)
93
+ })
94
+ this.addListener()
95
+ }
96
+ },
97
+ mounted () {
98
+ this.correctPageMinHeight(-this.tabsOffset)
99
+ },
100
+ beforeDestroy () {
101
+ this.removeListener()
102
+ this.correctPageMinHeight(this.tabsOffset)
103
+ },
104
+ watch: {
105
+ '$router.options.routes': function (val) {
106
+ this.excludeKeys = []
107
+ this.loadCacheConfig(val)
108
+ },
109
+ $route: function (newRoute) {
110
+ this.activePage = newRoute.fullPath
111
+ const page = this.pageList.find(item => item.path === newRoute.fullPath)
112
+ if (!this.multiPage) {
113
+ this.pageList = [this.createPage(newRoute)]
114
+ } else if (page) {
115
+ page.fullPath = newRoute.fullPath
116
+ } else if (!page) {
117
+ this.pageList.push(this.createPage(newRoute))
118
+ }
119
+ if (this.multiPage) {
120
+ this.$nextTick(() => {
121
+ this.setCachedKey(newRoute)
122
+ })
123
+ }
124
+ },
125
+ multiPage: function (newVal) {
126
+ if (!newVal) {
127
+ this.pageList = [this.createPage(this.$route)]
128
+ this.removeListener()
129
+ } else {
130
+ this.addListener()
131
+ }
132
+ },
133
+ tabsOffset (newVal, oldVal) {
134
+ this.correctPageMinHeight(oldVal - newVal)
135
+ }
136
+ },
137
+ methods: {
138
+ changePage (key) {
139
+ this.activePage = key
140
+ const page = this.pageList.find(item => item.path === key)
141
+ this.$router.push(page.fullPath)
142
+ },
143
+ remove (key, next) {
144
+ if (this.pageList.length === 1) {
145
+ return this.$message.warning(this.$t('warn'))
146
+ }
147
+ this.setSingle({ path: key, delete: true })
148
+ // 清除缓存
149
+ let index = this.pageList.findIndex(item => item.path === key)
150
+ this.clearCaches = this.pageList.splice(index, 1).map(page => page.cachedKey)
151
+ if (next) {
152
+ this.$router.push(next)
153
+ } else if (key === this.activePage) {
154
+ index = index >= this.pageList.length ? this.pageList.length - 1 : index
155
+ this.activePage = this.pageList[index].path
156
+ this.$router.push(this.activePage)
157
+ }
158
+ },
159
+ refresh (key, page) {
160
+ page = page || this.pageList.find(item => item.path === key)
161
+ page.loading = true
162
+ // 单页面刷新
163
+ const singlePage = this.allSinglePages.find(item => item.fullPath === key)
164
+ if (singlePage) {
165
+ window.frames[singlePage.fullPath].location.reload()
166
+ return
167
+ }
168
+ this.clearCache(page)
169
+ if (key === this.activePage) {
170
+ // eslint-disable-next-line no-return-assign
171
+ this.reloadContent(() => page.loading = false)
172
+ } else {
173
+ // 其实刷新很快,加这个延迟纯粹为了 loading 状态多展示一会儿,让用户感知刷新这一过程
174
+ // eslint-disable-next-line no-return-assign
175
+ setTimeout(() => page.loading = false, 500)
176
+ }
177
+ },
178
+ onSinglePageLoad (fullPath) {
179
+ const page = this.pageList.find(item => item.fullPath === fullPath)
180
+ page.loading = false
181
+ },
182
+ onContextmenu (pageKey, e) {
183
+ if (pageKey) {
184
+ e.preventDefault()
185
+ e.meta = pageKey
186
+ this.menuVisible = true
187
+ }
188
+ },
189
+ onMenuSelect (key, target, pageKey) {
190
+ switch (key) {
191
+ case '1': this.closeLeft(pageKey); break
192
+ case '2': this.closeRight(pageKey); break
193
+ case '3': this.closeOthers(pageKey); break
194
+ case '4': this.refresh(pageKey); break
195
+ default: break
196
+ }
197
+ },
198
+ closeOthers (pageKey) {
199
+ // 清除缓存
200
+ const clearPages = this.pageList.filter(item => item.path !== pageKey && !item.unclose)
201
+ this.clearCaches = clearPages.map(item => item.cachedKey)
202
+ this.pageList = this.pageList.filter(item => !clearPages.includes(item))
203
+ // 判断跳转
204
+ if (this.activePage !== pageKey) {
205
+ this.activePage = pageKey
206
+ this.$router.push(this.activePage)
207
+ }
208
+ },
209
+ closeLeft (pageKey) {
210
+ const index = this.pageList.findIndex(item => item.path === pageKey)
211
+ // 清除缓存
212
+ const clearPages = this.pageList.filter((item, i) => i < index && !item.unclose)
213
+ this.clearCaches = clearPages.map(item => item.cachedKey)
214
+ this.pageList = this.pageList.filter(item => !clearPages.includes(item))
215
+ // 判断跳转
216
+ if (!this.pageList.find(item => item.path === this.activePage)) {
217
+ this.activePage = pageKey
218
+ this.$router.push(this.activePage)
219
+ }
220
+ },
221
+ closeRight (pageKey) {
222
+ // 清除缓存
223
+ const index = this.pageList.findIndex(item => item.path === pageKey)
224
+ const clearPages = this.pageList.filter((item, i) => i > index && !item.unclose)
225
+ this.clearCaches = clearPages.map(item => item.cachedKey)
226
+ this.pageList = this.pageList.filter(item => !clearPages.includes(item))
227
+ // 判断跳转
228
+ if (!this.pageList.find(item => item.path === this.activePage)) {
229
+ this.activePage = pageKey
230
+ this.$router.push(this.activePage)
231
+ }
232
+ },
233
+ clearCache (page) {
234
+ page._init_ = false
235
+ this.clearCaches = [page.cachedKey]
236
+ },
237
+ reloadContent (onLoaded) {
238
+ this.refreshing = true
239
+ setTimeout(() => {
240
+ this.refreshing = false
241
+ this.$nextTick(() => {
242
+ this.setCachedKey(this.$route)
243
+ if (typeof onLoaded === 'function') {
244
+ onLoaded.apply(this, [])
245
+ }
246
+ })
247
+ }, 200)
248
+ },
249
+ pageName (page) {
250
+ return this.$t(getI18nKey(page.keyPath))
251
+ },
252
+ /**
253
+ * 添加监听器
254
+ */
255
+ addListener () {
256
+ window.addEventListener('page:close', this.closePageListener)
257
+ window.addEventListener('page:refresh', this.refreshPageListener)
258
+ window.addEventListener('unload', this.unloadListener)
259
+ },
260
+ /**
261
+ * 移出监听器
262
+ */
263
+ removeListener () {
264
+ window.removeEventListener('page:close', this.closePageListener)
265
+ window.removeEventListener('page:refresh', this.refreshPageListener)
266
+ window.removeEventListener('unload', this.unloadListener)
267
+ },
268
+ /**
269
+ * 页签关闭事件监听
270
+ * @param event 页签关闭事件
271
+ */
272
+ closePageListener (event) {
273
+ const { closeRoute, nextRoute } = event.detail
274
+ const closePath = typeof closeRoute === 'string' ? closeRoute : closeRoute.path
275
+ const path = closePath && closePath.split('?')[0]
276
+ this.remove(path, nextRoute)
277
+ },
278
+ /**
279
+ * 页面刷新事件监听
280
+ * @param event 页签关闭事件
281
+ */
282
+ refreshPageListener (event) {
283
+ const { pageKey } = event.detail
284
+ const path = pageKey && pageKey.split('?')[0]
285
+ this.refresh(path)
286
+ },
287
+ /**
288
+ * 页面 unload 事件监听器,添加页签到 session 缓存,用于刷新时保留页签
289
+ */
290
+ unloadListener () {
291
+ const tabs = this.pageList.map(item => ({ ...item, _init_: false }))
292
+ sessionStorage.setItem(process.env.VUE_APP_TBAS_KEY, JSON.stringify(tabs))
293
+ },
294
+ createPage (route) {
295
+ return {
296
+ keyPath: route.matched[route.matched.length - 1].path,
297
+ fullPath: route.fullPath,
298
+ loading: false,
299
+ path: route.fullPath,
300
+ title: route.meta && route.meta.page && route.meta.page.title,
301
+ unclose: route.meta && route.meta.page && (route.meta.page.closable === false)
302
+ }
303
+ },
304
+ /**
305
+ * 设置页面缓存的key
306
+ * @param route 页面对应的路由
307
+ */
308
+ setCachedKey (route) {
309
+ const page = this.pageList.find(item => item.path === route.path)
310
+ page.unclose = route.meta && route.meta.page && (route.meta.page.closable === false)
311
+ if (!page._init_) {
312
+ const vnode = this.$refs.tabContent.$vnode
313
+ page.cachedKey = vnode.key + vnode.componentOptions.Ctor.cid
314
+ page._init_ = true
315
+ }
316
+ },
317
+ /**
318
+ * 加载缓存的 tabs
319
+ */
320
+ loadCachedTabs () {
321
+ const cachedTabsStr = sessionStorage.getItem(process.env.VUE_APP_TBAS_KEY)
322
+ if (cachedTabsStr) {
323
+ try {
324
+ // 加载缓存的单页面
325
+ const allCachedPath = JSON.parse(cachedTabsStr).map(str => { return str.fullPath })
326
+ this.allSinglePages.forEach(item => {
327
+ if (allCachedPath.includes(item.fullPath)) {
328
+ // 如果缓存中有单页面 加载到 state.single
329
+ this.setSingle(item)
330
+ }
331
+ })
332
+ const cachedTabs = JSON.parse(cachedTabsStr)
333
+ if (cachedTabs.length > 0) {
334
+ this.pageList = cachedTabs
335
+ }
336
+ } catch (e) {
337
+ console.warn('failed to load cached tabs, got exception:', e)
338
+ } finally {
339
+ sessionStorage.removeItem(process.env.VUE_APP_TBAS_KEY)
340
+ }
341
+ }
342
+ },
343
+ loadCacheConfig (routes, pCache = true) {
344
+ routes.forEach(item => {
345
+ const cacheAble = item.meta?.page?.cacheAble ?? pCache ?? true
346
+ if (item.meta && item.meta.singlePage) {
347
+ // 找到所有单页面添加到 allSinglePages 中
348
+ this.allSinglePages.push(item)
349
+ }
350
+ if (!cacheAble) {
351
+ this.excludeKeys.push(new RegExp(`${item.path.replace(/:[^/]*/g, '[^/]*')}(\\?.*)?\\d*$`))
352
+ }
353
+ if (item.children) {
354
+ this.loadCacheConfig(item.children, cacheAble)
355
+ }
356
+ })
357
+ },
358
+ ...mapMutations('setting', ['correctPageMinHeight']),
359
+ ...mapMutations('account', ['setSingle'])
360
+ }
361
+ }
362
+ </script>
363
+
364
+ <style lang="less">
365
+ #TabsView {
366
+ .tabs-view {
367
+ margin: -16px auto 8px;
368
+ &.head.fixed {
369
+ max-width: 100%;
370
+ }
371
+ }
372
+ .tabs-view-content {
373
+ position: relative;
374
+ &.head.fixed {
375
+ width: 100%;
376
+ margin: 0 auto;
377
+ }
378
+ > div > div > .ant-card:first-of-type > .ant-card-body {
379
+ max-height: calc(100vh - 64px - 48px - 24px);
380
+ overflow-y: auto;
381
+ &::-webkit-scrollbar {
382
+ display: none;
383
+ }
384
+ }
385
+ }
386
+ }
387
+ </style>