uu
This commit is contained in:
parent
64670054fd
commit
63874c2afd
@ -208,18 +208,18 @@ const uniPromiseApi = (name, options = {}, extObj = { })=> {
|
|||||||
uni?.[name]?.({
|
uni?.[name]?.({
|
||||||
...options,
|
...options,
|
||||||
success(res){
|
success(res){
|
||||||
console.error(`uni.${name} -execResult:success====`, res)
|
console.log(`uni.${name} -execResult:success====`, res)
|
||||||
resolve({ res })
|
|
||||||
if(extObj.successText) {
|
if(extObj.successText) {
|
||||||
showToast(successText)
|
showToast(successText)
|
||||||
}
|
}
|
||||||
|
resolve({ res })
|
||||||
},
|
},
|
||||||
fail(err){
|
fail(err){
|
||||||
console.error(`uni.${name} -execResult:fail====`, err)
|
console.error(`uni.${name} -execResult:fail====`, err)
|
||||||
resolve({ err })
|
|
||||||
if(extObj.failText) {
|
if(extObj.failText) {
|
||||||
showToast(failText)
|
showToast(failText)
|
||||||
}
|
}
|
||||||
|
resolve({ err })
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@ -23,23 +23,13 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<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 canvasUtil from "@/common/canvasUtil"
|
||||||
import * as printJob from './print/printJob'
|
import * as printJob from './print/printJob'
|
||||||
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
props:{
|
props:{
|
||||||
imageUrl: {
|
|
||||||
default () {
|
|
||||||
return ''
|
|
||||||
}
|
|
||||||
},
|
|
||||||
paperWidth: {
|
|
||||||
default () {
|
|
||||||
return 320
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
|
|
||||||
@ -52,6 +42,8 @@ export default {
|
|||||||
imgData: '',
|
imgData: '',
|
||||||
percentage: 0, // 打印进度
|
percentage: 0, // 打印进度
|
||||||
threshold: 200, // 阈值(值越大,图片打印越深)
|
threshold: 200, // 阈值(值越大,图片打印越深)
|
||||||
|
imageUrl: '',
|
||||||
|
paperWidth: 320,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
mounted () {
|
mounted () {
|
||||||
@ -61,74 +53,71 @@ export default {
|
|||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
async initData() {
|
async initData() {
|
||||||
this.imgData = await this.renderCanvasImg();
|
await this.renderCanvasImg(this.imageUrl);
|
||||||
},
|
},
|
||||||
// 打印
|
// 打印
|
||||||
async handlePrint(){
|
handlePrint(){
|
||||||
const that = this
|
const that = this
|
||||||
let { deviceId, serviceId, characteristicId } = this.$refs.curBlueTooth.getInstance()
|
let { deviceId, serviceId, characteristicId } = this.$refs.curBlueTooth.getInstance()
|
||||||
|
return new Promise((resolve, reject)=> {
|
||||||
const opt = {
|
let { bl } = that.checkPrintParam()
|
||||||
...{ deviceId, serviceId, characteristicId },
|
if(!bl) {
|
||||||
onProgress: (percentage) => {
|
return reject('打印参数校验不通过')
|
||||||
console.log("打印进度====", percentage)
|
|
||||||
that.percentage = percentage
|
|
||||||
|
|
||||||
},
|
|
||||||
lasterSuccess: () => {
|
|
||||||
that.percentage = 0
|
|
||||||
hideLoading()
|
|
||||||
uni.showModal({
|
|
||||||
title: '提示',
|
|
||||||
content: '数据已发送完,请检查打印的内容是否正常',
|
|
||||||
showCancel: false,
|
|
||||||
confirmText: '好的',
|
|
||||||
});
|
|
||||||
},
|
|
||||||
onError: ()=> {
|
|
||||||
that.percentage = 0
|
|
||||||
showToast(`打印异常`)
|
|
||||||
hideLoading()
|
|
||||||
}
|
}
|
||||||
}
|
const opt = {
|
||||||
console.log('====this.imgData===', this.imgData)
|
...{ deviceId, serviceId, characteristicId },
|
||||||
//打印图片
|
onProgress: (percentage) => {
|
||||||
printJob.printImage(
|
console.log("打印进度====", percentage)
|
||||||
opt,
|
that.percentage = percentage
|
||||||
// 将图片数据转成位图数据
|
},
|
||||||
printJob.overwriteImageData({
|
lasterSuccess: () => {
|
||||||
threshold: this.threshold,
|
that.percentage = 0
|
||||||
imageData: this.imgData,
|
hideLoading()
|
||||||
width: this.canvasWidth,
|
uni.showModal({
|
||||||
height: this.canvasHeight,
|
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
|
renderCanvasImg(filePath) {
|
||||||
// chooseImage() {
|
|
||||||
// wx.chooseImage({
|
|
||||||
// success: (res) => {
|
|
||||||
// const tempFilePath = res.tempFilePaths[0];
|
|
||||||
|
|
||||||
// },
|
|
||||||
// });
|
|
||||||
// },
|
|
||||||
|
|
||||||
renderCanvasImg() {
|
|
||||||
let filePath = this.imageUrl
|
|
||||||
let that = this
|
let that = this
|
||||||
showLoading()
|
showLoading()
|
||||||
|
console.log("===renderCanvasImg:filePath=====", filePath)
|
||||||
return new Promise(async (resolve, reject)=> {
|
return new Promise(async (resolve, reject)=> {
|
||||||
try{
|
try{
|
||||||
let res = await getImageInfo(filePath)
|
let res = await getImageInfo(filePath)
|
||||||
|
console.log("getImageInfo res====", res)
|
||||||
const ctx = uni.createCanvasContext(that.canvasId, that);
|
const ctx = uni.createCanvasContext(that.canvasId, that);
|
||||||
// 打印宽度须是8的整数倍,这里处理掉多余的,使得宽度合适,不然有可能乱码
|
// 打印宽度须是8的整数倍,这里处理掉多余的,使得宽度合适,不然有可能乱码
|
||||||
const remainder = that.paperWidth % 8;
|
const remainder = that.paperWidth % 8;
|
||||||
const w = remainder === 0 ? that.paperWidth : that.paperWidth - remainder;
|
const w = remainder === 0 ? that.paperWidth : that.paperWidth - remainder;
|
||||||
// 等比算出图片的高度
|
// 等比算出图片的高度
|
||||||
const h = Math.floor((res.height * w) / res.width);
|
const h = Math.floor((res.height * w) / res.width);
|
||||||
this.canvasWidth = w
|
that.canvasWidth = w
|
||||||
this.canvasHeight = h
|
that.canvasHeight = h
|
||||||
// 在canvas 画一张图片
|
// 在canvas 画一张图片
|
||||||
let imgRect = canvasUtil.containImg(0, 0, w, h, res.width, res.height)
|
let imgRect = canvasUtil.containImg(0, 0, w, h, res.width, res.height)
|
||||||
let ctData = [imgRect.dx, imgRect.dy, imgRect.dWidth, imgRect.dHeight]
|
let ctData = [imgRect.dx, imgRect.dy, imgRect.dWidth, imgRect.dHeight]
|
||||||
@ -138,18 +127,50 @@ export default {
|
|||||||
// 把原图缩放到 打印宽度的比例 绘制
|
// 把原图缩放到 打印宽度的比例 绘制
|
||||||
ctx.drawImage(filePath, ...ctData);
|
ctx.drawImage(filePath, ...ctData);
|
||||||
|
|
||||||
ctx.draw(false, () => {
|
ctx.draw(false, async() => {
|
||||||
setTimeout( async ()=> {
|
await promiseTimeout(1000)
|
||||||
let { data } = await canvasGetImageData({ canvasId: that.canvasId, width: w, height: h, })
|
let { data } = await canvasGetImageData({ canvasId: that.canvasId, width: w, height: h, })
|
||||||
hideLoading()
|
hideLoading()
|
||||||
resolve(data)
|
that.imgData = data
|
||||||
}, 1000)
|
resolve(data)
|
||||||
});
|
});
|
||||||
}catch(e){
|
}catch(e){
|
||||||
hideLoading()
|
hideLoading()
|
||||||
reject(e)
|
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){
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,15 +6,15 @@
|
|||||||
custom-style="width: 600rpx;padding: 10rpx;"
|
custom-style="width: 600rpx;padding: 10rpx;"
|
||||||
title="打印"
|
title="打印"
|
||||||
>
|
>
|
||||||
<view v-if="show" style="height: calc(75vh);">
|
<view style="height: calc(75vh);">
|
||||||
<render-template ref="curPrint" :imageUrl="imageUrl" :paperWidth="paperWidth"></render-template>
|
<render-template ref="curPrint"></render-template>
|
||||||
</view>
|
</view>
|
||||||
</tpx-dialog>
|
</tpx-dialog>
|
||||||
|
|
||||||
</template>
|
</template>
|
||||||
<template v-else>
|
<template v-else>
|
||||||
<template v-if="show">
|
<template >
|
||||||
<render-template ref="curPrint" :imageUrl="imageUrl" :paperWidth="paperWidth"></render-template>
|
<render-template ref="curPrint"></render-template>
|
||||||
</template>
|
</template>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
@ -23,6 +23,7 @@
|
|||||||
<script>
|
<script>
|
||||||
|
|
||||||
import RenderTemplate from "./render-template"
|
import RenderTemplate from "./render-template"
|
||||||
|
import { promiseTimeout } from "@/common/untool"
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
props:{
|
props:{
|
||||||
@ -64,6 +65,11 @@ export default {
|
|||||||
this.imageUrl = imageUrl
|
this.imageUrl = imageUrl
|
||||||
this.paperWidth = paperWidth
|
this.paperWidth = paperWidth
|
||||||
this.show = true
|
this.show = true
|
||||||
|
await promiseTimeout(1000)
|
||||||
|
let res = await this.$refs.curPrint.callPrint({
|
||||||
|
imageUrl,
|
||||||
|
paperWidth
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
this.showToast('缺少打印内容')
|
this.showToast('缺少打印内容')
|
||||||
}
|
}
|
||||||
|
|||||||
@ -146,7 +146,7 @@ export default {
|
|||||||
async mounted () {
|
async mounted () {
|
||||||
// 初始化蓝牙模块
|
// 初始化蓝牙模块
|
||||||
await this.openBluetoothAdapter()
|
await this.openBluetoothAdapter()
|
||||||
this.autoConnect()
|
// this.autoConnect()
|
||||||
},
|
},
|
||||||
unmounted() {
|
unmounted() {
|
||||||
this.stopDiscoveryPrinter()
|
this.stopDiscoveryPrinter()
|
||||||
@ -166,10 +166,13 @@ export default {
|
|||||||
let { res, err } = await this.connect()
|
let { res, err } = await this.connect()
|
||||||
conSta = !!res
|
conSta = !!res
|
||||||
}
|
}
|
||||||
|
console.log("=========当前蓝牙连接状态=======", conSta)
|
||||||
// 连接不成功,打开蓝牙配置页面
|
// 连接不成功,打开蓝牙配置页面
|
||||||
if(!conSta) {
|
if(!conSta) {
|
||||||
this.showConnectPage = true
|
this.showConnectPage = true
|
||||||
this.discoveryPrinter()
|
this.discoveryPrinter()
|
||||||
|
} else {
|
||||||
|
this.stopDiscoveryPrinter()
|
||||||
}
|
}
|
||||||
return conSta
|
return conSta
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user