esc.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. var gpESC = {
  2. createNew: function () {
  3. var jpPrinter = {};
  4. var data = [];
  5. var bar = ["UPC-A", "UPC-E", "EAN13", "EAN8", "CODE39", "ITF", "CODABAR", "CODE93", "CODE128"];
  6. jpPrinter.name = "账单模式";
  7. jpPrinter.init = function () { //初始化打印机
  8. data.push(27)
  9. data.push(64)
  10. };
  11. jpPrinter.setText = function (content) { //设置文本内容
  12. let printData = encodeToGb2312(content)
  13. let code = gpTool.hexStringToByteArray(printData)
  14. for (var i = 0; i < code.length; ++i) {
  15. data.push(code[i])
  16. }
  17. }
  18. jpPrinter.setBarcodeWidth = function (width) { //设置条码宽度
  19. data.push(29)
  20. data.push(119)
  21. if (width > 6) {
  22. width = 6;
  23. }
  24. if (width < 2) {
  25. width = 1;
  26. }
  27. data.push(width)
  28. }
  29. jpPrinter.setBarcodeHeight = function (height) { //设置条码高度
  30. data.push(29)
  31. data.push(104)
  32. data.push(height)
  33. }
  34. jpPrinter.setBarcodeContent = function (t, content) {
  35. let printData = encodeToGb2312(content)
  36. let code = gpTool.hexStringToByteArray(printData)
  37. var ty = 73;
  38. data.push(29)
  39. data.push(107)
  40. switch (t) {
  41. case bar[0]:
  42. ty = 65;
  43. break;
  44. case bar[1]:
  45. ty = 66;
  46. break;
  47. case bar[2]:
  48. ty = 67;
  49. break;
  50. case bar[3]:
  51. ty = 68;
  52. break;
  53. case bar[4]:
  54. ty = 69;
  55. break;
  56. case bar[5]:
  57. ty = 70;
  58. break;
  59. case bar[6]:
  60. ty = 71;
  61. break;
  62. case bar[7]:
  63. ty = 72;
  64. break;
  65. case bar[8]:
  66. ty = 73;
  67. break;
  68. }
  69. data.push(ty)
  70. data.push(parseInt(code.length))
  71. for (var i = 0; i < code.length; ++i) {
  72. data.push(code[i])
  73. }
  74. }
  75. jpPrinter.setSelectSizeOfModuleForQRCode = function (n) { //设置二维码大小
  76. data.push(29)
  77. data.push(40)
  78. data.push(107)
  79. data.push(3)
  80. data.push(0)
  81. data.push(49)
  82. data.push(67)
  83. if (n > 15) {
  84. n = 15
  85. }
  86. if (n < 1) {
  87. n = 1
  88. }
  89. data.push(n)
  90. }
  91. jpPrinter.setSelectErrorCorrectionLevelForQRCode = function (n) { //设置纠错等级
  92. /*
  93. n 功能 纠错能力
  94. 48 选择纠错等级 L 7
  95. 49 选择纠错等级 M 15
  96. 50 选择纠错等级 Q 25
  97. 51 选择纠错等级 H 30
  98. */
  99. data.push(29)
  100. data.push(40)
  101. data.push(107)
  102. data.push(3)
  103. data.push(0)
  104. data.push(49)
  105. data.push(69)
  106. data.push(n)
  107. }
  108. jpPrinter.setStoreQRCodeData = function (content) { //设置二维码内容
  109. let printData = encodeToGb2312(content)
  110. let code = gpTool.hexStringToByteArray(printData)
  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. };