QrScanner.vue 6.08 KB
Newer Older
zhangsan's avatar
1  
zhangsan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
<template>
  <div class="scanner-wrapper">
    <div class="scanner-header">
      <div class="title">扫一扫</div>
      <var-button class="close-btn" round text @click="$emit('close')">
        <var-icon name="close" color="#fff" size="24" />
      </var-button>
    </div>
    
    <div class="container">
      <!-- 扫描区域 -->
      <div class="scanner" id="reader"></div>
      <!-- 扫描动画 -->
      <div class="scanner-frame">
        <div class="scanner-bar"></div>
        <div class="scanner-text">{{ lastResult }}</div>
      </div>
      
      <!-- 扫描结果显示 -->
      <div v-if="scanResult" class="scan-result">
        <div class="result-content">
          {{ scanResult }}
        </div>
      </div>
    </div>

    <!-- 底部关闭按钮 -->
    <div class="bottom-action">
      <var-button
        class="cancel-btn"
        
        @click="$emit('close')"
      >
        取消扫码
      </var-button>
    </div>
  </div>
</template>

<script setup>
import { onMounted, onUnmounted, ref } from 'vue'
import CryptoJS from 'crypto-js'
import { Html5Qrcode } from 'html5-qrcode'

const emit = defineEmits(['close', 'scan-success', 'scan-error'])
let html5QrCode = null
const scanResult = ref('')
const isScanning = ref(false)
const lastResult = ref('')

// 解密函数
function decrypt(encryptedText, key) {
  try {
    const bytes = CryptoJS.AES.decrypt(encryptedText, key)
    return bytes.toString(CryptoJS.enc.Utf8)
  } catch (error) {
    console.error('解密失败:', error)
    return ''
  }
}

function handleScanSuccess(decodedText, decodedResult) {
  try {
    // 更新扫描结果显示
    scanResult.value = decodedText
    lastResult.value = '扫描成功,正在处理...'
    
    // 解密
    const secretKey = 'mySecretKey'
    const decryptedText = decrypt(decodedText, secretKey)
    
    console.log('解密结果:', decryptedText) // 添加调试日志
    
    // 尝试解析参数
    if (decryptedText.startsWith('?')) {
      const params = new URLSearchParams(decryptedText)
      const userId = params.get('userId')
      const name = params.get('name')
      const phone = params.get('phone')
      
      console.log('解析参数:', { userId, name, phone }) // 添加调试日志
      
      if (userId && name && phone) {
        lastResult.value = '解析成功,即将跳转...'
        emit('scan-success', decryptedText)
      } else {
        lastResult.value = '二维码格式不正确'
        emit('scan-error', '无效的二维码格式')
      }
    } else {
      lastResult.value = '二维码格式不正确'
      emit('scan-error', '无效的二维码格式')
    }
  } catch (error) {
    console.error('处理扫描结果失败:', error) // 添加错误日志
    lastResult.value = '二维码解析失败'
    emit('scan-error', '无效的二维码')
  }
}

function startScanner() {
  isScanning.value = true
  lastResult.value = '正在启动摄像头...'
  
  html5QrCode.start(
    { facingMode: "environment" },
    {
      fps: 10,
      qrbox: { width: 250, height: 250 },
    },
    handleScanSuccess,
    (errorMessage) => {
      // 更新扫描状态
      lastResult.value = '请将二维码对准扫描框'
    }
  ).catch((err) => {
    isScanning.value = false
    lastResult.value = '摄像头启动失败'
    emit('scan-error', '无法访问摄像头,请确保已授权')
  })
}

function initializeScanner() {
  lastResult.value = '正在检测摄像头...'
  
  Html5Qrcode.getCameras()
    .then((devices) => {
      if (devices && devices.length) {
        html5QrCode = new Html5Qrcode("reader")
        startScanner()
      } else {
        lastResult.value = '没有找到可用的摄像头'
        emit('scan-error', '没有检测到摄像头')
      }
    })
    .catch((err) => {
      lastResult.value = '请授予相机访问权限'
      emit('scan-error', '请授予相机访问权限')
    })
}

onMounted(() => {
  setTimeout(initializeScanner, 500)
})

onUnmounted(() => {
  if (html5QrCode && isScanning.value) {
    html5QrCode.stop().catch(console.error)
  }
})
</script>

<style scoped>
.scanner-wrapper {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: #000;
  z-index: 999;
}

.scanner-header {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  padding: 40px 16px 16px;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.title {
  color: #fff;
  font-size: 18px;
  font-weight: 500;
}

.close-btn {
  position: absolute;
  right: 16px;
  top: 36px;
}

.container {
  position: relative;
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
}

.scanner {
  width: 100%;
  height: 100%;
}

.scanner-frame {
  position: absolute;
  width: 250px;
  height: 250px;
  border: 4px solid rgba(255, 255, 255, 0.8);
  border-radius: 16px;
  background-color: transparent;
  box-shadow: 0 0 0 100vmax rgba(0, 0, 0, 0.8);
  overflow: hidden;
}

.scanner-text {
  position: absolute;
  bottom: -40px;
  width: 100%;
  text-align: center;
  color: #fff;
  font-size: 14px;
  padding: 8px;
  background: rgba(0, 0, 0, 0.5);
  border-radius: 4px;
}

.scanner-bar {
  position: absolute;
  top: 0;
  width: 100%;
  height: 2px;
  background: linear-gradient(to right, transparent, #fff, transparent);
  animation: scanAnimation 4s infinite linear;
  box-shadow: 0 0 8px rgba(255, 255, 255, 0.6);
}

@keyframes scanAnimation {
  0% {
    top: 0;
  }
  50% {
    top: 50%;
  }
  100% {
    top: 100%;
  }
}

.bottom-action {
  position: absolute;
  bottom: 40px;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  padding: 0 20px;
}

.cancel-btn {
  width: 140px;
  height: 44px;
  background: rgba(255, 255, 255, 0.9) !important;
  color: #333 !important;
  font-size: 16px;
  border: none;
  backdrop-filter: blur(10px);
}

.scan-result {
  position: absolute;
  bottom: 120px;
  left: 20px;
  right: 20px;
  background: rgba(0, 0, 0, 0.7);
  padding: 12px;
  border-radius: 8px;
  
  .result-content {
    color: #fff;
    font-size: 14px;
    word-break: break-all;
    text-align: center;
    max-height: 60px;
    overflow-y: auto;
  }
}
</style>