You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. const BufferHelper = require("./buffer-helper");
  2. // const fs = require('fs');
  3. // const Jimp = require("jimp");
  4. // const request = require('request');
  5. const iconv = require("iconv-lite");
  6. // const Deasync = require("deasync");
  7. const Buffer = require('buffer').Buffer
  8. var exchange_text_with_times = exports.exchange_text_with_times = function exchange_text_with_times(text, times, options){
  9. times = (times === undefined ? 1 : times);
  10. var bytes = new BufferHelper();
  11. var text_buffer = exchange_text(text, options)
  12. for(var i=0;i<times;i++){
  13. bytes.concat(text_buffer);
  14. }
  15. return bytes.toBuffer()
  16. }
  17. var exchange_text = exports.exchange_text = function exchange_text(text, options){
  18. options = options || {
  19. beep: false,
  20. cut: true,
  21. tailingLine: true,
  22. encoding: 'UTF8',
  23. }
  24. // 初始化打印机
  25. var init_printer_bytes = new Buffer([27, 64]);
  26. // 读取设备状态
  27. var check_state_bytes = new Buffer([29, 153]);
  28. // 返回字符 1D 99 XX FF
  29. // 其中XX的每位表示的意义
  30. // 0 0:有纸 1:缺纸
  31. // 1 0:合盖 1:开盖
  32. // 2 0:温度正常 1:机芯过热
  33. // 3 0:电量正常 1:电量低
  34. // 4 0:未打印状态 1:打印状态
  35. // 5 未定义
  36. // 6 未定义
  37. // 7 未定义
  38. // 正常打印模式
  39. var init_bytes = new Buffer([27, 33, 0]);
  40. // 放大字体
  41. //<M></M> 中号字体(2倍高)
  42. //<D></D> 中号字体(2倍宽)
  43. //<B></B> 大号字体(2倍高宽)
  44. //<C></C> 居中
  45. //<CM></CM> 中号字体居中
  46. //<CD></CD> 中号字体居中
  47. //<CB></CB> 大号字体居中
  48. //<QR></QR> 二维码图片地址
  49. //<QRI></QRI> 二维码图片地址
  50. //<PCN></PCN> 公众号
  51. // var m_start_bytes = new Buffer([27, 50, 27, 97, 0, 29, 33, 1]);
  52. // var m_end_bytes = new Buffer([29, 33, 0]);
  53. // var b_start_bytes = new Buffer([27, 50, 27, 97, 0, 29, 33, 17]);
  54. // var b_end_bytes = new Buffer([29, 33, 0]);
  55. var c_start_bytes = new Buffer([27, 97, 1]);
  56. var c_end_bytes = new Buffer([]); // [ 27, 97, 0 ];
  57. // var d_start_bytes = new Buffer([27, 50, 27, 97, 0, 29, 33, 16]);
  58. // var d_end_bytes = new Buffer([29, 33, 0]);
  59. // var cm_start_bytes = new Buffer([27, 50, 29, 33, 1, 27, 97, 1]);
  60. // var cm_end_bytes = new Buffer([29, 33, 0]);
  61. // var cd_start_bytes = new Buffer([27, 50, 29, 33, 16, 27, 97, 1]);
  62. // var cd_end_bytes = new Buffer([29, 33, 0]);
  63. // var cb_start_bytes = new Buffer([27, 50, 29, 33, 17, 27, 97, 1]);
  64. // var cb_end_bytes = new Buffer([29, 33, 0]);
  65. var reset_bytes = new Buffer([27, 97, 0, 29, 33, 0, 27, 50]);
  66. //针对针式调整后
  67. var m_start_bytes = new Buffer([27, 33, 16, 28, 33, 8]);
  68. var m_end_bytes = new Buffer([27, 33, 0, 28, 33, 0]);
  69. var b_start_bytes = new Buffer([27, 33, 48, 28, 33, 12]);
  70. var b_end_bytes = new Buffer([27, 33, 0, 28, 33, 0]);
  71. var cm_start_bytes = new Buffer([27, 97, 1, 27, 33, 16, 28, 33, 8]);
  72. var cm_end_bytes = new Buffer([27, 33, 0, 28, 33, 0]);
  73. var cb_start_bytes = new Buffer([27, 97, 1, 27, 33, 48, 28, 33, 12]);
  74. var cb_end_bytes = new Buffer([27, 33, 0, 28, 33, 0]);
  75. var cd_start_bytes = new Buffer([27, 97, 1, 27, 33, 32, 28, 33, 4]);
  76. var cd_end_bytes = new Buffer([27, 33, 0, 28, 33, 0]);
  77. var d_start_bytes = new Buffer([27, 33, 32, 28, 33, 4]);
  78. var d_end_bytes = new Buffer([27, 33, 0, 28, 33, 0]);
  79. // 居中
  80. var align_center_bytes = new Buffer([27, 97, 1]);
  81. var align_left_bytes = new Buffer([27, 97, 0]);
  82. // 设置行间距 默认值为8即1mm
  83. var default_space_bytes = new Buffer([27, 50]);
  84. var none_space_bytes = new Buffer([27, 51, 0]);
  85. var b_space_bytes = new Buffer([27, 51, 120]);
  86. // 切纸
  87. var cut_bytes = new Buffer([27, 105]);
  88. // 弹钱箱
  89. var moneybox_bytes = new Buffer([27, 112, 7]);
  90. // 蜂鸣 { 27, 66, 次数, 时长 * 50ms }
  91. var beep_bytes = new Buffer([ 27, 66, 3, 2 ])
  92. var bytes = new BufferHelper();
  93. bytes.concat(init_printer_bytes);
  94. bytes.concat(default_space_bytes);
  95. var temp = "";
  96. for (var i = 0; i < text.length; i++)
  97. {
  98. var ch = text[i];
  99. var index;
  100. if (ch == '<')
  101. {
  102. bytes.concat(iconv.encode(temp, options.encoding));
  103. temp = "";
  104. if (text.substring(i, i+3) == "<M>")
  105. {
  106. bytes.concat(m_start_bytes); i += 2;
  107. }
  108. else if (text.substring(i, i+4) == "</M>")
  109. {
  110. bytes.concat(m_end_bytes); i += 3;
  111. }
  112. else if (text.substring(i, i+3) == "<B>")
  113. {
  114. bytes.concat(b_start_bytes); i += 2;
  115. }
  116. else if (text.substring(i, i+4) == "</B>")
  117. {
  118. bytes.concat(b_end_bytes); i += 3;
  119. }
  120. else if (text.substring(i, i+3) == "<D>")
  121. {
  122. bytes.concat(d_start_bytes); i += 2;
  123. }
  124. else if (text.substring(i, i+4) == "</D>")
  125. {
  126. bytes.concat(d_end_bytes); i += 3;
  127. }
  128. else if (text.substring(i, i+3) == "<C>")
  129. {
  130. bytes.concat(c_start_bytes); i += 2;
  131. }
  132. else if (text.substring(i, i+4) == "</C>")
  133. {
  134. bytes.concat(c_end_bytes); i += 3;
  135. }
  136. else if (text.substring(i, i+4) == "<CM>")
  137. {
  138. bytes.concat(cm_start_bytes); i += 3;
  139. }
  140. else if (text.substring(i, i+5) == "</CM>")
  141. {
  142. bytes.concat(cm_end_bytes); i += 4;
  143. }
  144. else if (text.substring(i, i+4) == "<CD>")
  145. {
  146. bytes.concat(cd_start_bytes); i += 3;
  147. }
  148. else if (text.substring(i, i+5) == "</CD>")
  149. {
  150. bytes.concat(cd_end_bytes); i += 4;
  151. }
  152. else if (text.substring(i, i+4) == "<CB>")
  153. {
  154. bytes.concat(cb_start_bytes); i += 3;
  155. }
  156. else if (text.substring(i, i+5) == "</CB>")
  157. {
  158. bytes.concat(cb_end_bytes); i += 4;
  159. }
  160. else if (text.substring(i, i+4) == "<QR>")
  161. {
  162. index = text.indexOf("</QR>", i + 4);
  163. if (index != -1)
  164. {
  165. var url = text.substring(i + 4, index);
  166. bytes.concat(align_center_bytes);
  167. bytes.concat(none_space_bytes);
  168. bytes.concat(change_image_url_to_bytes(url));
  169. bytes.concat(default_space_bytes);
  170. bytes.concat(align_left_bytes);
  171. i = index + 4;
  172. }
  173. else
  174. {
  175. temp = temp + ch;
  176. }
  177. }
  178. else if (text.substring(i, i+5) == "<IMG>")
  179. {
  180. index = text.indexOf("</IMG>", i + 5);
  181. if (index != -1)
  182. {
  183. var url = text.substring(i + 5, index);
  184. bytes.concat(align_center_bytes);
  185. bytes.concat(none_space_bytes);
  186. bytes.concat(change_base64_to_bytes(url));
  187. // console.log("change_image_url_to_bytes:",change_image_url_to_bytes(url))
  188. // console.log("change_base64_to_bytes:",change_base64_to_bytes(url))
  189. bytes.concat(default_space_bytes);
  190. bytes.concat(align_left_bytes);
  191. i = index + 5;
  192. }
  193. else
  194. {
  195. temp = temp + ch;
  196. }
  197. }
  198. else if (text.substring(i, i+5) == "<QRI>")
  199. {
  200. index = text.indexOf("</QRI>", i + 5);
  201. if (index != -1)
  202. {
  203. var url = text.substring(i + 5, index);
  204. bytes.concat(align_center_bytes);
  205. bytes.concat(none_space_bytes);
  206. bytes.concat(change_image_url_to_bytes(url));
  207. bytes.concat(default_space_bytes);
  208. bytes.concat(align_left_bytes);
  209. i = index + 5;
  210. }
  211. else
  212. {
  213. temp = temp + ch;
  214. }
  215. }
  216. else if (text.substring(i, i+5) == "<PCN>")
  217. {
  218. index = text.indexOf("</PCN>", i + 5);
  219. if (index != -1)
  220. {
  221. var url = "http://open.weixin.qq.com/qr/code/?username=" + text.substring(i + 5, index);
  222. bytes.concat(align_center_bytes);
  223. bytes.concat(none_space_bytes);
  224. bytes.concat(change_image_url_to_bytes(url));
  225. bytes.concat(default_space_bytes);
  226. bytes.concat(align_left_bytes);
  227. i = index + 5;
  228. }
  229. else
  230. {
  231. temp = temp + ch;
  232. }
  233. }
  234. }
  235. else if(ch == "\n"){
  236. temp = temp + ch;
  237. bytes.concat(iconv.encode(temp, options.encoding));
  238. bytes.concat(reset_bytes);
  239. temp = "";
  240. }
  241. else
  242. {
  243. temp = temp + ch;
  244. }
  245. }
  246. if (temp.length > 0)
  247. {
  248. bytes.concat(iconv.encode(temp, options.encoding));
  249. }
  250. var line_bytes = new Buffer([10, 10, 10, 10, 10])
  251. if (options.tailingLine){
  252. bytes.concat(line_bytes);
  253. }
  254. if (options.cut){
  255. bytes.concat(cut_bytes);
  256. }
  257. if (options.beep){
  258. bytes.concat(beep_bytes)
  259. }
  260. return bytes.toBuffer();
  261. }
  262. var change_base64_to_bytes = exports.change_base64_to_bytes = function change_base64_to_bytes(base64){
  263. var raw = window.atob(base64);
  264. var rawLength = raw.length;
  265. var array = new Uint8Array(new ArrayBuffer(rawLength));
  266. for(i = 0; i < rawLength; i++) {
  267. array[i] = raw.charCodeAt(i);
  268. }
  269. return array;
  270. }
  271. var change_image_url_to_bytes = exports.change_image_url_to_bytes = function change_image_url_to_bytes(url, options){
  272. options = options||{
  273. encoding: 'UTF8'
  274. };
  275. var buffer = new Buffer(iconv.encode(url, options.encoding));
  276. // var buffer = new Buffer(0);
  277. // var sync = true;
  278. // Jimp.read(url, function (err, img) {
  279. // if(err) {
  280. // console.error(err.message)
  281. // }else{
  282. // img.resize(250, 250).quality(60).greyscale();
  283. // buffer = exchange_image(img, 130);
  284. // }
  285. // sync = false
  286. // });
  287. // while(sync){ Deasync.sleep(100)}
  288. return buffer;
  289. }
  290. // var download_image = exports.download_image = function download_image(url, path){
  291. // try{
  292. // request(url).pipe(fs.createWriteStream(path));
  293. // return true
  294. // }catch(e){
  295. // return false
  296. // }
  297. // }
  298. // var exchange_image = exports.exchange_image = function exchange_image(img, threshold){
  299. // var hex;
  300. // var nl = img.bitmap.width % 256;
  301. // var nh = parseInt(img.bitmap.width / 256);
  302. // var bytes = new BufferHelper();
  303. // // data
  304. // var data = new Buffer([0, 0, 0]);
  305. // var line = new Buffer([10]);
  306. // for (var i = 0; i < parseInt(img.bitmap.height / 24) + 1; i++)
  307. // {
  308. // // ESC * m nL nH 点阵图
  309. // var header = new Buffer([27, 42, 33, nl, nh]);
  310. // bytes.concat(header);
  311. // for (var j = 0; j < img.bitmap.width; j++)
  312. // {
  313. // data[0] = data[1] = data[2] = 0; // Clear to Zero.
  314. // for (var k = 0; k < 24; k++)
  315. // {
  316. // if (((i * 24) + k) < img.bitmap.height) // if within the BMP size
  317. // {
  318. // hex = img.getPixelColor(j, (i * 24) + k);
  319. // if (Jimp.intToRGBA(hex).r <= threshold)
  320. // {
  321. // data[parseInt(k / 8)] += (128 >> (k % 8));
  322. // }
  323. // }
  324. // }
  325. // var dit = new Buffer([data[0], data[1], data[2]])
  326. // bytes.concat(dit);
  327. // }
  328. // bytes.concat(line);
  329. // } // data
  330. // return bytes.toBuffer();
  331. // }