fcx
3 天以前 4dc0880780b52c0c35456666a4643a7866b454a7
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<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>