You need to sign in or sign up before continuing.
hookehuyr

fix(payCard): 添加支付按钮加载状态防止重复点击

添加isPaymentLoading状态控制支付按钮,防止用户重复点击
处理支付过程中可能出现的错误并显示相应提示
确保在支付完成或失败后清除加载状态
......@@ -28,10 +28,11 @@
<nut-button
block
color="#fb923c"
:disabled="!hasAgreed && !isChecked"
:disabled="(!hasAgreed && !isChecked) || isPaymentLoading"
:loading="isPaymentLoading"
@tap="goToPay"
>
立即支付
{{ isPaymentLoading ? '处理中...' : '立即支付' }}
</nut-button>
</view>
</nut-action-sheet>
......@@ -110,6 +111,9 @@ const isChecked = ref(false);
const hasAgreed = ref(false);
const protocolVisible = ref(false);
// 支付loading状态
const isPaymentLoading = ref(false);
// 支付协议内容
const protocolContent = ref(`
1. 用户在使用捡个电驴支付服务时,需遵守相关法律法规。
......@@ -220,10 +224,14 @@ const handleAgreeProtocol = async () => {
}
} catch (error) {
console.error('更新协议状态失败:', error)
throw error // 重新抛出错误,让goToPay函数处理
}
}
const goToPay = async () => {
// 防止重复点击
if (isPaymentLoading.value) return
// 检查协议状态
if (!hasAgreed.value && !isChecked.value) {
Taro.showToast({
......@@ -233,12 +241,16 @@ const goToPay = async () => {
return
}
// 如果用户勾选了协议但还未提交,先处理协议同意
if (!hasAgreed.value && isChecked.value) {
await handleAgreeProtocol()
}
// 设置loading状态
isPaymentLoading.value = true
try {
// 如果用户勾选了协议但还未提交,先处理协议同意
if (!hasAgreed.value && isChecked.value) {
await handleAgreeProtocol()
}
if (price.value > 0) { // 金额大于0
if (price.value > 0) { // 金额大于0
// 获取支付参数
const { code, data } = await payAPI({ order_id: id.value });
if (code) {
......@@ -286,9 +298,23 @@ const goToPay = async () => {
showCancel: false,
});
}
},
complete: () => {
// 支付完成后清除loading状态
isPaymentLoading.value = false
}
});
}
}
} catch (error) {
console.error('支付过程出错:', error)
Taro.showToast({
title: '支付失败,请重试',
icon: 'error'
})
} finally {
// 确保loading状态被清除
isPaymentLoading.value = false
}
}
</script>
......