index.vue
5.27 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
<template>
<view class="pay-test-page">
<view class="hero-card">
<text class="hero-title">微信支付最小测试页</text>
<text class="hero-desc">
这里仅验证授权是否可用,以及点击按钮后能否成功拉起微信支付弹框。
</text>
</view>
<view class="panel">
<view class="panel-head">
<text class="panel-title">授权状态</text>
<text class="auth-tag" :class="{ authed: is_authed }">
{{ is_authed ? '已授权' : '未授权' }}
</text>
</view>
<text class="panel-tip">
当前 sessionid:{{ sessionid_preview }}
</text>
<button class="outline-btn" :loading="auth_loading" @tap="handleRefreshAuth">
重新静默授权
</button>
</view>
<view class="panel">
<view class="panel-head">
<text class="panel-title">支付测试</text>
</view>
<text class="field-label">测试订单 ID</text>
<input
class="pay-input"
type="text"
:value="order_id"
placeholder="请输入 meihuaApp 后端里的测试订单 ID"
@input="handleOrderIdInput"
/>
<text class="panel-tip">
说明:这里只要求拉起微信支付弹框,不要求支付成功;请使用后端可生成支付参数的未支付订单。
</text>
<button class="primary-btn" :loading="pay_loading" @tap="handlePay">
点击测试拉起微信支付
</button>
</view>
<view class="panel">
<view class="panel-head">
<text class="panel-title">最近结果</text>
</view>
<text class="result-text">{{ result_text }}</text>
</view>
</view>
</template>
<script setup>
import { ref, watch } from 'vue'
import { useDidShow, useLoad } from '@tarojs/taro'
import { useWechatMiniPay } from '@/composables/useWechatMiniPay'
const order_id = ref('')
const should_auto_pay = ref(false)
const has_auto_started = ref(false)
const {
auth_loading,
pay_loading,
is_authed,
sessionid_preview,
last_result_text,
sync_auth_state,
refresh_auth,
pay_by_order_id,
} = useWechatMiniPay()
const result_text = ref(last_result_text.value)
watch(last_result_text, (value) => {
result_text.value = value
})
const handleOrderIdInput = (event) => {
order_id.value = event?.detail?.value || ''
}
const handleRefreshAuth = async () => {
await refresh_auth({
force_refresh: true,
show_loading: true,
on_status: (text) => {
result_text.value = text
},
})
}
const handlePay = async () => {
await pay_by_order_id(order_id.value, {
auto_auth: false,
on_status: (text) => {
result_text.value = text
},
})
}
useLoad((options) => {
const route_order_id = String(options?.order_id || '').trim()
if (route_order_id) {
order_id.value = route_order_id
result_text.value = '已接收到来自 H5/WebView 的订单参数,准备测试支付。'
}
should_auto_pay.value = String(options?.auto_pay || '') === '1'
})
useDidShow(() => {
sync_auth_state()
if (should_auto_pay.value && order_id.value && !has_auto_started.value) {
has_auto_started.value = true
setTimeout(async () => {
await handlePay()
}, 300)
}
})
</script>
<style lang="less">
.pay-test-page {
min-height: 100vh;
padding: 32rpx 24rpx 48rpx;
box-sizing: border-box;
background:
radial-gradient(circle at top left, rgba(255, 214, 165, 0.45), transparent 36%),
linear-gradient(180deg, #fff9f0 0%, #f4f6fb 100%);
.hero-card,
.panel {
background: rgba(255, 255, 255, 0.92);
border: 2rpx solid rgba(17, 24, 39, 0.05);
border-radius: 28rpx;
padding: 32rpx;
box-sizing: border-box;
box-shadow: 0 20rpx 60rpx rgba(15, 23, 42, 0.06);
backdrop-filter: blur(10rpx);
}
.hero-card {
margin-bottom: 24rpx;
}
.hero-title {
display: block;
font-size: 40rpx;
font-weight: 700;
color: #111827;
}
.hero-desc {
display: block;
margin-top: 16rpx;
font-size: 26rpx;
line-height: 1.7;
color: #6b7280;
}
.panel {
margin-bottom: 24rpx;
}
.panel-head {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 20rpx;
}
.panel-title {
font-size: 32rpx;
font-weight: 600;
color: #111827;
}
.auth-tag {
padding: 8rpx 18rpx;
border-radius: 999rpx;
font-size: 24rpx;
color: #b45309;
background: #fef3c7;
}
.authed {
color: #166534;
background: #dcfce7;
}
.field-label {
display: block;
margin-bottom: 16rpx;
font-size: 26rpx;
color: #4b5563;
}
.panel-tip,
.result-text {
display: block;
font-size: 24rpx;
line-height: 1.7;
color: #6b7280;
}
.panel-tip {
margin-top: 16rpx;
}
.pay-input {
width: 100%;
height: 88rpx;
padding: 0 24rpx;
border-radius: 20rpx;
background: #f9fafb;
border: 2rpx solid #e5e7eb;
box-sizing: border-box;
font-size: 28rpx;
color: #111827;
}
.primary-btn,
.outline-btn {
margin-top: 24rpx;
border-radius: 999rpx;
font-size: 30rpx;
line-height: 88rpx;
}
.primary-btn {
color: #fff;
background: linear-gradient(135deg, #0f766e, #115e59);
}
.outline-btn {
color: #0f172a;
background: #fff;
border: 2rpx solid #d1d5db;
}
}
</style>