From 79fbfd24cf7ec45ed32a73162fba5b14c597f14c Mon Sep 17 00:00:00 2001 From: Rizumu Ayaka Date: Fri, 11 Oct 2024 02:07:57 +0800 Subject: [PATCH] feat: add scanner url to qrcode --- app/components/Generate.vue | 4 +- app/components/InputCheckbox.vue | 10 +++++ app/components/InputFile.vue | 2 +- app/components/Scan.vue | 17 +++++--- app/pages/index.vue | 67 +++++++++++++++++++++++--------- cspell.config.yaml | 1 + nuxt.config.ts | 6 +++ 7 files changed, 81 insertions(+), 26 deletions(-) create mode 100644 app/components/InputCheckbox.vue diff --git a/app/components/Generate.vue b/app/components/Generate.vue index c9c4db6..e1eab39 100644 --- a/app/components/Generate.vue +++ b/app/components/Generate.vue @@ -10,9 +10,11 @@ const props = withDefaults(defineProps<{ contentType?: string maxScansPerSecond: number sliceSize: number + prefix?: string }>(), { maxScansPerSecond: 20, sliceSize: 1000, + prefix: '', }) const count = ref(0) @@ -36,7 +38,7 @@ onMounted(() => { block.value = data const binary = blockToBinary(data) const str = fromUint8Array(binary) - svg.value = renderSVG(str, { border: 5 }) + svg.value = renderSVG(props.prefix + str, { border: 5 }) const now = performance.now() renderTime.value = now - frame frame = now diff --git a/app/components/InputCheckbox.vue b/app/components/InputCheckbox.vue new file mode 100644 index 0000000..262c0f0 --- /dev/null +++ b/app/components/InputCheckbox.vue @@ -0,0 +1,10 @@ + + + diff --git a/app/components/InputFile.vue b/app/components/InputFile.vue index f244cf5..29860e7 100644 --- a/app/components/InputFile.vue +++ b/app/components/InputFile.vue @@ -19,7 +19,7 @@ function onFileChange(e: Event) { text="hover:neutral-700 active:neutral-500 dark:hover:neutral-300 dark:active:neutral-400" flex="~" transition="all ease-in-out" - cursor-pointer items-center justify-center overflow-hidden rounded-md duration-300 + cursor-pointer items-center justify-center overflow-hidden rounded-lg duration-300 > | undefined async function scanFrame(result: QrScanner.ScanResult) { cameraSignalStatus.value = CameraSignalStatus.Ready + let strData = result.data - if (!result.data) + if (!strData) return - bytesReceived.value += result.data.length + if (strData.startsWith('http')) { + strData = strData.slice(strData.indexOf('#') + 1) + } + + bytesReceived.value += strData.length totalValidBytesReceived.value = decoderStatus.value.encodedCount * (decoderStatus.value.meta?.data.length ?? 0) // Do not process the same QR code twice - if (cached.has(result.data)) + if (cached.has(strData)) return - if (cached.size && result.data) { + if (cached.size && strData) { shutterCount.value += 1 } error.value = undefined - const binary = toUint8Array(result.data) + const binary = toUint8Array(strData) const data = binaryToBlock(binary) // Data set changed, reset decoder if (checksum.value !== data.checksum) { @@ -286,7 +291,7 @@ async function scanFrame(result: QrScanner.ScanResult) { } await decoderInitPromise - cached.add(result.data) + cached.add(strData) k.value = data.k data.indices.map(i => pluse(i)) diff --git a/app/pages/index.vue b/app/pages/index.vue index 61d6df1..d0a1582 100644 --- a/app/pages/index.vue +++ b/app/pages/index.vue @@ -18,6 +18,15 @@ const filename = ref() const contentType = ref() const data = ref(null) +const route = useRoute() +const router = useRouter() + +onMounted(() => { + if (route.hash.length > 1) { + router.replace('/scan') + } +}) + async function onFileChange(file?: File) { if (!file) { readPhase.value = ReadPhase.Idle @@ -45,12 +54,25 @@ async function onFileChange(file?: File) { data.value = null } } + +const config = useRuntimeConfig() + +const isPrefixed = ref(true) +const prefix = computed(() => { + if (!isPrefixed.value) { + return '' + } + if (config.public.qrcodePrefix) { + return `${config.public.qrcodePrefix}#` + } + return `${location.href}${location.pathname}#` +})