import { mapState, mapActions } from "vuex"; import { FakeConnectedDevice } from "@psdk/frame-father"; import bluetoothTool from "@/plugins/BluetoothTool.js"; import { TBar, TBarCode, TQRCode, TBox, TImage, TPage, TRotation, TCodeType, TLine, TText, TFont, TTLine, } from "@psdk/tspl"; ///转成安卓有符号的 function uint8ArrayToSignedArray(uint8Array) { let signedArray = new Array(uint8Array.length); for (let i = 0; i < uint8Array.length; i++) { if (uint8Array[i] >= 128) { signedArray[i] = uint8Array[i] - 256; } else { signedArray[i] = uint8Array[i]; } } return signedArray; } async function sendMessage(cmd) { console.log(cmd); const result = bluetoothTool.sendByteData(cmd); uni.showToast({ icon: "none", title: result ? "发送成功!" : "发送失败...", }); } export default { name: "blePrintMixin", computed: { ...mapState({ discoveredDevices: (state) => state.ble.discoveredDevices, // 已发现的蓝牙设备 connectedBleDevice: (state) => state.ble.connectedBleDevice, connectedDeviceId: (state) => state.ble.connectedDeviceId, // 已连接的蓝牙设备ID }), }, data() { return { bleErrorTipPop: { show: false, content: "未连接 蓝牙", extraText: "请连接蓝牙之后再继续操作", }, // 蓝牙打印弹框 blePrintPop: { show: false, data: {}, }, // 选择蓝牙设备弹框 bleSelectPop: { show: false, discoveredDevices: [], }, }; }, mounted() {}, methods: { ...mapActions("ble", [ "checkBleStatus", // 检查蓝牙设备连接状态 "discovery", // 搜索蓝牙设备 "connectBT", // 连接蓝牙设备 "cancelDiscovery", // 停止蓝牙搜索 ]), bleErrorTipConfirm() { this.bleErrorTipPop.show = false; this.openBleSelectPop(); }, //打开蓝牙选择弹框 openBleSelectPop() { this.discovery(); // 搜索蓝牙设备 this.bleSelectPop.show = true; }, //关闭蓝牙选择弹框 closeBleSelectPop() { this.cancelDiscovery(); this.bleSelectPop.show = false; }, //选择蓝牙设备 async handleSelectBle(item) { console.log("选择蓝牙设备=====", item); const device = await this.connectBT(item); if (device) { this.closeBleSelectPop(); this.$printer.init(new FakeConnectedDevice()); // 初始化打印机 this.blePrintPop.show = true; } console.log("this.blePrintPop.data=====", this.blePrintPop.data); }, // 打开蓝牙打印弹框 async openBlePrintPop(data) { const res = await this.checkBleStatus(); // 检查蓝牙设备打开状态 this.blePrintPop.data = data; switch (res.code) { case 0: // const devices = bluetoothTool.getPairedDevices(); // 获取已配对的蓝牙设备 // console.log("devices======", devices); if (this.connectedDeviceId != "") { this.blePrintPop.show = true; } else { // 如果没有配对的设备,提示用户去配对 this.bleErrorTipPop.show = true; } break; case 1: uni.showToast({ icon: "none", title: "请打开蓝牙", }); break; case 2: uni.showToast({ icon: "none", title: "当前设备不支持蓝牙", }); break; } }, // 重新搜索蓝牙设备 refreshBleDevice() { this.discovery(); }, /** * 打印 *@data {Object} 需要打印的数据 * */ async writeTsplModel(data) { const vm = this; const obj = { qrCode: data.qrCode, // 二维码 barCode: data.barCode, // 商品条码 customerName: data.customerName, //客户名 number: data.number, // 单据编号 batchNumber: data.batchNumber, // 批次条码 }; try { const tspl = await vm.$printer .tspl() .clear() .page( new TPage({ width: 76, height: 130, }) ) .qrcode( new TQRCode({ x: 50, y: 20, content: "283423982494284284828484", cellWidth: 10, }) ) .text( new TText({ x: 50 + 30, y: 20, content: obj.customerName, font: TFont.TSS24, }) ) .text( new TText({ x: 50 + 30, y: 20 + 35, content: obj.number, font: TFont.TSS24, }) ) .barcode( new TBarCode({ x: 50, y: 20 + 35 + 20, cellWidth: 2, height: 60, content: obj.barCode, rotation: TRotation.ROTATION_0, codeType: TCodeType.CODE128, showType: 2, }) ) .text( new TText({ x: 50 + 20, y: 50 + 100, content: obj.barCode, font: TFont.TSS24, }) ) .print(); var binary = tspl.command().binary(); await sendMessage(Array.from(uint8ArrayToSignedArray(binary))); } catch (e) { console.error(e); uni.showToast({ title: "失败", }); } }, async writeData({ num = 1, data }) { for (let i = 0; i < num; i++) { await this.writeTsplModel(data); } }, }, };