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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// @ts-nocheck
import router from '@/router'
const title = process.env.VUE_APP_SYSTEM_TITLE
const state = {
openKeys: [],
activeKeys: [],
tabsArray: [],
tabsActive: '',
cachedViews: [],
projType: null,
}
const mutations = {
setTabsActive: (state, payload) => {
state.tabsActive = payload
},
setTabsArray: (state, payload) => {
state.tabsArray = payload
},
setProjType: (state, payload) => {
state.projType = payload
},
addTabs: (state, payload) => {
if (state.tabsArray.some(v => v.key === payload.key)) {
return
}
state.tabsArray.push(
Object.assign({}, payload, { title: payload.title || 'no-name' })
)
},
closeTabs: (state, payload) => {
state.tabsArray.splice(payload, 1)
},
setOpenKeys (state, payload) {
if (state.openKeys.some(v => v === payload)) {
return
}
state.openKeys = payload
},
setActiveKeys (state, payload) {
state.activeKeys = payload
},
setCachedViews: (state, payload) => {
state.cachedViews = payload
},
addCachedViews: (state, payload) => {
if (payload.keepAlive && payload.keepAlive == 1) {
if (state.cachedViews.some(v => v === payload.code)) {
return
}
state.cachedViews.push(payload.code)
}
},
delCachedViews: (state, payload) => {
for (let i = 0; i < state.cachedViews.length; i++) {
if (state.cachedViews[i] == payload.code) {
state.cachedViews.splice(i, 1)
}
}
},
//点击页签
clickCard (state, payload) {
this.commit('app/setTabsActive', payload)
if (payload.length > 2) {
this.commit('app/setActiveKeys', [parseInt(payload)])
// this.commit('app/setOpenKeys', [parseInt(payload.substring(0, 2))])
}
let { obj, index } = filterTabsBykey(state.tabsArray, payload)
router.push(obj.router)
},
// 打开一个新页签
addCard (state, payload) {
this.commit('app/setTabsActive', payload.key)
this.commit('app/addTabs', payload)
this.commit('app/addCachedViews', payload)
router.push(payload.router)
},
// 关闭某一个打开页签
closeCard (state, payload) {
let list = state.tabsArray
let { obj, index } = filterTabsBykey(list, payload)
if (payload == state.tabsActive) {
this.commit('app/setTabsActive', list[index - 1].key)
if (list[index - 1].key.length > 2) {
this.commit('app/setActiveKeys', [parseInt(list[index - 1].key)])
// this.commit('app/setOpenKeys', [parseInt((list[index - 1].key).substring(0, 2))])
}
else
this.commit('app/setActiveKeys', [0])
router.push(list[index - 1].router)
}
this.commit('app/closeTabs', index)
this.commit('app/delCachedViews', obj)
},
/**
* 页签卡片操作
* @param type 0 点击切换 1 关闭当前页签 2关闭其他标签 3关闭所有标签
* @param key
*/
optCard (state, payload) {
let list = state.tabsArray
let { obj, index } = filterTabsBykey(list, payload.key)
if (payload.key != '0') {
if (payload.type == 1) {
this.commit('app/setTabsActive', list[index - 1].key)
if (list[index - 1].key.length > 2) {
this.commit('app/setActiveKeys', [parseInt(list[index - 1].key)])
// this.commit('app/setOpenKeys', [parseInt((list[index - 1].key).substring(0, 2))])
}
this.commit('app/closeTabs', index)
this.commit('app/delCachedViews', obj)
router.push(list[index - 1].router)
}
else if (payload.type == 2) {
this.commit('app/setTabsArray', [list[0]])
this.commit('app/setCachedViews', ['home', obj.code])
}
else {
this.commit('app/setTabsArray', [list[0]])
this.commit('app/setTabsActive', '0')
this.commit('app/setActiveKeys', [])
this.commit('app/setCachedViews', ['home'])
router.push('/')
}
} else {
this.commit('app/setTabsArray', [list[0]])
this.commit('app/setTabsActive', '0')
this.commit('app/setActiveKeys', [])
this.commit('app/setCachedViews', ['home'])
}
},
initStore (state, payload) {
this.commit('app/setOpenKeys', payload)
this.commit('app/setActiveKeys', [])
this.commit('app/setTabsArray', [{ title: '首页', key: '0', code: 'home', keepAlive: 1, router: '/home', closable: false }])
this.commit('app/setTabsActive', '0')
},
clearStore (state) {
this.commit('app/setOpenKeys', [])
this.commit('app/setActiveKeys', [])
this.commit('app/setTabsArray', [])
this.commit('app/setTabsActive', '')
this.commit('app/setCachedViews', [])
this.commit('app/setProjType', null)
},
}
const actions = {
auth ({ commit, state }, path,) {
return new Promise((resolve, reject) => {
try {
let tabs = null
tabs = filterTabsByPath(state.tabsArray, path)
if (!tabs) {
tabs = filterTabsByPath(defaultTabsList, path)
if (!tabs) {
tabs = filterAsyncMenuList(JSON.parse(window.sessionStorage.getItem('menuList')), path)
}
if (tabs) {
commit('addTabs', tabs)
}
}
if (tabs) {
resolve(true)
commit('setTabsActive', tabs.key)
} else {
resolve(false)
}
} catch (error) {
reject(error)
}
})
},
}
const defaultTabsList = [
{ title: '个人设置', key: '1005', code: 'personInfo', keepAlive: 1, router: '/person/info', closable: true },
{ title: '项目创建', key: '1299', code: 'projectCreate', keepAlive: 1, router: '/project/create', closable: true },
{ title: '首页', key: '0', code: 'home', keepAlive: 1, router: '/home', closable: false },
{ title: '404', key: '404', code: '404', keepAlive: 1, router: '/404', closable: true },
{ title: '403', key: '403', code: '403', keepAlive: 1, router: '/403', closable: true }
]
const filterTabsBykey = (list, key) => {
let res = { obj: null, index: 0 }
for (var i = 0; i < list.length; i++) {
if (list[i].key == key) {
res = { obj: list[i], index: i }
break
}
}
return res
}
const filterTabsByPath = (list, path) => {
let res = null
for (var i = 0; i < list.length; i++) {
if (list[i].router == path) {
res = list[i]
break
}
}
return res
}
const filterAsyncMenuList = (list, path) => {
let res = null
for (var i = 0; i < list.length; i++) {
if (list[i].frontActionUrl == path) {
res = { title: list[i].name, key: list[i].id + '', code: list[i].code, keepAlive: list[i].keepAlive, router: list[i].frontActionUrl, closable: true }
break
}
if (list[i].children && list[i].children.length > 0) {
res = filterAsyncMenuList(list[i].children, path)
if (res) break
}
if (res) break
}
return res
}
export default {
namespaced: true,
state,
mutations,
actions
}