feat: 接入微信小程序支付

This commit is contained in:
2026-05-14 00:16:17 +08:00
parent bf4423e53b
commit ae58a443a3
42 changed files with 2265 additions and 191 deletions

View File

@@ -0,0 +1,83 @@
function parsePayParams(rawValue) {
try {
const params = JSON.parse(decodeURIComponent(String(rawValue || '')));
if (!params || typeof params !== 'object') {
return null;
}
return params;
} catch (error) {
console.error('[wechat-pay] parse params failed', error);
return null;
}
}
function requestPayment(payParams) {
return new Promise((resolve) => {
wx.requestPayment({
timeStamp: String(payParams.timeStamp || ''),
nonceStr: String(payParams.nonceStr || ''),
package: String(payParams.package || ''),
signType: payParams.signType || 'RSA',
paySign: String(payParams.paySign || ''),
success() {
resolve('success');
},
fail(error) {
const errMsg = error && error.errMsg ? error.errMsg : '';
resolve(/cancel/i.test(errMsg) ? 'cancel' : 'fail');
},
});
});
}
function appendPayResult(url, requestId, status) {
const value = `${requestId}:${status}`;
const hashIndex = String(url || '').indexOf('#');
const baseUrl =
hashIndex >= 0 ? String(url).slice(0, hashIndex) : String(url || '');
const rawHash = hashIndex >= 0 ? String(url).slice(hashIndex + 1) : '';
const params = new URLSearchParams(rawHash);
params.set('wx_pay_result', value);
return `${baseUrl}#${params.toString()}`;
}
function notifyPreviousWebView(requestId, status) {
const pages = getCurrentPages();
const previousPage = pages.length >= 2 ? pages[pages.length - 2] : null;
if (previousPage && typeof previousPage.setData === 'function') {
previousPage.setData({
webViewUrl: appendPayResult(
previousPage.data.webViewUrl,
requestId,
status,
),
});
}
}
Page({
data: {
title: '正在拉起支付',
errorMessage: '',
},
async onLoad(query) {
const requestId = String(query.requestId || '');
const payParams = parsePayParams(query.payParams);
if (!requestId || !payParams) {
this.setData({
title: '支付失败',
errorMessage: '缺少支付参数。',
});
return;
}
const status = await requestPayment(payParams);
notifyPreviousWebView(requestId, status);
wx.navigateBack();
},
handleBack() {
wx.navigateBack();
},
});

View File

@@ -0,0 +1,3 @@
{
"navigationBarTitleText": "微信支付"
}

View File

@@ -0,0 +1,11 @@
<view class="pay-screen">
<view class="pay-card">
<view class="pay-title">{{title}}</view>
<view wx:if="{{errorMessage}}" class="pay-text pay-text--danger">
{{errorMessage}}
</view>
<button wx:if="{{errorMessage}}" class="ghost-button" bindtap="handleBack">
返回
</button>
</view>
</view>

View File

@@ -0,0 +1,48 @@
.pay-screen {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 48rpx;
background: #0b0f14;
box-sizing: border-box;
}
.pay-card {
width: 100%;
max-width: 560rpx;
padding: 36rpx;
border: 1rpx solid rgba(255, 255, 255, 0.14);
border-radius: 12rpx;
background: rgba(255, 255, 255, 0.06);
box-sizing: border-box;
}
.pay-title {
font-size: 34rpx;
font-weight: 600;
line-height: 1.35;
color: #f5f7fb;
}
.pay-text {
margin-top: 16rpx;
font-size: 26rpx;
line-height: 1.55;
color: rgba(245, 247, 251, 0.72);
}
.pay-text--danger {
color: #ffb4a9;
}
.ghost-button {
margin-top: 28rpx;
width: 100%;
border-radius: 8rpx;
border: 1rpx solid rgba(255, 255, 255, 0.24);
background: transparent;
color: rgba(245, 247, 251, 0.86);
font-size: 26rpx;
line-height: 2.6;
}