Files

978 lines
34 KiB
Vue
Executable File

<template>
<view class="print-content-class">
<view>
<p class="print-title-class">蓝牙打印模板</p>
<xfl-select :list="blueTemplateList" :listOrigin="blueTemplateList" :showItemNum="5" @change="blueTemplateListChange"
:clearable="false" :isCanInput="false" ref="blueTemplateList"
:defaultSelectText="defaultBluePrintTemplateFNumber"
style="height: 64rpx; margin-top: 16rpx;">
</xfl-select>
</view>
<!-- <button class="button-class" @tap="blueConnect()">蓝牙连接</button>-->
<!-- <view>打印机: {{printerName}}</view>-->
<view>
<p>蓝牙打印机列表 {{$store.state.blueConnectedFlag ? '(已连接)' : '(未连接)'}}</p>
<picker mode="selector" :value="index" :range="printerList" @change="onPrinterChanged">
<view class="uni-input">{{ printerList[index] }}</view>
</picker>
</view>
<button class="button-class" @tap="openPrinter()">连接打印机</button>
<button class="button-class" @tap="printLine()">打印测试</button>
<!-- <button type="primary" plain="true" @tap="printRectangle()">打印矩形</button>-->
<!-- <button type="primary" plain="true" @tap="printRoundRectangle()">打印圆角矩形</button>-->
<!-- <button type="primary" plain="true" @tap="printEllipse()">打印椭圆</button>-->
<!-- <button type="primary" plain="true" @tap="printCircle()">打印圆形</button>-->
<!-- <button type="primary" plain="true" @tap="printBarcode()">打印一维码</button>-->
<!-- <button type="primary" plain="true" @tap="printQrcode()">打印二维码</button>-->
<!-- <button type="primary" plain="true" @tap="printImage()">打印图片</button>-->
<!-- <button type="primary" plain="true" @tap="printText()">对齐方式测试</button>-->
<!-- <button type="primary" plain="true" @tap="printText_rotation()">内容旋转测试</button>-->
<!-- <button type="primary" plain="true" @tap="printTicket()">小票打印测试</button>-->
</view>
</template>
<script>
import {
mapState,
mapMutations
} from 'vuex';
const blueToothModule = uni.requireNativePlugin("BlueUtilModule");
import api from "./lpapi-uniplugin"; // DT-280 打印机插件
const dtapi = uni.requireNativePlugin("DothanTech-LPAPI");
let _self;
export default {
data() {
return {
defaultBluePrintTemplateFNumber: "请选择",
blueTemplateList: new Array(),
blueTemplate: "",
// dt-280 demo info
title: "",
printerList: [],
index: 0, // 打印机编号
printerName: "",
orgId: '',
templatePrinterNo: ''
}
},
computed: {
...mapState(['userInfo'])
},
props: {
currentTranType: {
type: [String, Number],
}
},
async beforeMount() {
_self = this;
// 获取打印机模板
if(_self.currentTranType == 'SP_PickMtrl' || _self.currentTranType == 'STK_MisDelivery'||
_self.currentTranType == 'STK_BARCODESMERGE' || _self.currentTranType == 'STK_BARCODESPLIT'||
_self.currentTranType == 'UN_AddPackaging' || _self.currentTranType == 'UN_DelPackaging'||
_self.currentTranType == 'UN_Packaging' || _self.currentTranType == 'STK_SellPicking'){
let systemParameter = uni.getStorageSync('systemParameter');
let bluetoothSwitch = systemParameter.bluetoothSwitch;
if(bluetoothSwitch){
_self.getBluePrintList();
}
}
this.printerList.splice(0);
const list = await api.getPrinters();
if (list && list.length > 0) {
for (let item of list) {
this.printerList.push(item.name);
}
}
else {
this.printerList.push("未检测到打印机");
}
// 切换到第一个 打印机
this.onPrinterChanged();
// 自动连接第一个打印机
if (this.printerName) {
// 连接新的打印机
api.openPrinter(this.printerName, (value) => {
if (value) {
this.$store.commit('blueConnectedFlag', true);
}
});
}
},
methods: {
// 切换打印机
onPrinterChanged(e) {
this.index = e ? e.target.value : 0;
this.printerName = this.printerList[this.index];
},
// 切换模板
blueTemplateListChange:function(orignItem){
this.defaultBluePrintTemplateFNumber = orignItem.orignItem.FNAME;
// 存储打印模板编号
this.templatePrinterNo = orignItem.orignItem.FNUMBER;
// 切换后尝试打印
// this.dtTemplatePrinter(this.blueTemplate)
},
// dt 打印执行函数, 被 bluePrintBarcode 调用
async dtTemplatePrinter(template,layout) {
if (layout.width && layout.height && await api.isPrinterOpened() && template.length > 0) {
// 开始打印任务
await api.startJob(layout);
// await dtapi.startPage();
// 绘制结构
// 一维码: 4
// 二维码: 5
// 文本: 3
// 方框: 2
let len = template.length;
for (let i = 0; i < len; i++) {
let item = template[i];
// 方框
if (item.ElementType === 2) {
await api.drawRectangle({
x: item.X,
y: item.Y,
width: item.Width,
height: item.Height,
lineWidth: item.LineWidth,
// orientation: item.orientation,
});
}
// 文本
else if (item.ElementType === 3) {
if (item.IsInterceptText) {
item.FieldValue = item.FieldValue.substr(0,item.InterceptLength)
}
await api.drawText({
text: item.FieldValue,
x: item.X,
y: item.Y,
width: item.Width,
height: item.Height,
fontHeight: item.FontHeight,
fontStyle: item.FontStyle,
autoReturn: item.AutoReturn
});
}
// 一维码
else if (item.ElementType === 4) {
// let text = getReflection(item.reflection)
await api.draw1DBarcode({
text: item.FieldValue,
x: item.X,
y: item.Y,
width: item.Width,
height: item.Height,
textHeight: item.TextHeight
});
}
// 二维码
else if (item.ElementType === 5) {
// let text = getReflection(item.reflection)
await api.draw2DQRCode(
{
text: item.FieldValue,
x: item.X,
y: item.Y,
width: item.Width,
height: item.Height,
}
);
}
// 直线
else if (item.ElementType === 6) {
await api.drawLine(
{
x1: item.X1,
y1: item.Y1,
x2: item.X2,
y2: item.Y2,
lineWidth: item.LineWidth,
}
);
}
}
// 绘制结束,开始提交打印
await api.commitJob();
await api.endJob();
// await dtapi.endPage();
}
},
blueConnect:function(){
let navigateUrl = '../BTOOTHCONNECT/BTOOTHCONNECT' ;//+"?item="+ encodeURIComponent(JSON.stringify(item));
uni.navigateTo({
url: navigateUrl,
});
},
// 打印函数
bluePrintBarcode(barcodeList){
if (barcodeList.length>0 && this.templatePrinterNo) {
let ifOrgan = uni.getStorageSync('systemParameter').organizationSwitch;
let requestParams = {
"ConfigNumber": this.templatePrinterNo,
"OrgId": ifOrgan ? this.orgId : undefined,
"PrinterBrand": "DT",
BarcodeList: barcodeList
};
uni.showLoading({
icon:'loading',
title:'加载中……',
mask:true
});
this.$request.post('/PrintServices/QueryBluetoothByBill',
requestParams
).then(res => {
uni.hideLoading();
console.log('当前模板', res);
// res.PrintConfig.PrintCount 打印份数
if (res && res.PrintConfig && res.PrintConfig.PrintCount > 0 && res.PrintConfig.ConfigDetails.length>0) {
uni.showToast({
icon: 'none',
title: "开始打印"
});
this.printLoop(res.PrintConfig, res.PrintConfig.PrintCount)
}
else if (res.PrintConfig.PrintCount < 1) {
uni.showToast({
icon: 'none',
title: "打印份数不能小于1"
});
}
else {
uni.showToast({
icon: 'none',
title: "打印参数有误"
});
}
// _self.blueTemplate = res;
}).catch(err => {
uni.hideLoading();
uni.showToast({
icon: 'none',
title: 'QBluetoothByBill 错误',err
});
});
}
// blueToothModule.printBarCode(
// _self.blueTemplate,
// (data)=>{
// uni.hideLoading();
// let msg = JSON.parse(data);
//
// uni.showToast({
// title:msg,
// icon:"none"
// });
// }
// );
},
/**
*
* @param config 打印配置
* @param count 打印次数
* @returns {Promise<void>}
*/
async printLoop(config, count) {
let len = config.ConfigDetails.length;
for (let i = 0; i < len; i++) {
for (let j = 0; j < count; j++) {
// 打印count次
await this.dtTemplatePrinter(config.ConfigDetails[i].ElementConfigs, {
width: config.LabelWidth,
height: config.LabelHeight,
orientation: config.Orientation
});
}
}
},
getBlueTemplateInfo:function(barcodeList){
const _self = this;
let ifOrgan = uni.getStorageSync('systemParameter').organizationSwitch;
let requestParams = {
"ConfigNumber": this.templatePrinterNo,
"OrgId": ifOrgan ? this.orgId : undefined,
"PrinterBrand": "DT",
BarcodeList: barcodeList
};
uni.showLoading({
icon:'loading',
title:'加载中……',
mask:true
});
this.$request.post('/PrintServices/QueryBluetoothByBill',
requestParams
).then(res => {
uni.hideLoading();
}
).catch(err => {
uni.hideLoading();
}
);
},
// 获取当前组织下所有的 蓝牙打印模板 列表
getBluePrintList:function(){
uni.showLoading({
title:"获取蓝牙打印模板中.... "
});
// 判断是否 启用组织隔离
let ifOrgan = uni.getStorageSync('systemParameter').organizationSwitch;
console.log('是否启用组织隔离', ifOrgan);
let requestParams = {
// "billNo": '666',
"orgId": ifOrgan ? this.orgId : undefined, // 嘉励销售公司 100004
"printerBrand": "DT"
};
if (true) {
this.$request.get('/PrintServices/QueryBluetooth', {params: requestParams})
.then(res => {
uni.hideLoading();
let list = res;
if (!list)
return 0;
list.forEach(
function(item, index){
item.value = item.FNAME;
// item.label = item.FNAME;
if (_self.$refs.blueTemplateList && index == 0) {
_self.$refs.blueTemplateList.selectText = item.value;
// 获取第一个模板 具体
_self.templatePrinterNo = item.FNUMBER;
}
else if(index == 0){
_self.defaultBluePrintTemplateFNumber = item.value;
_self.templatePrinterNo = item.FNUMBER;
}
}
);
_self.blueTemplateList = list;
}).catch(err => {uni.hideLoading();});
}
},
// 连接打印机
async openPrinter() {
if (this.printerName) {
// 连接新的打印机
api.openPrinter(this.printerName, (value) => {
if (value) {
uni.showToast({
// icon: 'none',
title: "打印机连接成功",
duration: 500,
});
this.$store.commit('blueConnectedFlag', true);
}
});
}
},
// 打印测试
async printLine() {
const width = 45;
const height = 20;
const lineSpace = 5;
if (await api.isPrinterOpened()) {
// 开始打印任务
await api.startJob({ width, height });
// 绘制直线
await api.drawLine({
x1: 0,
y1: 5,
x2: 40, //45
y2: 5,
lineWidth: 0.5,
});
await api.drawText({
text: '打印测试成功',
x: 5,
y: 5,
width: 20,
height: 10,
fontHeight: 4,
fontStyle: 0,
});
// 绘制虚线
// await api.drawDashLine({
// x1: 0,
// y1: 10,
// x2: width,
// y2: 10,
// dashLen: [0.5, 0.5],
// lineWidth: 0.5,
// });
// //
// await api.drawDashLine({
// x1: 0,
// y1: 15,
// x2: width,
// y2: 15,
// dashLen: [0.5, 0.75, 1],
// lineWidth: 0.5,
// });
// 提交打印任务
await api.commitJob();
}
},
// 打印矩形
async printRectangle() {
const width = 45;
const height = 30;
const padding = 5;
const lineWidth = 0.3;
if (await api.isPrinterOpened()) {
// 开始打印任务;
await api.startJob({ width, height });
// 绘制矩形框
await api.drawRectangle({ width, height, lineWidth });
// 绘制填充矩形
await api.fillRectangle({
x: padding,
y: padding,
width: width - padding * 2,
height: height - padding * 2,
});
// 提交打印任务;
await api.commitJob();
}
},
// 打印圆角矩形
async printRoundRectangle() {
const width = 45;
const height = 30;
const padding = 5;
const lineWidth = 0.3;
const corner = 2;
if (await api.isPrinterOpened()) {
// 开始打印任务;
await api.startJob({ width, height });
// 绘制矩形框
await api.drawRoundRectangle({
width: width,
height: height,
cornerWidth: corner,
cornerHeight: corner,
lineWidth: lineWidth,
});
// 绘制填充矩形
await api.fillRoundRectangle({
x: padding,
y: padding,
width: width - padding * 2,
height: height - padding * 2,
cornerWidth: corner,
cornerHeight: corner,
});
// 提交打印任务;
await api.commitJob();
}
},
// 打印椭圆
async printEllipse() {
const width = 45;
const height = 30;
const padding = 5;
const lineWidth = 0.3;
if (await api.isPrinterOpened()) {
// 开始打印任务;
await api.startJob({ width, height });
// 绘制矩形框
await api.drawEllipse({ width, height, lineWidth });
// 绘制填充矩形
await api.fillEllipse({
x: padding,
y: padding,
width: width - padding * 2,
height: height - padding * 2,
});
// 提交打印任务;
await api.commitJob();
}
},
// 打印圆形
async printCircle() {
const width = 30;
const height = 30;
const padding = 5;
const lineWidth = 0.3;
if (await api.isPrinterOpened()) {
// 开始打印任务;
await api.startJob({ width, height });
// 绘制矩形框
await api.drawCircle({
x: width * 0.5,
y: height * 0.5,
radius: width * 0.5,
lineWidth: lineWidth,
});
// 绘制填充矩形
await api.fillCircle({
x: width * 0.5,
y: height * 0.5,
radius: width * 0.5 - padding,
});
// 提交打印任务;
await api.commitJob();
}
},
// 打印一维码
async printBarcode() {
if (await api.isPrinterOpened()) {
await api.startJob({
width: 45,
height: 30,
});
await api.draw1DBarcode({
text: "DothanTech",
x: 2.5,
y: 2.5,
width: 40,
height: 25,
textHeight: 5,
});
await api.commitJob();
}
},
// 打印二维码
async printQrcode() {
// 第一步,先判断打印机是否已经打开
if (await api.isPrinterOpened()) {
await api.startJob({
width: 45,
height: 45,
});
await api.draw2DQRCode({
text: "德佟电子科技有限公司",
x: 5,
y: 5,
width: 35,
});
await api.commitJob();
}
},
// 打印图片
async printImage() {
const data =
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAYAAADimHc4AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAA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4DHp4R9sIBqlWZUWL63I28HW4XpwG7jxQhXQwGrFAFKLN/bkbeDrcJ0YLfxYoRXwQUlETcgwOOYMuo8aRKItDEND0JoyY/l+1AyTrFVmA7sNl6UMep6mqh8kyggiR7CHVUT9kEYLEeW7+XnkWCrMB3YbbwoYNQp6hZ1PPbmT5o06f9s+7n4cn55AgAAAABJRU5ErkJggg==";
const width = 30;
const height = 30;
if (await api.isPrinterOpened()) {
await api.startJob({ width, height });
await api.drawImage({
image: data,
width: width,
height: height,
});
await api.commitJob();
}
},
// 对齐方式测试
async printText() {
const width = 100;
const height = 45;
const orientation = 90;
const text = "德佟电子科技(上海)有限公司";
const fontSize = 5;
const lineHeight = height / 3; // 分三行显示,每行数据占整个标签纸高度的三分之一;
const fontStyle1 = 0; // 常规样式
const fontStyle2 = 1; // 粗体样式;
const fontStyle3 = 2; // 斜体样式;
if (await api.isPrinterOpened()) {
// 开始打印任务
await api.startJob({ width, height, orientation });
////////////////////////
///// 第一个字符串,水平靠左,垂直居上对齐;
/////////////////////////
// 绘制第一个字符串的显示区域
await api.drawRectangle({
x: 0,
y: 0,
width: width,
height: lineHeight,
lineWidth: 0.3,
});
// 水平靠左对齐
await api.setItemHorizontalAlignment({ alignment: 0 });
// 垂直考上对齐;
await api.setItemVerticalAlignment({ alignment: 0 });
// 第一个字符串,靠左对齐
await api.drawText({
text: text,
x: 0,
y: 0,
width: width,
height: lineHeight,
fontHeight: fontSize,
fontStyle: 0,
});
//////////////////////////////
//// 第二个字符串,水平居中,垂直居中,粗体显示
//////////////////////////////
await api.drawRectangle({
x: 0,
y: lineHeight,
width: width,
height: lineHeight,
lineWidth: 0.3,
});
// 水平居中
await api.setItemHorizontalAlignment({ alignment: 1 });
// 垂直居中
await api.setItemVerticalAlignment({ alignment: 1 });
// 绘制字符串
await api.drawText({
text: text,
x: 0,
y: lineHeight * 1,
width: width,
height: lineHeight,
fontHeight: fontSize,
fontStyle: 1,
});
////////////////////////////////
//// 第三个字符串,水平靠右,垂直距下,斜体
/////////////////////////////////
await api.drawRectangle({
x: 0,
y: lineHeight * 2,
width: width,
height: lineHeight,
lineWidth: 0.3,
});
await api.setItemHorizontalAlignment({ alignment: 2 });
await api.setItemVerticalAlignment({ alignment: 2 });
await api.drawText({
text: text,
x: 0,
y: lineHeight * 2,
width: width,
height: lineHeight,
fontHeight: fontSize,
fontStyle: 2,
});
// 提交打印任务;
await api.commitJob();
}
},
// 内容旋转测试
async printText_rotation() {
const width = 100;
const height = 45;
const orientation = 90;
const fontSize = 5;
const text = "德佟电子科技(上海)有限公司";
// 将显示区域分成四个单元格状态,
// 第一个单元格正常显示
// 第二个单元格进行90度旋转;
// 第三个单元格进行180度旋转
// 第四个单元格进行270度旋转
if (await api.isPrinterOpened()) {
await api.startJob({ width, height, orientation });
////////////////////////////////////////////////
// 绘制第一个单元格
await api.drawRectangle({
x: 0,
y: 0,
width: width * 0.5,
height: height * 0.5,
lineWidth: 0.3,
});
// 第一个单元格数据不进行任何旋转,默认不旋转
await api.setItemOrientation({ orientation: 0 });
// 第一个单元格数据
await api.drawText({
text: text,
x: 0,
y: 0,
width: width * 0.5,
height: height * 0.5,
fontHeight: fontSize,
fontStyle: 0,
autoReturn: false,
});
////////////////////////////////////////////////
// 绘制第二个单元格
await api.drawRectangle({
x: width * 0.5,
y: 0,
width: width * 0.5,
height: height * 0.5,
lineWidth: 0.3,
});
// 第二个单元格数据不进行任何旋转,默认不旋转
await api.setItemOrientation({ orientation: 90 });
// 第二个单元格数据
await api.drawText({
text: text,
x: width * 0.5,
y: 0,
width: width * 0.5,
height: height * 0.5,
fontHeight: fontSize,
fontStyle: 1,
});
////////////////////////////////////////////////
// 绘制第三个单元格
await api.drawRectangle({
x: 0,
y: height * 0.5,
width: width * 0.5,
height: height * 0.5,
lineWidth: 0.3,
});
// 第san个单元格数据不进行任何旋转,默认不旋转
await api.setItemOrientation({ orientation: 180 });
// 第san个单元格数据
await api.drawText({
text: text,
x: 0,
y: height * 0.5,
width: width * 0.5,
height: height * 0.5,
fontHeight: fontSize,
fontStyle: 2,
});
////////////////////////////////////////////////
// 绘制第四个单元格
await api.drawRectangle({
x: width * 0.5,
y: height * 0.5,
width: width * 0.5,
height: height * 0.5,
lineWidth: 0.3,
});
// 第四个单元格数据不进行任何旋转,默认不旋转
await api.setItemOrientation({ orientation: 270 });
// 第四个单元格数据
await api.drawText({
text: text,
x: width * 0.5,
y: height * 0.5,
width: width * 0.5,
height: height * 0.5,
fontHeight: fontSize,
fontStyle: 0,
});
await api.commitJob();
}
},
// 小票打印测试
printTicket() {
this.doPrintTicket({
title: "德佟旗舰店",
id: "20211010888888888",
address: "上海市宝山区长逸路XXX号",
productList: [
{
name: "DP23打印机",
price: 288,
count: 1,
money: 288
},
{
name: "DP30打印机",
price: 388,
count: 1,
money: 388
},
{
name: "DT60打印机",
price: 998,
count: 2,
money: 1996
},
{
name: "DT60PLUS打印机",
price: 9998,
count: 1,
money: 9998
},
],
qrcode: "https://www.detonger.com"
});
},
async doPrintTicket(data) {
const productList = data.productList || [];
// 标签宽度
const labelWidth = 48;
const headerHeight = 20;
const bottomHeight = 10;
// 小票一般都是按行打印的,设置单行的高度;
const lineHeight = 5;
// 二维码大小
const qrcodeWidth = 25;
// 字体大小
// 标签高度是动态的,可以根据实际情况动态的计算标签高度;
// 譬如,产品列表的长度是动态的,标题/二维码等其他信息高度是固定的,可以把两部分的高度相加得到打印内容的实际高度;
const labelHeight = headerHeight + bottomHeight + qrcodeWidth + (productList.length + 11) * lineHeight;
//
const fontSize = 3;
// 分割线线条宽度
const lineWidth = 0.3;
// 当前时间
const time = new Date();
const strTime = `${time.getFullYear()}-${time.getMonth() + 1}-${time.getDate()} ${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}`;
// 第一步,先判断打印机是否已经打开
if (await api.isPrinterOpened()) {
await api.startJob({width: labelWidth, height: labelHeight});
let posY = 0;
// 打印小票的标题
// 标题部分垂直居中,水平居中,字体大小5mm,加粗显示
await api.setItemHorizontalAlignment(1);
await api.setItemVerticalAlignment(1);
await api.drawText({text: data.title, x: 0, y: posY, width: labelWidth, height: headerHeight, fontHeight: 5, fontStyle: 1});
posY += headerHeight;
// 下面的信息组要水平靠左,垂直靠上对齐
await api.setItemHorizontalAlignment(0);
await api.setItemVerticalAlignment(0);
// 打印其他头部信息
// 显示小票的打印时间
await api.drawText({text: `打印时间:${strTime}`, x: 0, y: posY, width: labelWidth, height: lineHeight, fontHeight: fontSize});
posY += lineHeight;
// 显示订单号
await api.drawText({text: `单号:${data.id}`, x: 0, y: posY, width: labelWidth, height: lineHeight, fontHeight: fontSize});
posY += lineHeight;
// 显示公司地址
await api.drawText({text: `地址:${data.address}`, x: 0, y: posY, width: labelWidth, height: lineHeight, fontHeight: fontSize});
posY += lineHeight;
// 显示分割线
await api.drawLine({x1: 0, y1: posY + 2, x2: labelWidth, y2: posY + 2, lineWidth: lineWidth});
posY += lineHeight;
// 打印产品列表
const columnNames = ["产品名称", "单价", "数量", "小计"];
const columnWidths = [24, 8, 8, 8];
// 产品列表: 标题
let posX = 0;
for(let i = 0; i < columnNames.length; i++) {
await api.drawText({text: columnNames[i], x: posX, y: posY, width: columnWidths[i], height: lineHeight, fontHeight: fontSize});
posX += columnWidths[i];
}
posY += lineHeight;
// 产品列表: 详细列表
for(let i = 0; i < productList.length; i++) {
const product = productList[i] || {};
let posX = 0;
// 产品名称
await api.drawText({text: product.name, x: posX, y: posY, width: columnWidths[0], height: lineHeight, fontHeight: fontSize});
posX += columnWidths[0];
// 产品单价
await api.drawText({text: product.price, x: posX, y: posY, width: columnWidths[1], height: lineHeight, fontHeight: fontSize});
posX += columnWidths[1];
// 产品数量
await api.drawText({text: product.count, x: posX, y: posY, width: columnWidths[2], height: lineHeight, fontHeight: fontSize});
posX += columnWidths[2]
// 产品小计
await api.drawText({text: product.money, x: posX, y: posY, width: columnWidths[3], height: lineHeight, fontHeight: fontSize});
posX += columnWidths[3];
// 换行
posY += lineHeight;
}
// 空一行,更好看些
posY += lineHeight;
// 打印合计信息
const offset = 20;
const sumWidth1 = (labelWidth - offset) * 0.5;
const sumCount = productList.reduce((total, item) => total += item.count, 0);
const sumMoney = productList.reduce((total, item) => total += item.money, 0);
// 总件数
await api.drawText({text: `总件数:${sumCount}`, x: offset, y: posY, width: sumWidth1, height: lineHeight, fontHeight: fontSize});
// 总金额
await api.drawText({text: `总计:${sumMoney}`, x: offset + sumWidth1, y: posY, width: sumWidth1, height: lineHeight, fontHeight: fontSize});
posY += lineHeight;
// 显示分割线
await api.drawLine({x1: 0, y1: posY + 2, x2: labelWidth, y2: posY + 2, lineWidth: lineWidth});
posY += lineHeight;
// 换行
posY += lineHeight;
// 二维码标题 水平居中
await api.setItemHorizontalAlignment(1);
await api.drawText({text: "欢迎访问德佟电子官方旗舰店", x: 0, y: posY, width: labelWidth, height: lineHeight, fontHeight: fontSize});
posY += lineHeight;
// 换行
posY += lineHeight;
// 二维码提示信息
await api.drawText({text: "扫码访问", x: 0, y: posY, width: labelWidth, height: lineHeight, fontHeight: fontSize});
posY += lineHeight;
// 打印二维码
await api.draw2DQRCode({text: data.qrcode, x: (labelWidth - qrcodeWidth) * 0.5, y: posY, width: qrcodeWidth});
posY += qrcodeWidth;
// 绘制结束,开始提交打印
await api.commitJob();
}
},
},
watch: {
orgId(){
let ifOrgan = uni.getStorageSync('systemParameter').organizationSwitch;
console.log('是否启用组织隔离', ifOrgan);
if(_self.currentTranType == 'SP_PickMtrl' || _self.currentTranType == 'STK_MisDelivery'||
_self.currentTranType == 'STK_BARCODESMERGE' || _self.currentTranType == 'STK_BARCODESPLIT'||
_self.currentTranType == 'UN_AddPackaging' || _self.currentTranType == 'UN_DelPackaging'||
_self.currentTranType == 'UN_Packaging' || _self.currentTranType == 'STK_SellPicking'){
let systemParameter = uni.getStorageSync('systemParameter');
let bluetoothSwitch = systemParameter.bluetoothSwitch;
if(bluetoothSwitch){
if (ifOrgan) {
this.getBluePrintList();
}
}
}
}
}
}
</script>
<style>
.print-title-class{
width:200rpx;
height:36rpx;
font-size:28rpx;
color:rgba(51,51,51,1);
line-height:36rpx;
margin-top: 24rpx;
}
.button-class{
height:80rpx;
line-height:80rpx;
border-radius:4rpx;
font-size:32rpx;
margin-top: 32rpx;
background-color: #4662FF;
color: #FFFFFF;
}
.print-content-class{
margin-top: 32rpx;
margin-left: 32rpx;
margin-right: 32rpx;
font-family:Helvetica;
}
</style>