|
|
@@ -0,0 +1,84 @@
|
|
|
+import { dictApi } from '/@/api/support/dict-api';
|
|
|
+import { reactive } from 'vue';
|
|
|
+
|
|
|
+const useBsDict = class {
|
|
|
+ dicts = {};
|
|
|
+
|
|
|
+ constructor() {
|
|
|
+ this.dicts = reactive({
|
|
|
+ items: [],
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ */
|
|
|
+ async init() {
|
|
|
+ const dictStr = localStorage.getItem('dicts');
|
|
|
+ if (dictStr) {
|
|
|
+ this.dicts.items = JSON.parse(dictStr);
|
|
|
+ } else {
|
|
|
+ await this.fetchDictData();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 刷新字典数据
|
|
|
+ */
|
|
|
+ async refresh() {
|
|
|
+ this.clear();
|
|
|
+ await this.fetchDictData();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 清除字典数据
|
|
|
+ * @returns {void}
|
|
|
+ */
|
|
|
+ clear() {
|
|
|
+ this.dicts.items = [];
|
|
|
+ localStorage.removeItem('dicts');
|
|
|
+ }
|
|
|
+
|
|
|
+ getLabelByValue(dictCode, dictValue) {
|
|
|
+ const obj = this.getDictItem(dictCode, dictValue);
|
|
|
+ return obj?.label;
|
|
|
+ }
|
|
|
+
|
|
|
+ getDictByCode(dictCode) {
|
|
|
+ return this.dicts.items.find((v) => v.dictCode === dictCode);
|
|
|
+ }
|
|
|
+
|
|
|
+ getDictItems(dictCode) {
|
|
|
+ const dict = this.getDictByCode(dictCode);
|
|
|
+ return dict?.dictItems;
|
|
|
+ }
|
|
|
+
|
|
|
+ getDictItem(dictCode, dictValue) {
|
|
|
+ const dict = this.getDictByCode(dictCode);
|
|
|
+ return dict?.dictItems.find((v) => v.value === dictValue);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 参数归一化
|
|
|
+ normalizeDictNames(data) {
|
|
|
+ return data.map((v) => ({
|
|
|
+ dictName: v.key.keyName,
|
|
|
+ dictCode: v.key.keyCode,
|
|
|
+ raw: v,
|
|
|
+ dictItems: v.value.map((vv) => ({
|
|
|
+ label: vv.valueName,
|
|
|
+ value: vv.valueCode,
|
|
|
+ raw: vv,
|
|
|
+ })),
|
|
|
+ }));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取字典数据
|
|
|
+ async fetchDictData() {
|
|
|
+ const res = await dictApi.getDictAll();
|
|
|
+ if (res?.code === 0) {
|
|
|
+ this.dicts.items = this.normalizeDictNames(res.data);
|
|
|
+ localStorage.setItem('dicts', JSON.stringify(this.dicts.items));
|
|
|
+ }
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+export default new useBsDict();
|