couponNormalize.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. }