diff --git a/track-uniapp/src/pages/scan/index.vue b/track-uniapp/src/pages/scan/index.vue index 0d56add..d0469ad 100644 --- a/track-uniapp/src/pages/scan/index.vue +++ b/track-uniapp/src/pages/scan/index.vue @@ -1,30 +1,50 @@ @@ -58,9 +78,12 @@ export default { } }, handleSearch() { this.doQuery(this.serialNumber.trim()); }, - handleScanCode() { + + // 📷 全屏相机扫码 + handleScanCamera() { uni.scanCode({ - onlyFromCamera: true, scanType: ["qrCode", "barCode"], + onlyFromCamera: true, + scanType: ["qrCode", "barCode"], success: (res) => { const sn = (res.result || "").replace(/[^a-zA-Z0-9]/g, "").slice(0, 16); this.doQuery(sn); @@ -72,27 +95,99 @@ export default { }, }); }, + + // 🖼️ 从相册识别二维码 + handleScanAlbum() { + uni.chooseImage({ + count: 1, + sizeType: ["original", "compressed"], + sourceType: ["album"], + success: (chooseRes) => { + const path = chooseRes.tempFilePaths[0]; + if (!path) return; + // 使用 uni.getImageInfo 获取图片后,再调用 scanCode 的 base64 模式 + // 或者直接用 #ifdef 编译条件 + // #ifdef APP-PLUS + this.decodeQRFromImage(path); + // #endif + // #ifndef APP-PLUS + uni.scanCode({ + onlyFromCamera: false, + scanType: ["qrCode"], + success: (scanRes) => { + const sn = (scanRes.result || "").replace(/[^a-zA-Z0-9]/g, "").slice(0, 16); + this.doQuery(sn); + }, + fail: () => { + uni.showToast({ title: "未识别到二维码", icon: "none" }); + }, + }); + // #endif + }, + fail: (err) => { + if (!err.errMsg || !err.errMsg.includes("cancel")) { + uni.showToast({ title: "选择失败", icon: "none" }); + } + }, + }); + }, + + // APP-PLUS: 从图片解码二维码 + decodeQRFromImage(path) { + // #ifdef APP-PLUS + plus.barcode.scan(path, (type, result) => { + const sn = (result || "").replace(/[^a-zA-Z0-9]/g, "").slice(0, 16); + this.doQuery(sn); + }, (err) => { + uni.showToast({ title: "未识别到二维码", icon: "none" }); + }, { + filters: ["QRCODE"], + scanBoth: true, + }); + // #endif + }, }, };