摄像头逻辑进行修改,更改分辨率进行快速的读取识别
This commit is contained in:
@ -1,139 +1,150 @@
|
||||
<template>
|
||||
<div class="app-container">
|
||||
<el-card class="box-card">
|
||||
<div class="app-container mobile-optimized">
|
||||
<el-card class="box-card" shadow="never">
|
||||
<template #header>
|
||||
<div class="card-header">
|
||||
<span>批量扫码出库作业台</span>
|
||||
<div class="header-right">
|
||||
<span v-if="cartItems.length > 0" class="summary-text">
|
||||
已选: <span class="num">{{ cartItems.length }}</span> 项 |
|
||||
总金额: <span class="price">¥{{ totalAmount.toFixed(2) }}</span>
|
||||
</span>
|
||||
<el-button type="primary" @click="toggleCamera" style="margin-left: 10px;">
|
||||
{{ showCamera ? '关闭摄像头' : '打开摄像头扫码' }}
|
||||
</el-button>
|
||||
<div class="title-box">
|
||||
<span>批量出库作业</span>
|
||||
<el-tag v-if="cartItems.length > 0" type="warning" size="small" effect="dark">
|
||||
已选 {{ cartItems.length }} 项
|
||||
</el-tag>
|
||||
</div>
|
||||
<div class="header-price" v-if="totalAmount > 0">
|
||||
¥{{ totalAmount.toFixed(2) }}
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<div class="scan-area">
|
||||
<div v-if="showCamera" id="reader" class="camera-box"></div>
|
||||
<div v-if="isHttpAndNotLocal" class="http-warning">
|
||||
注意:当前为 HTTP 环境,摄像头可能无法启动。请使用 HTTPS 或 localhost,或配置浏览器安全白名单。
|
||||
<div class="scan-section">
|
||||
<div v-if="showCamera" class="camera-wrapper">
|
||||
<QrScanner @decode="onScanSuccess" />
|
||||
<div class="scan-overlay">
|
||||
<el-button type="info" size="small" bg text @click="showCamera = false" icon="Close">
|
||||
关闭摄像头
|
||||
</el-button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div v-else class="camera-placeholder" @click="showCamera = true">
|
||||
<el-icon :size="40" color="#409EFF"><CameraFilled /></el-icon>
|
||||
<span class="text">点击开启扫码</span>
|
||||
</div>
|
||||
|
||||
<div class="input-box">
|
||||
<el-input
|
||||
v-model="barcodeInput"
|
||||
placeholder="请连续扫描条码或手动输入后回车"
|
||||
@keyup.enter="handleScan"
|
||||
placeholder="扫描或输入条码回车"
|
||||
@keyup.enter="handleManualInput"
|
||||
clearable
|
||||
ref="barcodeRef"
|
||||
class="scan-input"
|
||||
size="large"
|
||||
>
|
||||
<template #prefix>
|
||||
<el-icon><Scissor /></el-icon>
|
||||
</template>
|
||||
<template #append>
|
||||
<el-button @click="handleScan">添加商品</el-button>
|
||||
<el-button @click="handleManualInput">添加</el-button>
|
||||
</template>
|
||||
</el-input>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="cart-area mt-20" v-if="cartItems.length > 0">
|
||||
<el-table :data="cartItems" border stripe style="width: 100%">
|
||||
<el-table-column prop="name" label="名称" show-overflow-tooltip />
|
||||
<el-table-column prop="spec_model" label="规格" show-overflow-tooltip />
|
||||
<el-table-column prop="sku" label="SKU" width="150" />
|
||||
<el-table-column label="单价" width="120">
|
||||
<template #default="{row}">
|
||||
¥{{ row.price }}
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="库存" width="100">
|
||||
<template #default="{row}">
|
||||
<el-tag :type="row.available_quantity > 0 ? 'success' : 'danger'">{{ row.available_quantity }}</el-tag>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="出库数量" width="160">
|
||||
<template #default="{row}">
|
||||
<el-input-number v-model="row.out_quantity" :min="1" :max="row.available_quantity" size="small" />
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="小计" width="120">
|
||||
<template #default="{row}">
|
||||
<span style="color: #F56C6C; font-weight: bold;">¥{{ (row.price * row.out_quantity).toFixed(2) }}</span>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="操作" width="80" align="center">
|
||||
<template #default="{$index}">
|
||||
<el-button type="danger" icon="Delete" circle size="small" @click="removeFromCart($index)" />
|
||||
</template>
|
||||
</el-table-column>
|
||||
</el-table>
|
||||
</div>
|
||||
<el-empty v-else description="暂无扫描商品,请扫描上方条码进行添加" />
|
||||
<div class="cart-section">
|
||||
<div v-if="cartItems.length > 0">
|
||||
<el-table :data="cartItems" border stripe style="width: 100%">
|
||||
<el-table-column prop="name" label="物品名称" min-width="120" show-overflow-tooltip />
|
||||
<el-table-column prop="spec_model" label="规格" min-width="100" show-overflow-tooltip />
|
||||
<el-table-column prop="sku" label="SKU" width="120" show-overflow-tooltip />
|
||||
|
||||
<div v-if="cartItems.length > 0" class="confirm-area mt-20">
|
||||
<el-divider content-position="left">单据信息</el-divider>
|
||||
<el-table-column label="库存" width="70" align="center">
|
||||
<template #default="{row}">
|
||||
<el-tag :type="row.available_quantity > 0 ? 'success' : 'danger'" size="small">
|
||||
{{ parseFloat(row.available_quantity) }}
|
||||
</el-tag>
|
||||
</template>
|
||||
</el-table-column>
|
||||
|
||||
<el-table-column label="出库数" width="130" align="center">
|
||||
<template #default="{row}">
|
||||
<el-input-number
|
||||
v-model="row.out_quantity"
|
||||
:min="1"
|
||||
:max="parseFloat(row.available_quantity)"
|
||||
size="small"
|
||||
style="width: 100px"
|
||||
/>
|
||||
</template>
|
||||
</el-table-column>
|
||||
|
||||
<el-table-column label="操作" width="60" align="center" fixed="right">
|
||||
<template #default="{$index}">
|
||||
<el-button type="danger" icon="Delete" circle size="small" @click="removeFromCart($index)" />
|
||||
</template>
|
||||
</el-table-column>
|
||||
</el-table>
|
||||
</div>
|
||||
<el-empty v-else description="暂无商品,请扫码添加" :image-size="80" />
|
||||
</div>
|
||||
|
||||
<div v-if="cartItems.length > 0" class="form-section">
|
||||
<el-divider content-position="left">出库单据信息</el-divider>
|
||||
|
||||
<el-form :model="form" ref="formRef" :rules="rules" label-position="top">
|
||||
<el-row :gutter="20">
|
||||
<el-col :span="8">
|
||||
<el-row :gutter="15">
|
||||
<el-col :span="24" :md="8">
|
||||
<el-form-item label="出库类型" prop="outbound_type">
|
||||
<el-select v-model="form.outbound_type" placeholder="请选择" style="width: 100%">
|
||||
<el-select v-model="form.outbound_type" placeholder="请选择类型" style="width: 100%">
|
||||
<el-option label="销售出库" value="SALES" />
|
||||
<el-option label="内部领用" value="USE" />
|
||||
<el-option label="调拨" value="TRANSFER" />
|
||||
<el-option label="调拨出库" value="TRANSFER" />
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
</el-col>
|
||||
<el-col :span="8">
|
||||
<el-form-item label="领用人/客户姓名" prop="consumer_name">
|
||||
|
||||
<el-col :span="24" :md="8">
|
||||
<el-form-item label="领用人/客户" prop="consumer_name">
|
||||
<el-input v-model="form.consumer_name" placeholder="请输入姓名" />
|
||||
</el-form-item>
|
||||
</el-col>
|
||||
<el-col :span="8">
|
||||
<el-form-item label="操作员 (库管)" prop="operator_name">
|
||||
|
||||
<el-col :span="24" :md="8">
|
||||
<el-form-item label="经办人 (库管)" prop="operator_name">
|
||||
<el-select
|
||||
v-model="form.operator_name"
|
||||
filterable
|
||||
allow-create
|
||||
default-first-option
|
||||
placeholder="请选择或输入操作员"
|
||||
placeholder="选择或输入"
|
||||
style="width: 100%"
|
||||
>
|
||||
<el-option
|
||||
v-for="item in operatorOptions"
|
||||
:key="item"
|
||||
:label="item"
|
||||
:value="item"
|
||||
/>
|
||||
<el-option v-for="item in operatorOptions" :key="item" :label="item" :value="item" />
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
</el-col>
|
||||
</el-row>
|
||||
|
||||
<el-form-item label="备注" prop="remark">
|
||||
<el-input v-model="form.remark" type="textarea" />
|
||||
<el-form-item label="备注说明" prop="remark">
|
||||
<el-input v-model="form.remark" type="textarea" :rows="2" placeholder="可选填" />
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item label="电子签名" required>
|
||||
<div class="signature-display-area">
|
||||
<div v-if="signaturePreviewUrl" class="signed-image-box">
|
||||
<img :src="signaturePreviewUrl" alt="电子签名" />
|
||||
<el-button type="text" @click="openSignatureDialog">重新签名</el-button>
|
||||
<el-form-item label="电子签名确认" required>
|
||||
<div class="signature-box" @click="openSignatureDialog">
|
||||
<div v-if="signaturePreviewUrl" class="signed-img">
|
||||
<img :src="signaturePreviewUrl" alt="签名" />
|
||||
<span class="re-sign-tip">点击重签</span>
|
||||
</div>
|
||||
<div v-else class="unsigned-placeholder">
|
||||
<el-icon :size="24"><EditPen /></el-icon>
|
||||
<span>点击此处进行全屏签名</span>
|
||||
</div>
|
||||
<el-button v-else type="primary" plain @click="openSignatureDialog" icon="EditPen">
|
||||
点击进行全屏签名
|
||||
</el-button>
|
||||
</div>
|
||||
</el-form-item>
|
||||
|
||||
<div class="form-actions">
|
||||
<el-button @click="clearAll">清空重置</el-button>
|
||||
<el-button type="primary" size="large" :loading="loading" @click="submitForm">确认批量出库</el-button>
|
||||
<div class="bottom-actions">
|
||||
<el-button @click="clearAll" icon="Refresh">清空</el-button>
|
||||
<el-button type="primary" size="large" :loading="loading" @click="submitForm" icon="Select">
|
||||
确认出库
|
||||
</el-button>
|
||||
</div>
|
||||
</el-form>
|
||||
</div>
|
||||
@ -160,14 +171,15 @@
|
||||
@touchmove="draw"
|
||||
@touchend="stopDrawing"
|
||||
></canvas>
|
||||
<div class="canvas-tip">请在此区域横屏书写</div>
|
||||
</div>
|
||||
|
||||
<div class="signature-sidebar">
|
||||
<div class="sidebar-title">请在此区域书写</div>
|
||||
<div class="sidebar-title">电子签名</div>
|
||||
<div class="sidebar-actions">
|
||||
<el-button type="warning" @click="clearCanvas">重置</el-button>
|
||||
<el-button type="warning" @click="clearCanvas">重写</el-button>
|
||||
<el-button @click="handleSignCancel">取消</el-button>
|
||||
<el-button type="success" class="confirm-btn" @click="handleSignConfirm">确认</el-button>
|
||||
<el-button type="success" class="confirm-btn" @click="handleSignConfirm">确认使用</el-button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -179,28 +191,25 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, reactive, nextTick, onUnmounted, onMounted, computed } from 'vue'
|
||||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||||
import { Html5Qrcode, Html5QrcodeSupportedFormats } from 'html5-qrcode'
|
||||
import { Scissor, EditPen, Delete } from '@element-plus/icons-vue'
|
||||
import { Scissor, EditPen, Delete, CameraFilled, Close, Refresh, Select } from '@element-plus/icons-vue'
|
||||
import QrScanner from '@/components/QrScanner/index.vue'
|
||||
import { getStockByBarcode, submitOutbound, getOutboundList } from '@/api/outbound'
|
||||
import { uploadFile } from '@/api/common/upload'
|
||||
import { useUserStore } from '@/stores/user'
|
||||
|
||||
// --- 状态定义 ---
|
||||
const barcodeInput = ref('')
|
||||
const cartItems = ref<any[]>([]) // 购物车列表
|
||||
const cartItems = ref<any[]>([])
|
||||
const loading = ref(false)
|
||||
const showCamera = ref(false)
|
||||
const html5QrCode = ref<Html5Qrcode | null>(null)
|
||||
const showCamera = ref(false) // ★ 核心修改:默认改为 false
|
||||
const barcodeRef = ref()
|
||||
const formRef = ref()
|
||||
const userStore = useUserStore()
|
||||
|
||||
// 签名相关状态
|
||||
// 签名相关
|
||||
const showSignatureDialog = ref(false)
|
||||
const signaturePreviewUrl = ref('')
|
||||
const signatureFile = ref<File | null>(null)
|
||||
|
||||
// 原生 Canvas 相关
|
||||
const nativeCanvasRef = ref<HTMLCanvasElement | null>(null)
|
||||
const canvasContainerRef = ref<HTMLElement | null>(null)
|
||||
const ctx = ref<CanvasRenderingContext2D | null>(null)
|
||||
@ -222,18 +231,12 @@ const rules = {
|
||||
operator_name: [{ required: true, message: '请指定操作员', trigger: 'change' }]
|
||||
}
|
||||
|
||||
const isHttpAndNotLocal = computed(() => {
|
||||
const isHttps = window.location.protocol === 'https:'
|
||||
const isLocal = ['localhost', '127.0.0.1'].includes(window.location.hostname)
|
||||
return !isHttps && !isLocal
|
||||
})
|
||||
|
||||
// 计算总金额
|
||||
const totalAmount = computed(() => {
|
||||
return cartItems.value.reduce((sum, item) => sum + (item.price * item.out_quantity), 0)
|
||||
})
|
||||
|
||||
// --- 初始化逻辑 ---
|
||||
// --- 初始化 ---
|
||||
onMounted(() => {
|
||||
if (userStore.username) {
|
||||
form.operator_name = userStore.username
|
||||
@ -248,11 +251,8 @@ const loadHistoryOperators = async () => {
|
||||
if (res.data && res.data.items) {
|
||||
const names = new Set<string>()
|
||||
if (userStore.username) names.add(userStore.username)
|
||||
// 注意:现在的列表结构变了,operator_name 在外层
|
||||
res.data.items.forEach((group: any) => {
|
||||
if (group.operator_name) {
|
||||
names.add(group.operator_name)
|
||||
}
|
||||
if (group.operator_name) names.add(group.operator_name)
|
||||
})
|
||||
operatorOptions.value = Array.from(names)
|
||||
}
|
||||
@ -261,56 +261,89 @@ const loadHistoryOperators = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
// --- 扫码逻辑 (加入购物车) ---
|
||||
const handleScan = async () => {
|
||||
// --- 核心扫码逻辑 (适配 QrScanner 组件) ---
|
||||
const onScanSuccess = (code: string) => {
|
||||
if (!code) return
|
||||
const trimCode = code.trim()
|
||||
|
||||
// ★★★ 核心修改:防误触校验 ★★★
|
||||
// 1. 正则校验:只允许 数字、字母、横杠、点
|
||||
// 这样可以屏蔽掉条码解析错误产生的 { } $ # 等乱码
|
||||
const validPattern = /^[A-Za-z0-9\-\.]+$/
|
||||
if (!validPattern.test(trimCode)) {
|
||||
ElMessage.warning(`识别到异常符号,已忽略:${trimCode}`)
|
||||
return
|
||||
}
|
||||
|
||||
// 2. 长度校验:避免误扫到环境中的短数字
|
||||
if (trimCode.length < 3) {
|
||||
ElMessage.warning('扫描结果过短,请对准重试')
|
||||
return
|
||||
}
|
||||
|
||||
// 防抖:防止同一条码连续触发
|
||||
if (loading.value) return
|
||||
|
||||
barcodeInput.value = trimCode
|
||||
handleManualInput() // 复用手动输入逻辑
|
||||
}
|
||||
|
||||
const handleManualInput = async () => {
|
||||
const code = barcodeInput.value.trim()
|
||||
if (!code) return
|
||||
|
||||
try {
|
||||
loading.value = true
|
||||
|
||||
// 1. 检查是否已经在购物车中
|
||||
// 1. 检查购物车重复
|
||||
const existIndex = cartItems.value.findIndex(item => item.barcode === code || item.sku === code)
|
||||
if (existIndex > -1) {
|
||||
// 存在则数量+1
|
||||
const item = cartItems.value[existIndex]
|
||||
if (item.out_quantity < item.available_quantity) {
|
||||
const maxQty = parseFloat(item.available_quantity)
|
||||
|
||||
if (item.out_quantity < maxQty) {
|
||||
item.out_quantity++
|
||||
ElMessage.success(`商品 ${item.name} 数量+1`)
|
||||
ElMessage.success(`数量+1 (当前: ${item.out_quantity})`)
|
||||
if (navigator.vibrate) navigator.vibrate(50)
|
||||
} else {
|
||||
ElMessage.warning(`库存不足 (余: ${item.available_quantity})`)
|
||||
ElMessage.warning(`库存不足 (余: ${maxQty})`)
|
||||
if (navigator.vibrate) navigator.vibrate([100, 50, 100])
|
||||
}
|
||||
barcodeInput.value = ''
|
||||
return
|
||||
}
|
||||
|
||||
// 2. 新商品调用 API
|
||||
// 2. 调用 API 查询
|
||||
const res = await getStockByBarcode(code)
|
||||
if (res.data) {
|
||||
const item = res.data
|
||||
if (item.available_quantity <= 0) {
|
||||
ElMessage.warning(`该商品库存不足或已出库!(当前库存: ${item.available_quantity})`)
|
||||
const availQty = parseFloat(item.available_quantity || 0)
|
||||
|
||||
if (availQty <= 0) {
|
||||
ElMessage.warning(`库存不足或已出库 (余: ${availQty})`)
|
||||
if (navigator.vibrate) navigator.vibrate([100, 50, 100])
|
||||
} else {
|
||||
// 加入购物车,默认出库 1
|
||||
// 加入购物车
|
||||
cartItems.value.push({
|
||||
...item,
|
||||
out_quantity: 1,
|
||||
price: item.price || 0
|
||||
price: parseFloat(item.price || 0)
|
||||
})
|
||||
ElMessage.success('添加成功')
|
||||
ElMessage.success(`添加成功: ${item.name}`)
|
||||
if (navigator.vibrate) navigator.vibrate(100)
|
||||
}
|
||||
barcodeInput.value = ''
|
||||
}
|
||||
} catch (error: any) {
|
||||
if (error.response && error.response.status === 404) {
|
||||
ElMessage.error(`未找到条码 ${barcodeInput.value} 的入库记录`)
|
||||
ElMessage.error(`未找到条码: ${code}`)
|
||||
} else {
|
||||
console.error(error)
|
||||
ElMessage.error('查询出错,请重试')
|
||||
ElMessage.error('查询出错')
|
||||
}
|
||||
if (navigator.vibrate) navigator.vibrate([200, 100, 200])
|
||||
} finally {
|
||||
loading.value = false
|
||||
// 聚焦回输入框
|
||||
// 聚焦输入框,方便连续扫
|
||||
nextTick(() => { barcodeRef.value?.focus() })
|
||||
}
|
||||
}
|
||||
@ -320,25 +353,21 @@ const removeFromCart = (index: number) => {
|
||||
}
|
||||
|
||||
const clearAll = () => {
|
||||
ElMessageBox.confirm('确定清空所有已扫商品和填写信息吗?', '提示', {
|
||||
type: 'warning'
|
||||
}).then(() => {
|
||||
cartItems.value = []
|
||||
form.consumer_name = ''
|
||||
form.remark = ''
|
||||
signatureFile.value = null
|
||||
signaturePreviewUrl.value = ''
|
||||
barcodeInput.value = ''
|
||||
})
|
||||
ElMessageBox.confirm('确定清空所有已选商品吗?', '提示', { type: 'warning' })
|
||||
.then(() => {
|
||||
cartItems.value = []
|
||||
form.consumer_name = ''
|
||||
form.remark = ''
|
||||
signatureFile.value = null
|
||||
signaturePreviewUrl.value = ''
|
||||
barcodeInput.value = ''
|
||||
})
|
||||
}
|
||||
|
||||
// --- 提交逻辑 (批量提交) ---
|
||||
// --- 提交逻辑 ---
|
||||
const submitForm = async () => {
|
||||
if (!formRef.value) return
|
||||
if (cartItems.value.length === 0) {
|
||||
ElMessage.warning('购物车为空,请先扫码')
|
||||
return
|
||||
}
|
||||
if (cartItems.value.length === 0) return ElMessage.warning('请先添加商品')
|
||||
|
||||
await formRef.value.validate(async (valid: boolean) => {
|
||||
if (!valid) return
|
||||
@ -351,11 +380,10 @@ const submitForm = async () => {
|
||||
try {
|
||||
loading.value = true
|
||||
|
||||
// 1. 上传签名
|
||||
// 上传签名
|
||||
const uploadRes = await uploadFile(signatureFile.value)
|
||||
const signatureUrl = uploadRes.data.url
|
||||
|
||||
// 2. 构造 items 数据
|
||||
const itemsPayload = cartItems.value.map(item => ({
|
||||
stock_id: item.id,
|
||||
source_table: item.source_table,
|
||||
@ -365,7 +393,6 @@ const submitForm = async () => {
|
||||
price: item.price
|
||||
}))
|
||||
|
||||
// 3. 提交业务单据
|
||||
await submitOutbound({
|
||||
items: itemsPayload,
|
||||
outbound_type: form.outbound_type,
|
||||
@ -375,160 +402,73 @@ const submitForm = async () => {
|
||||
signature_path: signatureUrl
|
||||
})
|
||||
|
||||
ElMessage.success('批量出库成功')
|
||||
// 清空
|
||||
ElMessage.success('出库成功')
|
||||
// 重置
|
||||
cartItems.value = []
|
||||
form.consumer_name = ''
|
||||
form.remark = ''
|
||||
signatureFile.value = null
|
||||
signaturePreviewUrl.value = ''
|
||||
|
||||
// 刷新操作员列表
|
||||
loadHistoryOperators()
|
||||
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
ElMessage.error('提交失败')
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// --- 摄像头控制 ---
|
||||
const toggleCamera = async () => {
|
||||
if (showCamera.value) {
|
||||
await stopCamera()
|
||||
} else {
|
||||
if (isHttpAndNotLocal.value) {
|
||||
ElMessage.error('浏览器安全限制:摄像头仅支持 HTTPS 或 localhost。请检查地址栏或配置 Chrome Flags。')
|
||||
}
|
||||
showCamera.value = true
|
||||
nextTick(() => {
|
||||
startCamera()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const startCamera = () => {
|
||||
html5QrCode.value = new Html5Qrcode("reader", {
|
||||
formatsToSupport: [ Html5QrcodeSupportedFormats.CODE_128 ],
|
||||
verbose: false
|
||||
})
|
||||
|
||||
html5QrCode.value.start(
|
||||
{ facingMode: "environment" },
|
||||
{
|
||||
fps: 10,
|
||||
qrbox: { width: 250, height: 150 },
|
||||
aspectRatio: 1.0
|
||||
},
|
||||
(decodedText) => {
|
||||
barcodeInput.value = decodedText
|
||||
handleScan()
|
||||
},
|
||||
(errorMessage) => { }
|
||||
).catch(err => {
|
||||
let msg = '无法启动摄像头'
|
||||
if (err.toString().includes('Permission')) {
|
||||
msg = '摄像头权限被拒绝,请检查浏览器设置'
|
||||
} else if (err.toString().includes('Secure Context')) {
|
||||
msg = '摄像头需要 HTTPS 环境'
|
||||
}
|
||||
ElMessage.error(`${msg}: ${err}`)
|
||||
showCamera.value = false
|
||||
})
|
||||
}
|
||||
|
||||
const stopCamera = async () => {
|
||||
if (html5QrCode.value) {
|
||||
try {
|
||||
if (html5QrCode.value.isScanning) {
|
||||
await html5QrCode.value.stop()
|
||||
}
|
||||
html5QrCode.value.clear()
|
||||
showCamera.value = false
|
||||
} catch (err) {
|
||||
showCamera.value = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- 签名核心逻辑 ---
|
||||
const openSignatureDialog = () => {
|
||||
showSignatureDialog.value = true
|
||||
}
|
||||
// --- 签名逻辑 (Canvas) ---
|
||||
const openSignatureDialog = () => { showSignatureDialog.value = true }
|
||||
|
||||
const initCanvas = async () => {
|
||||
// 必须等待 DOM 更新完成,确保 dialog 里的 ref 已经挂载
|
||||
await nextTick()
|
||||
const canvas = nativeCanvasRef.value
|
||||
const container = canvasContainerRef.value
|
||||
|
||||
if (canvas && container) {
|
||||
// 设置物理像素等于显示像素,避免模糊
|
||||
canvas.width = container.clientWidth
|
||||
canvas.height = container.clientHeight
|
||||
|
||||
ctx.value = canvas.getContext('2d')
|
||||
if (ctx.value) {
|
||||
ctx.value.lineWidth = 4
|
||||
ctx.value.lineCap = 'round'
|
||||
ctx.value.lineJoin = 'round'
|
||||
ctx.value.strokeStyle = '#000000'
|
||||
// 填充白色背景,防止生成透明图片
|
||||
ctx.value.fillStyle = '#ffffff'
|
||||
ctx.value.fillRect(0, 0, canvas.width, canvas.height)
|
||||
}
|
||||
} else {
|
||||
console.error('Canvas 初始化失败:无法获取 DOM 元素')
|
||||
}
|
||||
}
|
||||
|
||||
const getPos = (e: MouseEvent | TouchEvent) => {
|
||||
if (!nativeCanvasRef.value) return { x: 0, y: 0 }
|
||||
const rect = nativeCanvasRef.value.getBoundingClientRect()
|
||||
let clientX, clientY
|
||||
if (e.type.startsWith('touch')) {
|
||||
clientX = (e as TouchEvent).touches[0].clientX
|
||||
clientY = (e as TouchEvent).touches[0].clientY
|
||||
} else {
|
||||
clientX = (e as MouseEvent).clientX
|
||||
clientY = (e as MouseEvent).clientY
|
||||
}
|
||||
const clientX = e.type.startsWith('touch') ? (e as TouchEvent).touches[0].clientX : (e as MouseEvent).clientX
|
||||
const clientY = e.type.startsWith('touch') ? (e as TouchEvent).touches[0].clientY : (e as MouseEvent).clientY
|
||||
return { x: clientX - rect.left, y: clientY - rect.top }
|
||||
}
|
||||
|
||||
const startDrawing = (e: MouseEvent | TouchEvent) => {
|
||||
// 阻止默认事件(如页面滚动)
|
||||
e.preventDefault()
|
||||
isDrawing.value = true
|
||||
const { x, y } = getPos(e)
|
||||
lastX.value = x
|
||||
lastY.value = y
|
||||
if(ctx.value) {
|
||||
ctx.value.beginPath()
|
||||
ctx.value.moveTo(x, y)
|
||||
ctx.value.lineTo(x, y)
|
||||
ctx.value.stroke()
|
||||
}
|
||||
lastX.value = x; lastY.value = y
|
||||
ctx.value?.beginPath()
|
||||
ctx.value?.moveTo(x, y)
|
||||
}
|
||||
|
||||
const draw = (e: MouseEvent | TouchEvent) => {
|
||||
e.preventDefault()
|
||||
if (!isDrawing.value || !ctx.value) return
|
||||
const { x, y } = getPos(e)
|
||||
ctx.value.beginPath()
|
||||
ctx.value.moveTo(lastX.value, lastY.value)
|
||||
ctx.value.lineTo(x, y)
|
||||
ctx.value.stroke()
|
||||
lastX.value = x
|
||||
lastY.value = y
|
||||
}
|
||||
|
||||
const stopDrawing = () => {
|
||||
isDrawing.value = false
|
||||
if (ctx.value) ctx.value.beginPath()
|
||||
}
|
||||
const stopDrawing = () => { isDrawing.value = false }
|
||||
|
||||
const clearCanvas = () => {
|
||||
if (!ctx.value || !nativeCanvasRef.value) return
|
||||
@ -538,189 +478,86 @@ const clearCanvas = () => {
|
||||
}
|
||||
|
||||
const handleSignConfirm = () => {
|
||||
if (!nativeCanvasRef.value) return
|
||||
nativeCanvasRef.value.toBlob((blob) => {
|
||||
nativeCanvasRef.value?.toBlob((blob) => {
|
||||
if (blob) {
|
||||
const file = new File([blob], `signature_${Date.now()}.png`, { type: 'image/png' })
|
||||
const file = new File([blob], `sign_${Date.now()}.png`, { type: 'image/png' })
|
||||
signatureFile.value = file
|
||||
signaturePreviewUrl.value = URL.createObjectURL(file)
|
||||
showSignatureDialog.value = false
|
||||
} else {
|
||||
ElMessage.error('生成签名失败')
|
||||
}
|
||||
}, 'image/png')
|
||||
}
|
||||
|
||||
const handleSignCancel = () => {
|
||||
showSignatureDialog.value = false
|
||||
}
|
||||
const handleSignCancel = () => { showSignatureDialog.value = false }
|
||||
|
||||
onUnmounted(() => {
|
||||
stopCamera()
|
||||
if (signaturePreviewUrl.value) {
|
||||
URL.revokeObjectURL(signaturePreviewUrl.value)
|
||||
}
|
||||
if (signaturePreviewUrl.value) URL.revokeObjectURL(signaturePreviewUrl.value)
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.scan-area {
|
||||
text-align: center;
|
||||
padding: 20px;
|
||||
.app-container.mobile-optimized {
|
||||
padding: 10px; max-width: 600px; margin: 0 auto;
|
||||
}
|
||||
.camera-box {
|
||||
width: 100%;
|
||||
max-width: 400px;
|
||||
height: 300px;
|
||||
margin: 0 auto 20px;
|
||||
background: #000;
|
||||
overflow: hidden;
|
||||
}
|
||||
.http-warning {
|
||||
color: #e6a23c;
|
||||
background: #fdf6ec;
|
||||
padding: 10px;
|
||||
margin-bottom: 10px;
|
||||
border-radius: 4px;
|
||||
font-size: 12px;
|
||||
}
|
||||
.confirm-area {
|
||||
max-width: 800px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
.mt-20 {
|
||||
margin-top: 20px;
|
||||
}
|
||||
.form-actions {
|
||||
margin-top: 30px;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
}
|
||||
.signature-display-area {
|
||||
border: 1px dashed #dcdfe6;
|
||||
border-radius: 4px;
|
||||
padding: 10px;
|
||||
text-align: center;
|
||||
background: #fafafa;
|
||||
min-height: 80px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
.signed-image-box img {
|
||||
max-height: 100px;
|
||||
max-width: 100%;
|
||||
display: block;
|
||||
margin-bottom: 5px;
|
||||
border: 1px solid #eee;
|
||||
}
|
||||
/* 新增:头部右侧信息 */
|
||||
.header-right {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
.summary-text {
|
||||
margin-right: 15px;
|
||||
font-size: 14px;
|
||||
color: #606266;
|
||||
}
|
||||
.summary-text .num { color: #409EFF; font-weight: bold; margin: 0 4px; }
|
||||
.summary-text .price { color: #F56C6C; font-weight: bold; margin: 0 4px; font-size: 16px; }
|
||||
|
||||
/* 响应式签名弹窗 */
|
||||
:deep(.fullscreen-signature-dialog .el-dialog__body) {
|
||||
padding: 0;
|
||||
height: 100%;
|
||||
overflow: hidden;
|
||||
display: flex;
|
||||
/* 头部 */
|
||||
.card-header { display: flex; justify-content: space-between; align-items: center; }
|
||||
.title-box { font-size: 16px; font-weight: bold; display: flex; align-items: center; gap: 8px; }
|
||||
.header-price { font-size: 18px; color: #F56C6C; font-weight: bold; }
|
||||
|
||||
/* 扫码区 */
|
||||
.scan-section { margin-bottom: 20px; }
|
||||
.camera-wrapper {
|
||||
height: 25vh; background: #000; border-radius: 12px; overflow: hidden; position: relative; margin-bottom: 10px;
|
||||
}
|
||||
.signature-wrapper {
|
||||
display: flex;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background-color: #fff;
|
||||
.scan-overlay {
|
||||
position: absolute; bottom: 10px; right: 10px; z-index: 10;
|
||||
}
|
||||
.signature-canvas-container {
|
||||
flex: 1;
|
||||
position: relative;
|
||||
background-color: #fff;
|
||||
height: 100%;
|
||||
width: 100%;
|
||||
overflow: hidden;
|
||||
.camera-placeholder {
|
||||
height: 120px; background: #f5f7fa; border: 1px dashed #dcdfe6; border-radius: 8px;
|
||||
display: flex; flex-direction: column; justify-content: center; align-items: center;
|
||||
color: #909399; margin-bottom: 10px; cursor: pointer;
|
||||
}
|
||||
.native-canvas {
|
||||
display: block;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
cursor: crosshair;
|
||||
touch-action: none;
|
||||
.camera-placeholder .text { margin-top: 5px; font-size: 13px; }
|
||||
|
||||
/* 表单与购物车 */
|
||||
.cart-section { margin-bottom: 20px; }
|
||||
.form-section { background: #fff; }
|
||||
.signature-box {
|
||||
border: 1px dashed #dcdfe6; border-radius: 6px; height: 100px;
|
||||
background: #fcfcfc; display: flex; justify-content: center; align-items: center; cursor: pointer;
|
||||
}
|
||||
.unsigned-placeholder { display: flex; flex-direction: column; align-items: center; color: #909399; font-size: 13px; }
|
||||
.signed-img img { max-height: 90px; }
|
||||
.re-sign-tip { display: block; text-align: center; font-size: 12px; color: #409EFF; margin-top: 2px; }
|
||||
|
||||
.bottom-actions { display: flex; justify-content: space-between; margin-top: 30px; }
|
||||
.bottom-actions .el-button { width: 48%; }
|
||||
|
||||
/* 全屏签名弹窗 */
|
||||
:deep(.fullscreen-signature-dialog .el-dialog__body) { padding: 0; height: 100%; display: flex; }
|
||||
.signature-wrapper { display: flex; width: 100%; height: 100%; }
|
||||
.signature-canvas-container { flex: 1; position: relative; background: #fff; overflow: hidden; }
|
||||
.native-canvas { display: block; width: 100%; height: 100%; touch-action: none; }
|
||||
.canvas-tip {
|
||||
position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%);
|
||||
color: #ccc; font-size: 20px; pointer-events: none; opacity: 0.5; writing-mode: vertical-lr;
|
||||
}
|
||||
.signature-sidebar {
|
||||
width: 150px;
|
||||
background: #333;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
padding: 20px 10px;
|
||||
gap: 30px;
|
||||
color: #fff;
|
||||
z-index: 10;
|
||||
box-shadow: -2px 0 5px rgba(0,0,0,0.1);
|
||||
}
|
||||
.sidebar-title {
|
||||
writing-mode: vertical-rl;
|
||||
letter-spacing: 4px;
|
||||
font-size: 18px;
|
||||
font-weight: bold;
|
||||
opacity: 0.8;
|
||||
}
|
||||
.sidebar-actions {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 20px;
|
||||
width: 100%;
|
||||
}
|
||||
.sidebar-actions .el-button {
|
||||
width: 100%;
|
||||
margin-left: 0;
|
||||
height: 50px;
|
||||
}
|
||||
.confirm-btn {
|
||||
margin-top: 20px;
|
||||
font-weight: bold;
|
||||
width: 120px; background: #333; color: #fff;
|
||||
display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 20px 10px;
|
||||
}
|
||||
.sidebar-title { writing-mode: vertical-rl; font-size: 18px; letter-spacing: 5px; margin-bottom: 30px; font-weight: bold; }
|
||||
.sidebar-actions { display: flex; flex-direction: column; gap: 20px; width: 100%; }
|
||||
.sidebar-actions .el-button { width: 100%; margin: 0; height: 50px; }
|
||||
|
||||
@media screen and (max-width: 768px) {
|
||||
.signature-wrapper {
|
||||
flex-direction: column;
|
||||
}
|
||||
.signature-canvas-container {
|
||||
flex: 1;
|
||||
height: auto;
|
||||
}
|
||||
.signature-sidebar {
|
||||
width: 100%;
|
||||
height: auto;
|
||||
flex-direction: row;
|
||||
padding: 15px;
|
||||
gap: 10px;
|
||||
order: 2;
|
||||
}
|
||||
.sidebar-title {
|
||||
display: none;
|
||||
}
|
||||
.sidebar-actions {
|
||||
flex-direction: row;
|
||||
gap: 10px;
|
||||
width: 100%;
|
||||
}
|
||||
.sidebar-actions .el-button {
|
||||
flex: 1;
|
||||
height: 44px;
|
||||
font-size: 14px;
|
||||
}
|
||||
.confirm-btn {
|
||||
margin-top: 0;
|
||||
}
|
||||
.signature-wrapper { flex-direction: column; }
|
||||
.signature-canvas-container { flex: 1; }
|
||||
.canvas-tip { writing-mode: horizontal-tb; bottom: 50%; }
|
||||
.signature-sidebar { width: 100%; height: auto; flex-direction: row; padding: 10px; justify-content: space-between; }
|
||||
.sidebar-title { display: none; }
|
||||
.sidebar-actions { flex-direction: row; width: 100%; gap: 10px; }
|
||||
.sidebar-actions .el-button { flex: 1; height: 40px; }
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user