Skip to content

Commit 30f1ee7

Browse files
author
QM303176530
committed
前端utils中封装了前端获取字典并缓存到内存的方法getDict
1 parent f6c951d commit 30f1ee7

File tree

9 files changed

+71
-10
lines changed

9 files changed

+71
-10
lines changed

server/api/v1/sys_dictionary.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func UpdateSysDictionary(c *gin.Context) {
7878
func FindSysDictionary(c *gin.Context) {
7979
var sysDictionary model.SysDictionary
8080
_ = c.ShouldBindQuery(&sysDictionary)
81-
err, resysDictionary := service.GetSysDictionary(sysDictionary.ID)
81+
err, resysDictionary := service.GetSysDictionary(sysDictionary.Type, sysDictionary.ID)
8282
if err != nil {
8383
response.FailWithMessage(fmt.Sprintf("查询失败,%v", err), c)
8484
} else {

server/service/sys_dictionary.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package service
22

33
import (
4+
"errors"
45
"gin-vue-admin/global"
56
"gin-vue-admin/model"
67
"gin-vue-admin/model/request"
@@ -13,6 +14,9 @@ import (
1314
// @return err error
1415

1516
func CreateSysDictionary(sysDictionary model.SysDictionary) (err error) {
17+
if (!global.GVA_DB.First(&model.SysDictionary{}, "type = ?", sysDictionary.Type).RecordNotFound()) {
18+
return errors.New("存在相同的type,不允许创建")
19+
}
1620
err = global.GVA_DB.Create(&sysDictionary).Error
1721
return err
1822
}
@@ -42,7 +46,16 @@ func UpdateSysDictionary(sysDictionary *model.SysDictionary) (err error) {
4246
"Status": sysDictionary.Status,
4347
"Desc": sysDictionary.Desc,
4448
}
45-
err = global.GVA_DB.Where("id = ?", sysDictionary.ID).First(&dict).Updates(sysDictionaryMap).Error
49+
db := global.GVA_DB.Where("id = ?", sysDictionary.ID).First(&dict)
50+
if dict.Type == sysDictionary.Type {
51+
err = db.Updates(sysDictionaryMap).Error
52+
} else {
53+
if (!global.GVA_DB.First(&model.SysDictionary{}, "type = ?", sysDictionary.Type).RecordNotFound()) {
54+
return errors.New("存在相同的type,不允许创建")
55+
} else {
56+
err = db.Updates(sysDictionaryMap).Error
57+
}
58+
}
4659
return err
4760
}
4861

@@ -53,8 +66,8 @@ func UpdateSysDictionary(sysDictionary *model.SysDictionary) (err error) {
5366
// @return error
5467
// @return SysDictionary SysDictionary
5568

56-
func GetSysDictionary(id uint) (err error, sysDictionary model.SysDictionary) {
57-
err = global.GVA_DB.Where("id = ?", id).Preload("SysDictionaryDetails").First(&sysDictionary).Error
69+
func GetSysDictionary(Type string, Id uint) (err error, sysDictionary model.SysDictionary) {
70+
err = global.GVA_DB.Where("type = ? OR id = ?", Type, Id).Preload("SysDictionaryDetails").First(&sysDictionary).Error
5871
return
5972
}
6073

File renamed without changes.
File renamed without changes.

web/src/store/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import VuexPersistence from 'vuex-persist'
44

55
import { user } from "@/store/module/user"
66
import { router } from "@/store/module/router"
7-
7+
import { dictionary } from "@/store/module/dictionary"
88
Vue.use(Vuex)
99

1010

@@ -16,7 +16,8 @@ const vuexLocal = new VuexPersistence({
1616
export const store = new Vuex.Store({
1717
modules: {
1818
user,
19-
router
19+
router,
20+
dictionary
2021
},
2122
plugins: [vuexLocal.plugin]
2223
})

web/src/store/module/dictionary.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { findSysDictionary } from '@/api/sysDictionary'
2+
3+
export const dictionary = {
4+
namespaced: true,
5+
state: {
6+
dictionaryMap: {},
7+
},
8+
mutations: {
9+
setDictionaryMap(state, dictionaryMap) {
10+
state.dictionaryMap = { ...state.dictionaryMap, ...dictionaryMap }
11+
},
12+
13+
},
14+
actions: {
15+
// 从后台获取动态路由
16+
async getDictionary({ commit, state }, type) {
17+
if (state.dictionaryMap[type]) {
18+
return state.dictionaryMap[type]
19+
} else {
20+
const res = await findSysDictionary({ type })
21+
if (res.code == 0) {
22+
const dictionaryMap = {}
23+
const dict = []
24+
res.data.resysDictionary.sysDictionaryDetails && res.data.resysDictionary.sysDictionaryDetails.map(item => {
25+
dict.push({
26+
label: item.label,
27+
value: item.value
28+
})
29+
})
30+
dictionaryMap[res.data.resysDictionary.type] = dict
31+
commit("setDictionaryMap", dictionaryMap)
32+
return state.dictionaryMap[type]
33+
}
34+
}
35+
}
36+
},
37+
getters:{
38+
getDictionary(state){
39+
return state.dictionaryMap
40+
}
41+
}
42+
}

web/src/utils/dictionary.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { store } from '@/store/index'
2+
3+
export const getDict = async (type) => {
4+
await store.dispatch("dictionary/getDictionary", type)
5+
return store.getters["dictionary/getDictionary"][type]
6+
}

web/src/view/superAdmin/dictionary/sys_dictionary.vue renamed to web/src/view/superAdmin/dictionary/sysDictionary.vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,9 @@ import {
114114
updateSysDictionary,
115115
findSysDictionary,
116116
getSysDictionaryList
117-
} from "@/api/sys_dictionary"; // 此处请自行替换地址
117+
} from "@/api/sysDictionary"; // 此处请自行替换地址
118118
import { formatTimeToStr } from "@/utils/data";
119119
import infoList from "@/components/mixins/infoList";
120-
121120
export default {
122121
name: "SysDictionary",
123122
mixins: [infoList],
@@ -239,7 +238,7 @@ export default {
239238
this.dialogFormVisible = true;
240239
}
241240
},
242-
created() {
241+
async created() {
243242
this.getTableData();
244243
}
245244
};

web/src/view/superAdmin/dictionary/sys_dictionary_detail.vue renamed to web/src/view/superAdmin/dictionary/sysDictionaryDetail.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ import {
113113
updateSysDictionaryDetail,
114114
findSysDictionaryDetail,
115115
getSysDictionaryDetailList
116-
} from "@/api/sys_dictionary_detail"; // 此处请自行替换地址
116+
} from "@/api/sysDictionaryDetail"; // 此处请自行替换地址
117117
import { formatTimeToStr } from "@/utils/data";
118118
import infoList from "@/components/mixins/infoList";
119119

0 commit comments

Comments
 (0)