| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- import { shallowRef, ShallowRef } from 'vue'
- import { queryAllEnums } from '@/services/api/common'
- import { defineStore } from '../store'
- import { sessionData } from '../storage'
- /**
- * 枚举类型
- */
- export interface EnumType {
- label: string;
- value: number;
- disabled?: boolean;
- }
- const enumKeys = ['confirmStatus', 'deliveryPayMode', 'deliveryStatus', 'WRPresaleStatus', 'clientType', 'stepStatus', 'scoreConfigType', 'GZBSCPayStatus', 'performanceStatus', 'handlestatus', 'performanceType', 'accountBusinessCode', 'certificatetype', 'signstatus', 'thjOrderStatus', 'THJDeliveryMode', 'goodsunit', 'WROutInApplyStatus2', 'THJTransferStatus', 'WRTradeOrderStatus', 'THJMarket', 'THJProfitRoleType', 'appointmentModelOut', 'orderstatus', 'Pricemode2', 'buildtype', 'listingselecttype', 'certypeperson', 'runstatus'] as const
- const enumMap = new Map<typeof enumKeys[number], ShallowRef<Model.EnumRsp[]>>()
- // 初始化枚举列表
- for (const key of enumKeys) {
- enumMap.set(key, shallowRef<Model.EnumRsp[]>([]))
- }
- /**
- * 枚举存储对象
- */
- 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[])
- return {
- label: props?.length ? props.join('-') : e.enumdicname,
- value: e.enumitemname,
- }
- })
- }
- 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
- }
- return {
- loading,
- allEnums,
- enumMap,
- getAllEnumList,
- getEnumTypeInfo,
- getEnumTypeList,
- getEnumTypeName,
- getEnumTypeValue
- }
- })
|