diff --git a/pages/login/login.vue b/pages/login/login.vue index 1efa9b2..c2fffce 100644 --- a/pages/login/login.vue +++ b/pages/login/login.vue @@ -186,6 +186,22 @@ doLogin(); }; + const parseScanIdentifier = (resultStr) => { + const scanValue = (resultStr || '').trim(); + if (!scanValue) return ''; + + const barcodeMatch = scanValue.match(/^[A-Z0-9_]+,(.+)$/); + if (barcodeMatch) return barcodeMatch[1].trim(); + + if (scanValue.includes('?')) { + const params = new URLSearchParams(scanValue.split('?')[1]); + return params.get('cardNum') || params.get('identifier') || ''; + } + + const match = scanValue.match(/=([^=]+)$/); + return match ? match[1].trim() : scanValue; + }; + const handleScanLogin = async () => { // #ifdef H5 try { @@ -199,24 +215,13 @@ const resultStr = await wxScan(); console.log('扫码结果:', resultStr); - let value = ''; - - // 解析二维码内容 - if (resultStr.includes('?')) { - const params = new URLSearchParams(resultStr.split('?')[1]); - value = params.get('cardNum') || ''; - } - - if (!value) { - const match = resultStr.match(/=([^=]+)$/); - value = match ? match[1] : ''; - } + const value = parseScanIdentifier(resultStr); if (value) { identifier.value = value; handleLogin(); } else { - uni.showToast({ title: '无效的二维码', icon: 'none' }); + uni.showToast({ title: '无效的扫码内容', icon: 'none' }); } } catch (err) { console.error('扫码错误:', err);