esc.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. var encode = require("./encoding.js")
  2. var app = getApp();
  3. var dlabelPrinter = {
  4. createNew: function () {
  5. var dlabelPrinter = {};
  6. var data = [];
  7. var bar = ["UPC-A", "UPC-E", "EAN13", "EAN8", "CODE39", "ITF", "CODABAR", "CODE93", "CODE128"];
  8. dlabelPrinter.name = "蓝牙打印机";
  9. dlabelPrinter.init = function () {
  10. data.push(27)
  11. data.push(64)
  12. };
  13. dlabelPrinter.setPrint = function () {
  14. data.push(10)
  15. };
  16. dlabelPrinter.setPrintAndFeed = function (n) {
  17. data.push(27)
  18. data.push(74)
  19. data.push(n)
  20. };
  21. dlabelPrinter.setPrintAndFeedRow = function (n) {
  22. data.push(27)
  23. data.push(100)
  24. data.push(n)
  25. };
  26. dlabelPrinter.setHorTab = function () {
  27. data.push(9)
  28. };
  29. dlabelPrinter.setAbsolutePrintPosition = function (where) {
  30. data.push(27)
  31. data.push(36)
  32. data.push(parseInt(where % 256))
  33. data.push(parseInt(where / 256))
  34. };
  35. dlabelPrinter.setRelativePrintPositon = function (where) {
  36. data.push(27)
  37. data.push(92)
  38. data.push(parseInt(where % 256))
  39. data.push(parseInt(where / 256))
  40. };
  41. dlabelPrinter.setSelectJustification = function (which) {
  42. data.push(27)
  43. data.push(97)
  44. data.push(which)
  45. };
  46. dlabelPrinter.setLeftMargin = function (n) {
  47. data.push(29)
  48. data.push(76)
  49. data.push(parseInt(n % 256))
  50. data.push(parseInt(n / 256))
  51. };
  52. dlabelPrinter.setPrintingAreaWidth = function (width) {
  53. data.push(29)
  54. data.push(87)
  55. data.push(parseInt(width % 256))
  56. data.push(parseInt(width / 256))
  57. };
  58. dlabelPrinter.setHorizontalAndVertical = function (x, y) {
  59. data.push(29)
  60. data.push(80)
  61. data.push(x)
  62. data.push(y)
  63. };
  64. dlabelPrinter.setCashboxPulse = function (n, m, t) {
  65. data.push(16)
  66. data.push(20)
  67. data.push(n)
  68. data.push(m)
  69. data.push(t)
  70. };
  71. dlabelPrinter.setPrintPageSignal = function (n) {
  72. data.push(27)
  73. data.push(99)
  74. data.push(51)
  75. data.push(n)
  76. };
  77. dlabelPrinter.setSensorToStopPrint = function (n) {
  78. data.push(27)
  79. data.push(99)
  80. data.push(52)
  81. data.push(n)
  82. };
  83. dlabelPrinter.setSelectKey = function (n) {
  84. data.push(27)
  85. data.push(99)
  86. data.push(53)
  87. data.push(n)
  88. };
  89. dlabelPrinter.setCashCashboxPulse = function (m, t1, t2) {
  90. data.push(27)
  91. data.push(112)
  92. data.push(m)
  93. data.push(t1)
  94. data.push(t2)
  95. };
  96. dlabelPrinter.setSelectPrinter = function (n) {
  97. data.push(27)
  98. data.push(112)
  99. data.push(n)
  100. };
  101. dlabelPrinter.setDefaultLineSpace = function () {
  102. data.push(27)
  103. data.push(50)
  104. };
  105. dlabelPrinter.setLineSpace = function (n) {
  106. data.push(27)
  107. data.push(51)
  108. data.push(n)
  109. };
  110. dlabelPrinter.setCharacterRightSpace = function (n) {
  111. data.push(27)
  112. data.push(32)
  113. data.push(n)
  114. };
  115. dlabelPrinter.setPrintMode = function (mode) {
  116. data.push(27)
  117. data.push(33)
  118. data.push(mode)
  119. }
  120. dlabelPrinter.setUserDefinitionCharacter = function (n) {
  121. data.push(27)
  122. data.push(37)
  123. data.push(n)
  124. };
  125. dlabelPrinter.setUnderlineMode = function (n) {
  126. data.push(27)
  127. data.push(45)
  128. data.push(n)
  129. };
  130. dlabelPrinter.setCancleUserDefinitionCharacter = function (n) {
  131. data.push(27)
  132. data.push(63)
  133. data.push(n)
  134. };
  135. dlabelPrinter.setBoldMode = function (n) {
  136. data.push(27)
  137. data.push(69)
  138. data.push(n)
  139. };
  140. dlabelPrinter.setDoublePrintMode = function (n) {
  141. data.push(27)
  142. data.push(71)
  143. data.push(n)
  144. };
  145. dlabelPrinter.setSelectFont = function (n) {
  146. data.push(27)
  147. data.push(77)
  148. data.push(n)
  149. };
  150. dlabelPrinter.setInternationalCharacters = function (n) {
  151. data.push(27)
  152. data.push(82)
  153. data.push(n)
  154. };
  155. dlabelPrinter.setRotate90 = function (n) {
  156. data.push(27)
  157. data.push(86)
  158. data.push(n)
  159. };
  160. dlabelPrinter.setCodePage = function (n) {
  161. data.push(27)
  162. data.push(116)
  163. data.push(n)
  164. };
  165. dlabelPrinter.setInvertPrintMode = function (n) {
  166. data.push(27)
  167. data.push(123)
  168. data.push(n)
  169. };
  170. dlabelPrinter.setCharacterSize = function (n) {
  171. data.push(29)
  172. data.push(33)
  173. data.push(n)
  174. };
  175. dlabelPrinter.setReverseMode = function (n) {
  176. data.push(29)
  177. data.push(66)
  178. data.push(n)
  179. };
  180. function convertPartialToBitmap(w, start_y, bith, pitch, res) {
  181. console.log('convert bitmap: ' + w + ', ' + start_y + ', ' + bith + ', ' + pitch);
  182. var bits = new Uint8Array(bith * pitch);
  183. data.push(29)
  184. data.push(118)
  185. data.push(48)
  186. data.push(0)
  187. data.push(parseInt(pitch % 256));
  188. data.push(parseInt(pitch / 256));
  189. data.push(parseInt(bith % 256));
  190. data.push(parseInt(bith / 256));
  191. for (var y = 0; y < bith; y++) {
  192. for (var x = 0; x < w; x++) {
  193. var color = res.data[((y + start_y) * w + x) * 4];
  194. if (color < 128) {
  195. bits[parseInt(y * pitch + x / 8)] |= (0x80 >> (x % 8));
  196. }
  197. }
  198. }
  199. for (var i = 0; i < bits.length; i++) {
  200. data.push(bits[i]);
  201. }
  202. }
  203. function convertToMultiBitmap(res) {
  204. var w = res.width;
  205. var h = res.height;
  206. const BLOCK_SIZE = 128;
  207. var pitch = parseInt((w + 7) / 8);
  208. var block = parseInt((h + BLOCK_SIZE - 1) / BLOCK_SIZE);
  209. console.log(w + "--" + h);
  210. for (var i = 0; i < block; i++) {
  211. var bith = BLOCK_SIZE;
  212. if (i * BLOCK_SIZE + bith > h) {
  213. bith = h - i * BLOCK_SIZE;
  214. }
  215. convertPartialToBitmap(w, i * BLOCK_SIZE, bith, pitch, res);
  216. }
  217. console.log(data);
  218. }
  219. function convertToSingleBitmap(res) {
  220. console.log(res)
  221. var w = res.width;
  222. var h = res.height;
  223. var bitw = parseInt((w + 7) / 8) * 8;
  224. var bith = h;
  225. var pitch = parseInt(bitw / 8);
  226. var bits = new Uint8Array(bith * pitch);
  227. console.log(w + "--" + h);
  228. console.log("bitw=" + bitw + ", bith=" + bith + ", pitch=" + pitch);
  229. data.push(29)
  230. data.push(118)
  231. data.push(48)
  232. data.push(0)
  233. data.push(parseInt(pitch % 256));
  234. data.push(parseInt(pitch / 256));
  235. data.push(parseInt(bith % 256));
  236. data.push(parseInt(bith / 256));
  237. console.log(res.data.length)
  238. for (var y = 0; y < h; y++) {
  239. for (var x = 0; x < w; x++) {
  240. var color = res.data[(y * w + x) * 4];
  241. if (color < 128) {
  242. bits[parseInt(y * pitch + x / 8)] |= (0x80 >> (x % 8));
  243. }
  244. }
  245. }
  246. for (var i = 0; i < bits.length; i++) {
  247. data.push(bits[i]);
  248. }
  249. }
  250. dlabelPrinter.setBitmap = function (res) {
  251. console.log(res)
  252. convertToSingleBitmap(res)
  253. console.log(data);
  254. };
  255. dlabelPrinter.setHRIPosition = function (position) {
  256. data.push(29)
  257. data.push(72)
  258. data.push(position)
  259. };
  260. dlabelPrinter.setHRIFont = function (font) {
  261. data.push(29)
  262. data.push(102)
  263. data.push(font)
  264. };
  265. dlabelPrinter.setBarcodeWidth = function (width) {
  266. data.push(29)
  267. data.push(119)
  268. if (width > 6) {
  269. width = 6;
  270. }
  271. if (width < 2) {
  272. width = 1;
  273. }
  274. data.push(width)
  275. };
  276. dlabelPrinter.setBarcodeHeight = function (height) {
  277. data.push(29)
  278. data.push(104)
  279. data.push(height)
  280. };
  281. dlabelPrinter.setCode128 = function (content) {
  282. data.push(29)
  283. data.push(107)
  284. data.push(73)
  285. var code = new encode.TextEncoder(
  286. 'gb18030', {
  287. NONSTANDARD_allowLegacyEncoding: true
  288. }).encode(content)
  289. data.push(code.length)
  290. for (var i = 0; i < code.length; ++i) {
  291. data.push(code[i])
  292. }
  293. };
  294. dlabelPrinter.setBarcodeContent = function (t, content) {
  295. var ty = 73;
  296. data.push(29)
  297. data.push(107)
  298. switch (t) {
  299. case bar[0]:
  300. ty = 65;
  301. break;
  302. case bar[1]:
  303. ty = 66;
  304. break;
  305. case bar[2]:
  306. ty = 67;
  307. break;
  308. case bar[3]:
  309. ty = 68;
  310. break;
  311. case bar[4]:
  312. ty = 69;
  313. break;
  314. case bar[5]:
  315. ty = 70;
  316. break;
  317. case bar[6]:
  318. ty = 71;
  319. break;
  320. case bar[7]:
  321. ty = 72;
  322. break;
  323. case bar[8]:
  324. ty = 73;
  325. break;
  326. }
  327. data.push(ty)
  328. data.push(content)
  329. };
  330. dlabelPrinter.setChineseCharacterMode = function (n) {
  331. data.push(28)
  332. data.push(33)
  333. data.push(n)
  334. };
  335. dlabelPrinter.setSelectChineseCharacter = function () {
  336. data.push(28)
  337. data.push(38)
  338. };
  339. dlabelPrinter.setCancelChineseCharacter = function () {
  340. data.push(28)
  341. data.push(46)
  342. };
  343. dlabelPrinter.setCancelUnderLine = function (n) {
  344. data.push(28)
  345. data.push(45)
  346. data.push(n)
  347. };
  348. dlabelPrinter.setChineseCharacterSpace = function (n1, n2) {
  349. data.push(28)
  350. data.push(83)
  351. data.push(n1)
  352. data.push(n2)
  353. };
  354. dlabelPrinter.setChineseCharacteHeightWidth = function (n) {
  355. data.push(28)
  356. data.push(87)
  357. data.push(n)
  358. };
  359. dlabelPrinter.setBlackMaskOffset = function (p, a, m, n) {
  360. data.push(29)
  361. data.push(40)
  362. data.push(70)
  363. data.push(p % 256)
  364. data.push(p / 256)
  365. data.push(a)
  366. data.push(m)
  367. data.push(n % 256)
  368. data.push(n / 256)
  369. };
  370. dlabelPrinter.setBlackMarkStart = function () {
  371. data.push(29)
  372. data.push(12)
  373. };
  374. dlabelPrinter.setCutter = function () {
  375. data.push(29)
  376. data.push(86)
  377. data.push(1)
  378. };
  379. dlabelPrinter.setCut = function (n) {
  380. data.push(29)
  381. data.push(86)
  382. data.push(101)
  383. data.push(n)
  384. };
  385. dlabelPrinter.setSound = function (n, t) {
  386. data.push(27)
  387. data.push(66)
  388. if (n < 0) {
  389. n = 1;
  390. } else if (n > 9) {
  391. n = 9;
  392. }
  393. if (t < 0) {
  394. t = 1;
  395. } else if (t > 9) {
  396. t = 9;
  397. }
  398. data.push(n)
  399. data.push(t)
  400. };
  401. dlabelPrinter.setOrderTip = function (m, t, n) {
  402. data.push(27)
  403. data.push(67)
  404. if (m < 0) {
  405. m = 1;
  406. } else if (m > 20) {
  407. n = 20;
  408. }
  409. if (t < 0) {
  410. t = 1;
  411. } else if (t > 20) {
  412. t = 20;
  413. }
  414. if (n < 0) {
  415. n = 1;
  416. } else if (n > 3) {
  417. n = 3;
  418. }
  419. data.push(m)
  420. data.push(t)
  421. data.push(n)
  422. };
  423. dlabelPrinter.setSelectSizeOfModuleForQRCode = function (n) {
  424. data.push(29)
  425. data.push(40)
  426. data.push(107)
  427. data.push(3)
  428. data.push(0)
  429. data.push(49)
  430. data.push(67)
  431. if (n > 15) {
  432. n = 15
  433. }
  434. if (n < 1) {
  435. n = 1
  436. }
  437. data.push(n)
  438. };
  439. dlabelPrinter.setSelectErrorCorrectionLevelForQRCode = function (n) {
  440. data.push(29)
  441. data.push(40)
  442. data.push(107)
  443. data.push(3)
  444. data.push(0)
  445. data.push(49)
  446. data.push(69)
  447. data.push(n)
  448. };
  449. dlabelPrinter.setStoreQRCodeData = function (content) {
  450. var code = new encode.TextEncoder(
  451. 'gb18030', {
  452. NONSTANDARD_allowLegacyEncoding: true
  453. }).encode(content)
  454. data.push(29)
  455. data.push(40)
  456. data.push(107)
  457. data.push(parseInt((code.length + 3) % 256))
  458. data.push(parseInt((code.length + 3) / 256))
  459. data.push(49)
  460. data.push(80)
  461. data.push(48)
  462. for (var i = 0; i < code.length; ++i) {
  463. data.push(code[i])
  464. }
  465. };
  466. dlabelPrinter.setPrintQRCode = function () {
  467. data.push(29)
  468. data.push(40)
  469. data.push(107)
  470. data.push(3)
  471. data.push(0)
  472. data.push(49)
  473. data.push(81)
  474. data.push(48)
  475. };
  476. dlabelPrinter.setText = function (content) {
  477. var code = new encode.TextEncoder(
  478. 'gb18030', {
  479. NONSTANDARD_allowLegacyEncoding: true
  480. }).encode(content)
  481. for (var i = 0; i < code.length; ++i) {
  482. data.push(code[i])
  483. }
  484. };
  485. dlabelPrinter.setUserCommand = function (content) {
  486. data.push(content)
  487. };
  488. dlabelPrinter.getData = function () {
  489. return data;
  490. };
  491. dlabelPrinter.clearData = function () {
  492. if (data.length > 0) {
  493. data.clearData
  494. }
  495. };
  496. return dlabelPrinter;
  497. },
  498. Query: function () {
  499. var queryStatus = {};
  500. var buf;
  501. var dateView;
  502. queryStatus.getRealtimeStatusTransmission = function (n) {
  503. buf = new ArrayBuffer(3)
  504. dateView = new DataView(buf)
  505. dateView.setUint8(0, 16)
  506. dateView.setUint8(1, 4)
  507. dateView.setUint8(2, n)
  508. queryStatus.query(buf)
  509. }
  510. queryStatus.query = function (buf) {
  511. wx.writeBLECharacteristicValue({
  512. deviceId: app.BLEInformation.deviceId,
  513. serviceId: app.BLEInformation.writeServiceId,
  514. characteristicId: app.BLEInformation.writeCharaterId,
  515. value: buf,
  516. success: function (res) {
  517. },
  518. complete: function (res) {
  519. console.log(res)
  520. buf = null
  521. dateView = null;
  522. }
  523. })
  524. }
  525. return queryStatus;
  526. }
  527. };
  528. module.exports.dlabelPrinter = dlabelPrinter;