index.vue
8.46 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<template>
<view class="pay-confirm-page">
<view class="hero-panel" :style="hero_panel_style">
<text class="page-title">常住随用</text>
<view class="amount-card" :style="amount_card_style">
<text class="amount-label">支付金额:</text>
<view class="amount-value-group">
<text class="amount-value">{{ display_amount }}</text>
<text v-if="has_amount" class="amount-unit">元</text>
</view>
</view>
<text class="blessing-text">感恩随喜~</text>
</view>
<view class="footer-area">
<text v-if="status_text" class="status-text">{{ status_text }}</text>
<button
class="pay-button"
:style="pay_button_style"
hover-class="pay-button-hover"
:loading="pay_loading"
:disabled="!order_id || pay_loading"
@tap="handlePay"
>
立即支付
</button>
</view>
</view>
</template>
<script setup>
import { computed, ref, watch } from 'vue'
import Taro, { useLoad } from '@tarojs/taro'
import { useWechatMiniPay } from '@/composables/useWechatMiniPay'
import { getVersionedImageAssetByName, preloadImageAssetVersion } from '@/utils/assetUrl'
import { redirectAfterPaySuccess } from '@/utils/paySuccessRedirect'
const order_id = ref('')
const amount = ref('')
const status_text = ref('')
const navigating_after_pay_success = ref(false)
const mini_program_env_version = ref('')
const {
pay_loading,
last_result_text,
pay_by_order_id,
} = useWechatMiniPay()
const display_amount = computed(() => {
const normalized_amount = String(amount.value || '').trim().replace(/元$/, '').trim()
return normalized_amount || '--'
})
const has_amount = computed(() => display_amount.value !== '--')
const hero_panel_style = computed(() => ({
background: `url('${getVersionedImageAssetByName('bgg@2x.png')}') center top / 100% 100% no-repeat`,
}))
const amount_card_style = computed(() => ({
background: `url('${getVersionedImageAssetByName('money_bg@2x.png')}') center / 100% 100% no-repeat`,
}))
const pay_button_style = computed(() => ({
background: `url('${getVersionedImageAssetByName('btnn@2x.png')}') center / 100% 100% no-repeat`,
}))
const is_release_env = computed(() => mini_program_env_version.value === 'release')
const getMiniProgramEnvVersion = () => {
try {
const account_info = Taro.getAccountInfoSync?.()
return String(account_info?.miniProgram?.envVersion || account_info?.envVersion || '').trim().toLowerCase()
} catch (error) {
console.warn('获取小程序环境版本失败:', error)
return ''
}
}
const formatReleaseStatusText = (raw_text) => {
const normalized_text = String(raw_text || '').trim()
if (!normalized_text) return ''
const lower_text = normalized_text.toLowerCase()
if (normalized_text === '正在请求支付参数...') return normalized_text
if (normalized_text === '已获取支付参数,准备拉起微信支付弹框...') return normalized_text
if (normalized_text === '支付成功,正在跳转...') return normalized_text
if (normalized_text.includes('支付成功')) return normalized_text
if (lower_text.includes('cancel') || normalized_text.includes('取消支付')) {
return '您已取消本次支付,如需继续,可重新点击支付。'
}
if (
normalized_text.includes('未授权')
|| normalized_text.includes('授权失败')
|| normalized_text.includes('静默授权失败')
|| normalized_text.includes('登录')
) {
return '当前登录状态需要更新,请重新进入后再试。'
}
if (normalized_text.includes('缺少订单编号') || normalized_text.includes('订单 ID')) {
return '支付信息不完整,请返回上一页后重新发起。'
}
if (
normalized_text.includes('获取支付参数失败')
|| normalized_text.includes('拉起支付失败')
|| normalized_text.includes('支付未完成')
|| normalized_text.includes('失败')
|| normalized_text.includes('错误')
|| normalized_text.includes('异常')
) {
return '支付暂时没有完成,请稍后重试;如多次失败,请联系寺院工作人员协助处理。'
}
return '当前操作暂时没有完成,请稍后再试。'
}
const setStatusText = (raw_text) => {
const normalized_text = String(raw_text || '').trim()
if (!normalized_text) {
status_text.value = ''
return
}
status_text.value = is_release_env.value
? formatReleaseStatusText(normalized_text)
: normalized_text
}
watch(last_result_text, (value) => {
if (!pay_loading.value) {
setStatusText(value)
}
})
const goUserWebviewAfterPaySuccess = async () => {
if (navigating_after_pay_success.value) return
navigating_after_pay_success.value = true
try {
setStatusText('支付成功,正在跳转...')
const redirect_result = await redirectAfterPaySuccess()
if (redirect_result?.reason === 'missing-user-menu') {
setStatusText('支付成功,未找到“我的”菜单,正在返回首页...')
return
}
if (redirect_result?.reason === 'empty-user-link') {
setStatusText('支付成功,但“我的”菜单地址为空,正在返回首页...')
return
}
if (redirect_result?.target === 'home') {
setStatusText('支付成功,但菜单获取失败,正在返回首页...')
}
} catch (error) {
setStatusText('支付成功,但菜单获取失败,正在返回首页...')
} finally {
navigating_after_pay_success.value = false
}
}
const handlePay = async () => {
if (!order_id.value || pay_loading.value) return
status_text.value = ''
const pay_res = await pay_by_order_id(order_id.value, {
auto_auth: true,
on_status: (text) => {
setStatusText(text)
},
})
if (pay_res?.status === 'success') {
await goUserWebviewAfterPaySuccess()
}
}
useLoad((options) => {
mini_program_env_version.value = getMiniProgramEnvVersion()
preloadImageAssetVersion().catch((error) => {
console.error('支付确认页预加载图片版本配置失败:', error)
})
// TODO: 提供给外部 H5 配合同学的小程序跳转地址请按下面拼接:
// 1. 页面路径:/pages/pay-confirm/index
// 2. 必传参数:order_id(后端支付订单 ID)
// 3. 展示参数:amount(支付确认页展示金额,建议传字符串,如 99.00)
// 4. 完整示例:/pages/pay-confirm/index?order_id=202605150001&amount=99.00
// 5. 当前页面也兼容旧参数 money,但新接入默认统一传 amount,不要再继续扩散 money
order_id.value = String(options?.order_id || '').trim()
amount.value = String(options?.amount || options?.money || '').trim()
if (!order_id.value) {
setStatusText('缺少订单编号,暂时无法发起支付。')
}
})
</script>
<style lang="less">
.pay-confirm-page {
min-height: 100vh;
box-sizing: border-box;
padding: 0 0 calc(72rpx + env(safe-area-inset-bottom));
background: #fff;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.hero-panel {
min-height: 612rpx;
padding: 132rpx 48rpx 88rpx;
box-sizing: border-box;
}
.page-title {
display: block;
text-align: center;
font-size: 55rpx;
line-height: 1.2;
font-weight: 600;
color: #2f3133;
}
.amount-card {
margin-top: 84rpx;
width: 100%;
min-height: 145rpx;
padding: 0 40rpx;
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: space-between;
gap: 24rpx;
}
.amount-label {
flex-shrink: 0;
font-size: 30rpx;
color: #353535;
}
.amount-value-group {
display: flex;
align-items: baseline;
justify-content: flex-end;
flex: 1;
min-width: 0;
color: #252525;
}
.amount-value {
max-width: 100%;
font-size: 72rpx;
line-height: 1;
font-weight: 500;
text-align: right;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.amount-unit {
margin-left: 14rpx;
font-size: 30rpx;
line-height: 1;
}
.blessing-text {
display: block;
margin-top: 72rpx;
text-align: center;
font-size: 32rpx;
line-height: 1.4;
color: #3a3a3a;
}
.footer-area {
padding: 0 84rpx;
box-sizing: border-box;
}
.status-text {
display: block;
margin-bottom: 28rpx;
text-align: center;
font-size: 22rpx;
line-height: 1.7;
color: #8a6b45;
}
.pay-button {
width: 100%;
height: 90rpx;
line-height: 90rpx;
padding: 0;
border: 0;
border-radius: 0;
font-size: 34rpx;
font-weight: 500;
color: #fff6eb;
&::after {
border: 0;
}
&[disabled] {
opacity: 0.72;
color: #fff6eb;
}
}
.pay-button-hover {
opacity: 0.92;
}
</style>