@@ -8,10 +8,8 @@ import { useScrollHeight } from '@/hooks/useScrollHeight'
import { getMyAvailableCoupons } from '@/api/shop/shopUserCoupon'
import AddressCard from '@/components/business/AddressCard'
import CouponCard from '@/components/business/CouponCard'
import { createOrder , repairOrder , type WxPayResult } from '@/api/shop/shopOrder'
import { createOrder , type WxPayResult } from '@/api/shop/shopOrder'
import type { ShopOrder } from '@/api/shop/shopOrder/model'
import { payByBalance } from '@/api/system/payment'
import { getUserBalance } from '@/api/system/user'
import { listShopGoods } from '@/api/shop/shopGoods'
import type { ShopGoods } from '@/api/shop/shopGoods/model'
import type { ShopUserCoupon } from '@/api/shop/shopUserCoupon/model'
@@ -126,9 +124,8 @@ const CheckoutPage: React.FC = () => {
const [ rushBuyProducts , setRushBuyProducts ] = useState < ShopGoods [ ] > ( [ ] )
// 支付方式相关状态
const [ payType , setPayType ] = useState < number > ( 1 ) // 1: 微信支付, 0: 余额支付
const [ userBalance , setUserBalanc e] = useState < string > ( '0.00' )
const [ loadingBalance , setLoadingBalance ] = useState ( false )
// payType: 1=微信支付, 0=货到付款(默认)
const [ payType , setPayTyp e] = useState < number > ( 0 )
// 从本地存储获取立即购买的数据
const buyNowItems = useMemo ( ( ) = > {
@@ -183,7 +180,6 @@ const CheckoutPage: React.FC = () => {
useEffect ( ( ) = > {
loadCoupons ( )
loadRushBuyProducts ( )
loadUserBalance ( )
} , [ ] )
// 每次页面显示时刷新地址(从地址列表选择后返回)
@@ -221,21 +217,7 @@ const CheckoutPage: React.FC = () => {
}
}
// 加载用户余额
const loadUserBalance = async ( ) = > {
setLoadingBalance ( true )
try {
const data = await getUserBalance ( )
setUserBalance ( data ? . balance || '0.00' )
} catch ( e ) {
console . error ( '加载余额失败:' , e )
setUserBalance ( '0.00' )
} finally {
setLoadingBalance ( false )
}
}
// 选择地址
// 加载凑单推荐商品
const handleSelectAddress = ( ) = > {
Taro . navigateTo ( {
url : '/pages/user/address-list?from=checkout' ,
@@ -277,23 +259,6 @@ const CheckoutPage: React.FC = () => {
specInfo : item.skuSpec || item . sku ? . sku || item . product ? . specName ,
} ) )
// 检查余额是否足够(如果选择余额支付)
if ( payType === 0 && parseFloat ( userBalance ) < parseFloat ( totalPrice ) ) {
Taro . showModal ( {
title : '余额不足' ,
content : ` 当前余额 ¥ ${ parseFloat ( userBalance ) . toFixed ( 2 ) } ,需要 ¥ ${ totalPrice } ,是否前往充值? ` ,
confirmText : '去充值' ,
cancelText : '取消' ,
success : ( res ) = > {
if ( res . confirm ) {
Taro . navigateTo ( { url : '/pages/user/recharge' } )
}
}
} )
setSubmitting ( false )
return
}
// 创建订单
const orderParams : OrderCreateRequest = {
goodsItems ,
@@ -308,9 +273,12 @@ const CheckoutPage: React.FC = () => {
// 根据支付方式处理
if ( payType === 0 ) {
// 余额支付 - res 包含 { orderId, orderNo, payType, payPrice }
const balanceRes = res as { orderId? : number ; orderNo? : string }
await handleBalancePay ( Number ( balanceRes . orderId ) )
// 货到付款 — 直接创建订单,无需在线支付
Taro . showToast ( { title : '下单成功' , icon : 'success' } )
setTimeout ( ( ) = > {
Taro . redirectTo ( { url : '/pages/order/list' } )
} , 1500 )
return
} else {
// 微信支付 - res 是 WxPayResult
await handleWxPay ( res as WxPayResult )
@@ -353,23 +321,6 @@ const CheckoutPage: React.FC = () => {
} )
}
// 余额支付
const handleBalancePay = async ( orderId : number | string ) = > {
try {
await payByBalance ( { orderId : Number ( orderId ) } )
// 余额支付成功后,修复订单支付状态
try {
await repairOrder ( { orderId : Number ( orderId ) , payStatus : true } as Partial < ShopOrder > )
} catch {
// 修复失败不影响支付流程
}
// 刷新余额
loadUserBalance ( )
} catch ( err : any ) {
throw new Error ( err . message || '余额支付失败' )
}
}
// 安全的数字格式化
const formatPrice = ( price : number | string ) : string = > {
const num = typeof price === 'string' ? parseFloat ( price ) : price
@@ -540,20 +491,18 @@ const CheckoutPage: React.FC = () => {
{ /* </View>*/ }
{ /*</View>*/ }
{ /* 余额支付 */ }
{ /* 货到付款 */ }
< View
className = 'flex items-center py-3 px-2 rounded-lg'
style = { { backgroundColor : payType === 0 ? '#f0fdf4' : '#f9fafb' } }
onClick = { ( ) = > setPayType ( 0 ) }
>
< View className = 'w-8 h-8 rounded-full bg-amber -50 flex items-center justify-center mr-3' >
< Text className = 'text-sm' > 余 < / Text >
< View className = 'w-8 h-8 rounded-full bg-blue -50 flex items-center justify-center mr-3' >
< Text className = 'text-sm' > 货 < / Text >
< / View >
< View className = 'flex-1' >
< Text className = 'text-sm text-gray-800 block' > 余 额 支 付 < / Text >
< Text className = 'text-xs text-gray-400 block' >
{ loadingBalance ? '加载中...' : ` 可用: ¥ ${ parseFloat ( userBalance ) . toFixed ( 2 ) } ` }
< / Text >
< Text className = 'text-sm text-gray-800 block' > 货 到 付 款 < / Text >
< Text className = 'text-xs text-gray-400 block' > 商 品 送 达 后 付 款 < / Text >
< / View >
< View
className = 'w-5 h-5 rounded-full border-2 flex items-center justify-center'
@@ -564,15 +513,6 @@ const CheckoutPage: React.FC = () => {
) }
< / View >
< / View >
{ /* 余额不足提示 */ }
{ payType === 0 && parseFloat ( userBalance ) < parseFloat ( totalPrice ) && (
< View className = 'mt-2 px-2' >
< Text className = 'text-xs text-red-500' >
余 额 不 足 , 还 差 ¥ { ( parseFloat ( totalPrice ) - parseFloat ( userBalance ) ) . toFixed ( 2 ) }
< / Text >
< / View >
) }
< / View >
{ /* 凑单推荐 */ }