import { getDataDictList } from '../api/dict.js' import { storage } from './index.js' const DICT_KEY = 'dicts' /** * 字典工具模块 — 对标 PC 端 admin-web-new/src/utils/dict.ts */ const dictUtil = { /** * 加载全部字典数据,按 code 分组后存入 storage * @returns {Promise} 分组后的字典数据 */ loadDicts: async () => { try { const res = await getDataDictList() if (res && res.code === 200) { const list = res.data const dictGroup = dictUtil.groupByKey(list, 'code') storage.set(DICT_KEY, dictGroup) return dictGroup } return {} } catch (e) { console.error('加载字典数据失败:', e) return {} } }, /** * 获取缓存的全部字典数据 * @returns {Object} { code: [items] } */ getDicts: () => { return storage.get(DICT_KEY) || {} }, /** * 获取指定编码的字典项列表 * @param {string} code - 字典编码 * @returns {Array} 字典项数组 */ getDictList: (code) => { const dicts = dictUtil.getDicts() return dicts[code] || [] }, /** * 根据字典编码和值获取显示名称 * @param {string} code - 字典编码 * @param {*} value - 字典值 * @returns {string} 显示名称 */ getDictLabel: (code, value) => { if (value === null || value === undefined || value === '') { return value } const dicts = dictUtil.getDicts() const list = dicts[code] if (!list || list.length === 0) { return String(value) } const item = list.find(k => k.value == value) return item ? item.name : String(value) }, /** * 根据字典编码和名称反查值 * @param {string} code - 字典编码 * @param {string} name - 显示名称 * @returns {*} 字典值 */ getDictValue: (code, name) => { const dicts = dictUtil.getDicts() const list = dicts[code] if (!list || list.length === 0) { return null } const item = list.find(k => k.name === name) return item ? item.value : null }, /** * 根据字典编码和值获取颜色 * @param {string} code - 字典编码 * @param {*} value - 字典值 * @returns {string} 颜色值 */ getDictColor: (code, value) => { if (value === null || value === undefined) { return '' } const list = dictUtil.getDictList(code) const item = list.find(k => k.value == value) return item?.color || '' }, /** * 获取选项列表(用于下拉框等) * @param {string} code - 字典编码 * @returns {Array<{label: string, value: any}>} */ getDictOptions: (code) => { const list = dictUtil.getDictList(code) return list.map(item => ({ label: item.name, value: item.value })) }, /** * 获取带"全部"选项的筛选器选项列表 * @param {string} code - 字典编码 * @returns {Array<{label: string, value: any}>} */ getDictFilterOptions: (code) => { const opts = dictUtil.getDictOptions(code) return [{ label: '全部', value: '' }, ...opts] }, /** * 格式化字典值(返回显示名称) */ formatDict: (code, value) => { return dictUtil.getDictLabel(code, value) }, /** * 清除缓存的字典数据 */ clearDicts: () => { storage.remove(DICT_KEY) }, /** * 按 key 分组 * @param {Array} elements - 原始数组 * @param {string} key - 分组字段 * @returns {Object} 分组后的对象 */ groupByKey: (elements, key) => { const map = {} if (!elements || elements.length === 0) { return map } for (let i = 0; i < elements.length; i++) { if (!elements[i][key] && elements[i][key] !== 0) { continue } const k = String(elements[i][key]) const tmp = map[k] || [] tmp.push(elements[i]) map[k] = tmp } return map } } export default dictUtil // 便捷导出(兼容旧的调用方式) export const fmtDictName = (code, value) => dictUtil.getDictLabel(code, value) export const getDictColor = (code, value) => dictUtil.getDictColor(code, value) export const loadDicts = dictUtil.loadDicts