diff --git a/common/utils/dict.js b/common/utils/dict.js new file mode 100644 index 0000000..3195cf8 --- /dev/null +++ b/common/utils/dict.js @@ -0,0 +1,71 @@ +import globalConfig from '@/common/config/global'; +import cache from '@/common/utils/cache'; +import { useUserStore } from '@/pinia/user'; + +/** + * 内部辅助函数:获取完整字典数据(优先从userStore,其次缓存) + * @returns {Array} 字典数组 + */ +const getDictData = () => { + const userStore = useUserStore(); + // 优先取userStore中的字典,无则从缓存取,兜底空数组 + return userStore.dictData || cache.get(globalConfig.cache.dictDataKey) || []; +}; + +/** + * 获取指定dictType的所有字典项 + * @param {string} dictType 字典类型(如:ai_image_status) + * @returns {Array} 对应字典类型的[{value, label, ...}]数组 + */ +export const getDictList = (dictType) => { + if (!dictType) return []; + const dictData = getDictData(); + // 过滤时增加空值保护,避免filter报错 + return Array.isArray(dictData) ? dictData.filter(item => item?.dictType === dictType) : []; +}; + +/** + * 根据dictType和value获取对应的label + * @param {string} dictType 字典类型 + * @param {string|number} value 字典值 + * @returns {string} 对应的label(未找到返回空字符串) + */ +export const getDictLabel = (dictType, value) => { + if (!dictType || value === undefined || value === null) return ''; + const dictData = getDictData(); + if (!Array.isArray(dictData)) return ''; + + const targetItem = dictData.find( + item => item?.dictType === dictType && String(item?.value) === String(value) + ); + return targetItem?.label || ''; +}; + +/** + * 根据dictType和label获取对应的value + * @param {string} dictType 字典类型 + * @param {string} label 字典标签 + * @returns {string} 对应的value(未找到返回空字符串) + */ +export const getDictValue = (dictType, label) => { + if (!dictType || !label) return ''; + const dictData = getDictData(); + if (!Array.isArray(dictData)) return ''; + + const targetItem = dictData.find( + item => item?.dictType === dictType && item?.label === label + ); + return targetItem?.value || ''; +}; + +/** + * 获取指定dictType的字典项,仅返回{value, label}结构的简化数组 + * @param {string} dictType 字典类型 + * @returns {Array} 简化后的[{value, label}]数组 + */ +export const getDictSimpleList = (dictType) => { + return getDictList(dictType).map(item => ({ + value: item.value || '', + label: item.label || '' + })); +}; \ No newline at end of file diff --git a/main.js b/main.js index 9047c6c..54751ba 100644 --- a/main.js +++ b/main.js @@ -26,4 +26,24 @@ export function createApp() { pinia } } -// #endif \ No newline at end of file +// #endif + + +// 全局注册(可选):如果需要全局使用,可在 main.js 中注册为全局方法: +// javascript +// 运行 +// import { createSSRApp } from 'vue'; +// import * as dictUtils from '@/utils/dict'; +// +// export function createApp() { +// const app = createSSRApp(App); +// // 注册全局字典方法 +// app.config.globalProperties.$dict = dictUtils; +// return { app }; +// } +// 组件中使用: +// javascript +// 运行 +// import { getCurrentInstance } from 'vue'; +// const { proxy } = getCurrentInstance(); +// const label = proxy.$dict.getDictLabel('ai_image_status', 10); diff --git a/pages/mine/index.vue b/pages/mine/index.vue index 762f4a2..e8f021e 100644 --- a/pages/mine/index.vue +++ b/pages/mine/index.vue @@ -56,7 +56,25 @@ import { onShow } from '@dcloudio/uni-app' // 初始化Pinia仓库 const userStore = useUserStore() const userInfo = userStore.userInfo.user -console.log(userInfo.nickname) +import { getDictList, getDictLabel, getDictSimpleList } from '@/common/utils/dict'; +// 示例1:获取ai_image_status的完整字典列表 +const aiImageStatusList = ref([]); +// 示例2:获取ai_image_status中value=10的label +const aiImageStatus10Label = ref(''); + +// 获取ai_image_status的完整列表(包含所有字段) +aiImageStatusList.value = getDictList('ai_image_status'); +// 获取ai_image_status中value=10的label +aiImageStatus10Label.value = getDictLabel('ai_image_status', 10); +console.log('13') +console.log(aiImageStatusList.value) +console.log(aiImageStatus10Label.value) + +// 可选:获取仅包含value和label的简化列表 +const simpleList = getDictSimpleList('ai_image_status'); +console.log('简化列表:', simpleList); +console.log('24') +