| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- import { shallowRef, ShallowRef } from 'vue'
- import { queryAllEnums } from '@/services/api/common'
- import { createStore } from '../base'
- import { sessionData } from '../storage'
- /**
- * 枚举类型
- */
- export interface EnumType {
- label: string;
- value: number;
- disabled?: boolean;
- }
- const enumKeys = ['clientType', 'scoreConfigType', 'accountBusinessCode', 'certificatetype', 'signstatus', 'thjOrderStatus', 'THJDeliveryMode', 'goodsunit', 'WROutInApplyStatus2', 'THJTransferStatus', 'WRTradeOrderStatus', 'THJMarket', 'THJProfitRoleType'] as const
- export const enumMap = new Map<typeof enumKeys[number], ShallowRef<Model.EnumRsp[]>>()
- // 初始化枚举列表
- for (const key of enumKeys) {
- enumMap.set(key, shallowRef<Model.EnumRsp[]>([]))
- }
- /**
- * 枚举存储对象
- */
- export const enumStore = createStore({
- state() {
- return {
- loading: false,
- allEnums: sessionData.getRef('allEnums'),
- }
- },
- actions: {
- // 获取所有枚举列表
- async getAllEnumList() {
- await new Promise<void>((resolve) => {
- if (this.state.allEnums.length) {
- resolve()
- } else {
- this.state.loading = true
- queryAllEnums({
- success: (res) => {
- this.state.allEnums = res.data
- resolve()
- },
- complete: () => {
- this.state.loading = false
- }
- })
- }
- })
- // 清空列表数据
- for (const item of enumMap.values()) {
- item.value = []
- }
- this.state.allEnums.forEach((e) => {
- const mapKey = enumKeys.find((key) => key === e.enumdiccode)
- if (mapKey && e.enumitemstatus === 1) {
- const enumRef = enumMap.get(mapKey)
- enumRef?.value.push(e)
- }
- })
- },
- // 获取枚举信息
- getEnumTypeInfo(enumKey: typeof enumKeys[number], value: number) {
- const enums = enumMap.get(enumKey)
- return enums?.value.find((e) => e.enumitemname === value)
- },
- // 获取枚举列表
- 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 []
- },
- // 根据枚举值获取枚举名称
- getEnumTypeName(enums: EnumType[], value?: number) {
- const item = enums.find((e) => e.value === value)
- return item?.label ?? value?.toString() ?? '无效枚举'
- },
- // 根据枚举名称获取对应的值
- getEnumTypeValue(enums: EnumType[], label?: string) {
- const item = enums.find((e) => e.label === label)
- return item?.value ?? label
- }
- },
- })
|