|
|
@ -73,7 +73,21 @@ const OrderConfirm = () => { |
|
|
|
// 计算商品总价
|
|
|
|
const getGoodsTotal = () => { |
|
|
|
if (!goods) return 0 |
|
|
|
return parseFloat(goods.price || '0') * quantity |
|
|
|
const price = parseFloat(goods.price || '0') |
|
|
|
const total = price * quantity |
|
|
|
|
|
|
|
// 🔍 详细日志,用于排查数值精度问题
|
|
|
|
console.log('💵 商品总价计算:', { |
|
|
|
goodsPrice: goods.price, |
|
|
|
goodsPriceType: typeof goods.price, |
|
|
|
parsedPrice: price, |
|
|
|
quantity: quantity, |
|
|
|
total: total, |
|
|
|
totalFixed2: total.toFixed(2), |
|
|
|
totalString: total.toString() |
|
|
|
}) |
|
|
|
|
|
|
|
return total |
|
|
|
} |
|
|
|
|
|
|
|
// 计算优惠券折扣
|
|
|
@ -116,12 +130,12 @@ const OrderConfirm = () => { |
|
|
|
title: '当前优惠券不满足使用条件,已自动取消', |
|
|
|
icon: 'none' |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
// 🎯 自动推荐新的最优优惠券
|
|
|
|
if (usableCoupons.length > 0) { |
|
|
|
const bestCoupon = usableCoupons[0] |
|
|
|
const discount = calculateCouponDiscount(bestCoupon, newTotal) |
|
|
|
|
|
|
|
|
|
|
|
if (discount > 0) { |
|
|
|
setSelectedCoupon(bestCoupon) |
|
|
|
Taro.showToast({ |
|
|
@ -135,7 +149,7 @@ const OrderConfirm = () => { |
|
|
|
// 🔔 如果没有选中优惠券但有可用的,推荐最优的
|
|
|
|
const bestCoupon = usableCoupons[0] |
|
|
|
const discount = calculateCouponDiscount(bestCoupon, newTotal) |
|
|
|
|
|
|
|
|
|
|
|
if (discount > 0) { |
|
|
|
setSelectedCoupon(bestCoupon) |
|
|
|
Taro.showToast({ |
|
|
@ -149,7 +163,7 @@ const OrderConfirm = () => { |
|
|
|
const bestCoupon = usableCoupons[0] |
|
|
|
const currentDiscount = calculateCouponDiscount(selectedCoupon, newTotal) |
|
|
|
const bestDiscount = calculateCouponDiscount(bestCoupon, newTotal) |
|
|
|
|
|
|
|
|
|
|
|
// 如果有更好的优惠券(优惠超过0.01元)
|
|
|
|
if (bestDiscount > currentDiscount + 0.01 && bestCoupon.id !== selectedCoupon.id) { |
|
|
|
Taro.showModal({ |
|
|
@ -174,9 +188,45 @@ const OrderConfirm = () => { |
|
|
|
const handleCouponSelect = (coupon: CouponCardProps) => { |
|
|
|
const total = getGoodsTotal() |
|
|
|
|
|
|
|
// 🔍 详细日志记录,用于排查问题
|
|
|
|
console.log('🎫 手动选择优惠券详细信息:', { |
|
|
|
coupon: { |
|
|
|
id: coupon.id, |
|
|
|
title: coupon.title, |
|
|
|
type: coupon.type, |
|
|
|
amount: coupon.amount, |
|
|
|
minAmount: coupon.minAmount, |
|
|
|
status: coupon.status |
|
|
|
}, |
|
|
|
orderInfo: { |
|
|
|
goodsPrice: goods?.price, |
|
|
|
quantity: quantity, |
|
|
|
total: total, |
|
|
|
totalFixed: total.toFixed(2) |
|
|
|
}, |
|
|
|
validation: { |
|
|
|
isUsable: isCouponUsable(coupon, total), |
|
|
|
discount: calculateCouponDiscount(coupon, total), |
|
|
|
reason: getCouponUnusableReason(coupon, total) |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
// 检查是否可用
|
|
|
|
if (!isCouponUsable(coupon, total)) { |
|
|
|
const reason = getCouponUnusableReason(coupon, total) |
|
|
|
|
|
|
|
// 🚨 记录手动选择失败的详细信息
|
|
|
|
console.error('🚨 手动选择优惠券失败:', { |
|
|
|
reason, |
|
|
|
coupon, |
|
|
|
total, |
|
|
|
minAmount: coupon.minAmount, |
|
|
|
comparison: { |
|
|
|
totalVsMinAmount: `${total} < ${coupon.minAmount}`, |
|
|
|
result: total < (coupon.minAmount || 0) |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
Taro.showToast({ |
|
|
|
title: reason || '优惠券不可用', |
|
|
|
icon: 'none' |
|
|
@ -224,10 +274,33 @@ const OrderConfirm = () => { |
|
|
|
if (usableCoupons.length > 0 && !selectedCoupon) { |
|
|
|
const bestCoupon = usableCoupons[0] // 已经按优惠金额排序,第一个就是最优的
|
|
|
|
const discount = calculateCouponDiscount(bestCoupon, total) |
|
|
|
|
|
|
|
|
|
|
|
// 🔍 详细日志记录自动推荐的信息
|
|
|
|
console.log('🤖 自动推荐优惠券详细信息:', { |
|
|
|
coupon: { |
|
|
|
id: bestCoupon.id, |
|
|
|
title: bestCoupon.title, |
|
|
|
type: bestCoupon.type, |
|
|
|
amount: bestCoupon.amount, |
|
|
|
minAmount: bestCoupon.minAmount, |
|
|
|
status: bestCoupon.status |
|
|
|
}, |
|
|
|
orderInfo: { |
|
|
|
goodsPrice: goods?.price, |
|
|
|
quantity: quantity, |
|
|
|
total: total, |
|
|
|
totalFixed: total.toFixed(2) |
|
|
|
}, |
|
|
|
validation: { |
|
|
|
isUsable: isCouponUsable(bestCoupon, total), |
|
|
|
discount: discount, |
|
|
|
reason: getCouponUnusableReason(bestCoupon, total) |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
if (discount > 0) { |
|
|
|
setSelectedCoupon(bestCoupon) |
|
|
|
|
|
|
|
|
|
|
|
// 显示智能推荐提示
|
|
|
|
Taro.showToast({ |
|
|
|
title: `已为您推荐最优优惠券,可省¥${discount.toFixed(2)}`, |
|
|
@ -236,7 +309,7 @@ const OrderConfirm = () => { |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 🔔 优惠券提示:如果有可用优惠券,显示提示
|
|
|
|
if (usableCoupons.length > 0) { |
|
|
|
console.log(`发现${usableCoupons.length}张可用优惠券,已为您推荐最优惠券`) |
|
|
@ -313,11 +386,11 @@ const OrderConfirm = () => { |
|
|
|
// 🔔 支付前最后一次检查:提醒用户是否有可用优惠券
|
|
|
|
const total = getGoodsTotal() |
|
|
|
const usableCoupons = filterUsableCoupons(availableCoupons, total) |
|
|
|
|
|
|
|
|
|
|
|
if (usableCoupons.length > 0) { |
|
|
|
const bestCoupon = usableCoupons[0] |
|
|
|
const discount = calculateCouponDiscount(bestCoupon, total) |
|
|
|
|
|
|
|
|
|
|
|
if (discount > 0) { |
|
|
|
// 用模态框提醒用户
|
|
|
|
const confirmResult = await new Promise<boolean>((resolve) => { |
|
|
@ -328,7 +401,7 @@ const OrderConfirm = () => { |
|
|
|
fail: () => resolve(false) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
if (confirmResult) { |
|
|
|
setSelectedCoupon(bestCoupon) |
|
|
|
// 使用优惠券后重新计算价格,然后继续支付
|
|
|
@ -349,23 +422,37 @@ const OrderConfirm = () => { |
|
|
|
comments: goods.name, |
|
|
|
deliveryType: 0, |
|
|
|
buyerRemarks: orderRemark, |
|
|
|
// 确保couponId是数字类型
|
|
|
|
couponId: selectedCoupon ? Number(selectedCoupon.id) : undefined |
|
|
|
// 🔧 确保 couponId 是正确的数字类型,且不传递 undefined
|
|
|
|
couponId: selectedCoupon ? parseInt(String(selectedCoupon.id), 10) : undefined |
|
|
|
} |
|
|
|
); |
|
|
|
|
|
|
|
// 根据支付方式选择支付类型
|
|
|
|
const paymentType = payment.type === 0 ? PaymentType.BALANCE : PaymentType.WECHAT; |
|
|
|
|
|
|
|
console.log('开始支付:', { |
|
|
|
// 🔍 支付前的详细信息记录
|
|
|
|
console.log('💰 开始支付 - 详细信息:', { |
|
|
|
orderData, |
|
|
|
paymentType, |
|
|
|
selectedCoupon: selectedCoupon ? { |
|
|
|
id: selectedCoupon.id, |
|
|
|
title: selectedCoupon.title, |
|
|
|
type: selectedCoupon.type, |
|
|
|
amount: selectedCoupon.amount, |
|
|
|
minAmount: selectedCoupon.minAmount, |
|
|
|
discount: getCouponDiscount() |
|
|
|
} : null, |
|
|
|
finalPrice: getFinalPrice() |
|
|
|
priceCalculation: { |
|
|
|
goodsPrice: goods?.price, |
|
|
|
quantity: quantity, |
|
|
|
goodsTotal: getGoodsTotal(), |
|
|
|
couponDiscount: getCouponDiscount(), |
|
|
|
finalPrice: getFinalPrice() |
|
|
|
}, |
|
|
|
couponValidation: selectedCoupon ? { |
|
|
|
isUsable: isCouponUsable(selectedCoupon, getGoodsTotal()), |
|
|
|
reason: getCouponUnusableReason(selectedCoupon, getGoodsTotal()) |
|
|
|
} : null |
|
|
|
}); |
|
|
|
|
|
|
|
// 执行支付 - 移除这里的成功提示,让PaymentHandler统一处理
|
|
|
@ -527,7 +614,7 @@ const OrderConfirm = () => { |
|
|
|
height: '80px', |
|
|
|
}} lazyLoad={false}/> |
|
|
|
</View> |
|
|
|
<View className={'flex flex-col w-full'} style={{width: '100%'}}> |
|
|
|
<View className={'flex flex-col w-full ml-2'} style={{width: '100%'}}> |
|
|
|
<Text className={'font-medium w-full'}>{goods.name}</Text> |
|
|
|
<Text className={'number text-gray-400 text-sm py-2'}>80g/袋</Text> |
|
|
|
<View className={'flex justify-between items-center'}> |
|
|
|