<template>
|
<view>
|
<uni-popup ref="popup" type="bottom" popupshow @close="popupCloseHandler">
|
<view style="height: 80vh; background-color: #fff; padding-bottom: 2rem;">
|
<blueToothConnectorVue ref="bt"></blueToothConnectorVue>
|
</view>
|
</uni-popup>
|
</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';
|
|
export default {
|
name: "lablePrinterComponent",
|
data() {
|
return {
|
blueToothConnector: null,
|
};
|
},
|
props: {
|
printMode: {
|
validator: (value) => {
|
return typeof(value) === 'undefined' || ['tspl', 'cpcl', 'esc'].includes(value)
|
}
|
},
|
printInfo: {
|
type: String | Function
|
}
|
},
|
components: {
|
blueToothConnectorVue
|
},
|
methods: {
|
openPopup() {
|
this.$refs.popup.open();
|
this.$nextTick(() => {
|
this.blueToothConnector = this.$refs.bt
|
})
|
|
},
|
popupCloseHandler() {
|
|
},
|
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.blueToothConnector.sendMessage(msg)
|
} else if (typeof this.printInfo === 'string') {
|
// 传入的参数是字符串,则根据打印模式调用不同的打印API
|
// 这里的字符串需要对应打印模式
|
|
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 vm.blueToothConnector.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 vm.blueToothConnector.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 vm.blueToothConnector.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;
|
}
|
</style>
|