123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- 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) {
- console.log('设置',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) {
- wx.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;
|