esc.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. var encode = require("./encoding.js")
  2. var app = getApp();
  3. var jpPrinter = {    
  4. createNew: function() {      
  5. var jpPrinter = {};
  6. var data = [];
  7. var bar = ["UPC-A", "UPC-E", "EAN13", "EAN8", "CODE39", "ITF", "CODABAR", "CODE93", "CODE128"];
  8. jpPrinter.name = "蓝牙打印机";
  9. jpPrinter.init = function() { //初始化打印机
  10. data.push(27)
  11. data.push(64)
  12. };
  13. jpPrinter.setText = function(content) { //设置文本内容
  14. var code = new encode.TextEncoder(
  15. 'gb18030', {
  16. NONSTANDARD_allowLegacyEncoding: true
  17. }).encode(content)
  18. for (var i = 0; i < code.length; ++i) {
  19. data.push(code[i])
  20. }
  21. }
  22. jpPrinter.setFontSize=function(n){//设置字体大小
  23. data.push(29)
  24. data.push(33)
  25. data.push(n)
  26. }
  27. jpPrinter.bold = function (n) {//加粗
  28. data.push(27)
  29. data.push(69)
  30. data.push(n)
  31. }
  32. jpPrinter.setUnderline=function(n){//设置下划线
  33. data.push(27)
  34. data.push(45)
  35. data.push(n)
  36. }
  37. jpPrinter.setUnderline2 = function (n) {//设置下划线
  38. data.push(28)
  39. data.push(45)
  40. data.push(n)
  41. }
  42. // jpPrinter.setBarcodeWidth = function(width) { //设置条码宽度
  43. // data.push(29)
  44. // data.push(119)
  45. // if (width > 6) {
  46. // width = 6;
  47. // }
  48. // if (width < 2) {
  49. // width = 1;
  50. // }
  51. // data.push(width)
  52. // }
  53. // jpPrinter.setBarcodeHeight = function(height) { //设置条码高度
  54. // data.push(29)
  55. // data.push(104)
  56. // data.push(height)
  57. // }
  58. // jpPrinter.setBarcodeContent = function(t,content) {
  59. // var ty = 73;
  60. // data.push(29)
  61. // data.push(107)
  62. // switch (t) {
  63. // case bar[0]:
  64. // ty = 65;
  65. // break;
  66. // case bar[1]:
  67. // ty = 66;
  68. // break;
  69. // case bar[2]:
  70. // ty = 67;
  71. // break;
  72. // case bar[3]:
  73. // ty = 68;
  74. // break;
  75. // case bar[4]:
  76. // ty = 69;
  77. // break;
  78. // case bar[5]:
  79. // ty = 70;
  80. // break;
  81. // case bar[6]:
  82. // ty = 71;
  83. // break;
  84. // case bar[7]:
  85. // ty = 72;
  86. // break;
  87. // case bar[8]:
  88. // ty = 73;
  89. // break;
  90. // }
  91. // data.push(ty)
  92. // }
  93. jpPrinter.setSelectSizeOfModuleForQRCode = function(n) { //设置二维码大小
  94. data.push(29)
  95. data.push(40)
  96. data.push(107)
  97. data.push(3)
  98. data.push(0)
  99. data.push(49)
  100. data.push(67)
  101. if (n > 15) {
  102. n = 15
  103. }
  104. if (n < 1) {
  105. n = 1
  106. }
  107. data.push(n)
  108. }
  109. jpPrinter.setSelectErrorCorrectionLevelForQRCode = function(n) { //设置纠错等级
  110. /*
  111. n 功能 纠错能力
  112. 48 选择纠错等级 L 7
  113. 49 选择纠错等级 M 15
  114. 50 选择纠错等级 Q 25
  115. 51 选择纠错等级 H 30
  116. */
  117. data.push(29)
  118. data.push(40)
  119. data.push(107)
  120. data.push(3)
  121. data.push(0)
  122. data.push(49)
  123. data.push(69)
  124. data.push(n)
  125. }
  126. jpPrinter.setStoreQRCodeData = function(content) { //设置二维码内容
  127. console.log('content',content)
  128. var code = new encode.TextEncoder(
  129. 'utf-8', {
  130. NONSTANDARD_allowLegacyEncoding: true
  131. }).encode(content)
  132. data.push(29)
  133. data.push(40)
  134. data.push(107)
  135. data.push(parseInt((code.length + 3) % 256))
  136. data.push(parseInt((code.length + 3) / 256))
  137. data.push(49)
  138. data.push(80)
  139. data.push(48)
  140. for (var i = 0; i < code.length; ++i) {
  141. data.push(code[i])
  142. }
  143. }
  144. jpPrinter.setPrintQRCode = function() { //打印二维码
  145. data.push(29)
  146. data.push(40)
  147. data.push(107)
  148. data.push(3)
  149. data.push(0)
  150. data.push(49)
  151. data.push(81)
  152. data.push(48)
  153. }
  154. jpPrinter.setHorTab = function() { //移动打印位置到下一个水平定位点的位置
  155. data.push(9)
  156. }
  157. jpPrinter.setAbsolutePrintPosition = function(where) { //设置绝对打印位置
  158. data.push(27)
  159. data.push(36)
  160. data.push(parseInt(where % 256))
  161. data.push(parseInt(where / 256))
  162. }
  163. jpPrinter.setRelativePrintPositon = function(where) { //设置相对横向打印位置
  164. data.push(27)
  165. data.push(92)
  166. data.push(parseInt(where % 256))
  167. data.push(parseInt(where / 256))
  168. }
  169. jpPrinter.setSelectJustification = function(which) { //对齐方式
  170. /*
  171. 0, 48 左对齐
  172. 1, 49 中间对齐
  173. 2, 50 右对齐
  174. */
  175. data.push(27)
  176. data.push(97)
  177. data.push(which)
  178. }
  179. jpPrinter.space = function (n) { //设置横向跳格位置
  180. data.push(27)
  181. data.push(68)
  182. data.push(n)
  183. }
  184. jpPrinter.setLeftMargin = function(n) { //设置左边距
  185. data.push(29)
  186. data.push(76)
  187. data.push(parseInt(n % 256))
  188. data.push(parseInt(n / 256))
  189. }
  190. jpPrinter.textMarginRight = function (n) { //设置字符右间距
  191. data.push(27)
  192. data.push(32)
  193. data.push(n)
  194. }
  195. jpPrinter.rowSpace = function (n) { //设置行间距
  196. data.push(27)
  197. data.push(51)
  198. data.push(n)
  199. }
  200. jpPrinter.setPrintingAreaWidth = function(width) { //设置打印区域宽度
  201. data.push(29)
  202. data.push(87)
  203. data.push(parseInt(width % 256))
  204. data.push(parseInt(width / 256))
  205. }
  206. jpPrinter.setSound = function(n, t) { //设置蜂鸣器
  207. data.push(27)
  208. data.push(66)
  209. if (n < 0) {
  210. n = 1;
  211. } else if (n > 9) {
  212. n = 9;
  213. }
  214. if (t < 0) {
  215. t = 1;
  216. } else if (t > 9) {
  217. t = 9;
  218. }
  219. data.push(n)
  220. data.push(t)
  221. }
  222. jpPrinter.setBitmap = function(res) { //参数,画布的参数
  223. console.log(res)
  224. var width = parseInt((res.width + 7) / 8 * 8 / 8)
  225. var height = res.height;
  226. var time = 1;
  227. var temp = res.data.length - width * 32;
  228. var point_list = []
  229. console.log(width + "--" + height)
  230. data.push(29)
  231. data.push(118)
  232. data.push(48)
  233. data.push(0)
  234. data.push((parseInt((res.width + 7) / 8) * 8) / 8)
  235. data.push(0)
  236. data.push(parseInt(res.height % 256))
  237. data.push(parseInt(res.height / 256))
  238. console.log(res.data.length)
  239. console.log("temp=" + temp)
  240. for (var i = 0; i < height; ++i) {
  241. for (var j = 0; j < width; ++j) {
  242. for (var k = 0; k < 32; k += 4) {
  243. var po = {}
  244. if (res.data[temp] == 0 && res.data[temp + 1] == 0 && res.data[temp + 2] == 0 && res.data[temp + 3] == 0) {
  245. po.point = 0;
  246. } else {
  247. po.point = 1;
  248. }
  249. point_list.push(po)
  250. temp += 4
  251. }
  252. }
  253. time++
  254. temp = res.data.length - width * 32 * time
  255. }
  256. for (var i = 0; i < point_list.length; i += 8) {
  257. 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
  258. data.push(p)
  259. }
  260. }
  261. jpPrinter.setPrint = function() { //打印并换行
  262. data.push(10)
  263. }
  264. jpPrinter.setPrintAndFeed = function(feed) { //打印并走纸feed个单位
  265. data.push(27)
  266. data.push(74)
  267. data.push(feed)
  268. }
  269. jpPrinter.setPrintAndFeedRow = function(row) { //打印并走纸row行
  270. data.push(27)
  271. data.push(100)
  272. data.push(row)
  273. }
  274. jpPrinter.getData = function() { //获取打印数据
  275. return data;
  276. };
  277.   
  278. return jpPrinter; 
  279. },
  280. Query: function() {
  281. var queryStatus = {};
  282. var buf;
  283. var dateView;
  284. queryStatus.getRealtimeStatusTransmission = function(n) { //查询打印机实时状态
  285. /*
  286. n = 1:传送打印机状态
  287. n = 2:传送脱机状态
  288. n = 3:传送错误状态
  289. n = 4:传送纸传感器状态
  290. */
  291. buf = new ArrayBuffer(3)
  292. dateView = new DataView(buf)
  293. dateView.setUint8(0, 16)
  294. dateView.setUint8(1, 4)
  295. dateView.setUint8(2, n)
  296. queryStatus.query(buf)
  297. }
  298. queryStatus.query = function(buf) {
  299. wx.writeBLECharacteristicValue({
  300. deviceId: app.BLEInformation.deviceId,
  301. serviceId: app.BLEInformation.writeServiceId,
  302. characteristicId: app.BLEInformation.writeCharaterId,
  303. value: buf,
  304. success: function(res) {
  305. },
  306. complete: function(res) {
  307. console.log(res)
  308. buf = null
  309. dateView = null;
  310. }
  311. })
  312. }
  313. return queryStatus;
  314. }
  315. };
  316. module.exports.jpPrinter = jpPrinter;