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

136 lines
3.3 KiB
Vue
Raw Normal View History

2024-05-12 12:49:00 +00:00
<template>
2024-06-25 04:54:13 +00:00
<tpx-bluetooth ref="curBlueTooth"></tpx-bluetooth>
2024-05-12 12:49:00 +00:00
<tpx-dialog type="type-dialog" title="打印中" :show="percentage">
<u-row custom-style="width: 500rpx;min-height: 150rpx;padding: 10rpx;">
<view>
<u-line-progress :percentage="percentage" activeColor="#B12D29" height="30"></u-line-progress>
2024-06-23 13:13:25 +00:00
<view class="pt-20">正在发送数据, 请勿退出页面, 如果卡住了请重连打印机重试</view>
2024-05-12 12:49:00 +00:00
</view>
</u-row>
</tpx-dialog>
</template>
<script>
2024-05-13 09:47:19 +00:00
import { showLoading, showToast, hideLoading, canvasGetImageData, getImageInfo, promiseTimeout } from "@/common/untool"
2024-05-12 12:49:00 +00:00
import * as canvasUtil from "@/common/canvasUtil"
import * as printJob from './print/printJob'
export default {
props:{
2024-05-13 09:47:19 +00:00
2024-05-12 12:49:00 +00:00
},
computed: {
},
data () {
return {
2024-06-25 04:54:13 +00:00
imgRes: {},
2024-05-12 12:49:00 +00:00
percentage: 0, // 打印进度
threshold: 200, // 阈值(值越大,图片打印越深)
2024-06-24 18:19:58 +00:00
paperWidth: 560,
2024-05-12 12:49:00 +00:00
}
},
mounted () {
this.initData()
},
unmounted() {
},
methods: {
async initData() {
2024-06-25 04:54:13 +00:00
2024-05-12 12:49:00 +00:00
},
// 打印
2024-05-13 09:47:19 +00:00
handlePrint(){
2024-05-12 12:49:00 +00:00
const that = this
2024-05-13 10:03:54 +00:00
let data = this.$refs.curBlueTooth.getConData()
console.log("打印组件选中数据:data=====", data)
let { deviceId, serviceId, characteristicId } = data
2024-05-13 09:47:19 +00:00
return new Promise((resolve, reject)=> {
let { bl } = that.checkPrintParam()
if(!bl) {
return reject('打印参数校验不通过')
2024-05-13 10:03:54 +00:00
}
2024-05-13 09:47:19 +00:00
const opt = {
...{ deviceId, serviceId, characteristicId },
onProgress: (percentage) => {
console.log("打印进度====", percentage)
that.percentage = percentage
},
lasterSuccess: () => {
that.percentage = 0
hideLoading()
uni.showModal({
title: '提示',
content: '数据已发送完,请检查打印的内容是否正常',
showCancel: false,
confirmText: '好的',
});
resolve()
},
onError: (e)=> {
that.percentage = 0
showToast(`打印异常`)
hideLoading()
reject(e)
}
}
2024-06-25 04:54:13 +00:00
2024-05-13 09:47:19 +00:00
//打印图片
printJob.printImage(
opt,
// 将图片数据转成位图数据
printJob.overwriteImageData({
threshold: that.threshold,
2024-06-25 04:54:13 +00:00
imageData: that.imgRes.imgData,
width: that.imgRes.width,
height: that.imgRes.height,
2024-05-13 09:47:19 +00:00
}),
);
})
2024-05-12 12:49:00 +00:00
},
2024-05-13 09:47:19 +00:00
checkPrintParam() {
let msg = '', bl = false
2024-05-13 10:03:54 +00:00
let { deviceId, serviceId, characteristicId } = this.$refs.curBlueTooth.getConData()
2024-06-25 04:54:13 +00:00
!this.imgRes.imgData && (msg = '打印内容不能为空,请重试')
2024-05-13 10:03:54 +00:00
if(!msg && !(deviceId && serviceId && characteristicId)) {
2024-05-13 09:47:19 +00:00
msg = '请选选择打印设备'
}
msg && (showToast(msg))
return {
msg,
bl: !msg
}
},
// 给外部调用api,打印自定义图
2024-06-25 04:54:13 +00:00
async callPrint({ paperWidth, imgRes } = {}) {
2024-05-13 09:47:19 +00:00
try{
this.paperWidth = paperWidth
2024-06-25 04:54:13 +00:00
this.imgRes = imgRes
2024-05-13 09:47:19 +00:00
let conSta = await this.$refs.curBlueTooth.autoConnect()
2024-06-24 18:19:58 +00:00
this.$emit('autoConnectCallback', conSta)
2024-05-13 09:47:19 +00:00
if(conSta) {
2024-05-14 14:56:44 +00:00
await this.handlePrint()
return true
2024-05-13 09:47:19 +00:00
}
} catch(e){
2024-06-25 08:12:48 +00:00
console.error("callPrint error=====", e)
2024-05-13 09:47:19 +00:00
}
2024-05-12 12:49:00 +00:00
}
}
}
</script>
<style scoped lang="scss">
.canvas-con{
width: 100%;
overflow: auto;
}
/*图片隐藏到窗口外*/
canvas {
2024-06-24 18:19:58 +00:00
position: absolute;
z-index: -999;
top: -999px;
2024-05-12 12:49:00 +00:00
}
</style>