| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- import { toRefs, shallowRef, shallowReadonly, ShallowRef } from 'vue'
- import { queryAllEnums } from '@/services/api/common'
- import { VueStore } from '../base'
- import WebStorage from '@/utils/storage/base'
- /**
- * 枚举类型
- */
- export interface EnumType {
- label: string;
- value: number;
- disabled?: boolean;
- }
- interface StoreState {
- loading: boolean;
- allEnums: ShallowRef<Ermcp.EnumRsp[]>;
- }
- const enumKeys = ['ZSCategory', 'ZSCurrencyType', 'ZSCurrencyType', 'ZSColorType', 'ZSClarityType', 'ZSCutType', 'ZSShapeType', 'ZSSymmetryType', 'ZSPolishType', 'ZSFluorescenceType', 'ZSCertType', 'ZSCrystalType', 'ZSCZColor1Type', 'ZSCZColor2Type', 'ZSCZColor3Type', 'ZSStyleType', 'signstatus', 'applystatus', 'executetype', 'certificatetype', 'clientType', 'wrApplyStatus', 'performanceStatus', 'stepStatus', 'GZCJAccountType', 'GZCJCategoryType', 'GZCJDeliveryType', 'GZCJShapeType', 'GZCJMarkType', 'GZCJPublishType', 'GZCJServiceType', 'GZCJStatus', 'GZBSStatus', 'GZBSDeliveryType'] as const
- /**
- * 枚举存储类
- */
- const store = new (class extends VueStore<StoreState>{
- constructor() {
- const storage = new WebStorage<Ermcp.EnumRsp[]>(sessionStorage, 'allEnums', [])
- const state: StoreState = {
- loading: false,
- allEnums: storage.getRef(),
- }
- super(state)
- // 初始化枚举列表
- for (const key of enumKeys) {
- this.enumMap.set(key, shallowRef<Ermcp.EnumRsp[]>([]))
- }
- }
- enumMap = new Map<typeof enumKeys[number], ShallowRef<Ermcp.EnumRsp[]>>()
- private setEnumMap = () => {
- // 清空列表数据
- for (const item of this.enumMap.values()) {
- item.value = []
- }
- this.state.allEnums.forEach((e) => {
- const mapKey = enumKeys.find((key) => key === e.enumdiccode)
- if (mapKey && e.enumitemstatus === 1) {
- const enumRef = this.enumMap.get(mapKey)
- enumRef?.value.push(e)
- }
- })
- }
- actions = {
- /** 获取所有枚举列表 */
- getAllEnumList: () => {
- if (this.state.allEnums.length) {
- this.setEnumMap()
- return Promise.resolve()
- }
- this.state.loading = true
- return queryAllEnums({
- success: (res) => {
- this.state.allEnums = res.data
- this.setEnumMap()
- },
- complete: () => {
- this.state.loading = false
- }
- })
- },
- /** 获取枚举信息 */
- getEnumTypeInfo: (enumKey: typeof enumKeys[number], value: number) => {
- const enums = this.enumMap.get(enumKey)
- return enums?.value.find((e) => e.enumitemname === value)
- },
- /** 获取枚举列表 */
- getEnumTypeList: (enumKey: typeof enumKeys[number], propertys?: (keyof Ermcp.EnumRsp)[]) => {
- const enums = this.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
- }
- }
- })
- export function useEnumStore() {
- return shallowReadonly({
- ...toRefs(store.state),
- ...store.actions,
- ...store.methods,
- enumMap: store.enumMap,
- })
- }
|