import { post } from './request.js' import { storage } from './storage.js' const DICT_KEY = 'dicts' /** * 字典工具模块 — 对标 PC 端 admin-web-new/src/utils/dict.ts */ const COLOR_PALETTE = [ '#1890FF', '#52C41A', '#FAAD14', '#F5222D', '#722ED1', '#13C2C2', '#EB2F96', '#FA8C16', '#2F54EB', '#009688', ] const BUILTIN_COLORS = { 'Order.status': { 0: '#1890FF', // 开机 — 蓝色 1: '#52C41A', // 成功 — 绿色 2: '#F5222D', // 失败 — 红色 3: '#999999', // 取消 — 灰色 }, 'Order.pay': { 0: '#FAAD14', // 未支付 — 橙色 1: '#52C41A', // 已支付 — 绿色 }, } const dictUtil = { /** * 加载全部字典数据,按 code 分组后存入 storage * @returns {Promise} 分组后的字典数据 */ loadDicts: async () => { try { const res = await post('/dataDict/list', { pageSize: 1024 }) 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) if (item?.color) { return item.color } // 兜底:内置语义色板 if (BUILTIN_COLORS[code] && BUILTIN_COLORS[code][value]) { return BUILTIN_COLORS[code][value] } // 兜底:通用色板取模 const v = Number(value) if (!isNaN(v)) { return COLOR_PALETTE[Math.abs(v) % COLOR_PALETTE.length] } return '' }, /** * 获取选项列表(用于下拉框等) * @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