From 75f7b8bd8ba3fd22a75abcc69a656997be113570 Mon Sep 17 00:00:00 2001 From: weizong song Date: Tue, 29 Sep 2026 09:33:06 +0800 Subject: [PATCH] fix: use login token and validate payment response --- utils/util.js | 96 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 57 insertions(+), 39 deletions(-) diff --git a/utils/util.js b/utils/util.js index 57ebec2..d9a3d02 100755 --- a/utils/util.js +++ b/utils/util.js @@ -233,45 +233,63 @@ const request = options => { }); }; // 提交formID封装 //orderID, orderType, success, fail -const payOrder = (a, e, r, n) => { - uni.request({ - url: HOST + 'customer/recharge?token=' + uni.getStorageSync('userInfo').access_token + '&money=' + a + - '&order_id=' + e, - method: 'GET', - header: { - 'content-type': 'application/json' - }, - success: function(a) { - if ('' != a.data) { - console.log(a); - var e = a.data.jsApiParameters.replace(/\\/g, ''); - a.data = JSON.parse(e); - var t = a.data.package.indexOf('=') + 1; - a.data.package.substring(t); - uni.requestPayment({ - timeStamp: a.data.timeStamp, - nonceStr: a.data.nonceStr, - package: a.data.package, - signType: a.data.signType, - paySign: a.data.paySign, - success: function(a) { - r(a); - }, - fail: function(a) { - n(a); - } - }); - } else { - n('当前网络状况不好'); - } - }, - fail: function(a) { - n('当前网络状况不好'); - } - }); -} - - +const payOrder = (a, e, r, n) => { + const userInfo = uni.getStorageSync(user_info_key) || {}; + const token = userInfo.access_token; + if (isNull(token)) { + n('登录已失效,请重新登录'); + return; + } + uni.request({ + url: HOST + 'customer/recharge?token=' + encodeURIComponent(token) + '&money=' + encodeURIComponent(a) + + '&order_id=' + encodeURIComponent(e), + method: 'GET', + header: { + 'content-type': 'application/json' + }, + success: function(response) { + const data = response.data; + if (response.statusCode !== 200) { + n(data && (data.errormsg || data.message) || '支付请求失败(HTTP ' + response.statusCode + ')'); + return; + } + if (!data || typeof data.jsApiParameters !== 'string' || !data.jsApiParameters) { + n(data && (data.errormsg || data.message) || '支付接口未返回支付参数'); + return; + } + let paymentParams; + try { + paymentParams = JSON.parse(data.jsApiParameters.replace(/\\/g, '')); + } catch (error) { + n('支付参数格式错误'); + return; + } + if (!paymentParams || !paymentParams.timeStamp || !paymentParams.nonceStr || + !paymentParams.package || !paymentParams.signType || !paymentParams.paySign) { + n('支付参数不完整'); + return; + } + uni.requestPayment({ + timeStamp: paymentParams.timeStamp, + nonceStr: paymentParams.nonceStr, + package: paymentParams.package, + signType: paymentParams.signType, + paySign: paymentParams.paySign, + success: function(a) { + r(a); + }, + fail: function(a) { + n(a); + } + }); + }, + fail: function(a) { + n('当前网络状况不好'); + } + }); +} + + const isLogin = () => { var userInfo = uni.getStorageSync(user_info_key); return !isNull(userInfo.access_token);