功能(小程序):同步用户体验流程到小程序
This commit is contained in:
@@ -3,11 +3,6 @@
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
const cartApi = require('../../api/cart')
|
||||
/**
|
||||
* Purpose: couponApi supports index.js by holding or executing the coupon api page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
const couponApi = require('../../api/coupon')
|
||||
/**
|
||||
* Purpose: orderApi supports index.js by holding or executing the order api page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
@@ -31,6 +26,8 @@ const theme = require('../../utils/theme')
|
||||
const { resolveLegacyImage } = require('../../utils/file')
|
||||
const { NO_COUPON_KEY } = require('../../utils/config')
|
||||
const { runWithLoading } = require('../../utils/loading')
|
||||
const { clearBuyNowContext, isBuyNowCartItem, readBuyNowContext } = require('../../utils/buy-now')
|
||||
const { buildRecoveryState, clearRecoveryState } = require('../../utils/recovery')
|
||||
const {
|
||||
buildAppointmentValue,
|
||||
formatDateTime,
|
||||
@@ -53,6 +50,9 @@ function getCouponKey(coupon) {
|
||||
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
function getCouponDisplay(coupon) {
|
||||
if (coupon && coupon.displayText) {
|
||||
return coupon.displayText
|
||||
}
|
||||
if (Number(coupon.couponType) === 1) {
|
||||
return `${coupon.couponName} ${coupon.discountValue} 折`
|
||||
}
|
||||
@@ -71,6 +71,9 @@ function calculateCouponDiscount(coupon, amount) {
|
||||
if (!coupon || amount <= 0) {
|
||||
return 0
|
||||
}
|
||||
if (coupon.discountAmount !== undefined && coupon.discountAmount !== null) {
|
||||
return Number(coupon.discountAmount || 0)
|
||||
}
|
||||
|
||||
if (Number(coupon.couponType) === 1) {
|
||||
const discountValue = Number(coupon.discountValue || 0)
|
||||
@@ -160,6 +163,9 @@ Page({
|
||||
minDate: getMinDateText(),
|
||||
cartItems: [],
|
||||
availableCoupons: [],
|
||||
unavailableCoupons: [],
|
||||
checkoutPreview: null,
|
||||
vipTip: '',
|
||||
selectedCouponKey: NO_COUPON_KEY,
|
||||
/**
|
||||
* Purpose: couponSelectionTouched records whether the user explicitly chose a coupon option.
|
||||
@@ -167,13 +173,18 @@ Page({
|
||||
*/
|
||||
couponSelectionTouched: false,
|
||||
selectedCount: 0,
|
||||
isBuyNowMode: false,
|
||||
heroKicker: '结算准备',
|
||||
heroTitle: '已选 0 个预约项目',
|
||||
heroDesc: '确认美甲师和时间后再下单。',
|
||||
totalQuantity: 0,
|
||||
subtotalAmount: 0,
|
||||
subtotalText: '¥0.00',
|
||||
discountAmountText: '¥0.00',
|
||||
totalAmountText: '¥0.00',
|
||||
checkoutIssues: [],
|
||||
submittingOrder: false
|
||||
submittingOrder: false,
|
||||
recoveryState: clearRecoveryState()
|
||||
}),
|
||||
|
||||
/**
|
||||
@@ -214,13 +225,19 @@ Page({
|
||||
const selectedCoupon = this.getSelectedCoupon()
|
||||
const selectedCount = selectedItems.length
|
||||
const totalQuantity = selectedItems.reduce((sum, item) => sum + Number(item.quantity || 0), 0)
|
||||
const subtotalAmount = selectedItems.reduce((sum, item) => sum + Number(item.price || 0) * Number(item.quantity || 0), 0)
|
||||
const discountAmount = calculateCouponDiscount(selectedCoupon, subtotalAmount)
|
||||
const totalAmount = Math.max(0, subtotalAmount - discountAmount)
|
||||
const localSubtotalAmount = selectedItems.reduce((sum, item) => sum + Number(item.price || 0) * Number(item.quantity || 0), 0)
|
||||
const preview = this.data.checkoutPreview || {}
|
||||
const subtotalAmount = preview.subtotalAmount !== undefined ? Number(preview.subtotalAmount || 0) : localSubtotalAmount
|
||||
const discountAmount = preview.savedAmount !== undefined ? Number(preview.savedAmount || 0) : calculateCouponDiscount(selectedCoupon, subtotalAmount)
|
||||
const totalAmount = preview.payableAmount !== undefined ? Number(preview.payableAmount || 0) : Math.max(0, subtotalAmount - discountAmount)
|
||||
const checkoutIssues = buildCheckoutIssues(this.data.cartItems)
|
||||
const isBuyNowMode = Boolean(this.data.isBuyNowMode)
|
||||
|
||||
this.setData({
|
||||
selectedCount,
|
||||
heroKicker: isBuyNowMode ? '确认订单' : '结算准备',
|
||||
heroTitle: isBuyNowMode ? '确认本次预约' : `已选 ${selectedCount} 个预约项目`,
|
||||
heroDesc: isBuyNowMode ? '本次只会提交刚刚选择的作品,不会混入购物车其他项目。' : '确认美甲师和时间后再下单。',
|
||||
totalQuantity,
|
||||
subtotalAmount,
|
||||
subtotalText: `¥${formatPrice(subtotalAmount)}`,
|
||||
@@ -232,9 +249,13 @@ Page({
|
||||
|
||||
async loadCartItems() {
|
||||
const isLoggedIn = auth.isLoggedIn()
|
||||
const buyNowContext = isLoggedIn ? readBuyNowContext() : null
|
||||
const buyNowProductId = buyNowContext && buyNowContext.productId ? String(buyNowContext.productId) : ''
|
||||
this.setData({
|
||||
isLoggedIn,
|
||||
loading: isLoggedIn
|
||||
isBuyNowMode: Boolean(buyNowProductId),
|
||||
loading: isLoggedIn,
|
||||
recoveryState: clearRecoveryState()
|
||||
})
|
||||
|
||||
/**
|
||||
@@ -245,9 +266,13 @@ Page({
|
||||
this.setData({
|
||||
cartItems: [],
|
||||
availableCoupons: [],
|
||||
unavailableCoupons: [],
|
||||
checkoutPreview: null,
|
||||
vipTip: '',
|
||||
selectedCouponKey: NO_COUPON_KEY,
|
||||
couponSelectionTouched: false,
|
||||
loading: false
|
||||
loading: false,
|
||||
recoveryState: clearRecoveryState()
|
||||
})
|
||||
this.refreshSummary()
|
||||
return
|
||||
@@ -273,7 +298,7 @@ Page({
|
||||
...item,
|
||||
productImageUrl,
|
||||
priceText: `¥${formatPrice(item.price)}`,
|
||||
selected: true,
|
||||
selected: buyNowProductId ? isBuyNowCartItem(item, buyNowProductId) : true,
|
||||
artistId: item.artistId || '',
|
||||
artistName: artist ? artist.name : '',
|
||||
availableArtists,
|
||||
@@ -286,16 +311,26 @@ Page({
|
||||
this.setData({
|
||||
cartItems: formattedItems,
|
||||
selectedCouponKey: NO_COUPON_KEY,
|
||||
couponSelectionTouched: false
|
||||
couponSelectionTouched: false,
|
||||
recoveryState: clearRecoveryState()
|
||||
})
|
||||
if (buyNowProductId) {
|
||||
clearBuyNowContext()
|
||||
}
|
||||
await this.loadAvailableCoupons()
|
||||
} catch (error) {
|
||||
this.setData({
|
||||
cartItems: []
|
||||
})
|
||||
wx.showToast({
|
||||
title: error.message || '购物车加载失败',
|
||||
icon: 'none'
|
||||
cartItems: [],
|
||||
recoveryState: buildRecoveryState({
|
||||
title: '购物车加载失败',
|
||||
message: error,
|
||||
fallbackMessage: '购物车暂时没有加载出来,请重试或先去逛作品。',
|
||||
actions: [
|
||||
{ key: 'retryCartLoad', text: '重试' },
|
||||
{ key: 'products', text: '去逛作品' },
|
||||
{ key: 'contact', text: '联系客服' }
|
||||
]
|
||||
})
|
||||
})
|
||||
} finally {
|
||||
this.setData({
|
||||
@@ -314,6 +349,9 @@ Page({
|
||||
if (!selectedItems.length) {
|
||||
this.setData({
|
||||
availableCoupons: [],
|
||||
unavailableCoupons: [],
|
||||
checkoutPreview: null,
|
||||
vipTip: '',
|
||||
selectedCouponKey: NO_COUPON_KEY,
|
||||
couponSelectionTouched: false
|
||||
})
|
||||
@@ -321,24 +359,25 @@ Page({
|
||||
return
|
||||
}
|
||||
|
||||
const tags = new Set()
|
||||
const subtotalAmount = selectedItems.reduce((sum, item) => sum + Number(item.price || 0) * Number(item.quantity || 0), 0)
|
||||
selectedItems.forEach(item => {
|
||||
String(item.tagNames || '')
|
||||
.split(',')
|
||||
.map(tag => tag.trim())
|
||||
.filter(Boolean)
|
||||
.forEach(tag => tags.add(tag))
|
||||
})
|
||||
|
||||
try {
|
||||
const coupons = await couponApi.getAvailableCoupons(
|
||||
Array.from(tags).join(','),
|
||||
subtotalAmount,
|
||||
selectedItems[0].shopId
|
||||
)
|
||||
const selectedCoupon = this.getSelectedCoupon()
|
||||
const preview = await orderApi.checkoutPreview({
|
||||
couponId: selectedCoupon ? selectedCoupon.id : undefined,
|
||||
couponSourceType: selectedCoupon ? selectedCoupon.sourceType : (this.data.couponSelectionTouched && this.data.selectedCouponKey === NO_COUPON_KEY ? NO_COUPON_KEY : undefined),
|
||||
items: selectedItems.map(item => ({
|
||||
productId: item.productId,
|
||||
quantity: item.quantity,
|
||||
artistId: item.artistId,
|
||||
appointmentTime: item.appointmentValue
|
||||
}))
|
||||
})
|
||||
|
||||
const list = Array.isArray(coupons) ? coupons.map(item => ({
|
||||
const list = Array.isArray(preview && preview.availableCoupons) ? preview.availableCoupons.map(item => ({
|
||||
...item,
|
||||
key: getCouponKey(item),
|
||||
displayText: getCouponDisplay(item)
|
||||
})) : []
|
||||
const unavailableCoupons = Array.isArray(preview && preview.unavailableCoupons) ? preview.unavailableCoupons.map(item => ({
|
||||
...item,
|
||||
key: getCouponKey(item),
|
||||
displayText: getCouponDisplay(item)
|
||||
@@ -348,6 +387,7 @@ Page({
|
||||
if (selectedCouponKey !== NO_COUPON_KEY && !list.find(item => item.key === selectedCouponKey)) {
|
||||
selectedCouponKey = NO_COUPON_KEY
|
||||
}
|
||||
const previewSubtotalAmount = Number((preview && preview.subtotalAmount) || this.data.subtotalAmount || 0)
|
||||
|
||||
/**
|
||||
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
||||
@@ -355,8 +395,7 @@ Page({
|
||||
*/
|
||||
if (selectedCouponKey === NO_COUPON_KEY && !this.data.couponSelectionTouched) {
|
||||
const bestCoupons = list
|
||||
.filter(item => item.sourceType === 'SYSTEM_VIP')
|
||||
.sort((left, right) => calculateCouponDiscount(right, subtotalAmount) - calculateCouponDiscount(left, subtotalAmount))
|
||||
.sort((left, right) => calculateCouponDiscount(right, previewSubtotalAmount) - calculateCouponDiscount(left, previewSubtotalAmount))
|
||||
/**
|
||||
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
||||
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
||||
@@ -368,11 +407,17 @@ Page({
|
||||
|
||||
this.setData({
|
||||
availableCoupons: list,
|
||||
unavailableCoupons,
|
||||
checkoutPreview: preview || null,
|
||||
vipTip: preview && preview.vipTip ? preview.vipTip : '',
|
||||
selectedCouponKey
|
||||
})
|
||||
} catch (error) {
|
||||
this.setData({
|
||||
availableCoupons: [],
|
||||
unavailableCoupons: [],
|
||||
checkoutPreview: null,
|
||||
vipTip: '',
|
||||
selectedCouponKey: NO_COUPON_KEY,
|
||||
couponSelectionTouched: false
|
||||
})
|
||||
@@ -397,6 +442,60 @@ Page({
|
||||
navigation.switchTab('/pages/products/index')
|
||||
},
|
||||
|
||||
handleRecoveryAction(event) {
|
||||
const key = event.currentTarget.dataset.key
|
||||
if (key === 'retryCartLoad') {
|
||||
this.setData({ recoveryState: clearRecoveryState() })
|
||||
this.loadCartItems()
|
||||
return
|
||||
}
|
||||
if (key === 'retryCheckout') {
|
||||
this.setData({ recoveryState: clearRecoveryState() })
|
||||
this.handleCheckout()
|
||||
return
|
||||
}
|
||||
if (key === 'contact') {
|
||||
this.handleContactSupport()
|
||||
return
|
||||
}
|
||||
if (key === 'cart') {
|
||||
this.setData({ recoveryState: clearRecoveryState() })
|
||||
return
|
||||
}
|
||||
|
||||
const actions = this.data.recoveryState.actions || []
|
||||
const action = actions.find(item => item.key === key)
|
||||
if (action && action.route) {
|
||||
this.openRecoveryRoute(action.route)
|
||||
}
|
||||
},
|
||||
|
||||
openRecoveryRoute(route) {
|
||||
const pureRoute = String(route || '').split('?')[0]
|
||||
if (!pureRoute) {
|
||||
return
|
||||
}
|
||||
if (navigation.isTabPage(pureRoute)) {
|
||||
navigation.switchTab(pureRoute)
|
||||
return
|
||||
}
|
||||
navigation.navigate(route)
|
||||
},
|
||||
|
||||
handleContactSupport() {
|
||||
wx.showModal({
|
||||
title: '联系客服',
|
||||
content: '请截图保留失败提示;如果担心重复下单,请先查看订单。也可以前往“我的”页联系门店或平台客服。',
|
||||
confirmText: '去我的',
|
||||
cancelText: '留在本页',
|
||||
success: res => {
|
||||
if (res.confirm) {
|
||||
navigation.switchTab('/pages/profile/index')
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
async handleToggleSelect(event) {
|
||||
const index = Number(event.currentTarget.dataset.index)
|
||||
const cartItems = this.data.cartItems.slice()
|
||||
@@ -640,7 +739,7 @@ Page({
|
||||
selectedCouponKey: event.detail.value,
|
||||
couponSelectionTouched: true
|
||||
})
|
||||
this.refreshSummary()
|
||||
this.loadAvailableCoupons()
|
||||
},
|
||||
|
||||
async handleCheckout() {
|
||||
@@ -704,9 +803,18 @@ Page({
|
||||
icon: 'none'
|
||||
})
|
||||
} catch (error) {
|
||||
wx.showToast({
|
||||
title: error.message || '订单创建失败',
|
||||
icon: 'none'
|
||||
this.setData({
|
||||
recoveryState: buildRecoveryState({
|
||||
title: '下单失败',
|
||||
message: error,
|
||||
fallbackMessage: '订单创建失败,请重试;如果担心重复下单,请先查看订单。',
|
||||
actions: [
|
||||
{ key: 'retryCheckout', text: '重试下单' },
|
||||
{ key: 'orders', text: '查看订单' },
|
||||
{ key: 'contact', text: '联系客服' },
|
||||
{ key: 'cart', text: '返回购物车' }
|
||||
]
|
||||
})
|
||||
})
|
||||
} finally {
|
||||
this.setData({
|
||||
|
||||
Reference in New Issue
Block a user