47
components/ywx-jprinter/lib/encoding-indexes.js
Normal file
47
components/ywx-jprinter/lib/encoding-indexes.js
Normal file
File diff suppressed because one or more lines are too long
3312
components/ywx-jprinter/lib/encoding.js
Normal file
3312
components/ywx-jprinter/lib/encoding.js
Normal file
File diff suppressed because it is too large
Load Diff
311
components/ywx-jprinter/lib/esc.js
Normal file
311
components/ywx-jprinter/lib/esc.js
Normal 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) {
|
||||
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;
|
||||
222
components/ywx-jprinter/lib/tsc.js
Normal file
222
components/ywx-jprinter/lib/tsc.js
Normal file
@@ -0,0 +1,222 @@
|
||||
// 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为画布参数
|
||||
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 pointList = []
|
||||
console.log(width + "--" + height)
|
||||
data = "BITMAP " + x + "," + y + "," + width + "," + height + "," + mode + ","
|
||||
jpPrinter.addCommand(data)
|
||||
for (var i = 0; i < height; ++i) {
|
||||
console.log(temp)
|
||||
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;
|
||||
268
components/ywx-jprinter/ywx-jprinter.vue
Normal file
268
components/ywx-jprinter/ywx-jprinter.vue
Normal file
@@ -0,0 +1,268 @@
|
||||
<template>
|
||||
<view>
|
||||
<button @click="searchBle">搜索蓝牙</button>
|
||||
<view style="margin-top: 30upx;" :key="index" v-for="(item,index) in devices">
|
||||
<button style="width: 400upx; color: #0081FF;" @click="onConn(item)">{{item.name}}</button>
|
||||
</view>
|
||||
<button style="margin-top: 100upx;" @click="senBleLabel()">标签打印</button>
|
||||
<textarea @blur="bindTextAreaBlur" auto-height placeholder-style="color:#F76260" placeholder="请输入票据信息" v-model="piaojuText" />
|
||||
<button style="margin-top: 100upx;" @click="senBleLabel2()">票据打印</button>
|
||||
</view>
|
||||
|
||||
|
||||
</template>
|
||||
|
||||
<script>
|
||||
var tsc = import.meta.globEager("../../components/gprint/tsc.js")
|
||||
var esc = import.meta.globEager("../../components/gprint/esc.js")
|
||||
//var tsc = require('../../components/gprint/tsc.js')
|
||||
//var esc = require('../../components/gprint/esc.js')
|
||||
export default {
|
||||
name:"ywx-jprinter",
|
||||
data() {
|
||||
return {
|
||||
devices: [],
|
||||
currDev: null,
|
||||
connId: '',
|
||||
piaojuText:'',
|
||||
}
|
||||
},
|
||||
onLoad: function() {
|
||||
|
||||
},
|
||||
|
||||
methods: {
|
||||
destroyed: function() {
|
||||
console.log("destroyed----------")
|
||||
if (this.connId != '') {
|
||||
uni.closeBLEConnection({
|
||||
deviceId: this.connId,
|
||||
success(res) {
|
||||
console.log(res)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
},
|
||||
searchBle() {
|
||||
uni.showLoading({
|
||||
title:"请稍后!"
|
||||
})
|
||||
var that = this
|
||||
console.log("initBule")
|
||||
uni.openBluetoothAdapter({
|
||||
success(res) {
|
||||
console.log("打开 蓝牙模块")
|
||||
console.log(res)
|
||||
that.onDevice()
|
||||
uni.getBluetoothAdapterState({
|
||||
success: function(res) {
|
||||
uni.hideLoading();
|
||||
console.log(res)
|
||||
if (res.available) {
|
||||
if (res.discovering) {
|
||||
that.stopFindBule()
|
||||
}
|
||||
//搜索蓝牙
|
||||
//开始搜寻附近的蓝牙外围设备
|
||||
console.log("开始搜寻附近的蓝牙外围设备")
|
||||
uni.startBluetoothDevicesDiscovery({
|
||||
success(res) {
|
||||
uni.showModal({
|
||||
content: res
|
||||
})
|
||||
console.log(res)
|
||||
}
|
||||
})
|
||||
|
||||
} else {
|
||||
console.log('本机蓝牙不可用')
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
})
|
||||
},
|
||||
onDevice(){
|
||||
console.log("监听寻找到新设备的事件---------------")
|
||||
var that = this
|
||||
//监听寻找到新设备的事件
|
||||
uni.onBluetoothDeviceFound(function(devices) {
|
||||
console.log('--------------new-----------------------'+JSON.stringify(devices))
|
||||
var re = JSON.parse(JSON.stringify(devices))
|
||||
console.log(re.devices[0].name + " " + re.devices[0].deviceId)
|
||||
let name = re.devices[0].name
|
||||
if (name != "未知设备") {
|
||||
let deviceId = re.devices[0].deviceId
|
||||
that.devices.push({
|
||||
name: name,
|
||||
deviceId: deviceId,
|
||||
services: []
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
})
|
||||
},
|
||||
|
||||
stopFindBule() {
|
||||
console.log("停止搜寻附近的蓝牙外围设备---------------")
|
||||
uni.stopBluetoothDevicesDiscovery({
|
||||
success(res) {
|
||||
console.log(res)
|
||||
}
|
||||
})
|
||||
},
|
||||
onConn(item) {
|
||||
var that = this
|
||||
console.log("连接蓝牙---------------" + item.deviceId)
|
||||
let deviceId = item.deviceId
|
||||
uni.createBLEConnection({
|
||||
deviceId: deviceId,
|
||||
complete(res) {
|
||||
if (res.errMsg == "createBLEConnection:ok") {
|
||||
console.log("连接蓝牙-[" + item.name + "]--成功")
|
||||
that.connId = deviceId;
|
||||
that.currDev = item
|
||||
setTimeout(function() {
|
||||
that.getBLEServices(deviceId)
|
||||
}, 2000)
|
||||
} else {
|
||||
console.log(res)
|
||||
}
|
||||
//连接成功 关闭搜索
|
||||
that.stopFindBule()
|
||||
|
||||
},
|
||||
})
|
||||
},
|
||||
|
||||
getBLEServices(_deviceId) {
|
||||
var that = this;
|
||||
let deviceId = _deviceId
|
||||
console.log("获取蓝牙设备所有服务(service)。---------------")
|
||||
|
||||
uni.getBLEDeviceServices({
|
||||
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
|
||||
deviceId: deviceId,
|
||||
complete(res) {
|
||||
console.log(res)
|
||||
let serviceId = ""
|
||||
|
||||
for (var s = 0; s < res.services.length; s++) {
|
||||
console.log(res.services[s].uuid)
|
||||
let serviceId = res.services[s].uuid
|
||||
uni.getBLEDeviceCharacteristics({
|
||||
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
|
||||
deviceId: deviceId,
|
||||
// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
|
||||
serviceId: serviceId,
|
||||
success(res) {
|
||||
var re = JSON.parse(JSON.stringify(res))
|
||||
|
||||
console.log('deviceId = [' + deviceId + '] serviceId = [' + serviceId + ']')
|
||||
for (var c = 0; c < re.characteristics.length; c++) {
|
||||
if (re.characteristics[c].properties.write == true) {
|
||||
let uuid = re.characteristics[c].uuid
|
||||
console.log(' deviceId = [' + deviceId + '] serviceId = [' + serviceId + '] characteristics=[' +
|
||||
uuid)
|
||||
|
||||
for (var index in that.devices) {
|
||||
if (that.devices[index].deviceId == deviceId) {
|
||||
that.devices[index].services.push({
|
||||
serviceId: serviceId,
|
||||
characteristicId: uuid
|
||||
})
|
||||
break
|
||||
}
|
||||
|
||||
}
|
||||
console.log(JSON.stringify(that.devices))
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
},
|
||||
fail(res) {
|
||||
console.log(res)
|
||||
},
|
||||
})
|
||||
|
||||
},
|
||||
senBlData(deviceId, serviceId, characteristicId,uint8Array) {
|
||||
console.log('************deviceId = [' + deviceId + '] serviceId = [' + serviceId + '] characteristics=[' +characteristicId+ "]")
|
||||
var uint8Buf = Array.from(uint8Array);
|
||||
function split_array(datas,size){
|
||||
var result = {};
|
||||
var j = 0
|
||||
for (var i = 0; i < datas.length; i += size) {
|
||||
result[j] = datas.slice(i, i + size)
|
||||
j++
|
||||
}
|
||||
console.log(result)
|
||||
return result
|
||||
}
|
||||
var sendloop = split_array(uint8Buf, 20);
|
||||
// console.log(sendloop.length)
|
||||
function realWriteData(sendloop, i) {
|
||||
var data = sendloop[i]
|
||||
if(typeof(data) == "undefined"){
|
||||
return
|
||||
}
|
||||
console.log("第【" + i + "】次写数据"+data)
|
||||
var buffer = new ArrayBuffer(data.length)
|
||||
var dataView = new DataView(buffer)
|
||||
for (var j = 0; j < data.length; j++) {
|
||||
dataView.setUint8(j, data[j]);
|
||||
}
|
||||
uni.writeBLECharacteristicValue({
|
||||
deviceId,
|
||||
serviceId,
|
||||
characteristicId,
|
||||
value: buffer,
|
||||
success(res) {
|
||||
realWriteData(sendloop, i + 1);
|
||||
}
|
||||
})
|
||||
}
|
||||
var i = 0;
|
||||
realWriteData(sendloop, i);
|
||||
},
|
||||
senBleLabel() {
|
||||
//标签模式
|
||||
let deviceId = this.currDev.deviceId;
|
||||
let serviceId = this.currDev.services[0].serviceId;
|
||||
let characteristicId = this.currDev.services[0].characteristicId;
|
||||
var command = tsc.jpPrinter.createNew()
|
||||
// console.log(command)
|
||||
command.setSize(40, 30)
|
||||
command.setGap(2)
|
||||
command.setCls()
|
||||
command.setText(50, 10, "TSS24.BF2", 1, 1, "打印测试")
|
||||
command.setQR(50, 50, "L", 5, "A", "977767937@qq.com")
|
||||
command.setPagePrint()
|
||||
this.senBlData(deviceId, serviceId, characteristicId,command.getData())
|
||||
},
|
||||
senBleLabel2(){
|
||||
//票据模式
|
||||
let deviceId = this.currDev.deviceId;
|
||||
let serviceId = this.currDev.services[0].serviceId;
|
||||
let characteristicId = this.currDev.services[0].characteristicId;
|
||||
var command = esc.jpPrinter.createNew()
|
||||
command.init()
|
||||
command.setText(this.piaojuText);
|
||||
command.setPrintAndFeedRow(1)
|
||||
this.senBlData(deviceId, serviceId, characteristicId,command.getData())
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<template>
|
||||
<view>
|
||||
|
||||
<ywx-jprinter></ywx-jprinter>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
|
||||
@@ -7,6 +7,16 @@
|
||||
@confirm="confirm" />
|
||||
</view>
|
||||
</uni-nav-bar>
|
||||
|
||||
<uni-list>
|
||||
<uni-list-item title="产品查询" showArrow link to="/cpzs/chushou/chushou"></uni-list-item>
|
||||
<uni-list-item title="入库登记" showArrow link to="/cpzs/chushou/chushou"></uni-list-item>
|
||||
<uni-list-item title="入仓登记" showArrow link to="/cpzs/chushou/chushou"></uni-list-item>
|
||||
<uni-list-item title="出仓调货" showArrow link to="/cpzs/chushou/chushou"></uni-list-item>
|
||||
<uni-list-item title="出售登记" showArrow link to="/cpzs/chushou/chushou"></uni-list-item>
|
||||
<uni-list-item title="售后申请" showArrow link to="/cpzs/chushou/chushou"></uni-list-item>
|
||||
<uni-list-item title="售后操作" showArrow link to="/cpzs/chushou/chushou"></uni-list-item>
|
||||
</uni-list>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
|
||||
@@ -18,10 +18,10 @@
|
||||
<view class="GStitle">家族</view>
|
||||
</view>
|
||||
</view>
|
||||
<view class="Grid-Item">
|
||||
<view class="Grid-Item" @click="toPage('/cpzs/index/index')">
|
||||
<view class="Grid-Item-1">
|
||||
<image class="GSimg" src="../../../static/logo.png"></image>
|
||||
<view class="GStitle">任务</view>
|
||||
<view class="GStitle">产品追溯</view>
|
||||
</view>
|
||||
</view>
|
||||
<view class="Grid-Item">
|
||||
@@ -106,6 +106,11 @@
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
toPage(p){
|
||||
uni.navigateTo({
|
||||
url:p
|
||||
})
|
||||
}
|
||||
},
|
||||
onBackPress() {
|
||||
|
||||
Reference in New Issue
Block a user