2024-05-10 14:32:39 +00:00
|
|
|
|
<template>
|
|
|
|
|
|
<tpx-bluetooth v-model:value="blueToothData">
|
|
|
|
|
|
<template v-slot:button>
|
|
|
|
|
|
<u-button @click="writeBLECharacteristicValue" type="primary" :disabled="!(blueToothData.deviceId && blueToothData.serviceId && blueToothData.characteristicId)">打印</u-button>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
</tpx-bluetooth>
|
2024-05-11 05:11:46 +00:00
|
|
|
|
<canvas canvas-id="shareCanvas" :style="{width:canvasWidth + 'px', height: canvasHeight + 'px'}"></canvas>
|
2024-05-10 14:32:39 +00:00
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
|
import { showLoading, showToast, hideLoading } from "@/common/untool"
|
|
|
|
|
|
import PrinterJobs from './print/printerjobs.js'
|
|
|
|
|
|
import * as printerUtil from './print/printerutil.js'
|
|
|
|
|
|
import * as util from './print/util.js'
|
|
|
|
|
|
|
|
|
|
|
|
export default {
|
2024-05-11 05:11:46 +00:00
|
|
|
|
props:{
|
|
|
|
|
|
imageUrl: {
|
|
|
|
|
|
default () {
|
|
|
|
|
|
return ''
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
canvasWidth: {
|
|
|
|
|
|
default () {
|
|
|
|
|
|
return 320
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
canvasHeight: {
|
|
|
|
|
|
default () {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
2024-05-10 14:32:39 +00:00
|
|
|
|
computed: {
|
|
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
data () {
|
|
|
|
|
|
return {
|
|
|
|
|
|
canvasId: 'shareCanvas',
|
2024-05-11 05:11:46 +00:00
|
|
|
|
imgZipData: '',
|
2024-05-10 14:32:39 +00:00
|
|
|
|
blueToothData: {
|
|
|
|
|
|
deviceId: '',
|
|
|
|
|
|
serviceId: '',
|
|
|
|
|
|
characteristicId: ''
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
mounted () {
|
2024-05-10 15:33:45 +00:00
|
|
|
|
this.initData()
|
2024-05-10 14:32:39 +00:00
|
|
|
|
},
|
|
|
|
|
|
unmounted() {
|
|
|
|
|
|
},
|
|
|
|
|
|
methods: {
|
2024-05-10 15:33:45 +00:00
|
|
|
|
async initData() {
|
2024-05-11 05:11:46 +00:00
|
|
|
|
this.imgZipData = await this.renderCanvasImg();
|
2024-05-10 15:33:45 +00:00
|
|
|
|
},
|
2024-05-10 14:32:39 +00:00
|
|
|
|
// 打印-发送二进制数据
|
|
|
|
|
|
async writeBLECharacteristicValue(){
|
2024-05-11 05:11:46 +00:00
|
|
|
|
let imgZipData = this.imgZipData
|
2024-05-10 14:32:39 +00:00
|
|
|
|
let printerJobs = new PrinterJobs()
|
|
|
|
|
|
|
|
|
|
|
|
printerJobs
|
2024-05-10 16:19:59 +00:00
|
|
|
|
// .print(`下单时间:2021-07-25 15:30:23`)
|
|
|
|
|
|
// .print(printerUtil.fillLine())
|
|
|
|
|
|
// .print('备注')
|
|
|
|
|
|
// .setSize(2, 2)
|
|
|
|
|
|
// .setBold(true)
|
|
|
|
|
|
// .print(`叫那位非常漂亮的小姐姐送来,其他人送来拒收`)
|
|
|
|
|
|
// .print(printerUtil.fillLine())
|
2024-05-11 05:11:46 +00:00
|
|
|
|
.printQrcode(imgZipData, this.canvasWidth)
|
2024-05-10 14:32:39 +00:00
|
|
|
|
// .setSize(1, 1)
|
|
|
|
|
|
// .setBold(false)
|
|
|
|
|
|
// .print(printerUtil.fillLine('*'))
|
|
|
|
|
|
// .setAlign('lt')
|
|
|
|
|
|
// .printArray(arr)
|
|
|
|
|
|
// .print(printerUtil.fillAround('其它'))
|
|
|
|
|
|
// .print(printerUtil.inline('平台随机立减', `-2.10`))
|
|
|
|
|
|
// .print(printerUtil.inline('平台服务费', `-10.99`))
|
|
|
|
|
|
// .printArray(arr2)
|
|
|
|
|
|
// .print(printerUtil.fillLine())
|
|
|
|
|
|
// .setAlign('rt')
|
|
|
|
|
|
// .setSize(1, 2)
|
|
|
|
|
|
// .setBold(true)
|
|
|
|
|
|
// .print(`用户支付:¥99.99`)
|
2024-05-10 16:19:59 +00:00
|
|
|
|
// .print(printerUtil.fillLine())
|
2024-05-10 14:32:39 +00:00
|
|
|
|
|
|
|
|
|
|
let buffer = printerJobs.buffer();
|
|
|
|
|
|
console.log('buffer>>>',buffer)
|
|
|
|
|
|
showLoading('正在打印')
|
|
|
|
|
|
try{
|
|
|
|
|
|
await this.printbuffs(buffer)
|
|
|
|
|
|
hideLoading()
|
|
|
|
|
|
showToast('打印成功')
|
|
|
|
|
|
}catch(e){
|
|
|
|
|
|
console.log('打印异常', e)
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
printbuffs(buffer) {
|
|
|
|
|
|
let _this = this
|
|
|
|
|
|
return new Promise(async (resolve, reject)=> {
|
|
|
|
|
|
// 1.并行调用多次会存在写失败的可能性
|
|
|
|
|
|
// 2.建议每次写入不超过20字节
|
|
|
|
|
|
// 分包处理,延时调用
|
|
|
|
|
|
const maxChunk = 20;
|
|
|
|
|
|
for (let i = 0, j = 0, length = buffer.byteLength; i < length; i += maxChunk, j++) {
|
|
|
|
|
|
let subPackage = buffer.slice(i, i + maxChunk <= length ? (i + maxChunk) : length);
|
|
|
|
|
|
try{
|
|
|
|
|
|
await _this.printbuff(subPackage)
|
|
|
|
|
|
}catch(msg){
|
|
|
|
|
|
console.log("printbuffs:异常==", msg)
|
|
|
|
|
|
showToast('分段printbuff异常', msg)
|
|
|
|
|
|
reject(msg)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
resolve()
|
|
|
|
|
|
})
|
|
|
|
|
|
},
|
2024-05-11 02:26:02 +00:00
|
|
|
|
getImageZip(params = {}) {
|
2024-05-10 14:32:39 +00:00
|
|
|
|
return new Promise((resolve, reject)=> {
|
|
|
|
|
|
uni.canvasGetImageData({
|
|
|
|
|
|
x: 0,
|
|
|
|
|
|
y: 0,
|
|
|
|
|
|
...params,
|
|
|
|
|
|
success(res) {
|
|
|
|
|
|
console.log("img====",res);
|
|
|
|
|
|
if(res.data) {
|
|
|
|
|
|
let data = util.zip_image(res)
|
|
|
|
|
|
console.log("zip====", data);
|
|
|
|
|
|
resolve(data);
|
|
|
|
|
|
} else {
|
2024-05-11 02:26:02 +00:00
|
|
|
|
showToast('获取图片信息异常:getImageZip')
|
|
|
|
|
|
reject('获取图片信息异常:getImageZip')
|
2024-05-10 14:32:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
},
|
2024-05-11 05:11:46 +00:00
|
|
|
|
renderCanvasImg() {
|
|
|
|
|
|
let filePath = this.imageUrl
|
2024-05-10 14:32:39 +00:00
|
|
|
|
let that = this
|
|
|
|
|
|
return new Promise((resolve, reject)=> {
|
|
|
|
|
|
const cxt = uni.createCanvasContext(that.canvasId, that);
|
|
|
|
|
|
let scla = 1
|
|
|
|
|
|
uni.getImageInfo({
|
|
|
|
|
|
src: filePath,
|
|
|
|
|
|
success(res) {
|
2024-05-11 02:26:02 +00:00
|
|
|
|
// 按原图比例算出 新的画图高度
|
2024-05-11 05:11:46 +00:00
|
|
|
|
let height = that.canvasHeight
|
|
|
|
|
|
if(!height) {
|
|
|
|
|
|
height = parseInt(that.canvasWidth * (res.height / res.width))
|
|
|
|
|
|
that.$emit("update:canvasHeight", height)
|
|
|
|
|
|
}
|
|
|
|
|
|
cxt.drawImage(filePath, 0, 0, that.canvasWidth, that.canvasHeight);
|
2024-05-10 15:33:45 +00:00
|
|
|
|
cxt.draw(true, ()=> {
|
|
|
|
|
|
setTimeout( async ()=> {
|
2024-05-11 05:11:46 +00:00
|
|
|
|
let data = await that.getImageZip({ canvasId: that.canvasId, width: that.canvasWidth, height, })
|
2024-05-10 15:33:45 +00:00
|
|
|
|
resolve(data)
|
|
|
|
|
|
}, 1000)
|
|
|
|
|
|
});
|
2024-05-10 14:32:39 +00:00
|
|
|
|
},
|
|
|
|
|
|
fail(e) {
|
|
|
|
|
|
reject(e)
|
|
|
|
|
|
console.log(e)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
},
|
|
|
|
|
|
printbuff(buffer) {
|
|
|
|
|
|
var _this = this
|
|
|
|
|
|
return new Promise((resolve, reject)=> {
|
|
|
|
|
|
uni.writeBLECharacteristicValue({
|
|
|
|
|
|
..._this.blueToothData,
|
|
|
|
|
|
// 这里的value是ArrayBuffer类型
|
|
|
|
|
|
value: buffer,
|
|
|
|
|
|
success:(res)=> {
|
|
|
|
|
|
console.log('writeBLECharacteristicValue success', res.errMsg)
|
2024-05-10 16:32:44 +00:00
|
|
|
|
// 必须加延迟返回,防止高频率调用
|
|
|
|
|
|
setTimeout(()=> {
|
|
|
|
|
|
resolve()
|
2024-05-10 17:42:41 +00:00
|
|
|
|
}, 10)
|
2024-05-10 14:32:39 +00:00
|
|
|
|
},
|
|
|
|
|
|
fail:(res)=> {
|
|
|
|
|
|
console.log('writeBLECharacteristicValue fail', res.errMsg)
|
|
|
|
|
|
reject(res.errMsg)
|
|
|
|
|
|
},
|
|
|
|
|
|
complete (e) {
|
|
|
|
|
|
console.log('writeBLECharacteristicValue complete', e)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style scoped lang="scss">
|
|
|
|
|
|
/*图片隐藏到窗口外*/
|
|
|
|
|
|
canvas {
|
|
|
|
|
|
position: absolute;
|
|
|
|
|
|
z-index: -999;
|
2024-05-11 05:11:46 +00:00
|
|
|
|
top: -999px;
|
2024-05-10 14:32:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
</style>
|