hookehuyr

fix(submit): 修复支付流程逻辑并优化错误处理

重构支付流程以支持订单重试,避免重复创建订单
添加支付失败时的错误提示和状态管理
优化加载状态显示和代码结构
1 <!-- 1 <!--
2 * @Date: 2024-01-15 16:25:51 2 * @Date: 2024-01-15 16:25:51
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2026-01-12 18:42:42 4 + * @LastEditTime: 2026-01-12 18:54:33
5 * @FilePath: /xyxBooking-weapp/src/pages/submit/index.vue 5 * @FilePath: /xyxBooking-weapp/src/pages/submit/index.vue
6 * @Description: 预约人员信息 6 * @Description: 预约人员信息
7 --> 7 -->
...@@ -116,10 +116,15 @@ const goToVisitor = () => { ...@@ -116,10 +116,15 @@ const goToVisitor = () => {
116 const submitBtn = async () => { 116 const submitBtn = async () => {
117 if (!checked_visitors.value.length) { 117 if (!checked_visitors.value.length) {
118 Taro.showToast({ title: '请先添加参观者', icon: 'none' }) 118 Taro.showToast({ title: '请先添加参观者', icon: 'none' })
119 - } else { 119 + return;
120 + }
121 +
122 + let pay_id = pending_pay_id.value;
123 +
124 + if (!pay_id) {
120 // TAG: 提交订单跳转到支付页面 125 // TAG: 提交订单跳转到支付页面
121 Taro.showLoading({ title: '提交中...' }); 126 Taro.showLoading({ title: '提交中...' });
122 - const { code, data } = await addReserveAPI({ 127 + const { code, data, msg } = await addReserveAPI({
123 reserve_date: date.value, 128 reserve_date: date.value,
124 begin_time: time.value.split('-')[0], 129 begin_time: time.value.split('-')[0],
125 end_time: time.value.split('-')[1], 130 end_time: time.value.split('-')[1],
...@@ -128,10 +133,19 @@ const submitBtn = async () => { ...@@ -128,10 +133,19 @@ const submitBtn = async () => {
128 133
129 Taro.hideLoading(); 134 Taro.hideLoading();
130 135
131 - if (code) { 136 + if (!code || code !== 1) {
137 + Taro.showToast({ title: msg || '提交失败', icon: 'none' });
138 + return;
139 + }
140 + pay_id = data.pay_id;
141 + pending_pay_id.value = pay_id;
142 + }
143 +
132 // 如果金额大于零, 走微信支付, 如果等于零直接跳转成功页 144 // 如果金额大于零, 走微信支付, 如果等于零直接跳转成功页
133 if (total.value > 0) { 145 if (total.value > 0) {
134 - const payParams = await wxPayAPI({ pay_id: data.pay_id }); // 参数接口 146 + Taro.showLoading({ title: '支付准备中...' });
147 + const payParams = await wxPayAPI({ pay_id: pay_id }); // 参数接口
148 + Taro.hideLoading();
135 149
136 if (payParams.code) { 150 if (payParams.code) {
137 let pay = payParams.data; 151 let pay = payParams.data;
...@@ -142,16 +156,21 @@ const submitBtn = async () => { ...@@ -142,16 +156,21 @@ const submitBtn = async () => {
142 signType: pay.signType, 156 signType: pay.signType,
143 paySign: pay.paySign, 157 paySign: pay.paySign,
144 success (res) { 158 success (res) {
145 - go('/success', { pay_id: data.pay_id }); 159 + go('/success', { pay_id: pay_id });
146 }, 160 },
147 - fail (res) { } 161 + fail (res) {
162 + // 支付取消或失败,保留 pending_pay_id,允许用户再次点击按钮尝试支付同一订单
163 + // 只有当 wxPayAPI 获取支付参数失败时(如下面的 else 分支),才重置 ID 以便重新创建订单
164 + Taro.showToast({ title: '支付失败,请重试', icon: 'none' });
165 + }
148 }) 166 })
167 + } else {
168 + pending_pay_id.value = null; // 支付参数获取失败,重置订单ID
169 + Taro.showToast({ title: payParams.msg || '获取支付信息失败', icon: 'none' });
149 } 170 }
150 } else { 171 } else {
151 // 金额等于零, 直接跳转成功页 172 // 金额等于零, 直接跳转成功页
152 - go('/success', { pay_id: data.pay_id }); 173 + go('/success', { pay_id: pay_id });
153 - }
154 - }
155 } 174 }
156 } 175 }
157 176
......