This commit is contained in:
aihe 2024-05-13 17:47:19 +08:00
parent 64670054fd
commit 63874c2afd
4 changed files with 108 additions and 78 deletions

View File

@ -208,18 +208,18 @@ const uniPromiseApi = (name, options = {}, extObj = { })=> {
uni?.[name]?.({
...options,
success(res){
console.error(`uni.${name} -execResult:success====`, res)
resolve({ res })
console.log(`uni.${name} -execResult:success====`, res)
if(extObj.successText) {
showToast(successText)
}
resolve({ res })
},
fail(err){
console.error(`uni.${name} -execResult:fail====`, err)
resolve({ err })
if(extObj.failText) {
showToast(failText)
}
resolve({ err })
}
})
})

View File

@ -23,23 +23,13 @@
</template>
<script>
import { showLoading, showToast, hideLoading, canvasGetImageData, getImageInfo } from "@/common/untool"
import { showLoading, showToast, hideLoading, canvasGetImageData, getImageInfo, promiseTimeout } from "@/common/untool"
import * as canvasUtil from "@/common/canvasUtil"
import * as printJob from './print/printJob'
export default {
props:{
imageUrl: {
default () {
return ''
}
},
paperWidth: {
default () {
return 320
}
},
},
computed: {
@ -52,6 +42,8 @@ export default {
imgData: '',
percentage: 0, // 打印进度
threshold: 200, // 阈值(值越大,图片打印越深)
imageUrl: '',
paperWidth: 320,
}
},
mounted () {
@ -61,74 +53,71 @@ export default {
},
methods: {
async initData() {
this.imgData = await this.renderCanvasImg();
await this.renderCanvasImg(this.imageUrl);
},
// 打印
async handlePrint(){
handlePrint(){
const that = this
let { deviceId, serviceId, characteristicId } = this.$refs.curBlueTooth.getInstance()
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: '好的',
});
},
onError: ()=> {
that.percentage = 0
showToast(`打印异常`)
hideLoading()
return new Promise((resolve, reject)=> {
let { bl } = that.checkPrintParam()
if(!bl) {
return reject('打印参数校验不通过')
}
}
console.log('====this.imgData===', this.imgData)
//打印图片
printJob.printImage(
opt,
// 将图片数据转成位图数据
printJob.overwriteImageData({
threshold: this.threshold,
imageData: this.imgData,
width: this.canvasWidth,
height: this.canvasHeight,
}),
);
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)
}
}
console.log('====that.imgData===', that.imgData)
//打印图片
printJob.printImage(
opt,
// 将图片数据转成位图数据
printJob.overwriteImageData({
threshold: that.threshold,
imageData: that.imgData,
width: that.canvasWidth,
height: that.canvasHeight,
}),
);
})
},
// 选择本地文件打印-- TODO
// chooseImage() {
// wx.chooseImage({
// success: (res) => {
// const tempFilePath = res.tempFilePaths[0];
// },
// });
// },
renderCanvasImg() {
let filePath = this.imageUrl
renderCanvasImg(filePath) {
let that = this
showLoading()
showLoading()
console.log("===renderCanvasImg:filePath=====", filePath)
return new Promise(async (resolve, reject)=> {
try{
let res = await getImageInfo(filePath)
console.log("getImageInfo res====", res)
const ctx = uni.createCanvasContext(that.canvasId, that);
// 打印宽度须是8的整数倍,这里处理掉多余的,使得宽度合适,不然有可能乱码
const remainder = that.paperWidth % 8;
const w = remainder === 0 ? that.paperWidth : that.paperWidth - remainder;
// 等比算出图片的高度
const h = Math.floor((res.height * w) / res.width);
this.canvasWidth = w
this.canvasHeight = h
that.canvasWidth = w
that.canvasHeight = h
// 在canvas 画一张图片
let imgRect = canvasUtil.containImg(0, 0, w, h, res.width, res.height)
let ctData = [imgRect.dx, imgRect.dy, imgRect.dWidth, imgRect.dHeight]
@ -138,18 +127,50 @@ export default {
// 把原图缩放到 打印宽度的比例 绘制
ctx.drawImage(filePath, ...ctData);
ctx.draw(false, () => {
setTimeout( async ()=> {
let { data } = await canvasGetImageData({ canvasId: that.canvasId, width: w, height: h, })
hideLoading()
resolve(data)
}, 1000)
ctx.draw(false, async() => {
await promiseTimeout(1000)
let { data } = await canvasGetImageData({ canvasId: that.canvasId, width: w, height: h, })
hideLoading()
that.imgData = data
resolve(data)
});
}catch(e){
hideLoading()
reject(e)
}
})
},
checkPrintParam() {
let msg = '', bl = false
let { deviceId, serviceId, characteristicId } = this.$refs.curBlueTooth.getInstance()
!this.imgData && (msg = '打印内容不能为空,请重试')
if(!msg && (deviceId || serviceId || characteristicId)) {
msg = '请选选择打印设备'
}
msg && (showToast(msg))
return {
msg,
bl: !msg
}
},
// 给外部调用api,打印自定义图
async callPrint({ paperWidth, imageUrl } = {}) {
try{
this.paperWidth = paperWidth
if(this.imageUrl != imageUrl || !this.imgData) {
this.imageUrl = imageUrl
await this.renderCanvasImg(this.imageUrl);
}
let conSta = await this.$refs.curBlueTooth.autoConnect()
if(conSta) {
let { bl } = this.checkPrintParam()
if(bl) {
await this.handlePrint()
return true
}
}
} catch(e){
}
}
}
}

View File

@ -6,15 +6,15 @@
custom-style="width: 600rpx;padding: 10rpx;"
title="打印"
>
<view v-if="show" style="height: calc(75vh);">
<render-template ref="curPrint" :imageUrl="imageUrl" :paperWidth="paperWidth"></render-template>
<view style="height: calc(75vh);">
<render-template ref="curPrint"></render-template>
</view>
</tpx-dialog>
</template>
<template v-else>
<template v-if="show">
<render-template ref="curPrint" :imageUrl="imageUrl" :paperWidth="paperWidth"></render-template>
<template >
<render-template ref="curPrint"></render-template>
</template>
</template>
@ -23,6 +23,7 @@
<script>
import RenderTemplate from "./render-template"
import { promiseTimeout } from "@/common/untool"
export default {
props:{
@ -64,6 +65,11 @@ export default {
this.imageUrl = imageUrl
this.paperWidth = paperWidth
this.show = true
await promiseTimeout(1000)
let res = await this.$refs.curPrint.callPrint({
imageUrl,
paperWidth
})
} else {
this.showToast('缺少打印内容')
}

View File

@ -146,7 +146,7 @@ export default {
async mounted () {
// 初始化蓝牙模块
await this.openBluetoothAdapter()
this.autoConnect()
// this.autoConnect()
},
unmounted() {
this.stopDiscoveryPrinter()
@ -166,10 +166,13 @@ export default {
let { res, err } = await this.connect()
conSta = !!res
}
console.log("=========当前蓝牙连接状态=======", conSta)
// 连接不成功,打开蓝牙配置页面
if(!conSta) {
this.showConnectPage = true
this.discoveryPrinter()
} else {
this.stopDiscoveryPrinter()
}
return conSta
},