forked from gxwebsoft/mp-10550
优化下单流程
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user