esc.js 7.6 KB

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