ticket.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. var app = getApp();
  2. var esc = require("../../utils/esc.js");
  3. var encode = require("../../utils/encoding.js");
  4. function convertToGrayscale(data) {
  5. let g = 0
  6. for (let i = 0; i < data.length; i += 4) {
  7. g = (data[i] * 0.3 + data[i + 1] * 0.59 + data[i + 2] * 0.11)
  8. data[i] = g
  9. data[i + 1] = g
  10. data[i + 2] = g
  11. }
  12. return data
  13. }
  14. function inArray(arr, key, val) {
  15. for (let i = 0; i < arr.length; i++) {
  16. if (arr[i][key] === val) {
  17. return i;
  18. }
  19. }
  20. return -1;
  21. }
  22. // ArrayBuffer转16进度字符串示例
  23. function ab2hex(buffer) {
  24. var hexArr = Array.prototype.map.call(
  25. new Uint8Array(buffer),
  26. function (bit) {
  27. return ('00' + bit.toString(16)).slice(-2)
  28. }
  29. )
  30. return hexArr.join('');
  31. }
  32. Page({
  33. /**
  34. * 页面的初始数据
  35. */
  36. data: {
  37. looptime: 0,
  38. currentTime: 1,
  39. lastData: 0,
  40. oneTimeData: 0,
  41. returnResult: "",
  42. buffSize: [],
  43. buffIndex: 0,//发送字节数下标
  44. printNum: [],
  45. printNumIndex: 0,
  46. printerNum: 1,
  47. currentPrint: 1,
  48. isReceiptSend: false,
  49. isCheckTake:false,
  50. isQuery:false,
  51. chs: [],
  52. },
  53. /**
  54. * 生命周期函数--监听页面加载
  55. */
  56. onLoad: function (options) {
  57. // var that = this;
  58. // wx.notifyBLECharacteristicValueChange({
  59. // deviceId: app.BLEInformation.deviceId,
  60. // serviceId: app.BLEInformation.notifyServiceId,
  61. // characteristicId: app.BLEInformation.notifyCharaterId,
  62. // state: true,
  63. // success: function (res) {
  64. // wx.onBLECharacteristicValueChange(function (r) {
  65. // console.log(`characteristic ${r.characteristicId} has changed, now is ${r}`)
  66. // })
  67. // },
  68. // fail: function (e) {
  69. // console.log(e)
  70. // },
  71. // complete: function (e) {
  72. // console.log(e)
  73. // }
  74. // })
  75. },
  76. receiptTest: function () { //票据测试
  77. var that = this;
  78. var command = esc.jpPrinter.createNew()
  79. command.init()//初始化打印机
  80. command.setLeftMargin(0)//设置左边距
  81. command.setPrint()
  82. command.setPrintMode(41)
  83. command.setText(" 71106 29079 81101 66268")
  84. command.setPrint()
  85. command.setPrintMode(0)
  86. command.setText("测试票,机号:44010660 2007.09.07-10:43:32")
  87. command.setPrint()
  88. command.setPrintMode(0)
  89. command.setText("双色球(B001)/2007105(0623)期 序号:00067:单式")
  90. command.setPrint()
  91. command.setPrint()
  92. command.setPrintMode(0)
  93. command.setText("1> ")
  94. command.setPrintMode(41)
  95. command.setText("03 04 13 18 19 27 - 13* 001")
  96. command.setPrint()
  97. command.setPrintMode(0)
  98. command.setText("2> ")
  99. command.setPrintMode(41)
  100. command.setText("01 04 17 18 19 28 - 01* 001")
  101. command.setPrint()
  102. command.setPrintMode(0)
  103. command.setText("3> ")
  104. command.setPrintMode(41)
  105. command.setText("01 04 17 18 19 28 - 01* 001")
  106. command.setPrint()
  107. command.setPrintMode(41)
  108. command.setText(" CD33 7B07 5F 8D D2C2")
  109. command.setPrint()
  110. command.setPrintMode(0)
  111. command.setText("开奖日:2007-09-09 金额20元")
  112. command.setPrint()
  113. command.setText("站地址:好易内部测试")
  114. command.setPrint()
  115. command.setText("查询电话:16880345 客服电话:020-88320568")
  116. command.setPrint()
  117. command.setText("兑奖通知:中奖10000元以内(含10000元)由银行自动返奖;10000元以上持本票到广州市福利彩票发行中心领奖。(此票不能在电脑福利彩票投注站兑奖)")
  118. // command.setAbsolutePrintPosition(18)
  119. command.setPrint()
  120. command.setHRIPosition(0)//设置HRI位置
  121. command.setHRIFont(0)//HRI字体大小
  122. command.setBarcodeHeight(60)//条码高度
  123. command.setBarcodeWidth(2)//设置条码宽度
  124. command.setAbsolutePrintPosition(24)
  125. command.setCode128("{A71106290798110166238");//code128 A类型
  126. command.setText(" 888888888888")
  127. command.setPrint()
  128. command.setPrint()
  129. command.setPrint()
  130. command.setPrint()
  131. command.setPrint()
  132. command.setPrint()
  133. command.setPrint()
  134. command.setPrint()
  135. command.setPrint()
  136. command.setBlackMarkStart()
  137. command.setCut()
  138. that.prepareSend(command.getData())//准备发送数据
  139. },
  140. prepareSend: function (buff) { //准备发送,根据每次发送字节数来处理分包数量
  141. //console.log(buff)
  142. var that = this
  143. var time = that.data.oneTimeData
  144. var looptime = parseInt(buff.length / time);
  145. var lastData = parseInt(buff.length % time);
  146. //console.log(looptime + "---" + lastData)
  147. that.setData({
  148. looptime: looptime + 1,
  149. lastData: lastData,
  150. currentTime: 1,
  151. })
  152. that.Send(buff)
  153. },
  154. queryStatus:function(){//查询打印机状态
  155. var that = this
  156. var buf;
  157. var dateView;
  158. /*
  159. n = 1:传送打印机状态
  160. n = 2:传送脱机状态
  161. n = 3:传送错误状态
  162. n = 4:传送纸传感器状态
  163. */
  164. buf = new ArrayBuffer(3)
  165. dateView = new DataView(buf)
  166. dateView.setUint8(0, 16)
  167. dateView.setUint8(1, 4)
  168. dateView.setUint8(2, 2)
  169. wx.writeBLECharacteristicValue({
  170. deviceId: app.BLEInformation.deviceId,
  171. serviceId: app.BLEInformation.writeServiceId,
  172. characteristicId: app.BLEInformation.writeCharaterId,
  173. value: buf,
  174. success: function (res) {
  175. console.log("发送成功")
  176. that.setData({
  177. isQuery: true
  178. })
  179. },
  180. fail: function (e) {
  181. wx.showToast({
  182. title: '发送失败',
  183. icon: 'none',
  184. })
  185. //console.log(e)
  186. return;
  187. },
  188. complete: function () {
  189. }
  190. })
  191. wx.notifyBLECharacteristicValueChange({
  192. deviceId: app.BLEInformation.deviceId,
  193. serviceId: app.BLEInformation.notifyServiceId,
  194. characteristicId: app.BLEInformation.notifyCharaterId,
  195. state: true,
  196. success: function (res) {
  197. wx.onBLECharacteristicValueChange(function (r) {
  198. console.log(`characteristic ${r.characteristicId} has changed, now is ${r}`)
  199. var result = ab2hex(r.value)
  200. console.log("返回" + result)
  201. var tip = ''
  202. if (result == 12) {//正常
  203. tip = "正常"
  204. } else if (result == 32) {//缺纸
  205. tip = "缺纸"
  206. } else if (result == 36) {//开盖、缺纸
  207. tip = "开盖、缺纸"
  208. } else if (result == 16) {
  209. tip = "开盖"
  210. } else if (result == 40) {//其他错误
  211. tip = "其他错误"
  212. } else {//未处理错误
  213. tip = "未知错误"
  214. }
  215. wx.showModal({
  216. title: '打印机状态',
  217. content: tip,
  218. showCancel: false
  219. })
  220. })
  221. },
  222. fail: function (e) {
  223. wx.showModal({
  224. title: '打印机状态',
  225. content: '获取失败',
  226. showCancel: false
  227. })
  228. console.log(e)
  229. },
  230. complete: function (e) {
  231. that.setData({
  232. isQuery: false
  233. })
  234. console.log("执行完成")
  235. }
  236. })
  237. },
  238. checkTake: function () { //查询纸张状态
  239. var that = this
  240. var buf;
  241. var dateView;
  242. /*
  243. n = 1:传送打印机状态
  244. n = 2:传送脱机状态
  245. n = 3:传送错误状态
  246. n = 4:传送纸传感器状态
  247. */
  248. buf = new ArrayBuffer(3)
  249. dateView = new DataView(buf)
  250. dateView.setUint8(0, 16)
  251. dateView.setUint8(1, 4)
  252. dateView.setUint8(2, 4)
  253. wx.writeBLECharacteristicValue({
  254. deviceId: app.BLEInformation.deviceId,
  255. serviceId: app.BLEInformation.writeServiceId,
  256. characteristicId: app.BLEInformation.writeCharaterId,
  257. value: buf,
  258. success: function (res) {
  259. console.log("发送成功")
  260. that.setData({
  261. isCheckTake: true
  262. })
  263. },
  264. fail: function (e) {
  265. wx.showToast({
  266. title: '发送失败',
  267. icon: 'none',
  268. })
  269. //console.log(e)
  270. return;
  271. },
  272. complete: function () {
  273. }
  274. })
  275. wx.notifyBLECharacteristicValueChange({
  276. deviceId: app.BLEInformation.deviceId,
  277. serviceId: app.BLEInformation.notifyServiceId,
  278. characteristicId: app.BLEInformation.notifyCharaterId,
  279. state: true,
  280. success: function (res) {
  281. wx.onBLECharacteristicValueChange(function (r) {
  282. console.log(`characteristic ${r.characteristicId} has changed, now is ${r}`)
  283. var result = ab2hex(r.value)
  284. console.log("返回"+result)
  285. var tip = ''
  286. if (result == 12) {//纸张已取走
  287. tip = "已取走"
  288. } else if (result == 10) {//纸张未取走
  289. tip = "未取走"
  290. }else{//未处理错误
  291. tip = "未知"
  292. }
  293. wx.showModal({
  294. title: '彩票是否取走?',
  295. content: tip,
  296. showCancel: false
  297. })
  298. })
  299. },
  300. fail: function (e) {
  301. wx.showModal({
  302. title: '彩票是否取走?',
  303. content: '获取失败',
  304. showCancel: false
  305. })
  306. console.log(e)
  307. },
  308. complete: function (e) {
  309. that.setData({
  310. isCheckTake: false
  311. })
  312. console.log("执行完成")
  313. }
  314. })
  315. // 操作之前先监听,保证第一时间获取数据
  316. // wx.onBLECharacteristicValueChange((characteristic) => {
  317. // const idx = inArray(this.data.chs, 'uuid', characteristic.characteristicId)
  318. // const data = {}
  319. // if (idx === -1) {
  320. // var result = ab2hex(characteristic.value)
  321. // data[`chs[${this.data.chs.length}]`] = {
  322. // uuid: characteristic.characteristicId,
  323. // // value: ab2hex(characteristic.value)
  324. // value: result
  325. // }
  326. // console.log("获取失败")
  327. // wx.showModal({
  328. // title: '提示',
  329. // content: '获取状态失败',
  330. // showCancel: false
  331. // })
  332. // that.setData({
  333. // isQuery: false
  334. // })
  335. // } else {
  336. // var result = ab2hex(characteristic.value)
  337. // data[`chs[${idx}]`] = {
  338. // uuid: characteristic.characteristicId,
  339. // value: ab2hex(characteristic.value)
  340. // }
  341. // var tip = ''
  342. // if (result == 12) {//正常
  343. // tip = "正常"
  344. // } else if (result == 36) {//缺纸
  345. // tip = "缺纸"
  346. // } else if (result == 16) {//开盖
  347. // tip = "开盖"
  348. // } else if (result == 40) {//其他错误
  349. // tip = "其他错误"
  350. // }else{//未处理错误
  351. // tip = "未知错误"
  352. // }
  353. // wx.showModal({
  354. // title: '提示',
  355. // content: tip,
  356. // showCancel: false
  357. // })
  358. // that.setData({
  359. // isQuery: false
  360. // })
  361. // }
  362. // data[`chs[${this.data.chs.length}]`] = {
  363. // uuid: characteristic.characteristicId,
  364. // value: ab2hex(characteristic.value)
  365. // }
  366. // this.setData(data)
  367. // })
  368. },
  369. Send: function (buff) { //分包发送
  370. var that = this
  371. var currentTime = that.data.currentTime
  372. var loopTime = that.data.looptime
  373. var lastData = that.data.lastData
  374. var onTimeData = that.data.oneTimeData
  375. var printNum = that.data.printerNum
  376. var currentPrint = that.data.currentPrint
  377. var buf
  378. var dataView
  379. if (currentTime < loopTime) {
  380. buf = new ArrayBuffer(onTimeData)
  381. dataView = new DataView(buf)
  382. for (var i = 0; i < onTimeData; ++i) {
  383. dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
  384. }
  385. } else {
  386. buf = new ArrayBuffer(lastData)
  387. dataView = new DataView(buf)
  388. for (var i = 0; i < lastData; ++i) {
  389. dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
  390. }
  391. }
  392. //console.log("第" + currentTime + "次发送数据大小为:" + buf.byteLength)
  393. wx.writeBLECharacteristicValue({
  394. deviceId: app.BLEInformation.deviceId,
  395. serviceId: app.BLEInformation.writeServiceId,
  396. characteristicId: app.BLEInformation.writeCharaterId,
  397. value: buf,
  398. success: function (res) {
  399. if (currentPrint == printNum) {
  400. wx.showToast({
  401. title: '已打印第' + currentPrint + '张成功',
  402. })
  403. }
  404. //console.log(res)
  405. },
  406. fail: function (e) {
  407. wx.showToast({
  408. title: '打印第' + currentPrint + '张失败',
  409. icon: 'none',
  410. })
  411. //console.log(e)
  412. },
  413. complete: function () {
  414. currentTime++
  415. if (currentTime <= loopTime) {
  416. that.setData({
  417. currentTime: currentTime
  418. })
  419. that.Send(buff)
  420. } else {
  421. // wx.showToast({
  422. // title: '已打印第' + currentPrint + '张',
  423. // })
  424. if (currentPrint == printNum) {
  425. that.setData({
  426. looptime: 0,
  427. lastData: 0,
  428. currentTime: 1,
  429. isReceiptSend: false,
  430. currentPrint: 1
  431. })
  432. } else {
  433. currentPrint++
  434. that.setData({
  435. currentPrint: currentPrint,
  436. currentTime: 1,
  437. })
  438. that.Send(buff)
  439. }
  440. }
  441. }
  442. })
  443. },
  444. /**
  445. * 生命周期函数--监听页面初次渲染完成
  446. */
  447. onReady: function () {
  448. var list = []
  449. var numList = []
  450. var j = 0
  451. for (var i = 20; i < 200; i += 10) {
  452. list[j] = i;
  453. j++
  454. }
  455. for (var i = 1; i < 10; i++) {
  456. numList[i - 1] = i
  457. }
  458. this.setData({
  459. buffSize: list,
  460. oneTimeData: list[0],
  461. printNum: numList,
  462. printerNum: numList[0]
  463. })
  464. },
  465. /**
  466. * 生命周期函数--监听页面显示
  467. */
  468. onShow: function () {
  469. },
  470. buffBindChange: function (res) { //更改打印字节数
  471. var index = res.detail.value
  472. var time = this.data.buffSize[index]
  473. this.setData({
  474. buffIndex: index,
  475. oneTimeData: time
  476. })
  477. },
  478. printNumBindChange: function (res) { //更改打印份数
  479. var index = res.detail.value
  480. var num = this.data.printNum[index]
  481. this.setData({
  482. printNumIndex: index,
  483. printerNum: num
  484. })
  485. },
  486. /**
  487. * 生命周期函数--监听页面隐藏
  488. */
  489. onHide: function () {
  490. },
  491. /**
  492. * 生命周期函数--监听页面卸载
  493. */
  494. onUnload: function () {
  495. // wx.closeBLEConnection({
  496. // deviceId: app.BLEInformation.deviceId,
  497. // success: function(res) {
  498. // console.log("关闭蓝牙成功")
  499. // },
  500. // })
  501. },
  502. /**
  503. * 页面相关事件处理函数--监听用户下拉动作
  504. */
  505. onPullDownRefresh: function () {
  506. },
  507. /**
  508. * 页面上拉触底事件的处理函数
  509. */
  510. onReachBottom: function () {
  511. },
  512. /**
  513. * 用户点击右上角分享
  514. */
  515. onShareAppMessage: function () {
  516. }
  517. })