巴青农资商城

checkoutDisplay.js 3.3KB

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