From 13e53568985d0851b70a6d10a9661b01c02b46c5 Mon Sep 17 00:00:00 2001 From: aixianling Date: Tue, 15 Mar 2022 10:03:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E5=90=88js=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 3 ++ src/common/dict.js | 72 ++++++++++++++++++++++++++++------------------ 2 files changed, 47 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index e2b06e9f..c61b4d56 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,9 @@ "main": "src/apps/index.js", "files": [ "src/components", + "src/common/util.js", + "src/common/dict.js", + "src/common/monent.js", "src/apps", "src/saas" ], diff --git a/src/common/dict.js b/src/common/dict.js index c7d880ea..af917c00 100644 --- a/src/common/dict.js +++ b/src/common/dict.js @@ -1,50 +1,66 @@ -import request from "./axios"; -import store from "../store"; - /** * 封装字典工具类 */ const $dict = { - vueStore: null, + instance: null, url: "/admin/dictionary/queryValsByCodeList", - setUrl(v) { - this.url = v + init(config) { + this.instance = config?.instance + this.url = config?.url || this.url + }, + dicts() { + return uni.getStorageSync('dicts') || []; }, load(...code) { - if (!this.vueStore) this.vueStore = store - return request.post(this.url, null, { + return this.instance && this.instance.post(this.url, null, { + withoutToken:true, params: { - codeList: code.join(',') + codeList: code.toString() } }).then((res) => { - this.vueStore.commit("setDicts", res.data) + if (res && res.data) { + let cacheDicts = {}, + meta = {}; + this.dicts().map((e) => (cacheDicts[e.key] = e)); + res.data.map((e) => (meta[e.key] = e)); + let dicts = {...cacheDicts, ...meta}; + uni.setStorageSync('dicts', Object.values(dicts)); + } }) }, getDict(key) { - let dict = this.vueStore.getters.getDict(key) - return dict ? dict.values : [] + if (this.dicts().length) { + let dict = this.dicts().find((e) => e.key == key); + return dict ? dict.values : []; + } else return []; }, getValue(key, label) { - let dict = this.vueStore.getters.getDict(key) - if (dict) { - let item = dict.values.find(v => v.dictName == label) - return item ? item.dictValue : label - } else return label + if (this.dicts().length) { + let dict = this.dicts().find((e) => e.key == key); + if (dict) { + let item = dict.values.find((v) => v.dictName == label); + return item ? item.dictValue : label; + } else return label; + } else return label; }, getLabel(key, value) { - let dict = this.vueStore.getters.getDict(key) - if (dict) { - let item = dict.values.find(v => v.dictValue == value) - return item ? item.dictName : value - } else return value + if (this.dicts().length) { + let dict = this.dicts().find((e) => e.key == key); + if (dict) { + let item = dict.values.find((v) => v.dictValue == value); + return item ? item.dictName : value; + } else return value ? value : ''; + } else return value ? value : ''; }, getColor(key, value) { - let dict = this.vueStore.getters.getDict(key) - if (dict) { - let item = dict.values.find(v => v.dictValue == value) - return item ? item.dictColor : value - } else return value - }, + if (this.dicts().length) { + let dict = this.dicts().find((e) => e.key == key); + if (dict) { + let item = dict.values.find((v) => v.dictValue == value); + return item ? item.dictColor : value; + } else return value; + } else return value; + } } export default $dict