import {useEffect, useState} from "react"; import {Image, Button, Cell, CellGroup, Input, Space, ActionSheet} from '@nutui/nutui-react-taro' import {Location, ArrowRight} from '@nutui/icons-react-taro' import Taro, {useDidShow} from '@tarojs/taro' import {ShopGoods} from "@/api/shop/shopGoods/model"; import {getShopGoods} from "@/api/shop/shopGoods"; import {View, Text} from '@tarojs/components'; import {listShopUserAddress} from "@/api/shop/shopUserAddress"; import {ShopUserAddress} from "@/api/shop/shopUserAddress/model"; import './index.scss' import Gap from "@/components/Gap"; import {selectPayment} from "@/api/system/payment"; import {Payment} from "@/api/system/payment/model"; import {PaymentHandler, PaymentType, buildSingleGoodsOrder} from "@/utils/payment"; const OrderConfirm = () => { const [goods, setGoods] = useState(null); const [address, setAddress] = useState() const [payments, setPayments] = useState([]) const [payment, setPayment] = useState() const [isVisible, setIsVisible] = useState(false) const router = Taro.getCurrentInstance().router; const goodsId = router?.params?.goodsId; const reload = async () => { // 默认收货地址 const address = await listShopUserAddress({isDefault: true}); if (address.length > 0) { setAddress(address[0]) } // 支付方式 const paymentList = await selectPayment({}); if (paymentList && paymentList.length > 0) { setPayments(paymentList?.map((d, _) => { return { type: d.type, name: d.name } })) setPayment(paymentList[0]) } } const handleSelect = (item: any) => { setPayment(payments.find(payment => payment.name === item.name)) setIsVisible(false) } /** * 统一支付入口 */ const onPay = async (goods: ShopGoods) => { // 基础校验 if (!address) { Taro.showToast({ title: '请选择收货地址', icon: 'error' }); return; } if (!payment) { Taro.showToast({ title: '请选择支付方式', icon: 'error' }); return; } // 构建订单数据 const orderData = buildSingleGoodsOrder( goods.goodsId!, 1, address.id, { comments: goods.name, deliveryType: 0, buyerRemarks: '', } ); // 根据支付方式选择支付类型 const paymentType = payment.type === 0 ? PaymentType.BALANCE : PaymentType.WECHAT; // 执行支付 await PaymentHandler.pay(orderData, paymentType); }; useDidShow(() => { reload().then() }) useEffect(() => { if (goodsId) { getShopGoods(Number(goodsId)).then(res => { setGoods(res); }).catch(error => { console.error("Failed to fetch goods detail:", error); }); } reload().then() }, [goodsId]); if (!goods) { return
加载中...
; } return (
{ address && ( Taro.navigateTo({url: '/user/address/index'})}> 送至 {address.province} {address.city} {address.region} {address.address} {address.name} {address.phone} ) } {!address && ( Taro.navigateTo({url: '/user/address/index'})}> 添加收货地址 )} {goods.name} 80g/袋 ¥{goods.price} x 1 {payment?.name} )} onClick={() => setIsVisible(true)} /> {'¥' + goods.price}}/> -¥0.00 )}/> 已优惠 ¥0.0 小计 ¥{goods.price} )}/> )}/> setIsVisible(false)} />
实付金额: ¥{goods.price}
); }; export default OrderConfirm;