weappPayBridge.vue
6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<template>
<div class="weapp-pay-bridge-page">
<div class="hero-card">
<div class="eyebrow">WebView 支付测试</div>
<h1 class="title">从 H5 跳转到小程序支付桥页</h1>
<p class="desc">
这个页面用于在小程序 WebView 中点击按钮后,直接跳到外层小程序的原生支付桥页,并在桥页里自动拉起微信支付弹框。
</p>
</div>
<div class="panel">
<div class="panel-head">
<span class="panel-title">当前环境</span>
<span :class="['env-tag', isMiniProgramWebView ? 'env-ok' : 'env-warn']">
{{ isMiniProgramWebView ? '小程序 WebView 内' : '普通浏览器' }}
</span>
</div>
<p class="hint">只有在小程序 WebView 中,点击下方按钮才会真正调用外层小程序页面。</p>
</div>
<div class="panel">
<div class="panel-head">
<span class="panel-title">支付参数</span>
</div>
<label class="field-label" for="orderId">测试订单 ID</label>
<input
id="orderId"
v-model.trim="orderId"
class="field-input"
type="text"
placeholder="请输入后端可生成支付参数的 order_id"
/>
<p class="hint">建议使用未支付且可以正常返回支付参数的测试订单。</p>
<div class="path-box">
<div class="path-label">将跳转的小程序支付桥页</div>
<div class="path-value">{{ payBridgePagePath }}</div>
</div>
<button class="primary-btn" @click="goToMiniProgramPayBridge">点击触发小程序支付测试</button>
</div>
<div class="panel">
<div class="panel-head">
<span class="panel-title">最近结果</span>
</div>
<div class="result-text">{{ resultText }}</div>
</div>
</div>
</template>
<script setup>
import { computed, ref } from 'vue';
import { showToast } from 'vant';
import wx from 'weixin-js-sdk';
const orderId = ref('');
const resultText = ref('等待开始测试');
const isMiniProgramWebView = computed(() => {
return navigator.userAgent.includes('miniProgram');
});
/**
* 说明:
* 当前页面只是「H5 -> 小程序支付桥页」的最小测试页,只负责把 order_id 传给外层小程序。
* 小程序桥页支付完成后,会通过 navigateBack 返回 WebView,但不会把支付结果主动回调给当前 H5 页面。
*
* 如果后续要按正式业务接入
* 1. H5 发起支付前记录当前 order_id,并标记“等待支付结果”;
* 2. 从小程序桥页返回后,在 pageshow / visibilitychange / focus 等时机触发查单;
* 3. 调后端订单状态接口,以服务端订单状态作为最终支付结果;
* 4. 如后端存在支付落账延迟,可按 1~2 秒间隔短轮询 3~5 次。
*/
const payBridgePagePath = computed(() => {
const normalizedOrderId = encodeURIComponent(orderId.value || '');
return `/pages/pay-bridge/index?order_id=${normalizedOrderId}&auto_back=1&back_delay=2&back_mode=navigateBack&back_url=${encodeURIComponent('/pages/webview-preview/index')}&source=weapp-pay-bridge`;
});
const ensureOrderId = () => {
if (orderId.value) return true;
showToast('请先输入订单 ID');
resultText.value = '未填写订单 ID,无法继续触发支付测试。';
return false;
};
const goToMiniProgramPayBridge = () => {
if (!ensureOrderId()) return;
if (!isMiniProgramWebView.value) {
resultText.value = '当前不是小程序 WebView 环境,无法跳转到外层小程序支付桥页。';
showToast('请在小程序 WebView 中打开此页');
return;
}
try {
wx.miniProgram.navigateTo({
url: payBridgePagePath.value,
success: () => {
resultText.value = `已请求跳转到小程序支付桥页:${payBridgePagePath.value}`;
},
fail: (error) => {
const message = error?.errMsg || '跳转失败';
resultText.value = `调用小程序支付桥页失败:${message}`;
showToast(message);
},
});
} catch (error) {
const message = error?.message || '调用 wx.miniProgram.navigateTo 失败';
resultText.value = message;
showToast(message);
}
};
</script>
<style scoped lang="less">
.weapp-pay-bridge-page {
height: 100vh;
padding: 24px 18px 40px;
box-sizing: border-box;
overflow-y: auto;
-webkit-overflow-scrolling: touch;
background:
radial-gradient(circle at top left, rgba(84, 171, 174, 0.18), transparent 28%),
linear-gradient(180deg, #f7fbfb 0%, #eef4f6 100%);
color: #16323a;
}
.hero-card,
.panel {
background: rgba(255, 255, 255, 0.94);
border: 1px solid rgba(22, 50, 58, 0.08);
border-radius: 22px;
box-shadow: 0 18px 42px rgba(22, 50, 58, 0.08);
padding: 22px 18px;
box-sizing: border-box;
}
.hero-card {
margin-bottom: 16px;
}
.eyebrow {
display: inline-flex;
align-items: center;
padding: 6px 10px;
border-radius: 999px;
background: #e0f2f1;
color: #0f766e;
font-size: 12px;
font-weight: 600;
letter-spacing: 0.08em;
}
.title {
margin: 14px 0 0;
font-size: 28px;
line-height: 1.25;
}
.desc,
.hint,
.result-text {
margin: 12px 0 0;
font-size: 14px;
line-height: 1.7;
color: #4f6470;
}
.panel {
margin-bottom: 16px;
}
.panel-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.panel-title {
font-size: 18px;
font-weight: 700;
color: #16323a;
}
.env-tag {
padding: 6px 10px;
border-radius: 999px;
font-size: 12px;
font-weight: 600;
}
.env-ok {
color: #166534;
background: #dcfce7;
}
.env-warn {
color: #9a3412;
background: #ffedd5;
}
.field-label {
display: block;
margin-top: 16px;
margin-bottom: 8px;
font-size: 14px;
color: #36505b;
}
.field-input {
width: 100%;
height: 46px;
padding: 0 14px;
border: 1px solid #cfd8dc;
border-radius: 14px;
box-sizing: border-box;
background: #f9fbfb;
font-size: 15px;
color: #16323a;
}
.path-box {
margin-top: 14px;
padding: 14px;
border-radius: 14px;
background: #f2f7f8;
border: 1px dashed #b6c8cf;
}
.path-label {
font-size: 12px;
color: #68808a;
}
.path-value {
margin-top: 6px;
font-size: 13px;
line-height: 1.6;
word-break: break-all;
color: #16323a;
}
.primary-btn,
.ghost-btn {
width: 100%;
height: 46px;
border: none;
border-radius: 999px;
font-size: 15px;
font-weight: 600;
cursor: pointer;
}
.primary-btn {
margin-top: 16px;
color: #fff;
background: linear-gradient(135deg, #0f766e, #155e75);
}
.ghost-btn {
margin-top: 10px;
color: #155e75;
background: #e6f4f1;
}
</style>