新框架PDA前端(祈禧6月初版本)
南骏 池
3 天以前 3ff569f00d22919e47cab8fb6d7d9867fd115d97
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
var vm = new Vue({
    el: '#app',
    data: function () {
        return {
            isLoading: false,
            textInput: '',
            scanWorkstation: '', // 新增:扫描工位绑定字段
            html5QrCode: null    // 新增:扫码器实例
        }
    },
    mounted() {
        var that = this;
    },
    methods: {
        sendMessage(obj) {
            var detail = ["! 0 200 200 210 1\r\n" +
                "PAGE - WIDTH 700 \r\n" +
                "BOX 0 0 700 180 2 \r\n" +
                "T 16 0 200 10  条码1" + "\r\n" + // 添加字体名称
                "BARCODE 128 1 1 20 0 80 " + '123456' + "\r\n" +
                "TEXT 7 0 50 80 SIMSUN.GB2312 " + '123456' + "\r\n" + // 添加字体名称
                //"B QR 500 10 M 1 U 9 \r\n" +  
                //"MA," + '001' + "\r\n" +
                "B QR 580 15 M 2 U 4" + "\r\n" +
                "MA,QR code ABC123" + "\r\n" +
                "ENDQR\r\n" +
                "FORM\r\n" +
                "PRINT\r\n",
                "! 0 200 200 210 1\r\n" +
                "PAGE - WIDTH 700 \r\n" +
                "BOX 0 0 700 180 2 \r\n" +
                "T 16 0 200 10 条码2" + "\r\n" + // 添加字体名称
                "BARCODE 128 1 1 20 0 80 " + '123456' + "\r\n" +
                "TEXT270 10 0 200 50 10#格式大小测试" + "\r\n" +
                "TEXT 7 0 50 80 SIMSUN.GB2312 " + '123456' + "\r\n" + // 添加字体名称
                "B QR 580 15 M 2 U 4" + "\r\n" +
                "MA,QR code ABC123" + "\r\n" +
                "ENDQR\r\n" +
                "FORM\r\n" +
                "PRINT\r\n" ];
 
            let sendData = {
                Type: 'Bar',
                Barcode: 'TM250304-000104-2',
                Detail: detail,
                Ip: '192.168.38.135',
                Port: '9100',
            }
            console.log('sendMessage 开始1:')
            console.log('sendMessage 开始2:' + JSON.stringify(sendData))
            uni.webView.postMessage({
                data: JSON.stringify(sendData)
            })
        },
        // 修改sendScan方法为实际扫码功能
        sendScan() {
            const that = this;
            if (!this.html5QrCode) {
                // 鸿蒙设备需要使用特定设备ID
                this.html5QrCode = new Html5Qrcode("qr-reader", {
                    experimentalFeatures: {
                        useBarCodeDetectorIfSupported: true
                    },
                    formatsToSupport: [ Html5QrcodeSupportedFormats.QR_CODE ]
                });
            }
 
            const config = {
                fps: 10,
                qrbox: 250,
                // 鸿蒙需要明确指定视频输入设备
                videoConstraints: {
                    deviceId: 'default' // 使用默认摄像头
                }
            };
 
            // 修改摄像头调用方式
            this.html5QrCode.start(
                config,
                qrCodeMessage => {
                    that.scanWorkstation = qrCodeMessage;
                    that.stopScan();
                    that.$toast.success("扫码成功!");
                },
                errorMessage => {
                    console.error("扫码失败:", errorMessage);
                    that.$toast.fail("扫码失败");
                }
            ).catch(err => {
                console.error("摄像头错误:", err);
                // 增加鸿蒙设备错误处理
                const errMsg = err.message || err.name || '摄像头不可用';
                that.$toast.fail("无法启动摄像头: " + errMsg);
                
                // 原生扫码备用方案
                if(window.plus && plus.barcode){
                    plus.barcode.scan(
                        'qr',
                        (type, result) => {
                            that.scanWorkstation = result;
                            that.$toast.success("扫码成功!");
                        },
                        (error) => {
                            that.$toast.fail("扫码失败: "+error.code);
                        }
                    );
                }
            });
 
            // 显示扫码框(需在ASPX中添加<div id="qr-reader"></div>)
            document.getElementById('qr-reader').style.display = 'block';
        },
 
        // 新增:停止扫码方法
        stopScan() {
            if (this.html5QrCode && this.html5QrCode.isScanning) {
                this.html5QrCode.stop().then(() => {
                    document.getElementById('qr-reader').style.display = 'none';
                }).catch(err => {
                    console.error("关闭摄像头失败:", err);
                });
            }
        },
 
        testSuccessSound() {
            this.$playSound('success');
        },
        
        testErrorSound() {
            this.$playSound('error');
        },
 
 
 
        startScan() {
            const qrScanner = new Html5Qrcode("qr-reader");
            const config = { 
                fps: 10,
                qrbox: 250
            };
    
            qrScanner.start(
                { facingMode: "environment" },
                config,
                (decodedText) => {
                    // 扫码成功处理
                    this.scanWorkstation = decodedText;
                    qrScanner.stop();
                    document.getElementById('qr-reader').style.display = 'none';
                },
                (errorMessage) => {
                    // 错误处理
                    console.error(errorMessage);
                }
            ).catch(err => {
                alert('无法启动摄像头: ' + err);
            });
            
            // 显示扫码容器
            document.getElementById('qr-reader').style.display = 'block';
        }
    }
})