import { getDictionariesDetails } from '@/api/system' // 菜单类型字典 export const MENU_TYPE = { DIRECTORY: 0, MENU: 1, BUTTON: 2, COMPONENT: 3 } // 机构类型字典 export const ORGANIZATION_CATEGORY = [ { label: '支行', value: '支行' }, { label: '网点', value: '网点' }, { label: '部室', value: '部室' } ] // 审核状态字典 export const APPROVAL_STATUS = [ { text: '处理中', color: 'default' }, { text: '审核通过', color: 'success' }, { text: '审核拒绝', color: 'danger' }, { text: '异常结束', color: 'warning' } ] export const BONUS_ALLOCATION_STATUS = { 0: { text: '未分配', color: 'warning' }, 1: { text: '已分配', color: 'success' }, 2: { text: '已确认', color: 'info' } } // const EMPLOYEE_STATUS = {} // 字典缓存对象 const dictCache = {} // 字典配置 const DICT_CONFIG = { EMPLOYEE_STATUS: { code: 'employeeStatus', data: null } } /** * 获取字典数据 * @param {string} dictKey 字典键名 * @returns {Promise} 返回字典数据 */ async function getDict (dictKey) { // 如果缓存中有数据,直接返回 if (dictCache[dictKey]) { return dictCache[dictKey] } // 如果没有配置,抛出错误 if (!DICT_CONFIG[dictKey]) { throw new Error(`字典 ${dictKey} 未配置`) } // 如果已有数据但未缓存,先缓存 if (DICT_CONFIG[dictKey].data) { dictCache[dictKey] = DICT_CONFIG[dictKey].data return dictCache[dictKey] } // 从API获取数据 try { const { data } = await getDictionariesDetails({ dictCode: DICT_CONFIG[dictKey].code }) const _data = data.map(item => { return { label: item.dictTitle, value: item.dictValue, color: item.webContent?.dictColor } }) DICT_CONFIG[dictKey].data = _data dictCache[dictKey] = _data return _data } catch (error) { console.error(`获取字典 ${dictKey} 失败:`, error) throw error } } // 默认导出字典获取方法 export default { /** * 获取单个字典 * @param {string} dictKey 字典键名 * @returns {Promise} 返回字典数据 */ get: getDict, /** * 获取多个字典 * @param {Array} dictKeys 字典键名数组 * @returns {Promise} 返回字典对象 {dictKey1: data, dictKey2: data} */ async getMultiple (dictKeys) { const result = {} await Promise.all(dictKeys.map(async key => { result[key] = await getDict(key) })) return result }, /** * 预加载字典 * @param {Array} dictKeys 需要预加载的字典键名数组 */ preload (dictKeys) { return this.getMultiple(dictKeys) } }