forked from gxwebsoft/mp-10550
优化下单流程
This commit is contained in:
@@ -17,13 +17,13 @@ const GoodsDetail = () => {
|
||||
const goodsId = router?.params?.id;
|
||||
|
||||
// 使用购物车Hook
|
||||
const { cartCount, addToCart } = useCart();
|
||||
const {cartCount, addToCart} = useCart();
|
||||
|
||||
// 处理加入购物车
|
||||
const handleAddToCart = () => {
|
||||
if (!goods) return;
|
||||
|
||||
if(!Taro.getStorageSync('UserId')){
|
||||
if (!Taro.getStorageSync('UserId')) {
|
||||
return Taro.showToast({
|
||||
title: '请先登录',
|
||||
icon: 'none',
|
||||
@@ -125,7 +125,7 @@ const GoodsDetail = () => {
|
||||
top: "50px",
|
||||
right: "110px",
|
||||
}}
|
||||
onClick={() => navTo(`/pages/cart/cart`,true)}>
|
||||
onClick={() => navTo(`/pages/cart/cart`, true)}>
|
||||
<Badge value={cartCount} top="-2" right="2">
|
||||
<div style={{display: 'flex', alignItems: 'center'}}>
|
||||
<Cart size={16}/>
|
||||
@@ -209,7 +209,7 @@ const GoodsDetail = () => {
|
||||
onClick={() => handleAddToCart()}>加入购物车
|
||||
</div>
|
||||
<div className={'cart-buy pl-4 pr-5 text-sm'}
|
||||
onClick={() => navTo(`/shop/orderConfirm/index?goodsId=${goods?.goodsId}`,true)}>立即购买
|
||||
onClick={() => navTo(`/shop/orderConfirm/index?goodsId=${goods?.goodsId}`, true)}>立即购买
|
||||
</div>
|
||||
</div>
|
||||
</View>
|
||||
|
||||
@@ -9,10 +9,9 @@ import {listShopUserAddress} from "@/api/shop/shopUserAddress";
|
||||
import {ShopUserAddress} from "@/api/shop/shopUserAddress/model";
|
||||
import './index.scss'
|
||||
import Gap from "@/components/Gap";
|
||||
import {TenantId} from "@/config/app";
|
||||
import {payByBalance, selectPayment} from "@/api/system/payment";
|
||||
import {selectPayment} from "@/api/system/payment";
|
||||
import {Payment} from "@/api/system/payment/model";
|
||||
import {API_BASE_URL} from "@/config/env";
|
||||
import {PaymentHandler, PaymentType, buildSingleGoodsOrder} from "@/utils/payment";
|
||||
|
||||
const OrderConfirm = () => {
|
||||
const [goods, setGoods] = useState<ShopGoods | null>(null);
|
||||
@@ -20,6 +19,7 @@ const OrderConfirm = () => {
|
||||
const [payments, setPayments] = useState<any[]>([])
|
||||
const [payment, setPayment] = useState<Payment>()
|
||||
const [isVisible, setIsVisible] = useState<boolean>(false)
|
||||
|
||||
const router = Taro.getCurrentInstance().router;
|
||||
const goodsId = router?.params?.goodsId;
|
||||
|
||||
@@ -48,107 +48,44 @@ const OrderConfirm = () => {
|
||||
setIsVisible(false)
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一支付入口
|
||||
*/
|
||||
const onPay = async (goods: ShopGoods) => {
|
||||
// 支付方式
|
||||
if (payment?.type == 0) {
|
||||
await onBalancePay(goods)
|
||||
}
|
||||
if (payment?.type == 1) {
|
||||
await onWxPay(goods)
|
||||
}
|
||||
}
|
||||
|
||||
const onBalancePay = async (goods: ShopGoods) => {
|
||||
Taro.showLoading({title: '支付中...'})
|
||||
payByBalance({
|
||||
payType: 0, // 余额支付
|
||||
payPrice: goods.price,
|
||||
totalPrice: goods.price,
|
||||
addressId: address?.id,
|
||||
userId: Taro.getStorageSync('UserId'),
|
||||
tenantId: Number(TenantId)
|
||||
}).then().finally(() => {
|
||||
// 基础校验
|
||||
if (!address) {
|
||||
Taro.showToast({
|
||||
title: '支付成功',
|
||||
icon: 'success',
|
||||
duration: 2000
|
||||
})
|
||||
Taro.hideLoading()
|
||||
setTimeout(() => {
|
||||
Taro.switchTab({url: '/pages/order/order'})
|
||||
}, 2000)
|
||||
}).catch(() => {
|
||||
Taro.hideLoading()
|
||||
})
|
||||
}
|
||||
title: '请选择收货地址',
|
||||
icon: 'error'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const onWxPay = async (goods: ShopGoods) => {
|
||||
Taro.showLoading({title: '支付中...'})
|
||||
Taro.request({
|
||||
url: API_BASE_URL + '/shop/shop-order',
|
||||
// url: 'http://127.0.0.1:9200/api/shop/shop-order',
|
||||
method: 'POST',
|
||||
header: {
|
||||
'content-type': 'application/json',
|
||||
'Authorization': Taro.getStorageSync('access_token'),
|
||||
TenantId
|
||||
},
|
||||
data: {
|
||||
payType: 1,
|
||||
totalPrice: goods.price,
|
||||
payPrice: goods.price,
|
||||
userId: Taro.getStorageSync('UserId'),
|
||||
tenantId: TenantId,
|
||||
if (!payment) {
|
||||
Taro.showToast({
|
||||
title: '请选择支付方式',
|
||||
icon: 'error'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// 构建订单数据
|
||||
const orderData = buildSingleGoodsOrder(
|
||||
goods.goodsId!,
|
||||
1,
|
||||
address.id,
|
||||
{
|
||||
comments: goods.name,
|
||||
name: goods.name,
|
||||
addressId: address?.id,
|
||||
},
|
||||
success: function (res) {
|
||||
Taro.hideLoading()
|
||||
if(res.data.code != 0){
|
||||
Taro.showToast({
|
||||
title: res.data.message,
|
||||
icon: 'error',
|
||||
duration: 2000
|
||||
})
|
||||
return false;
|
||||
}
|
||||
// 支付结果
|
||||
const data = res.data.data
|
||||
console.log(data, 'payInfo')
|
||||
// Taro.showToast({
|
||||
// title: '下单成功',
|
||||
// })
|
||||
//
|
||||
// setTimeout(() => {
|
||||
// Taro.switchTab({
|
||||
// url: '/pages/order/order'
|
||||
// })
|
||||
// }, 1000);
|
||||
// return false;
|
||||
if (data) {
|
||||
Taro.requestPayment({
|
||||
timeStamp: data.timeStamp,
|
||||
nonceStr: data.nonceStr,
|
||||
package: data.package,
|
||||
signType: data.signType,
|
||||
paySign: data.paySign,
|
||||
success: function (res) {
|
||||
if (res.errMsg == "requestPayment:ok") {
|
||||
console.log('购买成功')
|
||||
}
|
||||
},
|
||||
fail: function (res) {
|
||||
console.log(res)
|
||||
}
|
||||
})
|
||||
}
|
||||
},
|
||||
complete: function () {
|
||||
Taro.hideLoading()
|
||||
deliveryType: 0
|
||||
}
|
||||
})
|
||||
}
|
||||
);
|
||||
|
||||
// 根据支付方式选择支付类型
|
||||
const paymentType = payment.type === 0 ? PaymentType.BALANCE : PaymentType.WECHAT;
|
||||
|
||||
// 执行支付
|
||||
await PaymentHandler.pay(orderData, paymentType);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (goodsId) {
|
||||
|
||||
@@ -10,10 +10,15 @@ import {ShopUserAddress} from "@/api/shop/shopUserAddress/model";
|
||||
import './index.scss'
|
||||
import {useCart} from "@/hooks/useCart";
|
||||
import Gap from "@/components/Gap";
|
||||
import {createOrder} from "@/api/shop/shopOrder";
|
||||
import {OrderCreateRequest} from "@/api/shop/shopOrder/model";
|
||||
import {Payment} from "@/api/system/payment/model";
|
||||
import {PaymentHandler, PaymentType, buildCartOrder} from "@/utils/payment";
|
||||
|
||||
const OrderConfirm = () => {
|
||||
const [goods, setGoods] = useState<ShopGoods | null>(null);
|
||||
const [address, setAddress] = useState<ShopUserAddress>()
|
||||
const [payment, setPayment] = useState<Payment>()
|
||||
const router = Taro.getCurrentInstance().router;
|
||||
const goodsId = router?.params?.goodsId;
|
||||
|
||||
@@ -29,6 +34,47 @@ const OrderConfirm = () => {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一支付入口
|
||||
*/
|
||||
const onPay = async () => {
|
||||
// 基础校验
|
||||
if (!address) {
|
||||
Taro.showToast({
|
||||
title: '请选择收货地址',
|
||||
icon: 'error'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (!cartItems || cartItems.length === 0) {
|
||||
Taro.showToast({
|
||||
title: '购物车为空',
|
||||
icon: 'error'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// 构建订单数据
|
||||
const orderData = buildCartOrder(
|
||||
cartItems.map(item => ({
|
||||
goodsId: item.goodsId!,
|
||||
quantity: item.quantity || 1
|
||||
})),
|
||||
address.id,
|
||||
{
|
||||
comments: '购物车下单',
|
||||
deliveryType: 0
|
||||
}
|
||||
);
|
||||
|
||||
// 根据支付方式选择支付类型,默认微信支付
|
||||
const paymentType = payment?.type === 0 ? PaymentType.BALANCE : PaymentType.WECHAT;
|
||||
|
||||
// 执行支付
|
||||
await PaymentHandler.pay(orderData, paymentType);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (goodsId) {
|
||||
getShopGoods(Number(goodsId)).then(res => {
|
||||
@@ -121,7 +167,7 @@ const OrderConfirm = () => {
|
||||
<span className={'text-red-500 text-xl font-bold'}>¥{goods.price}</span>
|
||||
</div>
|
||||
<div className={'buy-btn mx-4'}>
|
||||
<Button type="success" size="large">立即付款</Button>
|
||||
<Button type="success" size="large" onClick={onPay}>立即付款</Button>
|
||||
</div>
|
||||
</View>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user