import { dictionary, dictionaryDefine } from './dictionary-define' import { dictionaryOther, dictionaryOtherDefine } from './dictionary-other-define' import { defineStore } from 'pinia' import {dictGetAllSysDictsByValue} from "@/api/modules/manage/dict"; const listToMap = (list) => { const map = new Map() list.forEach(v => { map.set(v.dictValue, v.dictLabel) }) return map } export const useDictionaryStore = defineStore('dictionary', { state: () => ({ ...dictionary, ...dictionaryOther, typeLevelList: [ {dictLabel: '1级', dictValue: 1}, {dictLabel: '2级', dictValue: 2}, {dictLabel: '3级', dictValue: 3}, {dictLabel: '4级', dictValue: 4}, ] }), getters: { typeLevelMap() { return listToMap(this.typeLevelList) }, }, actions: { initDict(type) { return new Promise((resolve, reject) => { if (this[dictionaryDefine[type][0]].length === 0) { dictGetAllSysDictsByValue(`dictValue=${type}`).then((res: any) => { const data = res.data const map = new Map() const objMap = new Map() if (data.length > 0) { data.forEach((v: any) => { v.selectLabel = v.dictLabel v.selectValue = v.dictValue map.set(v.dictValue, v.dictLabel) objMap.set(v.dictValue, v) }) } this[dictionaryDefine[type][0]] = data this[dictionaryDefine[type][1]] = map this[dictionaryDefine[type]?.[2]] = objMap resolve(res.data) }).catch((e: any) => { reject('获取'+dictionaryDefine[type][0]+'字典错误') }) } else { resolve(this[dictionaryDefine[type][0]]) } }) }, initOtherDict(type, data) { return new Promise((resolve, reject) => { if (this[dictionaryOtherDefine[type][0]].length === 0) { const map = new Map() const objMap = new Map() if (data.length > 0) { data.forEach((v: any) => { v.selectLabel = v.dictLabel v.selectValue = v.dictValue map.set(v.dictValue, v.dictLabel) objMap.set(v.dictValue, v) }) } this[dictionaryOtherDefine[type][0]] = data this[dictionaryOtherDefine[type][1]] = map this[dictionaryOtherDefine[type]?.[2]] = objMap } else { resolve(this[dictionaryOtherDefine[type][0]]) } }) } }, })