esc.js 7.7 KB

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