emis-app/components/tpx-bluetooth-print/render-template-native.vue
2024-06-25 23:58:07 +08:00

127 lines
3.3 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">
<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"
export default {
props:{
},
computed: {
},
data () {
return {
prtPercentage: 0, // 打印进度
paperWidth: 560,
imgRes: {},
prtQueList: [],
lastExeMark: undefined,
prtLoading: false,
}
},
mounted () {
this.initData()
},
unmounted() {
},
methods: {
async initData() {
},
// 打印
handlePrint(){
const that = this
return new Promise(async (resolve, reject)=> {
let { bl, deviceId } = that.checkPrintParam()
if(!bl) {
return reject('打印参数校验不通过')
}
let prtInstance = tanexPrinterUniSdk.createInstance()
prtInstance.init(that.imgRes.height)
// prtInstance.diyCode(`PAGE-WIDTH 600\r\n`)
prtInstance.addBitmapByEG(0, 0, that.imgRes.imgData)
prtInstance.setPagePrint()
let commond = prtInstance.getPrintCmd()
console.log("打印命令组装完成,开始打印=====", commond)
printModule.print(commond)
that.showToast('数据发送成功,如果无法打印请重连设备重试')
resolve(true)
// await that.loadPrintProcess()
// printModule.printTestSimple(deviceId)
})
},
checkPrintParam() {
let msg = '', bl = false
let { deviceId, isStateOn } = this.$refs.curBlueTooth.getConData()
!this.imgRes.imgData && (msg = '打印内容不能为空,请重试')
if(!msg && !(deviceId && isStateOn)) {
msg = '未连接打印设备'
}
msg && (showToast(msg))
return {
deviceId,
msg,
bl: !msg
}
},
// 给外部调用api,打印自定义图
async callPrint(obj) {
this.prtQueList.push(obj)
clearTimeout(this.lastExeMark)
this.lastExeMark = setTimeout(()=> {
// if(this.prtLoading) {
// showToast('当前有打印任务未结束,请稍后再试')
// return false
// }
this.finallyExecPrint()
}, 500)
},
async finallyExecPrint() {
this.prtLoading = true
let conSta = await this.$refs.curBlueTooth.autoConnect()
this.$emit('autoConnectCallback', conSta)
for(let i = 0; i < this.prtQueList.length; i ++) {
let { paperWidth, imgRes } = this.prtQueList[i]
if(imgRes) {
this.paperWidth = paperWidth || this.paperWidth
this.imgRes = imgRes
if(conSta) {
await this.handlePrint()
this.prtQueList[i] = undefined // 打印成功 清除当前数据
}
}
}
this.prtQueList = []
this.prtLoading = false
return true
}
}
}
</script>
<style scoped lang="scss">
</style>