couponNormalize.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * 将 getUserCouponList 等接口返回的单条优惠券规范为前端统一结构(与券包页一致)
  3. * @param {object} raw 接口原始项
  4. * @param {number} [tabStatus=0] 列表 Tab 状态(券包页传入 currentTab)
  5. */
  6. export function normalizeUserCouponListItem(raw, tabStatus = 0) {
  7. if (!raw || typeof raw !== 'object') return null;
  8. const couponType = Number(raw.couponType) ?? 1;
  9. const nominalValue = Number(raw.nominalValue ?? raw.amount ?? 0) || 0;
  10. const discountRate = ((Number(raw.discountRate ?? 0) || 0) / 10) || 0;
  11. const minAmount = Number(raw.minimumSpendingAmount ?? raw.minAmount ?? 0) || 0;
  12. let amount = nominalValue;
  13. let amountUnit = '元';
  14. let conditionText = minAmount > 0 ? `满${minAmount}可用` : '无门槛';
  15. if (couponType === 2 && discountRate > 0) {
  16. amount = discountRate;
  17. amountUnit = '折';
  18. conditionText = minAmount > 0 ? `满${minAmount}可用` : '无门槛';
  19. }
  20. const expirationTime =
  21. raw.expirationTime ?? raw.endGetDate ?? raw.expireDate ?? '';
  22. const longTermValid = Number(raw.longTermValid) === 1 ? 1 : 0;
  23. // id 用于列表选中态与 :key,必须为「用户持有实例」唯一键;勿把模板 couponId 放首位,否则多张相同活动券会 id 重复、出现多选假象
  24. const listId =
  25. raw.userCouponId ??
  26. raw.user_coupon_id ??
  27. raw.memberCouponId ??
  28. raw.receiveRecordId ??
  29. raw.receiveId ??
  30. raw.id ??
  31. raw.couponId ??
  32. '';
  33. return {
  34. id: listId,
  35. /** 券模板/业务侧 couponId,与 id(用户券实例)区分,支付等场景按需选用 */
  36. couponId: raw.couponId ?? raw.coupon_id ?? '',
  37. amount,
  38. amountUnit,
  39. minAmount,
  40. name: raw.name ?? raw.title ?? raw.couponName ?? '',
  41. expirationTime,
  42. expireDate: expirationTime,
  43. longTermValid,
  44. status: tabStatus,
  45. couponType,
  46. nominalValue,
  47. discountRate,
  48. conditionText,
  49. specifiedDay: raw.specifiedDay ?? raw.validDays ?? '',
  50. supplementaryInstruction: raw.supplementaryInstruction ?? raw.description ?? '',
  51. qrCodeUrl: raw.qrCodeUrl ?? raw.qrcodeUrl ?? raw.qrUrl ?? '',
  52. verificationCode:
  53. raw.verificationCode ?? raw.verifyCode ?? raw.couponCode ?? raw.pickUpCode ?? ''
  54. };
  55. }
  56. /** 解析 getUserCouponList 分页响应(数组或 { records/list, total }) */
  57. export function parseCouponListPage(res) {
  58. if (res == null) return { list: [], total: 0 };
  59. if (Array.isArray(res)) return { list: res, total: 0 };
  60. const data = res.data !== undefined && res.data !== null && !Array.isArray(res) ? res.data : res;
  61. if (Array.isArray(data)) return { list: data, total: 0 };
  62. if (data && typeof data === 'object') {
  63. const list =
  64. data.records ??
  65. data.list ??
  66. data.rows ??
  67. data.items ??
  68. (Array.isArray(data.data) ? data.data : []);
  69. const total =
  70. Number(data.total ?? data.totalCount ?? data.totalElements ?? data.count ?? res.total ?? res.totalCount ?? 0) ||
  71. 0;
  72. return { list: Array.isArray(list) ? list : [], total };
  73. }
  74. return { list: [], total: 0 };
  75. }
  76. /** 分页追加时按 id 去重 */
  77. export function mergeCouponListById(existing, incoming) {
  78. const seen = new Set((existing ?? []).map((c) => String(c?.id ?? '')));
  79. const out = [...(existing ?? [])];
  80. for (const item of incoming ?? []) {
  81. const id = String(item?.id ?? '');
  82. if (id && !seen.has(id)) {
  83. seen.add(id);
  84. out.push(item);
  85. } else if (!id) {
  86. out.push(item);
  87. }
  88. }
  89. return out;
  90. }