From 162c49fc150925bd03674eae9d1ec798c6e76536 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E6=A2=93=E5=BD=AD?= Date: Sat, 9 May 2026 17:26:30 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B5=9B=E4=BA=8B=E6=8A=A5=E5=90=8D=E6=9C=AA?= =?UTF-8?q?=E6=94=AF=E4=BB=98=E4=B8=8D=E7=AE=97=E6=94=AF=E4=BB=98=E6=88=90?= =?UTF-8?q?=E5=8A=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/apply/apply.js | 46 +++++++++++++++++++++++++++++++++++++++----- utils/constants.js | 2 ++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/pages/apply/apply.js b/pages/apply/apply.js index 12c8142..93c932b 100644 --- a/pages/apply/apply.js +++ b/pages/apply/apply.js @@ -694,6 +694,7 @@ Page({ const payload = this._validateAndGetPayload(2); if (!payload) return; + let registrationId = null; try { // 1. 获取 openid const openid = await this._getOpenId(); @@ -702,9 +703,9 @@ Page({ return; } - // 2. 提交报名 + // 2. 提交报名(创建预报名记录) this.setData({ submitting: true }); - const registrationId = await this._submitApplyForPayment(payload); + registrationId = await this._submitApplyForPayment(payload); // 3. 选择支付方式并获取支付参数 await this._selectPaymentMethod(registrationId, openid, 2); @@ -725,6 +726,16 @@ Page({ } catch (error) { console.error('支付流程异常:', error); + + // 如果已经创建了预报名记录,尝试取消 + if (registrationId) { + try { + await this._cancelPreRegistration(registrationId); + } catch (cancelErr) { + console.error('取消预报名失败:', cancelErr); + } + } + wx.showToast({ title: error && error.message ? error.message : '支付失败,请稍后重试', icon: 'none' @@ -843,14 +854,14 @@ Page({ try { // 支付成功,同步支付状态 await request({ - path: `/api/eventclient/sync-payment/${registrationId}`, + path: API.syncPayment(registrationId), method: 'POST', needAuth: true, data: {}, contentType: 'application/json' }); - wx.showToast({ title: '支付成功', icon: 'success' }); + wx.showToast({ title: '支付成功,报名完成', icon: 'success' }); setTimeout(() => { wx.switchTab({ url: '/pages/home/home' }); }, 1500); @@ -868,8 +879,16 @@ Page({ resolve(); } }, - fail: (err) => { + fail: async (err) => { console.error('微信支付失败:', err); + + // 支付失败,尝试取消预报名记录 + try { + await this._cancelPreRegistration(registrationId); + } catch (cancelErr) { + console.error('取消预报名失败:', cancelErr); + } + if (err.errMsg && err.errMsg.includes('cancel')) { wx.showToast({ title: '支付已取消', icon: 'none' }); } else { @@ -884,6 +903,23 @@ Page({ }); }, + /** 取消预报名记录 */ + async _cancelPreRegistration(registrationId) { + try { + await request({ + path: API.cancelRegistration(registrationId), + method: 'POST', + needAuth: true, + data: {}, + contentType: 'application/json' + }); + console.log('预报名记录已取消:', registrationId); + } catch (err) { + console.error('取消预报名失败:', err); + // 取消预报名失败不影响主要流程,仅记录日志 + } + }, + _submitApply(payload) { this.setData({ submitting: true }); request({ diff --git a/utils/constants.js b/utils/constants.js index a24ecac..f55add3 100644 --- a/utils/constants.js +++ b/utils/constants.js @@ -14,6 +14,8 @@ const API = { calculateAmount: '/api/eventclient/calculate-amount', cancelRegistrationRefund: '/api/eventclient/cancel-registration-refund', couponValidate: '/api/eventclient/coupon/validate', + syncPayment: (registrationId) => `/api/eventclient/sync-payment/${registrationId}`, + cancelRegistration: (registrationId) => `/api/eventclient/cancel/${registrationId}`, wechatLogin: '/api/oauth/login', loginByCode: '/api/oauth/login',