Commit 83208d2e209a06a329b7ce36066cb4630643d931

Authored by 刘淇
1 parent 420cb443

字典翻译

common/utils/dict.js 0 → 100644
  1 +import globalConfig from '@/common/config/global';
  2 +import cache from '@/common/utils/cache';
  3 +import { useUserStore } from '@/pinia/user';
  4 +
  5 +/**
  6 + * 内部辅助函数:获取完整字典数据(优先从userStore,其次缓存)
  7 + * @returns {Array} 字典数组
  8 + */
  9 +const getDictData = () => {
  10 + const userStore = useUserStore();
  11 + // 优先取userStore中的字典,无则从缓存取,兜底空数组
  12 + return userStore.dictData || cache.get(globalConfig.cache.dictDataKey) || [];
  13 +};
  14 +
  15 +/**
  16 + * 获取指定dictType的所有字典项
  17 + * @param {string} dictType 字典类型(如:ai_image_status)
  18 + * @returns {Array} 对应字典类型的[{value, label, ...}]数组
  19 + */
  20 +export const getDictList = (dictType) => {
  21 + if (!dictType) return [];
  22 + const dictData = getDictData();
  23 + // 过滤时增加空值保护,避免filter报错
  24 + return Array.isArray(dictData) ? dictData.filter(item => item?.dictType === dictType) : [];
  25 +};
  26 +
  27 +/**
  28 + * 根据dictType和value获取对应的label
  29 + * @param {string} dictType 字典类型
  30 + * @param {string|number} value 字典值
  31 + * @returns {string} 对应的label(未找到返回空字符串)
  32 + */
  33 +export const getDictLabel = (dictType, value) => {
  34 + if (!dictType || value === undefined || value === null) return '';
  35 + const dictData = getDictData();
  36 + if (!Array.isArray(dictData)) return '';
  37 +
  38 + const targetItem = dictData.find(
  39 + item => item?.dictType === dictType && String(item?.value) === String(value)
  40 + );
  41 + return targetItem?.label || '';
  42 +};
  43 +
  44 +/**
  45 + * 根据dictType和label获取对应的value
  46 + * @param {string} dictType 字典类型
  47 + * @param {string} label 字典标签
  48 + * @returns {string} 对应的value(未找到返回空字符串)
  49 + */
  50 +export const getDictValue = (dictType, label) => {
  51 + if (!dictType || !label) return '';
  52 + const dictData = getDictData();
  53 + if (!Array.isArray(dictData)) return '';
  54 +
  55 + const targetItem = dictData.find(
  56 + item => item?.dictType === dictType && item?.label === label
  57 + );
  58 + return targetItem?.value || '';
  59 +};
  60 +
  61 +/**
  62 + * 获取指定dictType的字典项,仅返回{value, label}结构的简化数组
  63 + * @param {string} dictType 字典类型
  64 + * @returns {Array} 简化后的[{value, label}]数组
  65 + */
  66 +export const getDictSimpleList = (dictType) => {
  67 + return getDictList(dictType).map(item => ({
  68 + value: item.value || '',
  69 + label: item.label || ''
  70 + }));
  71 +};
0 72 \ No newline at end of file
... ...
... ... @@ -26,4 +26,24 @@ export function createApp() {
26 26 pinia
27 27 }
28 28 }
29   -// #endif
30 29 \ No newline at end of file
  30 +// #endif
  31 +
  32 +
  33 +// 全局注册(可选):如果需要全局使用,可在 main.js 中注册为全局方法:
  34 +// javascript
  35 +// 运行
  36 +// import { createSSRApp } from 'vue';
  37 +// import * as dictUtils from '@/utils/dict';
  38 +//
  39 +// export function createApp() {
  40 +// const app = createSSRApp(App);
  41 +// // 注册全局字典方法
  42 +// app.config.globalProperties.$dict = dictUtils;
  43 +// return { app };
  44 +// }
  45 +// 组件中使用:
  46 +// javascript
  47 +// 运行
  48 +// import { getCurrentInstance } from 'vue';
  49 +// const { proxy } = getCurrentInstance();
  50 +// const label = proxy.$dict.getDictLabel('ai_image_status', 10);
... ...
pages/mine/index.vue
... ... @@ -56,7 +56,25 @@ import { onShow } from '@dcloudio/uni-app'
56 56 // 初始化Pinia仓库
57 57 const userStore = useUserStore()
58 58 const userInfo = userStore.userInfo.user
59   -console.log(userInfo.nickname)
  59 +import { getDictList, getDictLabel, getDictSimpleList } from '@/common/utils/dict';
  60 +// 示例1:获取ai_image_status的完整字典列表
  61 +const aiImageStatusList = ref([]);
  62 +// 示例2:获取ai_image_status中value=10的label
  63 +const aiImageStatus10Label = ref('');
  64 +
  65 +// 获取ai_image_status的完整列表(包含所有字段)
  66 +aiImageStatusList.value = getDictList('ai_image_status');
  67 +// 获取ai_image_status中value=10的label
  68 +aiImageStatus10Label.value = getDictLabel('ai_image_status', 10);
  69 +console.log('13')
  70 +console.log(aiImageStatusList.value)
  71 +console.log(aiImageStatus10Label.value)
  72 +
  73 +// 可选:获取仅包含value和label的简化列表
  74 +const simpleList = getDictSimpleList('ai_image_status');
  75 +console.log('简化列表:', simpleList);
  76 +console.log('24')
  77 +
60 78  
61 79  
62 80  
... ...