diff --git a/src/pages-booking/index.vue b/src/pages-booking/index.vue index 014efc5..bc8ef14 100644 --- a/src/pages-booking/index.vue +++ b/src/pages-booking/index.vue @@ -210,8 +210,8 @@ const validateUserForms = () => { return true; }; -// 处理支付点击事件 -const handlePayClick = ThrottleUtils.createThrottle(async (goodsData) => { +// 处理支付点击事件 - 使用执行锁防止重复支付 +const handlePayClick = ThrottleUtils.createExecutionLock(async (goodsData) => { console.log("处理支付点击事件", userFormList.value); // 预约日期,酒店类型不需要 if (orderData.value.reservationEnabled) { @@ -267,72 +267,82 @@ const handlePayClick = ThrottleUtils.createThrottle(async (goodsData) => { // 点击后立即展示 loading uni.showLoading({ title: "正在提交订单..." }); - const res = await orderPay(params); - console.log("确认订单---2:", res); - uni.hideLoading(); + + try { + const res = await orderPay(params); + console.log("确认订单---2:", res); - // 检查接口返回数据 - if (!res || !res.data) { + // 检查接口返回数据 + if (!res || !res.data) { + setTimeout(() => { + uni.showToast({ + title: res.msg || "订单创建失败,请重试", + icon: "none", + }); + }, 100); + return; + } + + const { data } = res; + const { nonceStr, packageVal, paySign, signType, timeStamp } = data; + + // 验证支付参数是否完整 + if (!nonceStr || !packageVal || !paySign || !signType || !timeStamp) { + setTimeout(() => { + uni.showToast({ title: "支付参数错误,请重试", icon: "none" }); + }, 100); + return; + } + + // 关闭 loading uni.hideLoading(); - setTimeout(() => { - uni.showToast({ - title: res.msg || "订单创建失败,请重试", - icon: "none", - }); - }, 100); - return; - } - const { data } = res; - const { nonceStr, packageVal, paySign, signType, timeStamp } = data; - - // 验证支付参数是否完整 - if (!nonceStr || !packageVal || !paySign || !signType || !timeStamp) { - uni.hideLoading(); - setTimeout(() => { - uni.showToast({ title: "支付参数错误,请重试", icon: "none" }); - }, 100); - return; - } - - // 在发起微信支付前关闭 loading(避免与原生支付 UI 冲突) - uni.hideLoading(); - - // #ifdef MP-WEIXIN - // 调用微信支付 - uni.requestPayment({ - provider: "wxpay", - timeStamp: String(timeStamp), // 确保为字符串类型 - nonceStr: String(nonceStr), - package: String(packageVal), // 确保为字符串类型 - signType: String(signType), - paySign: String(paySign), - success: () => { - uni.showToast({ - title: "支付成功", - icon: "success", + // #ifdef MP-WEIXIN + // 调用微信支付 + await new Promise((resolve) => { + uni.requestPayment({ + provider: "wxpay", + timeStamp: String(timeStamp), // 确保为字符串类型 + nonceStr: String(nonceStr), + package: String(packageVal), // 确保为字符串类型 + signType: String(signType), + paySign: String(paySign), success: () => { - uni.navigateTo({ - url: "/pages-order/order/list", + uni.showToast({ + title: "支付成功", + icon: "success", + success: () => { + uni.navigateTo({ + url: "/pages-order/order/list", + }); + }, }); + resolve(); + }, + fail: (e) => { + console.error("支付失败:", e); + uni.showToast({ title: "支付失败,请重试", icon: "none" }); + resolve(); }, }); - }, - fail: (e) => { - console.error("支付失败:", e); - uni.showToast({ title: "支付失败,请重试", icon: "none" }); - }, - }); - // #endif + }); + // #endif - // #ifdef APP-PLUS - uni.showModal({ - title: "提示", - content: "支付功能开发中", - showCancel: false, - }); - // #endif -}, 1000); + // #ifdef APP-PLUS + uni.showModal({ + title: "提示", + content: "支付功能开发中", + showCancel: false, + }); + // #endif + } catch (error) { + console.error("支付流程出错:", error); + uni.showToast({ title: "支付失败,请重试", icon: "none" }); + } finally { + // 确保 loading 被关闭 + uni.hideLoading(); + } +});