给所有支付接口加上了支付宝

This commit is contained in:
2026-07-09 22:07:20 +08:00
parent 566aeaa3b7
commit 4b60ebbc10
18 changed files with 438 additions and 38 deletions

View File

@@ -197,14 +197,28 @@ Page({
content: `是否充值 ¥${numAmount.toFixed(2)}`,
success: (res) => {
if (res.confirm) {
this.startPayment(numAmount);
this.choosePayMethod(numAmount);
}
}
});
},
// 选择支付方式
choosePayMethod(amount) {
wx.showActionSheet({
itemList: ['微信支付', '支付宝(扫码)'],
success: (res) => {
if (res.tapIndex === 0) {
this.startPayment(amount, 'wechat');
} else if (res.tapIndex === 1) {
this.startPayment(amount, 'alipay');
}
}
});
},
// 开始支付流程
async startPayment(amount) {
async startPayment(amount, payMethod = 'wechat') {
this.showLoading('发起支付中...');
try {
@@ -212,19 +226,27 @@ Page({
url: '/shangpin/sjcz',
method: 'POST',
data: {
jine: amount // 关键字段:后端接口要求"jine"
jine: amount, // 关键字段:后端接口要求"jine"
pay_method: payMethod === 'alipay' ? 'alipay' : 'wechat'
}
});
if (res.data.code === 200) {
const payParams = res.data.payParams;
const dingdanid = res.data.dingdanid;
this.setData({
currentDingdanid: dingdanid,
pollingCount: 0
});
// 支付宝分支:跳转二维码页
if (payMethod === 'alipay' || (payParams && payParams.pay_method === 'alipay')) {
this.hideLoading();
this.triggerAlipayPayment(payParams, dingdanid);
return;
}
// 调起微信支付
await this.wechatPay(payParams);
} else {
@@ -244,6 +266,22 @@ Page({
}
},
// ========== 支付宝扫码支付封装 ==========
triggerAlipayPayment(payParams, orderId) {
const qrImage = payParams && payParams.qr_image ? payParams.qr_image : '';
if (!qrImage) {
this.hideLoading();
wx.showToast({ title: '支付宝二维码生成失败', icon: 'none' });
return;
}
const app = getApp();
app.globalData.alipayQrImage = qrImage;
const pollUrl = encodeURIComponent('/shangpin/sjcg');
wx.navigateTo({
url: `/pages/alipay-webview/alipay-webview?orderId=${orderId}&pollUrl=${pollUrl}`
});
},
// 微信支付
async wechatPay(payParams) {
this.showLoading('调起支付中...');