esc.js 6.7 KB

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