hookehuyr

feat(weapp-pay-bridge): 将支付消息通信改为直接跳转小程序支付桥页

重构支付触发逻辑,从使用 `postMessage` 发送消息给 WebView 容器,改为直接调用 `wx.miniProgram.navigateTo` 跳转到外层小程序的支付桥页。支付桥页 URL 已包含订单 ID、回调参数及提示文本,以支持自动拉起支付并处理结果回调。
...@@ -2,9 +2,9 @@ ...@@ -2,9 +2,9 @@
2 <div class="weapp-pay-bridge-page"> 2 <div class="weapp-pay-bridge-page">
3 <div class="hero-card"> 3 <div class="hero-card">
4 <div class="eyebrow">WebView 支付测试</div> 4 <div class="eyebrow">WebView 支付测试</div>
5 - <h1 class="title">从 H5 跳转到小程序支付测试页</h1> 5 + <h1 class="title">从 H5 跳转到小程序支付页</h1>
6 <p class="desc"> 6 <p class="desc">
7 - 这个页面用于在小程序 WebView 中点击按钮后,直接跳到外层小程序的支付测试页,并自动尝试拉起微信支付弹框。 7 + 这个页面用于在小程序 WebView 中点击按钮后,直接跳到外层小程序的原生支付桥页,并在桥页里自动拉起微信支付弹框。
8 </p> 8 </p>
9 </div> 9 </div>
10 10
...@@ -32,10 +32,10 @@ ...@@ -32,10 +32,10 @@
32 /> 32 />
33 <p class="hint">建议使用未支付且可以正常返回支付参数的测试订单。</p> 33 <p class="hint">建议使用未支付且可以正常返回支付参数的测试订单。</p>
34 <div class="path-box"> 34 <div class="path-box">
35 - <div class="path-label">发给小程序 WebView 容器的消息</div> 35 + <div class="path-label">将跳转的小程序支付桥页</div>
36 - <div class="path-value">{{ payMessagePreview }}</div> 36 + <div class="path-value">{{ payBridgePagePath }}</div>
37 </div> 37 </div>
38 - <button class="primary-btn" @click="sendPayMessage">点击触发小程序支付测试</button> 38 + <button class="primary-btn" @click="goToMiniProgramPayBridge">点击触发小程序支付测试</button>
39 </div> 39 </div>
40 40
41 <div class="panel"> 41 <div class="panel">
...@@ -59,12 +59,9 @@ const isMiniProgramWebView = computed(() => { ...@@ -59,12 +59,9 @@ const isMiniProgramWebView = computed(() => {
59 return navigator.userAgent.includes('miniProgram'); 59 return navigator.userAgent.includes('miniProgram');
60 }); 60 });
61 61
62 -const payMessagePreview = computed(() => { 62 +const payBridgePagePath = computed(() => {
63 - return JSON.stringify({ 63 + const normalizedOrderId = encodeURIComponent(orderId.value || '');
64 - type: 'pay_test', 64 + return `/pages/pay-bridge/index?order_id=${normalizedOrderId}&auto_back=1&back_delay=2&back_mode=navigateBack&back_url=${encodeURIComponent('/pages/webview-preview/index')}&success_text=${encodeURIComponent('支付成功,正在返回 WebView 页面。')}&cancel_text=${encodeURIComponent('已取消支付,正在返回 WebView 页面。')}&fail_text=${encodeURIComponent('支付未完成,正在返回 WebView 页面。')}&source=weapp-pay-bridge`;
65 - order_id: orderId.value || '',
66 - source: 'weapp-pay-bridge',
67 - });
68 }); 65 });
69 66
70 const ensureOrderId = () => { 67 const ensureOrderId = () => {
...@@ -75,26 +72,29 @@ const ensureOrderId = () => { ...@@ -75,26 +72,29 @@ const ensureOrderId = () => {
75 return false; 72 return false;
76 }; 73 };
77 74
78 -const sendPayMessage = () => { 75 +const goToMiniProgramPayBridge = () => {
79 if (!ensureOrderId()) return; 76 if (!ensureOrderId()) return;
80 77
81 if (!isMiniProgramWebView.value) { 78 if (!isMiniProgramWebView.value) {
82 - resultText.value = '当前不是小程序 WebView 环境,postMessage 不会发给小程序。'; 79 + resultText.value = '当前不是小程序 WebView 环境,无法跳转到外层小程序支付桥页。';
83 showToast('请在小程序 WebView 中打开此页'); 80 showToast('请在小程序 WebView 中打开此页');
84 return; 81 return;
85 } 82 }
86 83
87 try { 84 try {
88 - wx.miniProgram.postMessage({ 85 + wx.miniProgram.navigateTo({
89 - data: { 86 + url: payBridgePagePath.value,
90 - type: 'pay_test', 87 + success: () => {
91 - order_id: orderId.value, 88 + resultText.value = `已请求跳转到小程序支付桥页:${payBridgePagePath.value}`;
92 - source: 'weapp-pay-bridge', 89 + },
90 + fail: (error) => {
91 + const message = error?.errMsg || '跳转失败';
92 + resultText.value = `调用小程序支付桥页失败:${message}`;
93 + showToast(message);
93 }, 94 },
94 }); 95 });
95 - resultText.value = '已发送支付请求消息给当前小程序 WebView 容器。';
96 } catch (error) { 96 } catch (error) {
97 - const message = error?.message || '发送 postMessage 失败'; 97 + const message = error?.message || '调用 wx.miniProgram.navigateTo 失败';
98 resultText.value = message; 98 resultText.value = message;
99 showToast(message); 99 showToast(message);
100 } 100 }
......