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
import { getDictDetailList, getDictList } from "@/api/dict.js"
import { Local } from "@/utils/storage"
import { translateListToTree } from "@/utils/handleRoutes.js"
const state = {
dictSet: Local.get("dictSet") || [],
dictMap: Local.get("dictMap") || {},
}
const getters = {
dictSet: (state) => state.dictSet,
dictMap: (state) => state.dictMap,
}
const mutations = {
setDictSet(state, data) {
state.dictSet = data
Local.set("dictSet", data)
},
setDictData(state, data) {
state.dictMap = data
Local.set("dictMap", data)
},
}
const actions = {
async getDictData({ commit, state }) {
let dictSet = state.dictSet
const dictMap = state.dictMap
const { data: latestDict } = await getDictList()
const latestDictSet = latestDict.map((_) => _.type)
let newDictSet = []
latestDict.forEach((v) => {
if (!dictSet.find((_) => _.type === v.type)) {
newDictSet.push(v.type)
}
})
commit("setDictSet", latestDict)
newDictSet = Object.keys(dictMap).length === 0 ? latestDictSet : newDictSet
// 存在新增加的字典
if (newDictSet.length > 0) {
const originalDictMap = Object.assign({}, dictMap)
getDictDetailList(newDictSet).then((res) => {
const d = res.data
Object.keys(d).forEach((key) => {
const list = d[key].map((item) => {
return {
label: item.name,
value: item.code,
id: item.id,
parentId: item.parentId,
}
})
originalDictMap[key] = translateListToTree(list)
})
commit("setDictData", originalDictMap)
})
}
},
// 更新字典分类
async updateDictType({ commit, state }, data) {
console.log('执行updateDictType')
commit("setDictSet", data)
},
// 更新字典详情
async updateDictDetail({ commit, state }, { type, data }) {
console.log('执行updateDictDetail')
const dictMap = state.dictMap
if (type) {
const list = data.map((item) => {
return {
label: item.name,
value: item.code,
id: item.id,
parentId: item.parentId,
}
})
dictMap[type] = translateListToTree(list)
commit("setDictData", dictMap)
}
},
}
export default {
state,
getters,
mutations,
actions,
}