enum.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { shallowRef, ShallowRef } from 'vue'
  2. import { queryAllEnums } from '@/services/api/common'
  3. import { createStore } from '../base'
  4. import { sessionData } from '../storage'
  5. /**
  6. * 枚举类型
  7. */
  8. export interface EnumType {
  9. label: string;
  10. value: number;
  11. disabled?: boolean;
  12. }
  13. const enumKeys = ['clientType', 'scoreConfigType', 'accountBusinessCode', 'certificatetype', 'signstatus', 'thjOrderStatus', 'THJDeliveryMode', 'goodsunit', 'WROutInApplyStatus2', 'THJTransferStatus', 'WRTradeOrderStatus', 'THJMarket', 'THJProfitRoleType'] as const
  14. export const enumMap = new Map<typeof enumKeys[number], ShallowRef<Model.EnumRsp[]>>()
  15. // 初始化枚举列表
  16. for (const key of enumKeys) {
  17. enumMap.set(key, shallowRef<Model.EnumRsp[]>([]))
  18. }
  19. /**
  20. * 枚举存储对象
  21. */
  22. export const enumStore = createStore({
  23. state() {
  24. return {
  25. loading: false,
  26. allEnums: sessionData.getRef('allEnums'),
  27. }
  28. },
  29. actions: {
  30. // 获取所有枚举列表
  31. async getAllEnumList() {
  32. await new Promise<void>((resolve) => {
  33. if (this.state.allEnums.length) {
  34. resolve()
  35. } else {
  36. this.state.loading = true
  37. queryAllEnums({
  38. success: (res) => {
  39. this.state.allEnums = res.data
  40. resolve()
  41. },
  42. complete: () => {
  43. this.state.loading = false
  44. }
  45. })
  46. }
  47. })
  48. // 清空列表数据
  49. for (const item of enumMap.values()) {
  50. item.value = []
  51. }
  52. this.state.allEnums.forEach((e) => {
  53. const mapKey = enumKeys.find((key) => key === e.enumdiccode)
  54. if (mapKey && e.enumitemstatus === 1) {
  55. const enumRef = enumMap.get(mapKey)
  56. enumRef?.value.push(e)
  57. }
  58. })
  59. },
  60. // 获取枚举信息
  61. getEnumTypeInfo(enumKey: typeof enumKeys[number], value: number) {
  62. const enums = enumMap.get(enumKey)
  63. return enums?.value.find((e) => e.enumitemname === value)
  64. },
  65. // 获取枚举列表
  66. getEnumTypeList: (enumKey: typeof enumKeys[number], propertys?: (keyof Model.EnumRsp)[]) => {
  67. const enums = enumMap.get(enumKey)
  68. if (enums) {
  69. return enums.value.map((e) => {
  70. const props = propertys?.reduce((res, prop) => {
  71. const value = e[prop]
  72. if (value) res.push(value)
  73. return res
  74. }, [] as unknown[])
  75. return {
  76. label: props?.length ? props.join('-') : e.enumdicname,
  77. value: e.enumitemname,
  78. }
  79. })
  80. }
  81. return []
  82. },
  83. // 根据枚举值获取枚举名称
  84. getEnumTypeName(enums: EnumType[], value?: number) {
  85. const item = enums.find((e) => e.value === value)
  86. return item?.label ?? value?.toString() ?? '无效枚举'
  87. },
  88. // 根据枚举名称获取对应的值
  89. getEnumTypeValue(enums: EnumType[], label?: string) {
  90. const item = enums.find((e) => e.label === label)
  91. return item?.value ?? label
  92. }
  93. },
  94. })