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

116 lines
2.9 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: {},
}
},
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('数据发送成功,如果无法打印请重连设备重试')
await promiseTimeout(3000) // 发送数据差不多需要 3s
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({ paperWidth, imgsRes }) {
this.prtLoading = true
let conSta = await this.$refs.curBlueTooth.autoConnect()
this.$emit('autoConnectCallback', conSta)
if(!Array.isArray(imgsRes)) {
imgsRes = [imgsRes]
}
if(conSta) {
showLoading()
for(let i = 0; i < imgsRes.length; i ++) {
let item = imgsRes[i]
if(item) {
this.paperWidth = paperWidth || this.paperWidth
this.imgRes = item
await this.handlePrint()
}
}
hideLoading()
return true
} else {
return false
}
}
}
}
</script>
<style scoped lang="scss">
</style>