巴青农资商城

checkoutDisplay.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { resolveFileUrl } from '@/utils/image'
  2. import { formatPrice } from '@/utils/format'
  3. import { parseCartSpecText } from '@/utils/cartSpec'
  4. const GOODS_PLACEHOLDER = '/static/logo.png'
  5. const SHOP_PLACEHOLDER = '/static/logo.png'
  6. function formatAddress(addr) {
  7. if (!addr) return ''
  8. const region = (addr.regionName || '').replace(/\//g, '')
  9. return `${region}${addr.detailAddress || ''}`.trim()
  10. }
  11. export function mapGoodsItem(item) {
  12. if (!item) return null
  13. const specText = item.specText || '默认'
  14. return {
  15. cartItemId: item.cartItemId,
  16. goodsId: item.goodsId,
  17. goodsName: item.goodsName || '',
  18. mainPic: item.mainPic || '',
  19. displayPic: resolveFileUrl(item.mainPic) || GOODS_PLACEHOLDER,
  20. specText,
  21. specList: parseCartSpecText(specText),
  22. serviceDesc: item.serviceDesc || '',
  23. salePrice: item.salePrice,
  24. priceText: formatPrice(item.salePrice),
  25. quantity: Number(item.quantity) || 1,
  26. maxStock: Number(item.maxStock) || 1,
  27. subtotal: item.subtotal,
  28. subtotalText: formatPrice(item.subtotal),
  29. freightType: item.freightType,
  30. freightDesc: item.freightDesc || '',
  31. buyerRemark: item.buyerRemark || ''
  32. }
  33. }
  34. function mapShopAndAddress(data) {
  35. const shop = data.shop || {}
  36. const addr = data.address || null
  37. return {
  38. shop: {
  39. shopId: shop.shopId,
  40. shopName: shop.shopName || '',
  41. shopAvatar: shop.shopAvatar || '',
  42. shopStatus: shop.shopStatus
  43. },
  44. address: addr
  45. ? {
  46. addressId: addr.addressId,
  47. consigneeName: addr.consigneeName || '',
  48. mobile: addr.mobile || '',
  49. regionName: addr.regionName || '',
  50. detailAddress: addr.detailAddress || '',
  51. fullAddress: formatAddress(addr),
  52. isDefault: addr.isDefault === '1'
  53. }
  54. : null,
  55. goodsAmount: data.goodsAmount,
  56. freightAmount: data.freightAmount,
  57. freightDesc: data.freightDesc || '',
  58. payAmount: data.payAmount,
  59. goodsAmountText: formatPrice(data.goodsAmount),
  60. freightAmountText: formatPrice(data.freightAmount),
  61. payAmountText: formatPrice(data.payAmount),
  62. payType: data.payType || 'WECHAT'
  63. }
  64. }
  65. /** preview 接口 data → 单商品页面模型 */
  66. export function mapCheckoutPreview(data) {
  67. if (!data) return null
  68. const base = mapShopAndAddress(data)
  69. return {
  70. ...base,
  71. goods: mapGoodsItem((data.items || [])[0])
  72. }
  73. }
  74. /** preview 接口 data → 购物车多商品页面模型 */
  75. export function mapCheckoutPreviewCart(data, remarkMap = {}) {
  76. if (!data) return null
  77. const base = mapShopAndAddress(data)
  78. const goodsList = (data.items || [])
  79. .map((row) => {
  80. const item = mapGoodsItem(row)
  81. if (!item) return null
  82. if (item.cartItemId && remarkMap[item.cartItemId] != null) {
  83. item.buyerRemark = remarkMap[item.cartItemId]
  84. }
  85. return item
  86. })
  87. .filter(Boolean)
  88. return {
  89. ...base,
  90. goodsList
  91. }
  92. }
  93. /** 会员地址列表行 → 选择弹层 */
  94. export function mapAddressOption(row) {
  95. if (!row) return null
  96. const region = (row.regionName || '').replace(/\//g, '')
  97. const full = row.fullAddress || `${region}${row.detailAddress || ''}`
  98. return {
  99. addressId: row.addressId,
  100. consigneeName: row.consigneeName || '',
  101. mobile: row.mobile || '',
  102. fullAddress: full,
  103. isDefault: row.isDefault === '1'
  104. }
  105. }