enum.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { shallowRef, ShallowRef } from 'vue'
  2. import { queryAllEnums } from '@/services/api/common'
  3. import { defineStore } from '../store'
  4. import { sessionData } from '../storage'
  5. import { i18n } from '@/stores'
  6. /**
  7. * 枚举类型
  8. */
  9. export interface EnumType {
  10. label: string;
  11. value: number;
  12. disabled?: boolean;
  13. bankmappedvalue?: string
  14. }
  15. const enumKeys = ['confirmStatus', 'countrycode', 'executetype', 'currency', 'deliveryOrderStatus', 'operatetypesearch', 'deliveryPayMode', 'applystatus', 'deliveryStatus', 'operateType', 'WRPresaleStatus', 'clientType', 'stepStatus', 'scoreConfigType', 'GZBSCPayStatus', 'gt_kksjdw', 'performanceStatus', 'handlestatus', 'performanceType', 'accountBusinessCode', 'certificatetype', 'signstatus', 'thjOrderStatus', 'THJDeliveryMode', 'goodsunit', 'WROutInApplyStatus2', 'THJTransferStatus', 'WRTradeOrderStatus', 'THJMarket', 'THJProfitRoleType', 'appointmentModelOut', 'orderstatus', 'Pricemode2', 'buildtype', 'listingselecttype', 'certypeperson', 'runstatus', 'traderstatus', 'transferapplystatus', 'ePayFlag', 'goodscurrency', 'paymodel'] as const
  16. const enumMap = new Map<typeof enumKeys[number], ShallowRef<Model.EnumRsp[]>>()
  17. // 初始化枚举列表
  18. for (const key of enumKeys) {
  19. enumMap.set(key, shallowRef<Model.EnumRsp[]>([]))
  20. }
  21. /**
  22. * 枚举存储对象
  23. */
  24. export const useEnumStore = defineStore(() => {
  25. const loading = shallowRef(false)
  26. const allEnums = sessionData.getRef('allEnums')
  27. // 获取所有枚举列表
  28. const getAllEnumList = async () => {
  29. if (!allEnums.value.length) {
  30. loading.value = true
  31. await queryAllEnums().then((res) => {
  32. allEnums.value = res.data
  33. }).finally(() => {
  34. loading.value = false
  35. })
  36. }
  37. // 清空列表数据
  38. for (const item of enumMap.values()) {
  39. item.value = []
  40. }
  41. allEnums.value.forEach((e) => {
  42. const mapKey = enumKeys.find((key) => key === e.enumdiccode)
  43. if (mapKey && e.enumitemstatus === 1) {
  44. const enumRef = enumMap.get(mapKey)
  45. enumRef?.value.push(e)
  46. }
  47. })
  48. }
  49. // 获取枚举信息
  50. const getEnumTypeInfo = (enumKey: typeof enumKeys[number], value: number) => {
  51. const enums = enumMap.get(enumKey)
  52. return enums?.value.find((e) => e.enumitemname === value)
  53. }
  54. // 获取枚举列表
  55. const getEnumTypeList = (enumKey: typeof enumKeys[number], propertys?: (keyof Model.EnumRsp)[]) => {
  56. const enums = enumMap.get(enumKey)
  57. if (enums) {
  58. return enums.value.map((e) => {
  59. const props = propertys?.reduce((res, prop) => {
  60. const value = e[prop]
  61. if (value) res.push(value)
  62. return res
  63. }, [] as unknown[])
  64. const enumdicname = shallowRef(e.enumdicname)
  65. switch (i18n.global.locale) {
  66. case 'zh-CN':
  67. enumdicname.value = e.enumdicname
  68. break;
  69. case 'en-US':
  70. enumdicname.value = e.enumdicnameen
  71. break;
  72. case 'zh-TW':
  73. enumdicname.value = e.enumdicnamezh_tw
  74. break;
  75. default:
  76. enumdicname.value = e.enumdicnameth
  77. break;
  78. }
  79. return {
  80. label: props?.length ? props.join('-') : enumdicname.value,
  81. value: e.enumitemname,
  82. bankmappedvalue: e.bankmappedvalue
  83. }
  84. })
  85. }
  86. return []
  87. }
  88. // 根据枚举值获取枚举名称
  89. const getEnumTypeName = (enums: EnumType[], value?: number) => {
  90. const item = enums.find((e) => e.value === value)
  91. return item?.label ?? value?.toString() ?? '--'
  92. }
  93. // 根据枚举名称获取对应的值
  94. const getEnumTypeValue = (enums: EnumType[], label?: string) => {
  95. const item = enums.find((e) => e.label === label)
  96. return item?.value ?? label
  97. }
  98. // 根据枚举名称获取对应的值
  99. const getBankMappedValue = (enums: EnumType[], bankmappedvalue?: string) => {
  100. const item = enums.find((e) => e.bankmappedvalue === bankmappedvalue)
  101. return item?.label ?? bankmappedvalue
  102. }
  103. // 根据枚举名称获取对应的值
  104. const getEnumTypeBankMappedValue = (enums: EnumType[], value?: number) => {
  105. const item = enums.find((e) => e.value === value)
  106. return item?.bankmappedvalue ?? ''
  107. }
  108. return {
  109. loading,
  110. allEnums,
  111. enumMap,
  112. getAllEnumList,
  113. getEnumTypeInfo,
  114. getEnumTypeList,
  115. getEnumTypeName,
  116. getEnumTypeValue,
  117. getBankMappedValue,
  118. getEnumTypeBankMappedValue
  119. }
  120. })