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
<template>
    <view class="container" :hidden="isPopup === false">
        <uni-popup ref="popup" type="bottom" popupshow @change="popupChange">
            <view style="height: 80vh; background-color: #fff; padding-bottom: 2rem;">
                <blueToothConnectorVue ref="bt"></blueToothConnectorVue>
            </view>
        </uni-popup>
        <!-- 使用uni-popup类的组件时,当popup不显示时,组件树中没有popup内的模组,无法使用ref调用模组内的函数,故需要在外部挂载一个透明的组件 -->
        <blueToothConnectorVue style="display: none;position: absolute; bottom: 0;right: 0;" ref="bt2"></blueToothConnectorVue>
    </view>
</template>
 
<script>
    import bluetoothTool from '@/plugins/BluetoothTool.js'
    import permission from '@/plugins/permission.js'
    import {
        InputImage
    } from '@psdk/frame-imageb';
    import {
        ConnectedDevice,
        Lifecycle,
        Raw,
        FakeConnectedDevice,
        WriteOptions,
        TSPLCommand,
        TextAppendat,
        Commander,
    } from '@psdk/frame-father';
    import blueToothConnectorVue from '../../components/blueToothConnector/blueToothConnector.vue';
    import {
        CStatus
    } from '@psdk/cpcl';
    import {
        CommonUtils
    } from '../../utils/common';
import { nextTick } from "vue";
 
    export default {
        name: "lablePrinterComponent",
        data() {
            return {
                isPopup: false,
            };
        },
        props: {
            printMode: {
                validator: (value) => {
                    return typeof(value) === 'undefined' || ['tspl', 'cpcl', 'esc'].includes(value)
                }
            },
            printInfo: {
                type: String | Function
            }
        },
        components: {
            blueToothConnectorVue
        },
        methods: {
            async openPopup() {
                this.$refs.popup.open();
                // 在popup中的操作针对ref bt对应的组件,需要将bt组件的状态与bt2进行一次同步
                this.$nextTick(() => {
                    this.$refs.bt2 = this.$refs.bt
                })
                
            },
            async popupChange(e) {
                this.isPopup = e['show']
                await this.$nextTick()
            },
            async batchPrint(commandList) {
                console.log(this.$refs.bt2)
                let vm = this;
                let binarys = [];
                for (let command of commandList) {
                    switch (vm.printMode) {
                        case "tspl":
                            let tspl = await vm.$printer.tspl().clear();
                            tspl.raw(Raw.text(command))
                            console.log("print command:\n" + tspl.command().string())
                            binarys.push(tspl.command().binary());
                            break;
                        case "cpcl":
                            let cpcl = await vm.$printer.cpcl().clear();
                            cpcl.raw(Raw.text(command))
                            console.log("print command:\n" + cpcl.command().string())
                            binarys.push(cpcl.command().binary());
                            break;
                        case "esc":
                            let esc = await vm.$printer.esc().clear();
                            esc.raw(Raw.text(command))
                            console.log("print command:\n" + esc.command().string())
                            binarys.push(esc.command().binary());
                            break;
                        default:
                            return uni.showToast({
                                icon: 'none',
                                title: 'printMode类型错误!'
                            })
                    }
                }
                
                for(let binary of binarys){
                    try{
                        let sendSuccess = await this.$refs.bt2.sendMessage(binary);
                        console.log("发送是否成功: ", sendSuccess)
                        if(sendSuccess === false) {
                            return
                        }
                    }catch {
                        // 断开蓝牙连接
                        this.$refs.bt2.closeBluetooth()
                    }
                    
                }
            },
            async execPrint() {
                // 检查蓝牙连接
                let btStatus = bluetoothTool.getBluetoothStatus()
                if (btStatus != true) {
                    this.showToast("蓝牙连接异常!")
                    return
                }
 
                // 检查是否连接设备
                let pairedDevices = bluetoothTool.getPairedDevices()
                if (pairedDevices.length < 1) {
                    this.showToast("无设备连接!")
                    return
                }
                try {
                    if (typeof this.printInfo === 'function') {
                        // 传入的printInfo是函数,直接执行
                        let msg = await this.printInfo()
                        this.$refs.bt2.sendMessage(msg)
                    } else if (typeof this.printInfo === 'string') {
                        // 传入的参数是字符串,则根据打印模式调用不同的打印API
                        // 这里的字符串需要对应打印模式
                        if (CommonUtils.isJson(this.printInfo) === true) {
                            // 如果需要批量传输,需要传JSON
                            let commandList = JSON.parse(this.printInfo)
                            for (let command of commandList) {
                                command.replace(/\n/g, "\r\n")
                            }
                            this.batchPrint(commandList)
                        } else {
                            let printStr = this.printInfo.replace(/\n/g, "\r\n")
                            let vm = this;
                            let binary = null;
                            switch (vm.printMode) {
                                case "tspl":
                                    const tspl = await vm.$printer.tspl().clear();
                                    tspl.raw(Raw.text(printStr))
                                    console.log("print command:\n" + tspl.command().string())
                                    binary = tspl.command().binary();
                                    await this.$refs.bt2.sendMessage(binary);
                                    break;
                                case "cpcl":
                                    const cpcl = await vm.$printer.cpcl().clear();
                                    cpcl.raw(Raw.text(printStr))
                                    console.log("print command:\n" + cpcl.command().string())
                                    binary = cpcl.command().binary();
                                    await this.$refs.bt2.sendMessage(binary);
                                    break;
                                case "esc":
                                    const esc = await vm.$printer.esc().clear();
                                    esc.raw(Raw.text(printStr))
                                    console.log("print command:\n" + esc.command().string())
                                    binary = esc.command().binary();
                                    await this.$refs.bt2.sendMessage(binary);
                                    break;
                                default:
                                    return uni.showToast({
                                        icon: 'none',
                                        title: 'printMode类型错误!'
                                    })
                            }
                        }
                    } else {
                        uni.showToast({
                            icon: 'none',
                            title: 'printInfo类型错误!'
                        })
                    }
                } catch (e) {
                    console.error(e);
                    uni.showToast({
                        icon: "none",
                        title: e
                    })
                }
 
            },
        },
    }
</script>
 
<style lang="scss" scoped>
    * {
        box-sizing: border-box;
    }
    .container[hidden] {
        padding: 0 !important;
        height: 0 !important;
    }
</style>