import { defineStore } from 'pinia' /** * 业务字典插件(utils/vue-dict)的缓存。 * 存的是后端原始字典数组,与模板 useDict 缓存的 { label, value, elTagType, elTagClass } * 形状不同,因此独立存放,避免同一 dictType 被两种消费者互相污染。 */ const useBizDictStore = defineStore( 'bizDict', { 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) { 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) { return false } return false }, // 清空字典 cleanDict() { this.dict = new Array() } } }) export default useBizDictStore