diff --git a/src/api/system/dict/data.ts b/src/api/system/dict/data.ts new file mode 100644 index 0000000..6c83cca --- /dev/null +++ b/src/api/system/dict/data.ts @@ -0,0 +1,52 @@ +import request from '@/api/request' + +// 查询字典数据列表 +export function listData(params: any) { + return request({ + url: '/system/dict/data/list', + method: 'get', + params + }) +} + +// 查询字典数据详细 +export function getData(dictCode: string) { + return request({ + url: '/system/dict/data/' + dictCode, + method: 'get' + }) +} + +// 根据字典类型查询字典数据信息 +export function getDicts(dictType: string) { + return request({ + url: '/system/dict/data/type/' + dictType, + method: 'get' + }) +} + +// 新增字典数据 +export function addData(data: any) { + return request({ + url: '/system/dict/data', + method: 'post', + data + }) +} + +// 修改字典数据 +export function updateData(data: any) { + return request({ + url: '/system/dict/data', + method: 'put', + data + }) +} + +// 删除字典数据 +export function delData(dictCode: string) { + return request({ + url: '/system/dict/data/' + dictCode, + method: 'delete' + }) +} diff --git a/src/api/system/dict/index.ts b/src/api/system/dict/index.ts new file mode 100644 index 0000000..a2253fc --- /dev/null +++ b/src/api/system/dict/index.ts @@ -0,0 +1,2 @@ +export * from './data' +export * from './type' \ No newline at end of file diff --git a/src/api/system/dict/type.ts b/src/api/system/dict/type.ts new file mode 100644 index 0000000..a54ed5b --- /dev/null +++ b/src/api/system/dict/type.ts @@ -0,0 +1,61 @@ +import request from '@/api/request' +import type { ID } from '@/api/common' + +// 查询字典类型列表 +export function listType(params: any) { + return request({ + url: "/system/dict/type/list", + method: "get", + params, + }); +} + +// 查询字典类型详细 +export function getType(dictId: ID) { + return request({ + url: "/system/dict/type/" + dictId, + method: "get", + }); +} + +// 新增字典类型 +export function addType(data: any) { + return request({ + url: "/system/dict/type", + method: "post", + data, + }); +} + +// 修改字典类型 +export function updateType(data: any) { + return request({ + url: "/system/dict/type", + method: "put", + data, + }); +} + +// 删除字典类型 +export function delType(dictId: ID) { + return request({ + url: "/system/dict/type/" + dictId, + method: "delete", + }); +} + +// 刷新字典缓存 +export function refreshCache() { + return request({ + url: "/system/dict/type/refreshCache", + method: "delete", + }); +} + +// 获取字典选择框列表 +export function getOptions() { + return request({ + url: "/system/dict/type/optionselect", + method: "get", + }); +} diff --git a/src/components/common/DictTag/index.vue b/src/components/common/DictTag/index.vue new file mode 100644 index 0000000..ab20b2b --- /dev/null +++ b/src/components/common/DictTag/index.vue @@ -0,0 +1,123 @@ + + + + + diff --git a/src/main.ts b/src/main.ts index 56538a2..280a972 100644 --- a/src/main.ts +++ b/src/main.ts @@ -13,4 +13,9 @@ import "ant-design-vue/dist/reset.css"; import "@/assets/styles/index.scss"; const app = createApp(App); + +// 字典方法 +import { useDict } from "@/utils/dict"; +app.config.globalProperties.useDict = useDict; + app.use(pinia).use(router).mount("#app"); diff --git a/src/store/dict.ts b/src/store/dict.ts new file mode 100644 index 0000000..4b8b599 --- /dev/null +++ b/src/store/dict.ts @@ -0,0 +1,63 @@ +import { defineStore } from "pinia"; +import { ref } from "vue"; + +export const useDictStore = defineStore("dict", () => { + const dict = ref([]); + + // 获取字典 + function getDict(_key: string) { + if (_key == null && _key == "") { + return null; + } + try { + dict.value.forEach(item => { + if (item.key == _key) { + return item.value; + } + }); + } catch (e) { + return null; + } + } + + // 设置字典 + function setDict(_key: string, value: string | number) { + if (!_key) return; + dict.value.push({ + key: _key, + value: value + }); + } + + // 删除字典 + function removeDict(_key: string) { + var bln = false; + try { + dict.value.forEach((item, index) => { + if (item.key == _key) { + dict.value.splice(index, 1); + bln = true; + } + }); + } catch (e) { + bln = false; + } + return bln; + } + + // 清空字典 + function cleanDict() { + dict.value = new Array(); + } + // 初始字典 + function initDict() { } + + return { + dict, + getDict, + setDict, + removeDict, + cleanDict, + initDict, + }; +}); diff --git a/src/store/index.ts b/src/store/index.ts index 22c1ecb..52bf905 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -2,3 +2,4 @@ export * from './auth'; export * from './user'; export * from './pwo'; export * from './job'; +export * from './dict'; diff --git a/src/utils/dict.ts b/src/utils/dict.ts new file mode 100644 index 0000000..af954b2 --- /dev/null +++ b/src/utils/dict.ts @@ -0,0 +1,26 @@ +import { useDictStore } from "@/store"; +import { getDicts } from '@/api/system/dict' +import { ref, toRefs } from 'vue' + +/** + * 获取字典数据 + */ +export function useDict(...args: string[]) { + const res = ref({}) + + return (() => { + args.forEach((dictType, index) => { + res.value[dictType] = [] + const dicts = useDictStore().getDict(dictType) + if (dicts) { + res.value[dictType] = dicts + } else { + getDicts(dictType).then((resp: any) => { + res.value[dictType] = resp.data.map((p: any) => ({ label: p.dictLabel, value: p.dictValue, elTagType: p.listClass, elTagClass: p.cssClass })) + useDictStore().setDict(dictType, res.value[dictType]) + }) + } + }) + return toRefs(res.value) + })() +}