| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- import { defineStore } from 'pinia';
- import { ref } from 'vue';
- import { getAllDataDictList as getList } from '@/api';
- export const useDataDictStore = defineStore('dataDict', () => {
- const dataDict = ref([]);
- const map = new Map();
- /**
- * 加载数据字典
- */
- const loadDataDictList = async () => {
- dataDict.value = (await getList()).data;
- };
- /**
- * 获取type对应字典列表
- * @param type
- */
- const getDataDictList = type => {
- if (map.has(type) && map.get(type).length > 0) {
- return map.get(type);
- }
- const list = dataDict.value.filter(item => item.dictType === type);
- // const locale = uni.getLocale()
- // // label格式为'xxxx~~en**aaaa~~ru**bbbb', 根据当前语言环境进行处理
- // const list = dataDict.value.filter(item => item.dictType === type)
- // .map(item => {
- // if (item.label.includes('~~') && item.label.includes('**')) {
- // const s = item.label.split('~~')
- // if (locale.startsWith('zh')) {
- // item.label = s[0]
- // } else if (locale.startsWith('ru')) {
- // const s2 = s[2].split('**')
- // item.label = s2[1]
- // } else {
- // const s1 = s[1].split('**')
- // item.label = s1[1]
- // }
- // }
- // return JSON.parse(JSON.stringify(item))
- // })
- map.set(type, list);
- return list;
- };
- const getStrDictOptions = dictType => {
- // 获得通用的 DictDataType 列表
- const dictOptions = getDataDictList(dictType);
- // 转换成 string 类型的 StringDictDataType 类型
- // why 需要特殊转换:避免 IDEA 在 v-for="dict in getStrDictOptions(...)" 时,el-option 的 key 会告警
- const dictOption = [];
- dictOptions.forEach(dict => {
- dictOption.push({
- ...dict,
- value: dict.value + '',
- });
- });
- return dictOption;
- };
- const getIntDictOptions = dictType => {
- // 获得通用的 DictDataType 列表
- const dictOptions = getDataDictList(dictType);
- // 转换成 number 类型的 NumberDictDataType 类型
- // why 需要特殊转换:避免 IDEA 在 v-for="dict in getIntDictOptions(...)" 时,el-option 的 key 会告警
- const dictOption = [];
- dictOptions.forEach(dict => {
- dictOption.push({
- ...dict,
- value: parseInt(dict.value + ''),
- });
- });
- return dictOption;
- };
- return {
- dataDict,
- getDataDictList,
- loadDataDictList,
- getStrDictOptions,
- getIntDictOptions,
- };
- });
|