From 9a60220020d30c13e5c70cb8cb48f774738f32be Mon Sep 17 00:00:00 2001 From: RuoYi Date: Thu, 24 Apr 2025 10:28:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=AD=97=E5=85=B8=E9=80=9A?= =?UTF-8?q?=E7=94=A8=E8=8E=B7=E5=8F=96=E6=96=B9=E6=B3=95useDict?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/system/dict/data.js | 52 +++++++++++++++++++++++++++++++++++ api/system/dict/type.js | 60 +++++++++++++++++++++++++++++++++++++++++ main.js | 2 ++ store/modules/dict.js | 56 ++++++++++++++++++++++++++++++++++++++ utils/dict.js | 26 ++++++++++++++++++ 5 files changed, 196 insertions(+) create mode 100644 api/system/dict/data.js create mode 100644 api/system/dict/type.js create mode 100644 store/modules/dict.js create mode 100644 utils/dict.js diff --git a/api/system/dict/data.js b/api/system/dict/data.js new file mode 100644 index 0000000..6c9eb79 --- /dev/null +++ b/api/system/dict/data.js @@ -0,0 +1,52 @@ +import request from '@/utils/request' + +// 查询字典数据列表 +export function listData(query) { + return request({ + url: '/system/dict/data/list', + method: 'get', + params: query + }) +} + +// 查询字典数据详细 +export function getData(dictCode) { + return request({ + url: '/system/dict/data/' + dictCode, + method: 'get' + }) +} + +// 根据字典类型查询字典数据信息 +export function getDicts(dictType) { + return request({ + url: '/system/dict/data/type/' + dictType, + method: 'get' + }) +} + +// 新增字典数据 +export function addData(data) { + return request({ + url: '/system/dict/data', + method: 'post', + data: data + }) +} + +// 修改字典数据 +export function updateData(data) { + return request({ + url: '/system/dict/data', + method: 'put', + data: data + }) +} + +// 删除字典数据 +export function delData(dictCode) { + return request({ + url: '/system/dict/data/' + dictCode, + method: 'delete' + }) +} diff --git a/api/system/dict/type.js b/api/system/dict/type.js new file mode 100644 index 0000000..a0254ba --- /dev/null +++ b/api/system/dict/type.js @@ -0,0 +1,60 @@ +import request from '@/utils/request' + +// 查询字典类型列表 +export function listType(query) { + return request({ + url: '/system/dict/type/list', + method: 'get', + params: query + }) +} + +// 查询字典类型详细 +export function getType(dictId) { + return request({ + url: '/system/dict/type/' + dictId, + method: 'get' + }) +} + +// 新增字典类型 +export function addType(data) { + return request({ + url: '/system/dict/type', + method: 'post', + data: data + }) +} + +// 修改字典类型 +export function updateType(data) { + return request({ + url: '/system/dict/type', + method: 'put', + data: data + }) +} + +// 删除字典类型 +export function delType(dictId) { + return request({ + url: '/system/dict/type/' + dictId, + method: 'delete' + }) +} + +// 刷新字典缓存 +export function refreshCache() { + return request({ + url: '/system/dict/type/refreshCache', + method: 'delete' + }) +} + +// 获取字典选择框列表 +export function optionselect() { + return request({ + url: '/system/dict/type/optionselect', + method: 'get' + }) +} diff --git a/main.js b/main.js index 4b783a9..76aa9bd 100644 --- a/main.js +++ b/main.js @@ -3,10 +3,12 @@ import App from './App' import store from './store' // store import { install } from './plugins' // plugins import './permission' // permission +import { useDict } from '@/utils/dict' export function createApp() { const app = createSSRApp(App) app.use(store) + app.config.globalProperties.useDict = useDict install(app) return { app diff --git a/store/modules/dict.js b/store/modules/dict.js new file mode 100644 index 0000000..8650b0a --- /dev/null +++ b/store/modules/dict.js @@ -0,0 +1,56 @@ +import { defineStore } from "pinia" + +const useDictStore = defineStore("dict", { + state: () => ({ + dict: new Array(), + }), + actions: { + // 获取字典 + getDict(_key) { + if (_key == null && _key == "") { + return null; + } + try { + for (let i = 0; i < this.dict.length; i++) { + if (this.dict[i].key == _key) { + return this.dict[i].value; + } + } + } catch (e) { + return null; + } + }, + // 设置字典 + setDict(_key, value) { + if (_key !== null && _key !== "") { + this.dict.push({ + key: _key, + value: value, + }); + } + }, + // 删除字典 + removeDict(_key) { + var bln = false; + try { + for (let i = 0; i < this.dict.length; i++) { + if (this.dict[i].key == _key) { + this.dict.splice(i, 1); + return true; + } + } + } catch (e) { + bln = false; + } + return bln; + }, + // 清空字典 + cleanDict() { + this.dict = new Array(); + }, + // 初始字典 + initDict() {}, + } +}) + +export default useDictStore diff --git a/utils/dict.js b/utils/dict.js new file mode 100644 index 0000000..d94ece1 --- /dev/null +++ b/utils/dict.js @@ -0,0 +1,26 @@ +import useDictStore from "@/store/modules/dict"; +import { getDicts } from "@/api/system/dict/data"; +import { ref, toRefs } from "vue"; + +/** + * 获取字典数据 + */ +export function useDict(...args) { + const res = ref({}); + return (() => { + args.forEach((dictType, index) => { + res.value[dictType] = []; + const dicts = useDictStore().getDict(dictType); + if (dicts) { + alert(1) + res.value[dictType] = dicts; + } else { + getDicts(dictType).then((resp) => { + res.value[dictType] = resp.data.map((p) => ({ label: p.dictLabel, value: p.dictValue, elTagType: p.listClass, elTagClass: p.cssClass })); + useDictStore().setDict(dictType, res.value[dictType]); + }); + } + }); + return toRefs(res.value); + })() +} \ No newline at end of file