import { shallowRef, ShallowRef } from 'vue' import { queryAllEnums } from '@/services/api/common' import { defineStore } from '../store' import { sessionData } from '../storage' import { i18n } from '@/stores' /** * 枚举类型 */ export interface EnumType { label: string; value: number; disabled?: boolean; bankmappedvalue?: string } const enumKeys = ['confirmStatus', 'countrycode', 'executetype', 'currency', 'deliveryOrderStatus', 'operatetypesearch', 'deliveryPayMode', 'applystatus', 'deliveryStatus', 'operateType', 'WRPresaleStatus', 'clientType', 'stepStatus', 'scoreConfigType', 'GZBSCPayStatus', 'gt_kksjdw', 'performanceStatus', 'handlestatus', 'performanceType', 'accountBusinessCode', 'certificatetype', 'signstatus', 'thjOrderStatus', 'THJDeliveryMode', 'goodsunit', 'WROutInApplyStatus2', 'THJTransferStatus', 'WRTradeOrderStatus', 'THJMarket', 'THJProfitRoleType', 'appointmentModelOut', 'orderstatus', 'Pricemode2', 'buildtype', 'listingselecttype', 'certypeperson', 'runstatus', 'traderstatus', 'transferapplystatus', 'ePayFlag', 'goodscurrency', 'paymodel'] as const const enumMap = new Map>() // 初始化枚举列表 for (const key of enumKeys) { enumMap.set(key, shallowRef([])) } /** * 枚举存储对象 */ export const useEnumStore = defineStore(() => { const loading = shallowRef(false) const allEnums = sessionData.getRef('allEnums') // 获取所有枚举列表 const getAllEnumList = async () => { if (!allEnums.value.length) { loading.value = true await queryAllEnums().then((res) => { allEnums.value = res.data }).finally(() => { loading.value = false }) } // 清空列表数据 for (const item of enumMap.values()) { item.value = [] } allEnums.value.forEach((e) => { const mapKey = enumKeys.find((key) => key === e.enumdiccode) if (mapKey && e.enumitemstatus === 1) { const enumRef = enumMap.get(mapKey) enumRef?.value.push(e) } }) } // 获取枚举信息 const getEnumTypeInfo = (enumKey: typeof enumKeys[number], value: number) => { const enums = enumMap.get(enumKey) return enums?.value.find((e) => e.enumitemname === value) } // 获取枚举列表 const getEnumTypeList = (enumKey: typeof enumKeys[number], propertys?: (keyof Model.EnumRsp)[]) => { const enums = enumMap.get(enumKey) if (enums) { return enums.value.map((e) => { const props = propertys?.reduce((res, prop) => { const value = e[prop] if (value) res.push(value) return res }, [] as unknown[]) const enumdicname = shallowRef(e.enumdicname) switch (i18n.global.locale) { case 'zh-CN': enumdicname.value = e.enumdicname break; case 'en-US': enumdicname.value = e.enumdicnameen break; case 'zh-TW': enumdicname.value = e.enumdicnamezh_tw break; default: enumdicname.value = e.enumdicnameth break; } return { label: props?.length ? props.join('-') : enumdicname.value, value: e.enumitemname, bankmappedvalue: e.bankmappedvalue } }) } return [] } // 根据枚举值获取枚举名称 const getEnumTypeName = (enums: EnumType[], value?: number) => { const item = enums.find((e) => e.value === value) return item?.label ?? value?.toString() ?? '--' } // 根据枚举名称获取对应的值 const getEnumTypeValue = (enums: EnumType[], label?: string) => { const item = enums.find((e) => e.label === label) return item?.value ?? label } // 根据枚举名称获取对应的值 const getBankMappedValue = (enums: EnumType[], bankmappedvalue?: string) => { const item = enums.find((e) => e.bankmappedvalue === bankmappedvalue) return item?.label ?? bankmappedvalue } // 根据枚举名称获取对应的值 const getEnumTypeBankMappedValue = (enums: EnumType[], value?: number) => { const item = enums.find((e) => e.value === value) return item?.bankmappedvalue ?? '' } return { loading, allEnums, enumMap, getAllEnumList, getEnumTypeInfo, getEnumTypeList, getEnumTypeName, getEnumTypeValue, getBankMappedValue, getEnumTypeBankMappedValue } })