Просмотр исходного кода

feat(nav): 添加扫码调试日志选项并优化扫码结果处理

- 在 custom-nav 组件中新增 debugScan 属性用于控制扫码成功日志输出
- 将各页面中的扫码结果处理逻辑统一抽离至 src/utils/qr.ts 工具函数
- public 首页移除扫码按钮,保留通用处理逻辑供其他页面复用
- 扫码结果处理增加异常捕获和错误提示机制
- 各页面引入新的 handleQrScanResult 工具函数替代原有重复代码
mcbaiyun 1 месяц назад
Родитель
Сommit
a7967e35a0

+ 5 - 2
src/components/custom-nav.vue

@@ -33,12 +33,15 @@ interface Props {
   title: string
   // leftType: 'back' | 'home' | 'none' | 'scan',默认 'back'
   leftType?: 'back' | 'home' | 'none' | 'scan'
+  // 调试时输出扫码成功日志,默认 false
+  debugScan?: boolean
   opacity?: number
 }
 
 const props = withDefaults(defineProps<Props>(), {
   leftType: 'back',
-  opacity: 0.9
+  opacity: 0.9,
+  debugScan: false
 })
 
 const leftTypeComputed = computed(() => props.leftType || 'back')
@@ -75,7 +78,7 @@ const handleLeft = () => {
       uni.scanCode({
         onlyFromCamera: false,
         success: (res: any) => {
-          console.log('[custom-nav] scan success', res)
+          if (props.debugScan) console.log('[custom-nav] scan success', res)
           emit('scan', res)
         },
         fail: (err: any) => {

+ 2 - 7
src/pages/doctor/index/index.vue

@@ -99,6 +99,7 @@ import { onShow } from '@dcloudio/uni-app'
 import CustomNav from '@/components/custom-nav.vue'
 import TabBar from '@/components/tab-bar.vue'
 import { fetchUserInfo as fetchUserInfoApi } from '@/api/user'
+import { handleQrScanResult } from '@/utils/qr'
 
 const user = ref<{ avatar?: string; nickname?: string; title?: string }>({})
 
@@ -255,13 +256,7 @@ onShow(() => {
 })
 
 function handleScan(res: any) {
-  console.log('[index] scan result', res)
-  const resultText = res?.result || ''
-  if (resultText) {
-    uni.showToast({ title: String(resultText), icon: 'none', duration: 2000 })
-  } else {
-    uni.showToast({ title: '未识别到有效内容', icon: 'none' })
-  }
+  return handleQrScanResult(res)
 }
 
 function onItemClick(type: string) {

+ 2 - 7
src/pages/patient-family/index/index.vue

@@ -119,6 +119,7 @@ import { onShow } from '@dcloudio/uni-app'
 import CustomNav from '@/components/custom-nav.vue'
 import { fetchUserInfo as fetchUserInfoApi } from '@/api/user'
 import TabBar from '@/components/tab-bar.vue'
+import { handleQrScanResult } from '@/utils/qr'
 
 const user = ref<{ avatar?: string; nickname?: string }>({})
 
@@ -276,13 +277,7 @@ onShow(() => {
 })
 
 function handleScan(res: any) {
-  console.log('[family-index] scan result', res)
-  const resultText = res?.result || ''
-  if (resultText) {
-    uni.showToast({ title: String(resultText), icon: 'none', duration: 2000 })
-  } else {
-    uni.showToast({ title: '未识别到有效内容', icon: 'none' })
-  }
+  return handleQrScanResult(res)
 }
 
 function onItemClick(type: string) {

+ 2 - 7
src/pages/patient/index/index.vue

@@ -93,6 +93,7 @@ import { ref, computed } from 'vue'
 import { onShow } from '@dcloudio/uni-app'
 import CustomNav from '@/components/custom-nav.vue'
 import TabBar from '@/components/tab-bar.vue'
+import { handleQrScanResult } from '@/utils/qr'
 import { fetchUserInfo as fetchUserInfoApi, downloadAvatar as downloadAvatarApi } from '@/api/user'
 
 const user = ref<{ avatar?: string; nickname?: string; age?: number }>({})
@@ -205,13 +206,7 @@ onShow(() => {
 })
 
 function handleScan(res: any) {
-  console.log('[index] scan result', res)
-  const resultText = res?.result || ''
-  if (resultText) {
-    uni.showToast({ title: String(resultText), icon: 'none', duration: 2000 })
-  } else {
-    uni.showToast({ title: '未识别到有效内容', icon: 'none' })
-  }
+  return handleQrScanResult(res)
 }
 
 function onItemClick(type: string) {

+ 2 - 10
src/pages/public/index/index.vue

@@ -1,5 +1,5 @@
 <template>
-  <CustomNav title="首页" leftType="scan" @scan="handleScan" />
+  <CustomNav title="首页" leftType="none" />
   <view class="content">
     <!-- 原始页面已移至此文件,保留以备参考 -->
     <swiper class="banner-swiper" :indicator-dots="true" :autoplay="true" :interval="3000" :circular="true">
@@ -57,15 +57,7 @@ onShow(() => {
   }
 })
 
-function handleScan(res: any) {
-  console.log('[index] scan result', res)
-  const resultText = res?.result || ''
-  if (resultText) {
-    uni.showToast({ title: String(resultText), icon: 'none', duration: 2000 })
-  } else {
-    uni.showToast({ title: '未识别到有效内容', icon: 'none' })
-  }
-}
+// public 首页已取消扫码按钮,保留处理逻辑在公共工具 `src/utils/qr.ts` 以便其他页面复用
 </script>
 
 <style>

+ 17 - 0
src/utils/qr.ts

@@ -0,0 +1,17 @@
+export function handleQrScanResult(res: any) {
+  try {
+    console.log('[qr] scan result', res)
+    const resultText = res?.result || ''
+    if (resultText) {
+      uni.showToast({ title: String(resultText), icon: 'none', duration: 2000 })
+      return resultText
+    } else {
+      uni.showToast({ title: '未识别到有效内容', icon: 'none' })
+      return ''
+    }
+  } catch (e) {
+    console.error('handleQrScanResult error', e)
+    try { uni.showToast({ title: '扫码处理失败', icon: 'none' }) } catch (err) {}
+    return ''
+  }
+}