emis-app/components/tpx-bluetooth-print/render-template-native.vue

143 lines
3.7 KiB
Vue
Raw Permalink Normal View History

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