refactor(nfcTest): 移除模拟扫描功能并改进NFC结果显示
- 删除开发调试用的模拟扫描按钮及相关逻辑 - 添加safe_stringify方法处理ArrayBuffer类型数据 - 改进NFC结果显示格式,包含原始数据和解析结果 - 调整样式增加卡片边距
Showing
1 changed file
with
29 additions
and
60 deletions
| ... | @@ -13,10 +13,6 @@ | ... | @@ -13,10 +13,6 @@ |
| 13 | <nut-button v-if="isScanning" type="danger" size="large" @click="stopScan" style="margin-top: 40rpx;"> | 13 | <nut-button v-if="isScanning" type="danger" size="large" @click="stopScan" style="margin-top: 40rpx;"> |
| 14 | 停止扫描 | 14 | 停止扫描 |
| 15 | </nut-button> | 15 | </nut-button> |
| 16 | - | ||
| 17 | - <nut-button v-if="showMockButton" type="info" size="large" @click="mockScan" style="margin-top: 40rpx;"> | ||
| 18 | - 模拟扫描 (开发调试) | ||
| 19 | - </nut-button> | ||
| 20 | </view> | 16 | </view> |
| 21 | 17 | ||
| 22 | <view class="status-area"> | 18 | <view class="status-area"> |
| ... | @@ -52,11 +48,36 @@ const isScanning = ref(false); | ... | @@ -52,11 +48,36 @@ const isScanning = ref(false); |
| 52 | const status = ref('等待操作'); | 48 | const status = ref('等待操作'); |
| 53 | const result = ref(''); | 49 | const result = ref(''); |
| 54 | const error = ref(''); | 50 | const error = ref(''); |
| 55 | -const showMockButton = ref(false); | ||
| 56 | const debugInfo = ref(''); | 51 | const debugInfo = ref(''); |
| 57 | 52 | ||
| 58 | let nfcAdapter = null; | 53 | let nfcAdapter = null; |
| 59 | 54 | ||
| 55 | +const safe_stringify = (data) => { | ||
| 56 | + try { | ||
| 57 | + return JSON.stringify( | ||
| 58 | + data, | ||
| 59 | + (key, value) => { | ||
| 60 | + if (value instanceof ArrayBuffer) { | ||
| 61 | + return { | ||
| 62 | + __type: 'ArrayBuffer', | ||
| 63 | + hex: bufferToHex(value), | ||
| 64 | + }; | ||
| 65 | + } | ||
| 66 | + if (value && value.buffer instanceof ArrayBuffer) { | ||
| 67 | + return { | ||
| 68 | + __type: 'TypedArray', | ||
| 69 | + hex: bufferToHex(value.buffer), | ||
| 70 | + }; | ||
| 71 | + } | ||
| 72 | + return value; | ||
| 73 | + }, | ||
| 74 | + 2 | ||
| 75 | + ); | ||
| 76 | + } catch (e) { | ||
| 77 | + return String(data); | ||
| 78 | + } | ||
| 79 | +}; | ||
| 80 | + | ||
| 60 | const startScan = async () => { | 81 | const startScan = async () => { |
| 61 | error.value = ''; | 82 | error.value = ''; |
| 62 | result.value = ''; | 83 | result.value = ''; |
| ... | @@ -74,30 +95,21 @@ const startScan = async () => { | ... | @@ -74,30 +95,21 @@ const startScan = async () => { |
| 74 | const version = systemInfo && systemInfo.version ? systemInfo.version : ''; | 95 | const version = systemInfo && systemInfo.version ? systemInfo.version : ''; |
| 75 | debugInfo.value = `env: ${envType}\nplatform: ${platform}\nsystem: ${system}\nmodel: ${model}\nSDKVersion: ${SDKVersion}\nversion: ${version}`; | 96 | debugInfo.value = `env: ${envType}\nplatform: ${platform}\nsystem: ${system}\nmodel: ${model}\nSDKVersion: ${SDKVersion}\nversion: ${version}`; |
| 76 | 97 | ||
| 77 | - if (platform === 'devtools') { | ||
| 78 | - showMockButton.value = true; | ||
| 79 | - } else { | ||
| 80 | - showMockButton.value = false; | ||
| 81 | - } | ||
| 82 | - | ||
| 83 | if (platform === 'ios') { | 98 | if (platform === 'ios') { |
| 84 | error.value = 'iOS 端微信小程序通常不支持 NFC(该能力主要在 Android 可用)'; | 99 | error.value = 'iOS 端微信小程序通常不支持 NFC(该能力主要在 Android 可用)'; |
| 85 | status.value = '启动失败'; | 100 | status.value = '启动失败'; |
| 86 | - showMockButton.value = true; | ||
| 87 | return; | 101 | return; |
| 88 | } | 102 | } |
| 89 | 103 | ||
| 90 | if (!Taro.getNFCAdapter) { | 104 | if (!Taro.getNFCAdapter) { |
| 91 | error.value = '当前环境不支持 NFC 接口'; | 105 | error.value = '当前环境不支持 NFC 接口'; |
| 92 | status.value = '启动失败'; | 106 | status.value = '启动失败'; |
| 93 | - showMockButton.value = true; | ||
| 94 | return; | 107 | return; |
| 95 | } | 108 | } |
| 96 | 109 | ||
| 97 | if (envType && Taro.ENV_TYPE && envType !== Taro.ENV_TYPE.WEAPP) { | 110 | if (envType && Taro.ENV_TYPE && envType !== Taro.ENV_TYPE.WEAPP) { |
| 98 | error.value = '当前不是微信小程序环境,无法使用 NFC'; | 111 | error.value = '当前不是微信小程序环境,无法使用 NFC'; |
| 99 | status.value = '启动失败'; | 112 | status.value = '启动失败'; |
| 100 | - showMockButton.value = true; | ||
| 101 | return; | 113 | return; |
| 102 | } | 114 | } |
| 103 | 115 | ||
| ... | @@ -130,11 +142,9 @@ const startScan = async () => { | ... | @@ -130,11 +142,9 @@ const startScan = async () => { |
| 130 | } else if (err.errCode === 13001) { | 142 | } else if (err.errCode === 13001) { |
| 131 | error.value = '系统 NFC 开关未开启'; | 143 | error.value = '系统 NFC 开关未开启'; |
| 132 | } else if (err.errMsg && err.errMsg.includes('platform is not supported')) { | 144 | } else if (err.errMsg && err.errMsg.includes('platform is not supported')) { |
| 133 | - error.value = '开发者工具不支持 NFC,请使用真机调试或点击下方模拟按钮'; | 145 | + error.value = '开发者工具不支持 NFC,请使用真机调试'; |
| 134 | - showMockButton.value = true; | ||
| 135 | } else { | 146 | } else { |
| 136 | error.value = 'NFC 启动失败: ' + (err.errMsg || JSON.stringify(err)); | 147 | error.value = 'NFC 启动失败: ' + (err.errMsg || JSON.stringify(err)); |
| 137 | - showMockButton.value = true; | ||
| 138 | } | 148 | } |
| 139 | status.value = '启动失败'; | 149 | status.value = '启动失败'; |
| 140 | } | 150 | } |
| ... | @@ -157,51 +167,9 @@ const startScan = async () => { | ... | @@ -157,51 +167,9 @@ const startScan = async () => { |
| 157 | debugInfo.value = `${debugInfo.value}\n\ngetNFCAdapter error:\n${e && (e.errMsg || e.message || JSON.stringify(e))}`; | 167 | debugInfo.value = `${debugInfo.value}\n\ngetNFCAdapter error:\n${e && (e.errMsg || e.message || JSON.stringify(e))}`; |
| 158 | error.value = 'NFC 初始化失败(可能是设备/系统不支持,或不在可用环境)'; | 168 | error.value = 'NFC 初始化失败(可能是设备/系统不支持,或不在可用环境)'; |
| 159 | status.value = '错误'; | 169 | status.value = '错误'; |
| 160 | - showMockButton.value = true; | ||
| 161 | } | 170 | } |
| 162 | }; | 171 | }; |
| 163 | 172 | ||
| 164 | -const mockScan = () => { | ||
| 165 | - status.value = '模拟扫描成功'; | ||
| 166 | - error.value = ''; | ||
| 167 | - | ||
| 168 | - // 构造模拟数据 | ||
| 169 | - // 模拟一个包含 Text Record 的 NDEF 消息 | ||
| 170 | - const text = 'Hello NFC!'; | ||
| 171 | - const langCode = 'en'; | ||
| 172 | - const payload = new Uint8Array(1 + langCode.length + text.length); | ||
| 173 | - payload[0] = langCode.length; // Status byte (simplified) | ||
| 174 | - // 写入 lang code | ||
| 175 | - for (let i = 0; i < langCode.length; i++) { | ||
| 176 | - payload[1 + i] = langCode.charCodeAt(i); | ||
| 177 | - } | ||
| 178 | - // 写入 text | ||
| 179 | - for (let i = 0; i < text.length; i++) { | ||
| 180 | - payload[1 + langCode.length + i] = text.charCodeAt(i); | ||
| 181 | - } | ||
| 182 | - | ||
| 183 | - // 模拟 type 'T' | ||
| 184 | - const type = new Uint8Array([84]); // 'T' | ||
| 185 | - | ||
| 186 | - const mockRes = { | ||
| 187 | - id: new Uint8Array([0x04, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0]).buffer, | ||
| 188 | - techs: ['NFC-A', 'NDEF'], | ||
| 189 | - messages: [ | ||
| 190 | - { | ||
| 191 | - records: [ | ||
| 192 | - { | ||
| 193 | - type: type.buffer, | ||
| 194 | - payload: payload.buffer, | ||
| 195 | - tnf: 1 | ||
| 196 | - } | ||
| 197 | - ] | ||
| 198 | - } | ||
| 199 | - ] | ||
| 200 | - }; | ||
| 201 | - | ||
| 202 | - handleNfcMessage(mockRes); | ||
| 203 | -}; | ||
| 204 | - | ||
| 205 | const stopScan = () => { | 173 | const stopScan = () => { |
| 206 | if (nfcAdapter) { | 174 | if (nfcAdapter) { |
| 207 | nfcAdapter.stopDiscovery({ | 175 | nfcAdapter.stopDiscovery({ |
| ... | @@ -299,7 +267,7 @@ const handleNfcMessage = (res) => { | ... | @@ -299,7 +267,7 @@ const handleNfcMessage = (res) => { |
| 299 | content += '\n(无 NDEF 消息)\n'; | 267 | content += '\n(无 NDEF 消息)\n'; |
| 300 | } | 268 | } |
| 301 | 269 | ||
| 302 | - result.value = content; | 270 | + result.value = `--- 原始数据 ---\n${safe_stringify(res)}\n\n--- 解析结果 ---\n${content}`; |
| 303 | }; | 271 | }; |
| 304 | 272 | ||
| 305 | onUnmounted(() => { | 273 | onUnmounted(() => { |
| ... | @@ -368,6 +336,7 @@ onUnmounted(() => { | ... | @@ -368,6 +336,7 @@ onUnmounted(() => { |
| 368 | background: #fff; | 336 | background: #fff; |
| 369 | border-radius: 24rpx; | 337 | border-radius: 24rpx; |
| 370 | padding: 40rpx; | 338 | padding: 40rpx; |
| 339 | + margin: 10rpx auto; | ||
| 371 | box-shadow: 0 4rpx 24rpx rgba(0, 0, 0, 0.05); | 340 | box-shadow: 0 4rpx 24rpx rgba(0, 0, 0, 0.05); |
| 372 | 341 | ||
| 373 | .result-header { | 342 | .result-header { | ... | ... |
-
Please register or login to post a comment