wxScanTest.vue
3.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
<template>
<div class="scan-test-page">
<div class="card">
<div class="row">
<div class="label">环境</div>
<div class="value">{{ in_wechat ? '微信内' : '非微信' }}</div>
</div>
<div class="row last">
<div class="label">JSSDK</div>
<div class="value" :class="{ ok: wx_ready === true, fail: wx_ready === false }">
{{ wx_ready_text }}
</div>
</div>
</div>
<div class="card">
<div class="card-title">扫码结果</div>
<van-field
v-model="scan_result"
type="textarea"
rows="3"
autosize
readonly
placeholder="点击下方按钮开始扫码"
/>
<div class="btn-wrap">
<van-button
block
color="#A67939"
:loading="scanning"
:disabled="scanning"
@click="start_scan"
>
调起扫码
</van-button>
</div>
</div>
</div>
</template>
<script setup>
import { computed, onMounted, ref } from 'vue'
import wx from 'weixin-js-sdk'
import { showToast } from 'vant'
import { wxInfo } from '@/utils/tools'
const scan_result = ref('')
const scanning = ref(false)
const wx_ready = ref(null)
const in_wechat = computed(() => wxInfo().isTable === true)
const wx_ready_text = computed(() => {
if (!in_wechat.value) return '非微信环境无需初始化'
if (wx_ready.value === true) return '已就绪'
if (wx_ready.value === false) return '初始化失败'
return '未检测'
})
const ensure_wx_ready = async () => {
if (!window.__wx_ready_promise) return false
const ok = await window.__wx_ready_promise
return ok === true
}
const start_scan = async () => {
if (!in_wechat.value) {
showToast('请在微信内打开该页面')
return
}
if (scanning.value) return
scanning.value = true
try {
const ok = await ensure_wx_ready()
wx_ready.value = ok
if (!ok) {
showToast('wx 初始化失败')
return
}
const result = await new Promise((resolve) => {
wx.scanQRCode({
needResult: 1,
scanType: ['qrCode', 'barCode'],
success: (res) => resolve(res?.resultStr || ''),
fail: () => resolve(''),
cancel: () => resolve(''),
})
})
scan_result.value = String(result || '')
if (!scan_result.value) showToast('未获取到二维码内容')
} finally {
scanning.value = false
}
}
onMounted(async () => {
if (!in_wechat.value) {
wx_ready.value = false
return
}
wx_ready.value = await ensure_wx_ready()
})
</script>
<style lang="less" scoped>
.scan-test-page {
min-height: 100vh;
background-color: #F6F6F6;
padding: 16px;
box-sizing: border-box;
.card {
background: #fff;
border-radius: 12px;
padding: 16px;
box-shadow: 0 6px 18px rgba(0, 0, 0, 0.04);
}
.card + .card {
margin-top: 12px;
}
.card-title {
font-size: 14px;
color: #6B7280;
margin-bottom: 12px;
}
.row {
display: flex;
align-items: center;
justify-content: space-between;
padding: 10px 0;
border-bottom: 1px solid #F3F4F6;
.label {
font-size: 14px;
color: #6B7280;
}
.value {
font-size: 16px;
color: #111827;
font-weight: 600;
margin-left: 12px;
word-break: break-all;
text-align: right;
}
.ok {
color: #16A34A;
}
.fail {
color: #E24A4A;
}
}
.row.last {
border-bottom: 0;
}
.btn-wrap {
margin-top: 12px;
}
}
</style>