emis-app/components/tpx-bluetooth-print/render-template-native.vue
2024-08-19 13:53:31 +08:00

143 lines
3.7 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<tpx-bluetooth-native ref="curBlueTooth" @onConnected="handleConnected">
<template v-slot:content></template>
</tpx-bluetooth-native>
<!-- 打印进度 -->
<tpx-dialog type="type-dialog" title="打印中" :show="prtPercentage">
<u-row custom-style="width: 500rpx;min-height: 150rpx;padding: 10rpx;">
<view>
<u-line-progress :percentage="prtPercentage" activeColor="#B12D29" height="30"></u-line-progress>
<view class="pt-20">正在发送数据, 请勿退出页面, 如果卡住了请重连打印机重试</view>
</view>
</u-row>
</tpx-dialog>
</template>
<script>
import { showLoading, showToast, hideLoading, canvasGetImageData, getImageInfo, promiseTimeout } from "@/common/untool"
import * as canvasUtil from "@/common/canvasUtil"
import tanexPrinterUniSdk from './nativePrint/tanexPrinterUniSdk'
import printModule from './nativePrint/print'
import dayjs from "dayjs"
import { Base64 } from "js-base64"
export default {
props:{
},
computed: {
},
data () {
return {
prtPercentage: 0, // 打印进度
paperWidth: 560,
}
},
mounted () {
this.initData()
},
unmounted() {
},
methods: {
async initData() {
},
// 打印图片
handlePrintImgs(imgsRes){
const that = this
return new Promise(async (resolve, reject)=> {
let imgLen = imgsRes.length
let { bl, deviceId } = that.checkPrintParam()
if(!bl) {
return reject('打印参数校验不通过')
}
// prtInstance.diyCode(`PAGE-WIDTH 600\r\n`)
for(let i = 0; i < imgsRes.length; i ++) {
let item = imgsRes[i]
if(item) {
let prtInstance = tanexPrinterUniSdk.createInstance()
prtInstance.init( item.height )
prtInstance.addBitmapByEG(0, 0, item.imgData)
prtInstance.setFormPage() // 每张图打印一整页
prtInstance.setPagePrint()
let commond = prtInstance.getPrintCmd()
console.log("打印命令组装完成,开始打印=====")
printModule.print(commond)
await promiseTimeout(300) // 打印命令发送后,加个延迟
}
}
resolve(true)
// await that.loadPrintProcess()
// printModule.printTestSimple(deviceId)
})
},
// 直接传打印指令给打印机
handlePrintCommandData(bs64Str){
let that = this
return new Promise((resolve, reject)=> {
let { bl, deviceId } = that.checkPrintParam()
if(!bl) {
return reject('打印参数校验不通过')
}
let cmdStr = Base64.decode(bs64Str)
console.log("===handlePrintCommandData cmdStr====", cmdStr)
printModule.sendData(cmdStr)
resolve(true)
})
},
checkPrintParam() {
let msg = '', bl = false
let { deviceId, isStateOn } = this.$refs.curBlueTooth.getConData()
if(!msg && !(deviceId && isStateOn)) {
msg = '未连接打印设备'
}
msg && (showToast(msg))
return {
deviceId,
msg,
bl: !msg
}
},
// 给外部调用api,打印自定义图
async callPrint({ paperWidth, imgsRes, prtCmdData }) {
this.paperWidth = paperWidth || this.paperWidth
let conSta = await this.$refs.curBlueTooth.autoConnect()
this.$emit('autoConnectCallback', conSta)
if(!Array.isArray(imgsRes)) {
imgsRes = [imgsRes]
}
let cBl = false
if(conSta) {
showLoading('正在发送打印数据..')
try{
if(prtCmdData) {
await this.handlePrintCommandData(prtCmdData)
} else {
await this.handlePrintImgs(imgsRes)
}
showToast('数据发送成功,如果无法打印请重连设备重试')
cBl = true
}catch(e){
console.log('callPrint 打印异常=======')
console.error(e)
}
hideLoading()
}
return cBl
},
handleConnected(){
this.$emit('onConnected')
}
}
}
</script>
<style scoped lang="scss">
</style>