enum.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { toRefs, shallowRef, shallowReadonly, ShallowRef } from 'vue'
  2. import { queryAllEnums } from '@/services/api/common'
  3. import { VueStore } from '../base'
  4. import WebStorage from '@/utils/storage/base'
  5. /**
  6. * 枚举类型
  7. */
  8. export interface EnumType {
  9. label: string;
  10. value: number;
  11. disabled?: boolean;
  12. }
  13. interface StoreState {
  14. loading: boolean;
  15. allEnums: ShallowRef<Ermcp.EnumRsp[]>;
  16. }
  17. 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
  18. /**
  19. * 枚举存储类
  20. */
  21. const store = new (class extends VueStore<StoreState>{
  22. constructor() {
  23. const storage = new WebStorage<Ermcp.EnumRsp[]>(sessionStorage, 'allEnums', [])
  24. const state: StoreState = {
  25. loading: false,
  26. allEnums: storage.getRef(),
  27. }
  28. super(state)
  29. // 初始化枚举列表
  30. for (const key of enumKeys) {
  31. this.enumMap.set(key, shallowRef<Ermcp.EnumRsp[]>([]))
  32. }
  33. }
  34. enumMap = new Map<typeof enumKeys[number], ShallowRef<Ermcp.EnumRsp[]>>()
  35. private setEnumMap = () => {
  36. // 清空列表数据
  37. for (const item of this.enumMap.values()) {
  38. item.value = []
  39. }
  40. this.state.allEnums.forEach((e) => {
  41. const mapKey = enumKeys.find((key) => key === e.enumdiccode)
  42. if (mapKey && e.enumitemstatus === 1) {
  43. const enumRef = this.enumMap.get(mapKey)
  44. enumRef?.value.push(e)
  45. }
  46. })
  47. }
  48. actions = {
  49. /** 获取所有枚举列表 */
  50. getAllEnumList: () => {
  51. if (this.state.allEnums.length) {
  52. this.setEnumMap()
  53. return Promise.resolve()
  54. }
  55. this.state.loading = true
  56. return queryAllEnums({
  57. success: (res) => {
  58. this.state.allEnums = res.data
  59. this.setEnumMap()
  60. },
  61. complete: () => {
  62. this.state.loading = false
  63. }
  64. })
  65. },
  66. /** 获取枚举信息 */
  67. getEnumTypeInfo: (enumKey: typeof enumKeys[number], value: number) => {
  68. const enums = this.enumMap.get(enumKey)
  69. return enums?.value.find((e) => e.enumitemname === value)
  70. },
  71. /** 获取枚举列表 */
  72. getEnumTypeList: (enumKey: typeof enumKeys[number], propertys?: (keyof Ermcp.EnumRsp)[]) => {
  73. const enums = this.enumMap.get(enumKey)
  74. if (enums) {
  75. return enums.value.map((e) => {
  76. const props = propertys?.reduce((res, prop) => {
  77. const value = e[prop]
  78. if (value) res.push(value)
  79. return res
  80. }, [] as unknown[])
  81. return {
  82. label: props?.length ? props.join('-') : e.enumdicname,
  83. value: e.enumitemname,
  84. }
  85. })
  86. }
  87. return []
  88. },
  89. /** 根据枚举值获取枚举名称 */
  90. getEnumTypeName: (enums: EnumType[], value?: number) => {
  91. const item = enums.find((e) => e.value === value)
  92. return item?.label ?? value
  93. }
  94. }
  95. })
  96. export function useEnumStore() {
  97. return shallowReadonly({
  98. ...toRefs(store.state),
  99. ...store.actions,
  100. ...store.methods,
  101. enumMap: store.enumMap,
  102. })
  103. }