<template>
|
<view class="barcode-container">
|
<!-- nvue 官方扫码组件 -->
|
<barcode
|
ref="barcode"
|
class="barcode-view"
|
:autostart="false"
|
background="#000000"
|
frameColor="#007aff"
|
scanbarColor="#007aff"
|
:filters="barcodeFilters"
|
@marked="onBarcodeMarked"
|
@error="onBarcodeError"
|
/>
|
|
<!-- 顶部标题栏 -->
|
<view class="header">
|
<text class="header-title">{{ scanTitle }}</text>
|
<text class="header-close" @click="closeScan">✕</text>
|
</view>
|
|
<!-- 底部控制栏 -->
|
<view class="footer">
|
<view class="controls">
|
<view class="control-btn" @click="toggleLight">
|
<text class="btn-text">{{ lightOn ? '关闭' : '打开' }}闪光灯</text>
|
</view>
|
<view class="control-btn" @click="manualInput">
|
<text class="btn-text">⌨️ 手动输入</text>
|
</view>
|
</view>
|
|
<view class="tips">
|
<text class="tips-text">💡 将条码对准扫描框内,保持15-30cm距离</text>
|
</view>
|
</view>
|
</view>
|
</template>
|
|
<script>
|
export default {
|
data() {
|
return {
|
scanTitle: 'SN确认',
|
lightOn: false,
|
// 关键优化:只启用工业常用的条码类型,提高识别速度和成功率
|
barcodeFilters: [
|
8, // CODE39 - 工业常用
|
10, // CODE128 - 工业最常用,支持特殊字符(/ - _ 等)
|
]
|
};
|
},
|
|
onLoad(options) {
|
// 获取扫码标题
|
if (options.title) {
|
this.scanTitle = decodeURIComponent(options.title);
|
}
|
},
|
|
onReady() {
|
console.log('BarcodeScan 页面准备完成');
|
// 页面加载完成后延迟启动扫码(等待界面完全渲染)
|
setTimeout(() => {
|
console.log('准备启动扫码...');
|
this.startScan();
|
}, 500);
|
},
|
|
onUnload() {
|
// 页面卸载时取消扫码
|
this.cancelScan();
|
},
|
|
methods: {
|
// 开始扫码
|
startScan() {
|
console.log('startScan 被调用');
|
|
if (!this.$refs.barcode) {
|
console.error('错误:barcode 组件引用不存在');
|
uni.showToast({
|
title: '扫码组件初始化失败',
|
icon: 'none'
|
});
|
return;
|
}
|
|
try {
|
console.log('开始扫码 - 启用码制: CODE39, CODE128');
|
this.$refs.barcode.start({
|
conserve: false, // 不保存截图
|
vibrate: true, // 震动提示
|
sound: 'default', // 播放提示音
|
filename: '' // 不保存文件
|
});
|
console.log('扫码已启动');
|
} catch (error) {
|
console.error('启动扫码失败:', error);
|
uni.showModal({
|
title: '扫码启动失败',
|
content: '可能原因:\n1. 相机权限未授予\n2. 相机被其他应用占用\n\n请检查相机权限或重启应用',
|
confirmText: '手动输入',
|
success: (res) => {
|
if (res.confirm) {
|
this.manualInput();
|
}
|
}
|
});
|
}
|
},
|
|
// 取消扫码
|
cancelScan() {
|
if (this.$refs.barcode) {
|
this.$refs.barcode.cancel();
|
}
|
},
|
|
// 切换闪光灯
|
toggleLight() {
|
this.lightOn = !this.lightOn;
|
if (this.$refs.barcode) {
|
this.$refs.barcode.setFlash(this.lightOn);
|
}
|
},
|
|
// 扫码成功回调
|
onBarcodeMarked(e) {
|
console.log('扫码成功 - 原始数据:', e);
|
console.log('扫码成功 - 完整detail:', e.detail);
|
|
const result = e.detail.message; // 扫码结果
|
const type = e.detail.code; // 条码类型
|
|
console.log('扫码结果:', result);
|
console.log('条码类型:', type);
|
console.log('结果长度:', result ? result.length : 0);
|
|
if (result) {
|
// 去除首尾空格
|
const cleanResult = result.trim();
|
console.log('清理后结果:', cleanResult);
|
console.log('清理后长度:', cleanResult.length);
|
|
// 震动反馈
|
uni.vibrateShort();
|
|
// 显示成功提示
|
uni.showToast({
|
title: '扫码成功',
|
icon: 'success',
|
duration: 800
|
});
|
|
// 延迟关闭,让用户看到提示
|
setTimeout(() => {
|
// 返回上一页并传递扫码结果
|
const pages = getCurrentPages();
|
const prevPage = pages[pages.length - 2];
|
if (prevPage) {
|
// 调用上一页的回调方法
|
if (prevPage.$vm && prevPage.$vm.onScanSuccess) {
|
console.log('准备传递结果到上一页:', cleanResult);
|
prevPage.$vm.onScanSuccess(cleanResult);
|
} else {
|
console.error('上一页没有 onScanSuccess 方法');
|
}
|
}
|
|
// 关闭当前页面
|
uni.navigateBack();
|
}, 800);
|
} else {
|
console.error('警告:扫码结果为空');
|
uni.showToast({
|
title: '扫码结果为空,请重试',
|
icon: 'none'
|
});
|
}
|
},
|
|
// 扫码失败回调
|
onBarcodeError(e) {
|
console.error('扫码错误 - 详细信息:', e);
|
console.error('错误对象:', JSON.stringify(e));
|
|
// 根据错误类型给出不同提示
|
let errorMsg = '扫码失败';
|
if (e && e.detail) {
|
if (e.detail.code === -1) {
|
errorMsg = '相机权限未授予';
|
} else if (e.detail.code === -2) {
|
errorMsg = '相机启动失败';
|
} else {
|
errorMsg = `扫码错误: ${e.detail.message || '未知错误'}`;
|
}
|
}
|
|
uni.showModal({
|
title: errorMsg,
|
content: '建议:\n1. 检查相机权限\n2. 重启应用\n3. 使用手动输入',
|
confirmText: '手动输入',
|
cancelText: '重试',
|
success: (res) => {
|
if (res.confirm) {
|
this.manualInput();
|
} else if (res.cancel) {
|
// 重新启动扫码
|
setTimeout(() => {
|
this.startScan();
|
}, 300);
|
}
|
}
|
});
|
},
|
|
// 手动输入
|
manualInput() {
|
const self = this;
|
uni.showModal({
|
title: `输入${this.scanTitle}`,
|
editable: true,
|
placeholderText: '请输入条码内容',
|
success(res) {
|
if (res.confirm && res.content) {
|
// 返回上一页并传递输入结果
|
const pages = getCurrentPages();
|
const prevPage = pages[pages.length - 2];
|
if (prevPage && prevPage.$vm && prevPage.$vm.onScanSuccess) {
|
prevPage.$vm.onScanSuccess(res.content.trim());
|
}
|
uni.navigateBack();
|
}
|
}
|
});
|
},
|
|
// 关闭扫码
|
closeScan() {
|
this.cancelScan();
|
uni.navigateBack();
|
}
|
}
|
};
|
</script>
|
|
<style>
|
.barcode-container {
|
position: absolute;
|
top: 0;
|
left: 0;
|
right: 0;
|
bottom: 0;
|
background-color: #000000;
|
}
|
|
.barcode-view {
|
position: absolute;
|
top: 0;
|
left: 0;
|
right: 0;
|
bottom: 0;
|
width: 750rpx;
|
background-color: #000000;
|
}
|
|
/* 顶部标题栏 */
|
.header {
|
position: absolute;
|
top: 0;
|
left: 0;
|
right: 0;
|
height: 120rpx;
|
background-color: rgba(0, 122, 255, 0.9);
|
flex-direction: row;
|
justify-content: space-between;
|
align-items: center;
|
padding-left: 30rpx;
|
padding-right: 30rpx;
|
z-index: 10;
|
}
|
|
.header-title {
|
font-size: 36rpx;
|
font-weight: bold;
|
color: #ffffff;
|
}
|
|
.header-close {
|
font-size: 48rpx;
|
font-weight: bold;
|
color: #ffffff;
|
padding-left: 20rpx;
|
padding-right: 20rpx;
|
}
|
|
/* 底部控制栏 */
|
.footer {
|
position: absolute;
|
bottom: 0;
|
left: 0;
|
right: 0;
|
background-color: rgba(0, 0, 0, 0.7);
|
z-index: 10;
|
}
|
|
.controls {
|
flex-direction: row;
|
justify-content: space-around;
|
padding-top: 20rpx;
|
padding-bottom: 20rpx;
|
padding-left: 20rpx;
|
padding-right: 20rpx;
|
}
|
|
.control-btn {
|
flex: 1;
|
height: 80rpx;
|
background-color: #007aff;
|
border-radius: 10rpx;
|
justify-content: center;
|
align-items: center;
|
margin-left: 10rpx;
|
margin-right: 10rpx;
|
}
|
|
.btn-text {
|
font-size: 28rpx;
|
color: #ffffff;
|
}
|
|
.tips {
|
padding-top: 20rpx;
|
padding-bottom: 30rpx;
|
padding-left: 20rpx;
|
padding-right: 20rpx;
|
background-color: rgba(255, 251, 230, 0.9);
|
align-items: center;
|
}
|
|
.tips-text {
|
font-size: 26rpx;
|
color: #333333;
|
text-align: center;
|
font-weight: bold;
|
}
|
</style>
|