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

113 lines
2.8 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: 600,
}
},
mounted () {
this.initData()
},
unmounted() {
},
methods: {
async initData() {
},
// 打印
handlePrint(imgsRes){
const that = this
return new Promise(async (resolve, reject)=> {
let imgLen = imgsRes.length
let { bl, deviceId } = that.checkPrintParam()
if(!bl) {
return reject('打印参数校验不通过')
}
let prtInstance = tanexPrinterUniSdk.createInstance()
prtInstance.init(
imgsRes?.[0]?.height * imgLen // 总高度
)
// prtInstance.diyCode(`PAGE-WIDTH 600\r\n`)
for(let i = 0; i < imgsRes.length; i ++) {
let item = imgsRes[i]
if(item) {
prtInstance.addBitmapByEG(0, 0, item.imgData)
prtInstance.setFormPage() // 每张图打印一整页
}
}
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()
if(!msg && !(deviceId && isStateOn)) {
msg = '未连接打印设备'
}
msg && (showToast(msg))
return {
deviceId,
msg,
bl: !msg
}
},
// 给外部调用api,打印自定义图
async callPrint({ paperWidth, imgsRes }) {
this.paperWidth = paperWidth || this.paperWidth
let conSta = await this.$refs.curBlueTooth.autoConnect()
this.$emit('autoConnectCallback', conSta)
if(!Array.isArray(imgsRes)) {
imgsRes = [imgsRes]
}
if(conSta) {
await this.handlePrint(imgsRes)
return true
} else {
return false
}
}
}
}
</script>
<style scoped lang="scss">
</style>