diff --git a/index.html b/index.html index 40d9164..95303c1 100644 --- a/index.html +++ b/index.html @@ -5,6 +5,7 @@ +
diff --git a/package-lock.json b/package-lock.json index 09d0361..78cd65b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,8 +10,6 @@ "@dcloudio/uni-h5": "3.0.0-4080720251210001", "clipboard": "^2.0.11", "dayjs": "^1.11.19", - "html5-qrcode": "^2.3.8", - "jsqr": "^1.4.0", "uview-plus": "^3.6.29", "vue": "^3.4.0" }, @@ -4870,12 +4868,6 @@ "node": ">= 0.4" } }, - "node_modules/html5-qrcode": { - "version": "2.3.8", - "resolved": "https://registry.npmmirror.com/html5-qrcode/-/html5-qrcode-2.3.8.tgz", - "integrity": "sha512-jsr4vafJhwoLVEDW3n1KvPnCCXWaQfRng0/EEYk1vNcQGcG/htAdhJX0be8YyqMoSz7+hZvOZSTAepsabiuhiQ==", - "license": "Apache-2.0" - }, "node_modules/http-errors": { "version": "2.0.0", "resolved": "https://registry.npmmirror.com/http-errors/-/http-errors-2.0.0.tgz", @@ -5104,12 +5096,6 @@ "graceful-fs": "^4.1.6" } }, - "node_modules/jsqr": { - "version": "1.4.0", - "resolved": "https://registry.npmmirror.com/jsqr/-/jsqr-1.4.0.tgz", - "integrity": "sha512-dxLob7q65Xg2DvstYkRpkYtmKm2sPJ9oFhrhmudT1dZvNFFTlroai3AWSpLey/w5vMcLBXRgOJsbXpdN9HzU/A==", - "license": "Apache-2.0" - }, "node_modules/lcid": { "version": "3.1.1", "resolved": "https://registry.npmmirror.com/lcid/-/lcid-3.1.1.tgz", diff --git a/package.json b/package.json index 12e3dc6..72c8d1e 100644 --- a/package.json +++ b/package.json @@ -18,8 +18,6 @@ "@dcloudio/uni-h5": "3.0.0-4080720251210001", "clipboard": "^2.0.11", "dayjs": "^1.11.19", - "html5-qrcode": "^2.3.8", - "jsqr": "^1.4.0", "uview-plus": "^3.6.29", "vue": "^3.4.0" } diff --git a/pages/login/login.vue b/pages/login/login.vue index 6cb407a..378aefe 100644 --- a/pages/login/login.vue +++ b/pages/login/login.vue @@ -48,35 +48,17 @@ - - - - - 登录即表示同意 - 《用户协议》 - - 《隐私政策》 + - - - - - - - 扫一扫 - × - - - - - - 点击下方按钮选择相机拍照 - + + + 登录即表示同意 + 《用户协议》 + + 《隐私政策》 - 将二维码放入框内即可自动扫描 @@ -87,7 +69,7 @@ import { authApi } from '@/api/index.js'; import { useUserStore } from '@/store/index.js'; import { APP_ID, USE_WECHAT_AUTH } from '@/utils/env.js'; - import jsQR from 'jsqr'; + import { initWxSdk } from '@/utils/wxsdk.js'; const userStore = useUserStore(); @@ -105,7 +87,6 @@ const agreed = ref(true); const inputFocus = ref(false); const showError = ref(false); - const showScanner = ref(false); const getCode = () => { const params = new URLSearchParams(window.location.search); @@ -126,12 +107,10 @@ const clearInput = () => { identifier.value = ''; showError.value = false; - // 保持焦点状态 inputFocus.value = true; }; const handleBlur = () => { - // 延迟失焦,给清除按钮点击留出时间 setTimeout(() => { inputFocus.value = false; }, 200); @@ -146,46 +125,33 @@ }; const handleScanLogin = async () => { - showScanner.value = true; - }; - - const onFileSelected = async (e) => { - const file = e.target.files[0]; - if (!file) return; - - const reader = new FileReader(); - reader.onload = (event) => { - const img = new Image(); - img.onload = () => { - const canvas = document.createElement('canvas'); - canvas.width = img.width; - canvas.height = img.height; - const ctx = canvas.getContext('2d'); - ctx.drawImage(img, 0, 0); - const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); - const code = jsQR(imageData.data, imageData.width, imageData.height); - - if (code) { - const match = code.data.match(/=([^=]+)$/); - if (match && match[1]) { - identifier.value = match[1]; - closeScanner(); - handleLogin(); - } else { - uni.showToast({ title: '无效的二维码', icon: 'none' }); - } + // #ifdef H5 + const ready = await initWxSdk(); + if (!ready) { + uni.showToast({ title: '扫一扫功能暂不可用', icon: 'none' }); + return; + } + wx.scanQRCode({ + needResult: 1, + scanType: ['qrCode', 'barCode'], + success: (res) => { + const result = res.resultStr; + const match = result.match(/=([^=]+)$/); + if (match && match[1]) { + identifier.value = match[1]; + handleLogin(); } else { - uni.showToast({ title: '无法识别二维码', icon: 'none' }); + uni.showToast({ title: '无效的二维码', icon: 'none' }); } - }; - img.src = event.target.result; - }; - reader.readAsDataURL(file); - }; - - const closeScanner = async () => { - showScanner.value = false; - document.getElementById('file-input').value = ''; + }, + fail: () => { + uni.showToast({ title: '扫码失败', icon: 'none' }); + } + }); + // #endif + // #ifndef H5 + uni.showToast({ title: '请在微信中打开', icon: 'none' }); + // #endif }; const getPathDeviceId = () => { @@ -209,7 +175,6 @@ userStore.setToken(loginData.token); uni.setStorageSync('identifier', userStore.state.identifier); - // 判断是否需要绑定手机号 if (loginData.need_bind_phone) { uni.redirectTo({ url: '/pages/bind/bind?fromLogin=true' }); } else { @@ -241,44 +206,18 @@ userStore.setToken(loginData.token); uni.setStorageSync('identifier', userStore.state.identifier); - // H5 环境获取微信用户信息 // #ifdef H5 - try { - // 通过 uni.getUserProfile 获取用户信息(需要用户点击授权) - // 注意:这个方法在微信公众号 H5 中不可用,需要通过后端接口获取 - // 这里暂时保存默认信息,实际应该由后端返回用户信息 - if (loginData.user_info) { - userStore.setUserInfo({ - avatar: loginData.user_info.avatar || '', - nickname: loginData.user_info.nickname || '' - }); - } - } catch (err) { - console.log('获取用户信息失败', err); - } - // #endif - - // #ifdef MP-WEIXIN - // 小程序环境可以直接获取用户信息 - try { - const { userInfo } = await uni.getUserProfile({ - desc: '用于完善用户资料' - }); + if (loginData.user_info) { userStore.setUserInfo({ - avatar: userInfo.avatarUrl || '', - nickname: userInfo.nickName || '' + avatar: loginData.user_info.avatar || '', + nickname: loginData.user_info.nickname || '' }); - } catch (err) { - console.log('用户取消授权'); } // #endif - // 判断是否需要绑定手机号 if (loginData.need_bind_phone) { - // 需要绑定手机号,跳转到绑定页面 uni.redirectTo({ url: '/pages/bind/bind?fromLogin=true' }); } else { - // 不需要绑定,直接进入首页 uni.redirectTo({ url: '/pages/index/index' }); } } catch (e) { @@ -286,10 +225,6 @@ loading.value = false; } }; - - onMounted(() => { - handleWechatCallback(); - }); + \ No newline at end of file