From 2ab108ebf381e9478a6621cec89f45900db904fb Mon Sep 17 00:00:00 2001 From: lynn Date: Wed, 7 May 2025 10:19:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90APP=E7=9B=98=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/index/index.vue | 102 +++++++++++++----- unpackage/dist/dev/app-plus/app-service.js | 80 ++++++++------ .../dist/dev/app-plus/pages/index/index.css | 10 ++ 3 files changed, 129 insertions(+), 63 deletions(-) diff --git a/pages/index/index.vue b/pages/index/index.vue index 7cf614e..7eb49dc 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -17,6 +17,11 @@ + +
+
+ +
@@ -45,7 +50,10 @@ status: 'in-progress', statusText: '进行中' } - ] + ], + showH5Scan: false, + html5QrCode: null, + scanType: '' // 'inventory' or 'view' } }, onLoad() { @@ -59,32 +67,42 @@ const day = String(now.getDate()).padStart(2, '0') this.currentDate = `${year}-${month}-${day}` }, - scanInventory() { - uni.scanCode({ - success: (res) => { - console.log("url:", res.result); - let url = res.result; - let id = ''; - // 用正则提取 id=xxx - const match = url.match(/[?&]id=([^&]+)/); - if (match) { - id = match[1]; - } - console.log("id:", id); - if (!id) { - uni.showToast({ title: '二维码无效', icon: 'none' }); - return; - } - uni.navigateTo({ - url: `/pages/inventory/inventory?code=${encodeURIComponent(id)}` - }); - }, - fail: () => { - uni.showToast({ title: '扫码失败', icon: 'none' }); + handleScan(type) { + this.scanType = type; + // #ifdef H5 + this.showH5Scan = true; + this.$nextTick(() => { + if (!window.Html5Qrcode) { + uni.showToast({ title: '扫码库未加载', icon: 'none' }); + return; } + this.html5QrCode = new window.Html5Qrcode("reader"); + this.html5QrCode.start( + { facingMode: "environment" }, + { fps: 10, qrbox: 250 }, + qrCodeMessage => { + this.closeH5Scan(); + let url = qrCodeMessage; + let id = ''; + const match = url.match(/[?&]id=([^&]+)/); + if (match) { + id = match[1]; + } + if (!id) { + uni.showToast({ title: '二维码无效', icon: 'none' }); + return; + } + if (this.scanType === 'inventory') { + uni.navigateTo({ url: `/pages/inventory/inventory?code=${encodeURIComponent(id)}` }); + } else { + uni.navigateTo({ url: `/pages/inventory/inventory?code=${encodeURIComponent(id)}&view=1` }); + } + }, + errorMessage => {} + ); }); - }, - scanView() { + // #endif + // #ifndef H5 uni.scanCode({ success: (res) => { let url = res.result; @@ -97,14 +115,31 @@ uni.showToast({ title: '二维码无效', icon: 'none' }); return; } - uni.navigateTo({ - url: `/pages/inventory/inventory?code=${encodeURIComponent(id)}&view=1` - }); + if (type === 'inventory') { + uni.navigateTo({ url: `/pages/inventory/inventory?code=${encodeURIComponent(id)}` }); + } else { + uni.navigateTo({ url: `/pages/inventory/inventory?code=${encodeURIComponent(id)}&view=1` }); + } }, fail: () => { uni.showToast({ title: '扫码失败', icon: 'none' }); } }); + // #endif + }, + scanInventory() { + this.handleScan('inventory'); + }, + scanView() { + this.handleScan('view'); + }, + closeH5Scan() { + this.showH5Scan = false; + if (this.html5QrCode) { + this.html5QrCode.stop().then(() => { + this.html5QrCode.clear(); + }); + } }, goTaskDetail(item) { uni.navigateTo({ @@ -257,4 +292,15 @@ background-color: #e8f5e9; color: #4caf50; } + + .h5-scan-modal { + position: fixed; + left: 0; top: 0; right: 0; bottom: 0; + background: rgba(0,0,0,0.6); + z-index: 9999; + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + } diff --git a/unpackage/dist/dev/app-plus/app-service.js b/unpackage/dist/dev/app-plus/app-service.js index 3aa620f..3ef77c5 100644 --- a/unpackage/dist/dev/app-plus/app-service.js +++ b/unpackage/dist/dev/app-plus/app-service.js @@ -31,13 +31,6 @@ if (uni.restoreGlobal) { } (function(vue) { "use strict"; - function formatAppLog(type, filename, ...args) { - if (uni.__log__) { - uni.__log__(type, filename, ...args); - } else { - console[type].apply(console, [...args, filename]); - } - } const _export_sfc = (sfc, props) => { const target = sfc.__vccOpts || sfc; for (const [key, val] of props) { @@ -68,7 +61,11 @@ if (uni.restoreGlobal) { status: "in-progress", statusText: "进行中" } - ] + ], + showH5Scan: false, + html5QrCode: null, + scanType: "" + // 'inventory' or 'view' }; }, onLoad() { @@ -82,51 +79,44 @@ if (uni.restoreGlobal) { const day = String(now.getDate()).padStart(2, "0"); this.currentDate = `${year}-${month}-${day}`; }, - scanInventory() { + handleScan(type) { + this.scanType = type; uni.scanCode({ success: (res) => { - formatAppLog("log", "at pages/index/index.vue:65", "url:", res.result); let url = res.result; let id = ""; const match = url.match(/[?&]id=([^&]+)/); if (match) { id = match[1]; } - formatAppLog("log", "at pages/index/index.vue:73", "id:", id); if (!id) { uni.showToast({ title: "二维码无效", icon: "none" }); return; } - uni.navigateTo({ - url: `/pages/inventory/inventory?code=${encodeURIComponent(id)}` - }); + if (type === "inventory") { + uni.navigateTo({ url: `/pages/inventory/inventory?code=${encodeURIComponent(id)}` }); + } else { + uni.navigateTo({ url: `/pages/inventory/inventory?code=${encodeURIComponent(id)}&view=1` }); + } }, fail: () => { uni.showToast({ title: "扫码失败", icon: "none" }); } }); }, + scanInventory() { + this.handleScan("inventory"); + }, scanView() { - uni.scanCode({ - success: (res) => { - let url = res.result; - let id = ""; - const match = url.match(/[?&]id=([^&]+)/); - if (match) { - id = match[1]; - } - if (!id) { - uni.showToast({ title: "二维码无效", icon: "none" }); - return; - } - uni.navigateTo({ - url: `/pages/inventory/inventory?code=${encodeURIComponent(id)}&view=1` - }); - }, - fail: () => { - uni.showToast({ title: "扫码失败", icon: "none" }); - } - }); + this.handleScan("view"); + }, + closeH5Scan() { + this.showH5Scan = false; + if (this.html5QrCode) { + this.html5QrCode.stop().then(() => { + this.html5QrCode.clear(); + }); + } }, goTaskDetail(item) { uni.navigateTo({ @@ -191,11 +181,31 @@ if (uni.restoreGlobal) { /* KEYED_FRAGMENT */ )) ]) - ]) + ]), + vue.createCommentVNode(" H5扫码弹窗 "), + $data.showH5Scan ? (vue.openBlock(), vue.createElementBlock("div", { + key: 0, + class: "h5-scan-modal" + }, [ + vue.createElementVNode("div", { + id: "reader", + style: { "width": "300px", "height": "300px", "margin": "0 auto" } + }), + vue.createElementVNode("button", { + onClick: _cache[2] || (_cache[2] = (...args) => $options.closeH5Scan && $options.closeH5Scan(...args)) + }, "关闭") + ])) : vue.createCommentVNode("v-if", true) ]) ]); } const PagesIndexIndex = /* @__PURE__ */ _export_sfc(_sfc_main$5, [["render", _sfc_render$4], ["__file", "D:/Lynn/Langye/stocktaking/pages/index/index.vue"]]); + function formatAppLog(type, filename, ...args) { + if (uni.__log__) { + uni.__log__(type, filename, ...args); + } else { + console[type].apply(console, [...args, filename]); + } + } const BASE_API = "http://192.168.60.99:8004/"; const config = { BASE_API diff --git a/unpackage/dist/dev/app-plus/pages/index/index.css b/unpackage/dist/dev/app-plus/pages/index/index.css index 272a43a..3a0a6eb 100644 --- a/unpackage/dist/dev/app-plus/pages/index/index.css +++ b/unpackage/dist/dev/app-plus/pages/index/index.css @@ -120,4 +120,14 @@ .task-status.in-progress { background-color: #e8f5e9; color: #4caf50; +} +.h5-scan-modal { + position: fixed; + left: 0; top: 0; right: 0; bottom: 0; + background: rgba(0,0,0,0.6); + z-index: 9999; + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; }