From a79902651a5f9368d5123c64904f2b87e419a3d3 Mon Sep 17 00:00:00 2001 From: kubbo <390378816@qq.com> Date: Wed, 8 Feb 2023 23:53:08 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8F=9C=E5=8D=95=E7=AE=A1=E7=90=86=E5=9F=BA?= =?UTF-8?q?=E6=9C=AC=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/apps/AppMenu.vue | 60 ++++++++++++++++++++--- web/src/components/KuSelect.vue | 85 +++++++++++++++++++++++++++++++++ web/src/utils/dict.js | 74 ++++++++++++++++++++++++++++ web/src/utils/tools.js | 3 +- 4 files changed, 215 insertions(+), 7 deletions(-) create mode 100644 web/src/components/KuSelect.vue create mode 100644 web/src/utils/dict.js diff --git a/web/src/apps/AppMenu.vue b/web/src/apps/AppMenu.vue index 4027b70..dfe04d8 100644 --- a/web/src/apps/AppMenu.vue +++ b/web/src/apps/AppMenu.vue @@ -3,10 +3,10 @@ @@ -17,22 +17,61 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/web/src/utils/dict.js b/web/src/utils/dict.js new file mode 100644 index 0000000..99eef24 --- /dev/null +++ b/web/src/utils/dict.js @@ -0,0 +1,74 @@ +import http from "./axios"; + +/** + * 封装字典工具类 + */ +const $dict = { + url: "/api/dict/listByCodes", + loading: [], + resolves: [], + getStorage() { + const dicts = JSON.parse(localStorage.getItem('dicts') || null) + return dicts?.data || dicts || []; + }, + getData(codeList) { + codeList = [codeList].flat().filter(Boolean).toString() + return http.post(this.url, null, { + withoutToken: true, params: {codeList} + }).then(res => res?.data && this.setStorage(res.data)) + }, + load(...code) { + return new Promise(resolve => { + this.resolves.push(resolve) + if (this.loading.length == 2) { + const [timer, codes] = this.loading; + clearTimeout(timer) + code = Array.from(new Set([codes, code].flat())) + } + const timer = setTimeout(() => { + this.getData(code).then(() => Promise.all(this.resolves.map(e => e())).then(() => this.resolves = [])) + }, 500) + this.loading = [timer, code] + }) + }, + setStorage(data) { + let ds = this.getStorage() + data.map(p => { + if (ds.some(d => d.key == p.key)) { + const index = ds.findIndex(d => d.key == p.key) + ds.splice(index, 1, p) + } else { + ds.push(p) + } + }) + localStorage.setItem("dicts", JSON.stringify([ds].flat())) + }, + getDict(key) { + let dict = this.getStorage().find(e => e.key == key) + !dict && console.warn("字典%s缺少加载...", key) + return dict ? dict.values : [] + }, + getValue(key, label) { + let dict = this.getDict(key) + if (dict) { + let item = dict.find(v => v.dictName == label) + return item ? item.dictValue : label + } else return label + }, + getLabel(key, value) { + let dict = this.getDict(key) + if (dict) { + let item = dict.find(v => v.dictValue == value) + return item ? item.dictName : value + } else return value + }, + getColor(key, value) { + let dict = this.getDict(key) + if (dict) { + let item = dict.find(v => v.dictValue == value) + return item ? item.dictColor : value + } else return value + }, +} + +export default $dict diff --git a/web/src/utils/tools.js b/web/src/utils/tools.js index b8473a6..afe3edd 100644 --- a/web/src/utils/tools.js +++ b/web/src/utils/tools.js @@ -1,4 +1,5 @@ import {mainStore} from "./store"; +import $dict from "./dict"; export const getToken = () => mainStore()?.token export const $confirm = () => { @@ -28,5 +29,5 @@ const $arr2tree = (list, config = {}) => { return result } export default { - getToken, $confirm, $arr2tree + getToken, $confirm, $arr2tree, $dict }