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

140 lines
3.6 KiB
Vue
Raw Normal View History

2024-05-10 14:32:39 +00:00
<template>
2024-06-24 18:19:58 +00:00
<!-- 打印预览 -->
<tpx-dialog
v-model:show="previewModel.show"
@onClose="handleCloseModal"
custom-style="width: 600rpx;padding: 10rpx;"
title="打印预览"
>
<view style="height: calc(75vh);">
<tpx-layout>
<template v-slot:header></template>
<template v-slot:body>
2024-07-23 16:07:10 +00:00
<view v-for="(url, index) in canvImages" @click="handlePreview(index)">
<create-image-data-bycanvas :ref="`curCanvas${index}`"></create-image-data-bycanvas>
2024-06-24 18:19:58 +00:00
</view>
</template>
<template v-slot:footer>
<view class="blcok-white-noradius">
<u-row>
2024-06-26 15:14:41 +00:00
<u-button @click="confirmPrint" :disabled="allImgRes.length == 0" type="primary" size="large" shape="square"
2024-06-24 18:19:58 +00:00
custom-style="height:80rpx;font-size: 30rpx;border-radius: 14rpx;margin: 0 10rpx;">确认打印</u-button>
</u-row>
</view>
</template>
</tpx-layout>
</view>
</tpx-dialog>
<!-- 打印+蓝牙连接 -->
<tpx-dialog
v-model:show="printModel.show"
@onClose="handleCloseModal"
custom-style="width: 600rpx;padding: 10rpx;"
title="打印设备"
>
<view style="height: calc(75vh);">
<render-template ref="curPrint" @autoConnectCallback="autoConnectCallback"></render-template>
</view>
</tpx-dialog>
2024-05-12 03:18:44 +00:00
2024-05-10 14:32:39 +00:00
</template>
<script>
2024-06-23 07:32:32 +00:00
import RenderTemplate from "./render-template-native"
2024-05-13 09:47:19 +00:00
import { promiseTimeout } from "@/common/untool"
2024-06-25 04:09:42 +00:00
import CreateImageDataBycanvas from "./create-image-data-bycanvas"
2024-05-12 03:18:44 +00:00
2024-05-10 14:32:39 +00:00
export default {
2024-05-11 05:11:46 +00:00
props:{
2024-06-24 18:19:58 +00:00
2024-05-11 05:11:46 +00:00
},
2024-05-10 14:32:39 +00:00
computed: {
2024-06-25 16:21:43 +00:00
// !!!注意 canvas预览图 必须用不同变量,否则获取不到imgdata
canvImages(){
return [...this.imageUrl]
}
2024-05-12 12:49:00 +00:00
},
components: {
2024-06-25 04:09:42 +00:00
RenderTemplate, RenderTemplate, CreateImageDataBycanvas
2024-05-10 14:32:39 +00:00
},
data () {
return {
2024-06-24 18:19:58 +00:00
previewModel: { show: false },
printModel: { show: false },
imageUrl: [],
2024-06-26 16:04:13 +00:00
paperWidth: 560,
2024-05-12 12:49:00 +00:00
show: false,
2024-06-26 15:14:41 +00:00
allImgRes: []
2024-05-10 14:32:39 +00:00
}
},
mounted () {
2024-06-23 07:56:29 +00:00
// Test
// this.startPrint({ imageUrl: 'https://image-c.weimobwmc.com/ol-6NoX9/ed4f6d5da41f4606811aa8018e2918da.jpg', paperWidth: this.paperWidth })
2024-05-10 14:32:39 +00:00
},
unmounted() {
2024-05-12 12:49:00 +00:00
2024-05-10 14:32:39 +00:00
},
methods: {
2024-05-12 12:49:00 +00:00
handleCloseModal(){
2024-05-10 15:33:45 +00:00
},
2024-06-24 18:19:58 +00:00
handlePreview(index){
uni.previewImage({
urls: this.imageUrl,
current:index
})
},
async startPrint({ imageUrl, paperWidth } = {}, preview = true) {
2024-08-01 05:52:44 +00:00
2024-06-25 16:48:25 +00:00
this.imageUrl = [] // 每次打印前,清空历史数据
2024-06-26 15:14:41 +00:00
this.allImgRes = []
2024-06-24 18:19:58 +00:00
// 默认先做预览
2024-05-12 12:49:00 +00:00
if(imageUrl.length > 0) {
2024-06-24 18:19:58 +00:00
if(!Array.isArray(imageUrl)){
imageUrl = [imageUrl]
2024-05-12 03:18:44 +00:00
}
2024-05-12 12:49:00 +00:00
this.imageUrl = imageUrl
2024-06-23 07:32:32 +00:00
this.paperWidth = paperWidth || this.paperWidth
2024-06-24 18:19:58 +00:00
if(preview) {
this.previewModel.show = true
}
2024-06-26 15:14:41 +00:00
await promiseTimeout(450) // 保证页面节点渲染出来
await this.setAllImgRes()
!preview && this.confirmPrint()
2024-05-12 12:49:00 +00:00
} else {
this.showToast('缺少打印内容')
2024-05-10 14:32:39 +00:00
}
2024-06-24 18:19:58 +00:00
},
2024-06-26 15:14:41 +00:00
setAllImgRes(){
let that = this
2024-06-25 16:48:25 +00:00
let allRes = this.imageUrl.map((url, index)=> {
2024-06-25 15:58:07 +00:00
// !!!注意:必须修改。 canvas渲染的图必须修改url,否则渲染不出来
2024-06-25 16:21:43 +00:00
url = `${url}?t=${new Date().valueOf()}`
2024-06-25 16:48:25 +00:00
return this.$refs[`curCanvas${index}`][0].renderCanvasImg(url, this.paperWidth)
})
2024-08-01 05:52:44 +00:00
this.showLoading()
2024-06-26 15:14:41 +00:00
return Promise.all(allRes).then((imgsRes)=> {
that.allImgRes = imgsRes
2024-06-26 16:49:47 +00:00
console.log("setAllImgRes len======", imgsRes.length)
2024-08-01 05:52:44 +00:00
that.hideLoading()
2024-06-26 15:14:41 +00:00
return imgsRes
})
},
async confirmPrint(){
let res = await this.$refs.curPrint.callPrint({
imgsRes: this.allImgRes,
paperWidth: this.paperWidth
2024-06-24 18:19:58 +00:00
})
},
autoConnectCallback(bl){
// 如果打印设备连接失败 需要显示打印设备弹框
this.printModel.show = !bl
2024-05-10 14:32:39 +00:00
}
}
}
</script>
<style scoped lang="scss">
2024-05-12 12:49:00 +00:00
2024-05-10 14:32:39 +00:00
</style>