新增(初始化):初始化

This commit is contained in:
肖晓霖
2021-06-27 17:28:31 +08:00
commit 282e05beb2
162 changed files with 38931 additions and 0 deletions

View File

@@ -0,0 +1,200 @@
function getStatus() {
return new Promise((reslove, reject) => {
uni.getBluetoothAdapterState({
complete(res) {
console.log(res);
reslove(res);
}
})
});
}
let kit = {
writeCharacter: false,
writeCharacterId: false,
writeServiceId: '',
readCharacter: false,
readCharacterId: '',
readServiceId: '',
notifyCharacter: false,
notifyCharaterId: '',
notifyServiceId: '',
deviceId: '',
serviceId: '',
openSettingPage() {
// #ifdef APP-PLUS
let main = plus.android.runtimeMainActivity();
let Intent = plus.android.importClass('android.content.Intent');
let Settings = plus.android.importClass('android.provider.Settings');
main.startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS));
// #endif
},
/**
* 蓝牙是否启用
*/
isEnabled() {
return getStatus().then(res => {
return res.errCode != 10000
});
},
isAvailable() {
return getStatus().then(res => res.available || false);
},
isDiscovering() {
return getStatus().then(res => res.discovering || false);
},
open() {
return this.openAdapter();
},
openAdapter() {
let _this = this;
return new Promise((reslove, reject) => {
uni.openBluetoothAdapter({
success: function(res) {
reslove(res);
},
fail: (res) => {
console.log(res);
if (res.errCode === 10001) {
//如果没打开蓝牙提示用户打开蓝牙
// #ifdef APP-PLUS
let device = uni.getSystemInfoSync().platform;
let BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
let BAdapter = BluetoothAdapter.getDefaultAdapter();
if(!BAdapter.isEnabled()) {
BAdapter.enable();
}else {
_this.openAdapter();
}
// #endif
}
reject(res);
}
})
})
},
close() {
return new Promise((reslove, reject) => {
uni.closeBluetoothAdapter({
success() {
reslove();
},
fail(res) {
reject(res);
}
});
});
},
closeConnect(deviceId) {
deviceId = deviceId || this.deviceId;
return new Promise((reslove, reject) => {
uni.closeBLEConnection({
deviceId: deviceId,
success() {
reslove();
},
fail(res) {
if (res.errCode === 10004) {
reslove();
} else {
console.log(res);
reject(res);
}
}
})
})
},
async startSearch(onDeviceFind) {
return new Promise((reslove, reject) => {
uni.startBluetoothDevicesDiscovery({
success: (res) => {
let i = 0;
let intervalId = setInterval(() => {
this.getDevices().then(devices => {
onDeviceFind(devices);
if (i++ >= 5) {
clearInterval(intervalId);
this.stopSearch();
reslove();
}
});
}, 1000);
},
fail: (res) => {
reject(res);
}
})
});
},
getDevices() {
return new Promise((reslove, reject) => {
uni.getBluetoothDevices({
success: function(res) {
let devices = res.devices.filter(e => e
.name && e.name != '未知设备');
reslove(devices);
},
fail(res) {
reject(res);
}
})
});
},
stopSearch() {
return new Promise((reslove, reject) => {
uni.stopBluetoothDevicesDiscovery({
success: (res) => {
reslove(res);
},
fail: (res) => {
reject(res);
}
})
});
},
async connectDevice(deviceId) {
await this.stopSearch();
return new Promise((reslove, rejcet) => {
uni.createBLEConnection({
deviceId: deviceId,
success: function(res) {
this.deviceId = deviceId;
reslove(res);
},
fail: function(res) {
rejcet(res)
}
})
});
},
getServices(deviceId) {
deviceId = deviceId || this.deviceId;
let times = 0;
return new Promise((reslove, reject) => {
tryGetService(reslove, reject);
})
function tryGetService(reslove, reject) {
uni.getBLEDeviceServices({
deviceId: deviceId,
success: (res) => {
// 处理在刚连接到蓝牙时,services获取为空的问题
if (res.services.length > 0) {
reslove(res.services);
} else if (times++ < 10) {
setTimeout(() => {
tryGetService(reslove, reject);
}, 200 * times);
} else {
console.log("services get error");
reject(res);
}
},
fail: function(res) {
console.log(res);
reject(res);
},
})
}
}
}
export default kit;

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

311
common/js/kit/print/esc.js Normal file
View File

@@ -0,0 +1,311 @@
var encode = require("./encoding.js")
var app = getApp();
var jpPrinter = {    
createNew: function() {      
var jpPrinter = {};
var data = [];
var bar = ["UPC-A", "UPC-E", "EAN13", "EAN8", "CODE39", "ITF", "CODABAR", "CODE93", "CODE128"];
jpPrinter.name = "蓝牙打印机";
jpPrinter.init = function() { //初始化打印机
data.push(27)
data.push(64)
};
jpPrinter.setText = function(content) { //设置文本内容
var code = new encode.TextEncoder(
'gb18030', {
NONSTANDARD_allowLegacyEncoding: true
}).encode(content)
for (var i = 0; i < code.length; ++i) {
data.push(code[i])
}
}
// jpPrinter.setBarcodeWidth = function(width) { //设置条码宽度
// data.push(29)
// data.push(119)
// if (width > 6) {
// width = 6;
// }
// if (width < 2) {
// width = 1;
// }
// data.push(width)
// }
// jpPrinter.setBarcodeHeight = function(height) { //设置条码高度
// data.push(29)
// data.push(104)
// data.push(height)
// }
// jpPrinter.setBarcodeContent = function(t,content) {
// var ty = 73;
// data.push(29)
// data.push(107)
// switch (t) {
// case bar[0]:
// ty = 65;
// break;
// case bar[1]:
// ty = 66;
// break;
// case bar[2]:
// ty = 67;
// break;
// case bar[3]:
// ty = 68;
// break;
// case bar[4]:
// ty = 69;
// break;
// case bar[5]:
// ty = 70;
// break;
// case bar[6]:
// ty = 71;
// break;
// case bar[7]:
// ty = 72;
// break;
// case bar[8]:
// ty = 73;
// break;
// }
// data.push(ty)
// }
jpPrinter.setSelectSizeOfModuleForQRCode = function(n) { //设置二维码大小
data.push(29)
data.push(40)
data.push(107)
data.push(3)
data.push(0)
data.push(49)
data.push(67)
if (n > 15) {
n = 15
}
if (n < 1) {
n = 1
}
data.push(n)
}
jpPrinter.setSelectErrorCorrectionLevelForQRCode = function(n) { //设置纠错等级
/*
n 功能 纠错能力
48 选择纠错等级 L 7
49 选择纠错等级 M 15
50 选择纠错等级 Q 25
51 选择纠错等级 H 30
*/
data.push(29)
data.push(40)
data.push(107)
data.push(3)
data.push(0)
data.push(49)
data.push(69)
data.push(n)
}
jpPrinter.setStoreQRCodeData = function(content) { //设置二维码内容
var code = new encode.TextEncoder(
'gb18030', {
NONSTANDARD_allowLegacyEncoding: true
}).encode(content)
data.push(29)
data.push(40)
data.push(107)
data.push(parseInt((code.length + 3) % 256))
data.push(parseInt((code.length + 3) / 256))
data.push(49)
data.push(80)
data.push(48)
for (var i = 0; i < code.length; ++i) {
data.push(code[i])
}
}
jpPrinter.setPrintQRCode = function() { //打印二维码
data.push(29)
data.push(40)
data.push(107)
data.push(3)
data.push(0)
data.push(49)
data.push(81)
data.push(48)
}
jpPrinter.setHorTab = function() { //移动打印位置到下一个水平定位点的位置
data.push(9)
}
jpPrinter.setAbsolutePrintPosition = function(where) { //设置绝对打印位置
data.push(27)
data.push(36)
data.push(parseInt(where % 256))
data.push(parseInt(where / 256))
}
jpPrinter.setRelativePrintPositon = function(where) { //设置相对横向打印位置
data.push(27)
data.push(92)
data.push(parseInt(where % 256))
data.push(parseInt(where / 256))
}
jpPrinter.setSelectJustification = function(which) { //对齐方式
/*
0, 48 左对齐
1, 49 中间对齐
2, 50 右对齐
*/
data.push(27)
data.push(97)
data.push(which)
}
jpPrinter.setLeftMargin = function(n) { //设置左边距
data.push(29)
data.push(76)
data.push(parseInt(n % 256))
data.push(parseInt(n / 256))
}
jpPrinter.setPrintingAreaWidth = function(width) { //设置打印区域宽度
data.push(29)
data.push(87)
data.push(parseInt(width % 256))
data.push(parseInt(width / 256))
}
jpPrinter.setSound = function(n, t) { //设置蜂鸣器
data.push(27)
data.push(66)
if (n < 0) {
n = 1;
} else if (n > 9) {
n = 9;
}
if (t < 0) {
t = 1;
} else if (t > 9) {
t = 9;
}
data.push(n)
data.push(t)
}
jpPrinter.setBitmap = function(res) { //参数,画布的参数
console.log(res)
var width = parseInt((res.width + 7) / 8 * 8 / 8)
var height = res.height;
var time = 1;
var temp = res.data.length - width * 32;
var point_list = []
console.log(width + "--" + height)
data.push(29)
data.push(118)
data.push(48)
data.push(0)
data.push((parseInt((res.width + 7) / 8) * 8) / 8)
data.push(0)
data.push(parseInt(res.height % 256))
data.push(parseInt(res.height / 256))
console.log(res.data.length)
console.log("temp=" + temp)
for (var i = 0; i < height; ++i) {
for (var j = 0; j < width; ++j) {
for (var k = 0; k < 32; k += 4) {
var po = {}
if (res.data[temp] == 0 && res.data[temp + 1] == 0 && res.data[temp + 2] == 0 && res.data[temp + 3] == 0) {
po.point = 0;
} else {
po.point = 1;
}
point_list.push(po)
temp += 4
}
}
time++
temp = res.data.length - width * 32 * time
}
for (var i = 0; i < point_list.length; i += 8) {
var p = point_list[i].point * 128 + point_list[i + 1].point * 64 + point_list[i + 2].point * 32 + point_list[i + 3].point * 16 + point_list[i + 4].point * 8 + point_list[i + 5].point * 4 + point_list[i + 6].point * 2 + point_list[i + 7].point
data.push(p)
}
}
jpPrinter.setPrint = function() { //打印并换行
data.push(10)
}
jpPrinter.setPrintAndFeed = function(feed) { //打印并走纸feed个单位
data.push(27)
data.push(74)
data.push(feed)
}
jpPrinter.setPrintAndFeedRow = function(row) { //打印并走纸row行
data.push(27)
data.push(100)
data.push(row)
}
jpPrinter.getData = function() { //获取打印数据
return data;
};
  
return jpPrinter; 
},
Query: function() {
var queryStatus = {};
var buf;
var dateView;
queryStatus.getRealtimeStatusTransmission = function(n) { //查询打印机实时状态
/*
n = 1传送打印机状态
n = 2传送脱机状态
n = 3传送错误状态
n = 4传送纸传感器状态
*/
buf = new ArrayBuffer(3)
dateView = new DataView(buf)
dateView.setUint8(0, 16)
dateView.setUint8(1, 4)
dateView.setUint8(2, n)
queryStatus.query(buf)
}
queryStatus.query = function(buf) {
uni.writeBLECharacteristicValue({
deviceId: app.BLEInformation.deviceId,
serviceId: app.BLEInformation.writeServiceId,
characteristicId: app.BLEInformation.writeCharaterId,
value: buf,
success: function(res) {
},
complete: function(res) {
console.log(res)
buf = null
dateView = null;
}
})
}
return queryStatus;
}
};
module.exports.jpPrinter = jpPrinter;

View File

@@ -0,0 +1,146 @@
// 标签打印命令封装
var tsc = require('@/common/js/kit/print/tsc.js');
// 打印机单位 1mm = 8dots
export function labelCommand(width, height, unit = 'percent') {
// unit 单位: mm|percent ,当单位为百分比时,最小值为1,最大值为100
this.width = width;
this.height = height;
this.unit = unit;
this.tscCommand = tsc.jpPrinter.createNew();
this.tscCommand.setSize(width, height);
this.tscCommand.setGap(3);
this.tscCommand.setCls();
this.toDotX = function(x) {
if (this.unit === 'percent') {
x = this.width * x / 100;
}
return (x * 8).toFixed(0);
}
this.toDotY = function(y) {
if (this.unit === 'percent') {
y = this.height * y / 100;
}
return (y * 8).toFixed(0);
}
this.setHome = function() {
this.tscCommand.setHome();
}
/**
* 该指令用于定义两张卷标纸间的垂直间距距离
* @param {Object} printGap 单位 mm
*/
this.setGap = function(printGap) {
this.tscCommand.setGap(printGap);
}
/**
* 将纸向前推出指定长度 单位mm
* @param {Object} length
*/
this.setFeed = function(length) {
length = length * 8;
this.tscCommand.setFeed(length)
};
/**
* 将纸向后回拉指定长度
* @param {Object} backupLength 单位mm
*/
this.setBackFeed = function(backupLength) {
backupLength = backupLength * 8;
this.tscCommand.setBackFeed(backupLength);
}
/**
* 设置文字
* @param {Object} x
* @param {Object} y
* @param {Object} text
* @param {Number} rotation {0,90,180,270}
*/
this.setText = function(x, y, text, option = {}) {
option = Object.assign({
rotation: 0,
font: 'TSS24.BF2',
xScale: 1, // 横向放大系数
yScale: 1 // 纵向放大系数
}, option)
x = this.toDotX(x);
y = this.toDotY(y);
let data =
`TEXT ${x},${y},"${option.font}",${option.rotation},${option.xScale},${option.yScale},"${text}"\r\n`;
console.log(data);
this.tscCommand.addCommand(data);
}
/**
* 设置垂直放置的文字
* y% x% 内容
*/
this.setVerticalText = function(x, y, text, option = {}) {
option = Object.assign({}, option, {
rotation: 90
})
this.setText(x, y, text, option)
}
/**
* 设置二维码
* width 1-10
* (y%, x%, 内容,大小(单位未知) 向上打印
*/
this.setQrCode = function(x, y, content, width, option = {}) {
option = Object.assign({
eccLevel: 'L', // 纠错等级 L(7%) M(15%) Q(25%) H(30%)
mode: 'M', // A 自动 M 手动
rotation: 0 // 旋转角度 0,90,180,270
}, option)
x = this.toDotX(x);
y = this.toDotY(y);
let data =
`QRCODE ${x},${y},${option.eccLevel},${width},${option.mode},${option.rotation},"${content}"\r\n`;
this.tscCommand.addCommand(data)
}
// 设置线条
//y%, x%, 粗细(一般0.5) 宽度)
this.setBar = function(x, y, width, height = 0.5) {
x = this.toDotX(x);
y = this.toDotY(y);
width = (width * 8).toFixed(0);
height = (height * 8).toFixed(0);
this.setBarByDot(x, y, width, height);
}
this.setBarByDot = function(x, y, width, height = 4) {
let data = `BAR ${x},${y},${width},${height}\r\n`;
console.log(data);
this.tscCommand.addCommand(data);
}
// 设置边框
this.setBox = function(xStart, yStart, xEnd, yEnd, thickness = 0.5) {
thickness = Number(thickness * 8).toFixed(0);
xStart = this.toDotX(xStart);
xEnd = this.toDotX(xEnd);
yStart = this.toDotY(yStart);
yEnd = this.toDotY(yEnd);
let width = (xEnd - xStart).toFixed(0);
let height = (yEnd - yStart).toFixed(0);
// 横线
this.setBarByDot(xStart, yStart, width, thickness);
// 竖线
this.setBarByDot(xStart, yStart, thickness, height);
this.setBarByDot(xEnd, yStart, thickness, height);
// 横线
this.setBarByDot(xStart, yEnd, width, thickness);
// 下面指令不能用,自己封装一个
/* let data = `BOX ${xStart},${yStart},${xEnd},${yEnd},${thickness}\r\n`;
console.log(data);
this.tscCommand.addCommand(data); */
}
this.setPagePrint = function() {
this.tscCommand.setPagePrint();
}
// 获取data
this.getData = function() {
console.log(this.tscCommand.getData().length);
return this.tscCommand.getData();
}
}

View File

@@ -0,0 +1,203 @@
var tsc = require("@/common/js/kit/print/tsc.js");
var esc = require("@/common/js/kit/print/esc.js");
import bleKit from '@/common/js/kit/bluetoothKit.js'
/**
* 返回蓝牙的读写特征值
* @param {Object} bluetoothAddress
* @param {Object} services
*/
function getCharacteristics(bluetoothAddress, services) {
let result = {
notifyCharaterId: '',
notifyServiceId: '',
writeCharaterId: '',
writeServiceId: '',
readServiceId: '',
readCharaterId: ''
}
let serviceIndex = 0;
return new Promise((reslove, reject) => {
getBLEDeviceCharacteristics(reslove, reject);
});
function getBLEDeviceCharacteristics(reslove, reject) {
uni.getBLEDeviceCharacteristics({
deviceId: bluetoothAddress,
serviceId: services[serviceIndex].uuid,
success: (res) => {
for (var i = 0; i < res.characteristics.length; ++i) {
var properties = res.characteristics[i].properties
console.log(properties);
var item = res.characteristics[i].uuid
if (!result.notifyCharaterId && properties.notify) {
result.notifyCharaterId = item
result.notifyServiceId = services[serviceIndex].uuid
} else if (!result.writeCharaterId && properties.write) {
result.writeCharaterId = item
result.writeServiceId = services[serviceIndex].uuid
} else if (!result.readCharaterId && properties.read) {
result.readCharaterId = item
result.readServiceId = services[serviceIndex].uuid
}
}
if (result.readCharaterId && result.writeCharaterId && result.notifyCharaterId) {
reslove(result);
return;
}
if (serviceIndex == services.length) {
reject(result);
return;
}
serviceIndex++
getBLEDeviceCharacteristics(reslove, reject);
},
fail: function(res) {
reject(res);
}
})
}
}
let kit = {
bluetoothAddress: '',
characteristics: {
notifyCharaterId: '',
notifyServiceId: '',
writeCharaterId: '',
writeServiceId: '',
readServiceId: '',
readCharaterId: ''
},
isInited() {
return Boolean(this.bluetoothAddress);
},
// 打印机初始化
async init(bluetoothAddress) {
uni.showLoading({
mask: true,
title: '连接中'
});
//console.log('关闭蓝牙连接');
await bleKit.closeConnect(bluetoothAddress);
console.log('连接蓝牙');
await bleKit.connectDevice(bluetoothAddress);
console.log('蓝牙连接成功');
let services = await bleKit.getServices(bluetoothAddress);
//console.log("services: ",services);
this.characteristics = await getCharacteristics(bluetoothAddress, services);
console.log("this.characteristics: ", this.characteristics);
uni.notifyBLECharacteristicValueChange({
deviceId: bluetoothAddress,
serviceId: this.characteristics.notifyServiceId,
characteristicId: this.characteristics.notifyCharaterId,
state: true,
success: (res) => {
console.log(res);
console.log('printer init finish');
this.bluetoothAddress = bluetoothAddress;
uni.hideLoading();
uni.onBLECharacteristicValueChange(function(r) {
console.log(
`characteristic ${r.characteristicId} has changed, now is ${r}`)
})
},
fail: function(e) {
console.log(e)
}
});
// this.queryStatus();
},
// 查询打印机状态
queryStatus() {
/*
n = 1传送打印机状态
n = 2传送脱机状态
n = 3传送错误状态
n = 4传送纸传感器状态
*/
let n = 1;
let buf = new ArrayBuffer(3)
let dateView = new DataView(buf)
dateView.setUint8(0, 16)
dateView.setUint8(1, 4)
dateView.setUint8(2, n)
console.log("this.characteristics.writeCharaterId: ", this.characteristics.writeCharaterId);
uni.writeBLECharacteristicValue({
deviceId: this.bluetoothAddress,
serviceId: this.characteristics.writeServiceId,
characteristicId: this.characteristics.writeCharaterId,
value: buf,
success: function(res) {
},
complete: function(res) {
console.log(res)
buf = null
dateView = null;
}
})
},
// 发送数据至打印机
sendData(buff) {
if (!this.bluetoothAddress) {
throw new Error('请先连接蓝牙');
}
let currentTime = 1,
onTimeData = 100;
let loopTime = parseInt(buff.length / onTimeData) + 1;
let lastData = parseInt(buff.length % onTimeData);
let deviceId = this.bluetoothAddress;
let serviceId = this.characteristics.writeServiceId;
let characteristicId = this.characteristics.writeCharaterId;
let delay = true;
// 发送错误重试次数
let repeateTimes = 0;
// 最大重试次数
var maxRepeateTimes = 100;
send();
function send() {
let buf, dataView
if (currentTime < loopTime) {
buf = new ArrayBuffer(onTimeData)
dataView = new DataView(buf)
for (var i = 0; i < onTimeData; ++i) {
dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
}
} else {
buf = new ArrayBuffer(lastData)
dataView = new DataView(buf)
for (var i = 0; i < lastData; ++i) {
dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
}
}
console.log("第" + currentTime + "次发送数据大小为:" + buf.byteLength);
uni.writeBLECharacteristicValue({
deviceId: deviceId,
serviceId: serviceId,
characteristicId: characteristicId,
value: buf,
success: function(res) {
currentTime++
},
fail: function(e) {
maxRepeateTimes++;
delay = true;
console.error(e)
},
complete: () => {
if (currentTime <= loopTime && repeateTimes < maxRepeateTimes) {
if (delay) {
delay = false;
setTimeout(() => {
send();
}, 100);
} else {
send();
}
}
}
})
}
}
}
export default kit;

220
common/js/kit/print/tsc.js Normal file
View File

@@ -0,0 +1,220 @@
var app = getApp();
var encode = require("./encoding.js");
var jpPrinter = {    
createNew: function() {      
var jpPrinter = {};
var data = "";
var command = []
jpPrinter.name = "蓝牙打印机";
jpPrinter.init = function() {};
jpPrinter.addCommand = function (content){ //将指令转成数组装起
var code = new encode.TextEncoder(
'gb18030', {
NONSTANDARD_allowLegacyEncoding: true
}).encode(content)
for (var i = 0; i < code.length; ++i) {
command.push(code[i])
}
}
jpPrinter.setSize = function(pageWidght, pageHeight) { //设置页面大小
data = "SIZE " + pageWidght.toString() + " mm" + "," + pageHeight.toString() + " mm" + "\r\n";
jpPrinter.addCommand(data)
};
jpPrinter.setSpeed = function(printSpeed) { //设置打印机速度
data = "SPEED " + printSpeed.toString() + "\r\n";
jpPrinter.addCommand(data)
};
jpPrinter.setDensity = function(printDensity) { //设置打印机浓度
data = "DENSITY " + printDensity.toString() + "\r\n";
jpPrinter.addCommand(data)
};
jpPrinter.setGap = function(printGap) { //传感器
data = "GAP " + printGap.toString() + " mm\r\n";
jpPrinter.addCommand(data)
};
jpPrinter.setCountry = function(country) { //选择国际字符集
/*
001:USA
002:French
003:Latin America
034:Spanish
039:Italian
044:United Kingdom
046:Swedish
047:Norwegian
049:German
*/
data = "COUNTRY " + country + "\r\n";
jpPrinter.addCommand(data)
};
jpPrinter.setCodepage = function(codepage) { //选择国际代码页
/*
8-bit codepage 字符集代表
437:United States
850:Multilingual
852:Slavic
860:Portuguese
863:Canadian/French
865:Nordic
Windows code page
1250:Central Europe
1252:Latin I
1253:Greek
1254:Turkish
以下代码页仅限于 12×24 dot 英数字体
WestEurope:WestEurope
Greek:Greek
Hebrew:Hebrew
EastEurope:EastEurope
Iran:Iran
IranII:IranII
Latvian:Latvian
Arabic:Arabic
Vietnam:Vietnam
Uygur:Uygur
Thai:Thai
1252:Latin I
1257:WPC1257
1251:WPC1251
866:Cyrillic
858:PC858
747:PC747
864:PC864
1001:PC100
*/
data = "CODEPAGE " + codepage + "\r\n";
jpPrinter.addCommand(data)
}
jpPrinter.setCls = function() { //清除打印机缓存
data = "CLS" + "\r\n";
jpPrinter.addCommand(data)
};
jpPrinter.setFeed = function(feed) { //将纸向前推出n
data = "FEED " + feed + "\r\n";
jpPrinter.addCommand(data)
};
jpPrinter.setBackFeed = function(backup) { //将纸向后回拉n
data = "BACKFEED " + backup + "\r\n";
jpPrinter.addCommand(data)
}
jpPrinter.setDirection = function(direction) { //设置打印方向,参考编程手册
data = "DIRECTION " + direction + "\r\n";
jpPrinter.addCommand(data)
};
jpPrinter.setReference = function(x, y) { //设置坐标原点,与打印方向有关
data = "REFERENCE " + x + "," + y + "\r\n";
jpPrinter.addCommand(data)
};
jpPrinter.setFromfeed = function() { //根据Size进一张标签纸
data = "FORMFEED \r\n";
jpPrinter.addCommand(data)
};
jpPrinter.setHome = function() { //根据Size找到下一张标签纸的位置
data = "HOME \r\n";
jpPrinter.addCommand(data)
};
jpPrinter.setSound = function(level, interval) { //控制蜂鸣器
data = "SOUND " + level + "," + interval + "\r\n";
jpPrinter.addCommand(data)
};
jpPrinter.setLimitfeed = function(limit) { // 检测垂直间距
data = "LIMITFEED " + limit + "\r\n";
jpPrinter.addCommand(data)
};
jpPrinter.setBar = function(x, y, width, height) { //绘制线条
data = "BAR " + x + "," + y + "," + width + "," + height + "\r\n"
jpPrinter.addCommand(data)
};
jpPrinter.setBox = function(x_start, y_start, x_end, y_end, thickness) { //绘制方框
data = "BOX " + x_start + "," + y_start + "," + x_end + "," + y_end + "," + thickness + "\r\n";
jpPrinter.addCommand(data)
};
jpPrinter.setErase = function(x_start, y_start, x_width, y_height) { //清除指定区域的数据
data = "ERASE " + x_start + "," + y_start + "," + x_width + "," + y_height + "\r\n";
jpPrinter.addCommand(data)
};
jpPrinter.setReverse = function(x_start, y_start, x_width, y_height) { //将指定的区域反相打印
data = "REVERSE " + x_start + "," + y_start + "," + x_width + "," + y_height + "\r\n";
jpPrinter.addCommand(data)
};
jpPrinter.setText = function(x, y, font, x_, y_, str) { //打印文字
data = "TEXT " + x + "," + y + ",\"" + font + "\"," + 0 + "," + x_ + "," + y_ + "," + "\"" + str + "\"\r\n"
jpPrinter.addCommand(data)
};
jpPrinter.setQR = function(x, y, level, width, mode, content) { //打印二维码
data = "QRCODE " + x + "," + y + "," + level + "," + width + "," + mode + "," + 0 + ",\"" + content + "\"\r\n"
jpPrinter.addCommand(data)
};
jpPrinter.setBar = function (x, y, codetype, height, readable, narrow, wide, content) { //打印条形码
data = "BARCODE " + x + "," + y + ",\"" + codetype + "\"," + height + "," + readable + "," + 0 + "," + narrow + "," + wide + ",\"" + content + "\"\r\n"
jpPrinter.addCommand(data)
};
jpPrinter.setBitmap = function(x, y, mode, res) { //添加图片res为画布参数
var width = parseInt((res.width + 7) / 8 * 8 / 8)
var height = res.height;
var time = 1;
var temp = res.data.length - width * 32;
var pointList = []
console.log(width + "--" + height)
data = "BITMAP " + x + "," + y + "," + width + "," + height + "," + mode + ","
jpPrinter.addCommand(data)
for (var i = 0; i < height; ++i) {
for (var j = 0; j < width; ++j) {
for (var k = 0; k < 32; k += 4) {
if (res.data[temp] == 0 && res.data[temp + 1] == 0 && res.data[temp + 2] == 0 && res.data[temp + 3] == 0) {
pointList.push(1)
} else {
pointList.push(0)
}
temp += 4
}
}
time++
temp = res.data.length - width * 32 * time
}
for (var i = 0; i < pointList.length; i += 8) {
var p = pointList[i] * 128 + pointList[i + 1] * 64 + pointList[i + 2] * 32 + pointList[i + 3] * 16 + pointList[i + 4] * 8 + pointList[i + 5] * 4 + pointList[i + 6] * 2 + pointList[i + 7]
command.push(p)
}
}
jpPrinter.setPagePrint = function() { //打印页面
data = "PRINT 1,1\r\n"
jpPrinter.addCommand(data)
};
//获取打印数据
jpPrinter.getData = function() {
return command;
};
  
return jpPrinter; 
}
};
module.exports.jpPrinter = jpPrinter;

View File

@@ -0,0 +1,99 @@
/**
* 打印标签示例
* @param {Object} pinrter 打印机对象
* @param {Object} printConent 需打印的内容
* deviceCode:设备
* shift 班次
* materialCode 编码
* partNumber 名称
* batchNo 批次号
* prepareDate 生产日期 2021.3.19
* num 数量
* prepareUserName 操作人员
* state 检验状态 合格/不合格
* qrcodeContent 条码中的内容
* barcode 条码编号
*/
export function printLabel(printer, content) {
let title = '标签demo';
printer.print(80, 122, command => {
//设置标题
command.setVerticalText(80 + 10, 24, title, {
xScale: 2
});
// 创建盒子
command.setBox(2, 2, 80, 80);
//绘制分割线分割左边的内容区域和右边的二维码区域
command.setBar(2, 40, 62.5);
//第一列分隔线
command.setBar(2, 14, 62.5);
// 第一行内容 设备\班次
command.setVerticalText(80 - 80 * 0.0466, 6, '设备');
command.setVerticalText(80 - 80 * 0.0466, 15, content.deviceCode);
command.setBar(80 - 80 * 0.0466 * 3, 25, 80 * 0.0466 * 3 - 1.5);
command.setVerticalText(80 - 80 * 0.0466, 26, '班次');
command.setBar(80 - 80 * 0.0466 * 3, 32, 80 * 0.0466 * 3 - 1.5);
command.setVerticalText(80 - 80 * 0.0466, 34, content.shift);
//line
command.setBar(80 - 80 * 0.0466 * 3, 2, 0.5, 46);
var t = 80 - 80 * 0.0466 * 3;
// 第二行内容 编码
command.setVerticalText(t - 80 * 0.04, 4.5, '编码');
command.setVerticalText(t - 80 * 0.04, 20, content.materialCode);
//line
command.setBar(t - 80 * 0.12, 2, 0.5, 46);
t = t - 80 * 0.12;
// 第三行内容 名称
command.setVerticalText(t - 80 * 0.04, 4.5, '名称');
command.setVerticalText(t - 80 * 0.04, 20, content.partNumber);
//line
command.setBar(t - 80 * 0.12, 2, 0.5, 46);
t = t - 80 * 0.12;
// 第四行内容 批次
command.setVerticalText(t - 80 * 0.04, 6, '批次');
command.setVerticalText(t - 80 * 0.04, 20, content.batchNo);
//line
command.setBar(t - 80 * 0.12, 2, 0.5, 46);
t = t - 80 * 0.12;
// 第五行内容 生产日期
command.setVerticalText(t - 80 * 0.04, 3, '生产日期');
command.setVerticalText(t - 80 * 0.04, 16, content.prepareDate, {
yScale: 2
});
//line
command.setBar(t - 80 * 0.12, 2, 0.5, 46);
t = t - 80 * 0.12;
// 第六行内容 数量
command.setVerticalText(t - 80 * 0.06, 6, '数量');
command.setVerticalText(t - 80 * 0.06, 16, content.num);
//line
command.setBar(t - 80 * 0.18, 2, 0.5, 26.5);
t = t - 80 * 0.18;
// 第七行内容 操作人员
command.setVerticalText(t - 80 * 0.06, 3, '操作人员');
command.setVerticalText(t - 80 * 0.06, 15, content.prepareUserName);
// 合格标识
command.setBar(2, 24, 23);
if (content.state.length === 2) {
command.setVerticalText(80 * 0.18 + 80 * 0.06, 27, content.state, {
xScale: 2,
yScale: 2
});
} else if (content.state.length === 3) {
command.setVerticalText(80 * 0.18 + 80 * 0.06, 24.5, content.state, {
xScale: 2,
yScale: 2
});
}
// 二维码
command.setQrCode(19, 42, content.qrcodeContent, 14);
command.setVerticalText(17, 45, content.barcode, {
xScale: 2,
yScale: 2
});
});
}