index.vue
6.14 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
<template>
<view class="pay-bridge-page">
<view class="status-card">
<text class="eyebrow">支付桥页</text>
<text class="title">{{ page_title }}</text>
<text class="desc">
{{ page_desc }}
</text>
</view>
<view class="info-card">
<text class="label">订单 ID</text>
<text class="value">{{ order_id || '暂无' }}</text>
<text class="label">授权状态</text>
<text class="value" :class="{ success: is_authed }">
{{ is_authed ? '已授权' : '未授权' }}
</text>
<text class="label">当前状态</text>
<text class="value">{{ result_text }}</text>
<text class="label">结果类型</text>
<text class="value">{{ result_status }}</text>
</view>
<view v-if="should_auto_back" class="info-card">
<text class="label">返回提示</text>
<text class="value">
{{ back_countdown > 0 ? `${back_countdown} 秒后自动返回上一页` : '正在返回上一页...' }}
</text>
</view>
<button class="ghost-btn" @tap="goBackNow">立即返回上一页</button>
</view>
</template>
<script setup>
import { ref, watch, onBeforeUnmount } from 'vue'
import Taro, { useDidShow, useLoad } from '@tarojs/taro'
import { useWechatMiniPay } from '@/composables/useWechatMiniPay'
const order_id = ref('')
const result_text = ref('等待接收支付参数')
const result_status = ref('pending')
const has_started = ref(false)
const should_auto_back = ref(true)
const back_countdown = ref(0)
const back_delay_seconds = ref(2)
const back_mode = ref('navigateBack')
const back_url = ref('/pages/webview-preview/index')
const page_title = ref('正在准备微信支付')
const page_desc = ref('该页面用于承接 WebView/H5 发起的小程序支付请求,支付结束后会返回上一页。')
const {
is_authed,
last_result_text,
pay_by_order_id,
} = useWechatMiniPay()
let back_timer = null
watch(last_result_text, (value) => {
result_text.value = value
})
const clearBackTimer = () => {
if (!back_timer) return
clearInterval(back_timer)
back_timer = null
}
const goBackNow = async () => {
clearBackTimer()
try {
if (back_mode.value === 'redirectTo' && back_url.value) {
await Taro.redirectTo({ url: back_url.value })
return
}
if (back_mode.value === 'reLaunch' && back_url.value) {
await Taro.reLaunch({ url: back_url.value })
return
}
await Taro.navigateBack()
} catch (error) {
await Taro.reLaunch({
url: back_url.value || '/pages/webview-preview/index',
})
}
}
const startAutoBack = () => {
if (!should_auto_back.value) return
clearBackTimer()
back_countdown.value = back_delay_seconds.value
back_timer = setInterval(() => {
back_countdown.value -= 1
if (back_countdown.value <= 0) {
clearBackTimer()
goBackNow()
}
}, 1000)
}
const startPay = async () => {
if (!order_id.value || has_started.value) return
has_started.value = true
const pay_res = await pay_by_order_id(order_id.value, {
auto_auth: true,
on_status: (text) => {
result_text.value = text
},
})
result_status.value = pay_res?.status || 'unknown'
if (pay_res?.status === 'success') {
result_text.value = '支付成功,准备返回上一页。'
page_title.value = '支付已完成'
page_desc.value = '支付桥页已经完成本次支付,你可以等待自动返回,或手动立即返回。'
} else if (pay_res?.status === 'cancel') {
result_text.value = '你已取消支付,准备返回上一页。'
page_title.value = '支付已取消'
page_desc.value = '你刚刚取消了本次支付,如需重试,可以重新从 WebView 发起。'
} else {
result_text.value = '支付未完成,准备返回上一页。'
page_title.value = '支付未完成'
page_desc.value = '本次支付没有完成,你可以返回上一页重新发起,或保留在当前页查看状态。'
}
startAutoBack()
}
useLoad((options) => {
order_id.value = String(options?.order_id || '').trim()
should_auto_back.value = String(options?.auto_back || '1') !== '0'
back_mode.value = String(options?.back_mode || 'navigateBack')
back_url.value = String(options?.back_url || '/pages/webview-preview/index')
const parsed_back_delay = Number(options?.back_delay || 2)
back_delay_seconds.value = Number.isFinite(parsed_back_delay) && parsed_back_delay > 0
? parsed_back_delay
: 2
if (!order_id.value) {
result_text.value = '缺少订单 ID,无法发起支付。'
result_status.value = 'invalid'
page_title.value = '缺少支付参数'
page_desc.value = '当前页面没有拿到有效的订单 ID,无法继续发起支付。'
startAutoBack()
}
})
useDidShow(() => {
startPay()
})
onBeforeUnmount(() => {
clearBackTimer()
})
</script>
<style lang="less">
.pay-bridge-page {
min-height: 100vh;
padding: 32rpx 24rpx 48rpx;
box-sizing: border-box;
background:
radial-gradient(circle at top right, rgba(15, 118, 110, 0.16), transparent 30%),
linear-gradient(180deg, #f5fbfa 0%, #edf5f4 100%);
}
.status-card,
.info-card {
background: rgba(255, 255, 255, 0.94);
border-radius: 28rpx;
padding: 32rpx;
box-sizing: border-box;
border: 2rpx solid rgba(15, 23, 42, 0.05);
box-shadow: 0 18rpx 50rpx rgba(15, 23, 42, 0.06);
}
.status-card {
margin-bottom: 24rpx;
}
.info-card {
margin-bottom: 24rpx;
}
.eyebrow {
display: inline-block;
padding: 8rpx 18rpx;
border-radius: 999rpx;
background: #d1fae5;
color: #065f46;
font-size: 24rpx;
font-weight: 600;
}
.title {
display: block;
margin-top: 18rpx;
font-size: 40rpx;
font-weight: 700;
color: #111827;
}
.desc {
display: block;
margin-top: 16rpx;
font-size: 26rpx;
line-height: 1.7;
color: #6b7280;
}
.label {
display: block;
margin-top: 12rpx;
font-size: 24rpx;
color: #6b7280;
}
.value {
display: block;
margin-top: 8rpx;
font-size: 30rpx;
line-height: 1.7;
color: #111827;
word-break: break-all;
}
.success {
color: #047857;
}
.ghost-btn {
width: 100%;
border-radius: 999rpx;
font-size: 30rpx;
line-height: 88rpx;
color: #134e4a;
background: #ffffff;
border: 2rpx solid #cbd5e1;
}
</style>