sendCommand.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. <template>
  2. <view class="content">
  3. <view class="body">
  4. <!-- <view>
  5. <textarea class="result" v-model="returnResult"></textarea>
  6. </view>
  7. <textarea class="input" @input="inputEvent" /> -->
  8. <!-- <button type="primary" @tap="sendData">发送(票据可使用)</button> -->
  9. <view style='margin-top:4%;display: flex;flex-direction: column;'>
  10. <button type='primary' class="btn" @tap='receiptTest' :loading='isReceiptSend'
  11. :disabled='isReceiptSend'>打印质检单小票</button>
  12. <button type='primary' class="btn" @tap='goToList'>返回列表页</button>
  13. <!-- <button type='primary' @tap='labelTest' :loading='isLabelSend' :disabled='isLabelSend'>标签测试</button> -->
  14. </view>
  15. <view style='margin-top:4%;display: flex;flex-direction: row;'>
  16. <!-- hidden='true' -->
  17. <canvas canvas-id='edit_area_canvas'
  18. :style="{width:canvasWidth+'px',height:canvasHeight+'px'}"></canvas>
  19. </view>
  20. <!-- <picker style='margin:20px' mode='selector' :range='buffSize' :value='buffIndex' @change='buffBindChange'>
  21. 当前每次发送字节数为(点击可更换):{{buffSize[buffIndex]}}
  22. </picker>
  23. <picker style='margin:20px' mode='selector' :range='printNum' :value='printNumIndex' @change='printNumBindChange'>
  24. 当前打印份数(点击可更换):{{printNum[printNumIndex]}}
  25. </picker> -->
  26. </view>
  27. </view>
  28. </template>
  29. <script>
  30. var tsc = require("../../util/ble/tsc.js");
  31. var esc = require("../../util/ble/esc.js");
  32. var encode = require("../../util/ble/encoding.js");
  33. import {
  34. mapState
  35. } from 'vuex';
  36. export default {
  37. data() {
  38. return {
  39. sendContent: "",
  40. looptime: 0,
  41. currentTime: 1,
  42. lastData: 0,
  43. oneTimeData: 0,
  44. returnResult: "",
  45. canvasWidth: 180,
  46. canvasHeight: 180,
  47. imageSrc: '../../static/img/abc_ic_star_black_16dp.png',
  48. buffSize: [],
  49. buffIndex: 0,
  50. printNum: [],
  51. printNumIndex: 0,
  52. printerNum: 1,
  53. currentPrint: 1,
  54. isReceiptSend: false,
  55. isLabelSend: false,
  56. gridList: {}
  57. };
  58. },
  59. computed: mapState(['sysinfo', 'Bluetooth']),
  60. onLoad() {
  61. uni.showLoading({
  62. title: '加载中',
  63. mask: true
  64. })
  65. this.$api.doRequest('get', '/qualityInspectionManagement/api/getQualityInspection', {
  66. id: uni.getStorageSync('quality_print_id')
  67. }).then(res => {
  68. uni.hideLoading()
  69. if (res.data.data) {
  70. let data = res.data.data
  71. this.gridList.qualityNo = data.qualityNo
  72. }
  73. })
  74. let that = this;
  75. let {
  76. BLEInformation
  77. } = that.Bluetooth;
  78. uni.notifyBLECharacteristicValueChange({
  79. deviceId: BLEInformation.deviceId,
  80. serviceId: BLEInformation.notifyServiceId,
  81. characteristicId: BLEInformation.notifyCharaterId,
  82. state: true,
  83. success(res) {
  84. uni.onBLECharacteristicValueChange(function(r) {
  85. console.log(`characteristic ${r.characteristicId} has changed, now is ${r}`)
  86. })
  87. },
  88. fail: function(e) {
  89. console.log(e)
  90. },
  91. complete: function(e) {
  92. console.log(e)
  93. }
  94. })
  95. },
  96. onReady() {
  97. let list = []
  98. let numList = []
  99. let j = 0
  100. for (let i = 20; i < 200; i += 10) {
  101. list[j] = i;
  102. j++
  103. }
  104. for (let i = 1; i < 10; i++) {
  105. numList[i - 1] = i
  106. }
  107. this.buffSize = list;
  108. this.oneTimeData = list[0];
  109. this.printNum = numList;
  110. this.printerNum = numList[0];
  111. },
  112. onShow() {
  113. this.gridList = uni.getStorageSync("quality_print")
  114. this.utils.nullToString(this.gridList)
  115. if (!this.gridList.compId) {
  116. this.gridList.compId = uni.getStorageSync('pcUserInfo').compId
  117. }
  118. console.log(this.gridList)
  119. let that = this;
  120. let width;
  121. let height;
  122. uni.getImageInfo({
  123. src: that.imageSrc,
  124. success(res) {
  125. console.log(res.width)
  126. console.log(res.height)
  127. width = res.width
  128. height = res.height
  129. that.canvasWidth = res.width;
  130. that.canvasHeight = res.height;
  131. }
  132. })
  133. const ctx = uni.createCanvasContext("edit_area_canvas", this);
  134. // if (app.globalData.platform == "android") {
  135. // ctx.translate(width, height)
  136. // ctx.rotate(180 * Math.PI / 180)
  137. // }
  138. ctx.drawImage(this.imageSrc, 0, 0, width, height);
  139. ctx.draw();
  140. },
  141. onUnload() {
  142. let that = this;
  143. let {
  144. BLEInformation
  145. } = that.Bluetooth;
  146. // uni.closeBLEConnection({
  147. // deviceId: BLEInformation.deviceId,
  148. // success: function(res) {
  149. // console.log("关闭蓝牙成功")
  150. // },
  151. // })
  152. },
  153. methods: {
  154. clearBlue() {
  155. let tmp = this.Bluetooth;
  156. uni.closeBLEConnection({
  157. deviceId: tmp.BLEInformation.deviceId
  158. })
  159. tmp.BLEInformation.deviceId = ''
  160. this.$store.commit('BLEInformationSet', tmp);
  161. // uni.showToast({
  162. // title: '请重新打印质检小票',
  163. // icon: 'none'
  164. // })
  165. uni.navigateBack({
  166. delta: 1
  167. })
  168. },
  169. goToList() {
  170. uni.navigateTo({
  171. url: '/pages/erpbusiness/quality_testing?managementType=1'
  172. })
  173. },
  174. //获取输入内容
  175. inputEvent(e) {
  176. this.sendContent = e.detail.value;
  177. },
  178. //输入框点击发送
  179. sendData() {
  180. let data = this.sendContent + "\n"
  181. this.looptime = 0;
  182. var content = new encode.TextEncoder(
  183. 'gb18030', {
  184. NONSTANDARD_allowLegacyEncoding: true
  185. }).encode(data);
  186. this.prepareSend(content);
  187. },
  188. //打印票据数据
  189. receiptTest() {
  190. console.log(this.gridList)
  191. var that = this;
  192. var canvasWidth = that.canvasWidth
  193. var canvasHeight = that.canvasHeight
  194. var command = esc.jpPrinter.createNew()
  195. command.init()
  196. //编号
  197. // command.rowSpace(10);
  198. if (that.gridList.qualityNo) {
  199. command.setText(that.gridList.qualityNo.substring(that.gridList.qualityNo.length - 3));
  200. command.bold(3); //加粗
  201. command.setFontSize(36); //字体大小
  202. command.setSelectJustification(1) //居中
  203. command.rowSpace(200);
  204. command.setPrint();
  205. // command.rowSpace(60);
  206. // command.bold(0); //取消加粗
  207. // command.setFontSize(0); //正常字体
  208. }
  209. // 标题
  210. command.setText("粮食质检单");
  211. command.bold(1); //加粗
  212. command.setSelectJustification(1) //居中
  213. command.rowSpace(60);
  214. // command.rowSpace(10);
  215. command.setPrint();
  216. // 二维码
  217. command.setSelectSizeOfModuleForQRCode(5);
  218. command.setSelectErrorCorrectionLevelForQRCode(7)
  219. command.setStoreQRCodeData(that.gridList.compId + '&' + that.gridList.qualityNo + '&' + that.gridList
  220. .warehouseId);
  221. command.rowSpace(60);
  222. command.setPrintQRCode();
  223. //客户姓名
  224. command.setText("客户姓名:" + that.gridList.customerName);
  225. command.setSelectJustification(0); //居左
  226. command.setLeftMargin(30)
  227. command.setPrint();
  228. //客户身份证
  229. command.setText("客户身份证:" + that.gridList.customerNumberCard);
  230. command.setSelectJustification(0); //居左
  231. command.setLeftMargin(30)
  232. command.setPrint();
  233. //客户电话
  234. command.setText("客户电话:" + that.gridList.customerPhone);
  235. command.setSelectJustification(0); //居左
  236. command.setLeftMargin(30)
  237. command.setPrint();
  238. //车牌号
  239. command.setText("车牌号:" + that.gridList.carNumber);
  240. command.setSelectJustification(0); //居左
  241. command.setLeftMargin(30)
  242. command.setPrint();
  243. if (that.gridList.boxNo) {
  244. //箱号
  245. command.setText("箱号-1:" + that.gridList.boxNo);
  246. command.setSelectJustification(0); //居左
  247. command.setLeftMargin(30)
  248. command.setPrint();
  249. }
  250. if (that.gridList.boxNoOther) {
  251. //箱号
  252. command.setText("箱号-2:" + that.gridList.boxNoOther);
  253. command.setSelectJustification(0); //居左
  254. command.setLeftMargin(30)
  255. command.setPrint();
  256. }
  257. if (that.gridList.titleNo) {
  258. //封号
  259. command.setText("封号-1:" + that.gridList.titleNo);
  260. command.setSelectJustification(0); //居左
  261. command.setLeftMargin(30)
  262. command.setPrint();
  263. }
  264. if (that.gridList.titleNoOther) {
  265. //封号
  266. command.setText("封号-2:" + that.gridList.titleNoOther);
  267. command.setSelectJustification(0); //居左
  268. command.setLeftMargin(30)
  269. command.setPrint();
  270. }
  271. //仓位号
  272. command.setText("仓位号:" + that.gridList.binNumber);
  273. command.setSelectJustification(0); //居左
  274. command.setLeftMargin(30)
  275. command.setPrint();
  276. //收货单位
  277. command.setText("收货单位:" + that.gridList.compName);
  278. command.setSelectJustification(0); //居左
  279. command.setLeftMargin(30)
  280. command.setPrint();
  281. //联系人
  282. command.setText("联系人:" + that.gridList.person);
  283. command.setSelectJustification(0); //居左
  284. command.setLeftMargin(30)
  285. command.setPrint();
  286. //联系电话
  287. command.setText("联系电话:" + that.gridList.personPhone);
  288. command.setSelectJustification(0); //居左
  289. command.setLeftMargin(30)
  290. command.setPrint();
  291. //货名
  292. command.setText("货名:" + that.gridList.goodsName);
  293. command.setSelectJustification(0); //居左
  294. command.setLeftMargin(30)
  295. command.setPrint();
  296. //类型
  297. command.setText("类型:" + that.gridList.type);
  298. command.setSelectJustification(0); //居左
  299. command.setLeftMargin(30)
  300. command.setPrint();
  301. //容量
  302. command.setText("容量(克/升):" + that.gridList.bulkDensity);
  303. command.setSelectJustification(0); //居左
  304. command.setLeftMargin(30)
  305. command.setPrint();
  306. //不完善粒
  307. command.setText("不完善粒(%):" + that.gridList.imperfectGrain);
  308. command.setSelectJustification(0); //居左
  309. command.setLeftMargin(30)
  310. command.setPrint();
  311. //等级
  312. command.setText("等级:" + that.gridList.grade);
  313. command.setSelectJustification(0); //居左
  314. command.setLeftMargin(30)
  315. command.setPrint();
  316. //水分
  317. command.setText("水分(%):" + that.gridList.waterContent);
  318. command.setSelectJustification(0); //居左
  319. command.setLeftMargin(30)
  320. command.setPrint();
  321. //热损伤
  322. command.setText("热损伤(%):" + that.gridList.jiaorenli);
  323. command.setSelectJustification(0); //居左
  324. command.setLeftMargin(30)
  325. command.setPrint();
  326. //单价
  327. if (that.gridList.type == '潮粮') {
  328. command.setText("单价(元/公斤):" + that.gridList.tidalGrainPrice);
  329. command.setPrint();
  330. }
  331. command.setSelectJustification(0); //居左
  332. command.setLeftMargin(30)
  333. //杂质
  334. command.setText("杂质(%):" + that.gridList.impurity);
  335. command.setSelectJustification(0); //居左
  336. command.setLeftMargin(30)
  337. command.setPrint();
  338. //霉变粒
  339. command.setText("霉变粒(%):" + that.gridList.mildewGrain);
  340. command.setSelectJustification(0); //居左
  341. command.setLeftMargin(30)
  342. command.setPrint();
  343. //购粮性质
  344. command.setText("购粮性质:" + that.gridList.natureOfGrainPurchase);
  345. command.setSelectJustification(0); //居左
  346. command.setLeftMargin(30)
  347. command.setPrint();
  348. //仓位号
  349. command.setText("仓位号:" + that.gridList.binNumber);
  350. command.setSelectJustification(0); //居左
  351. command.setLeftMargin(30)
  352. command.setPrint();
  353. //囤位号
  354. command.setText("囤位号:" + that.gridList.storageTagNo);
  355. command.setSelectJustification(0); //居左
  356. command.setLeftMargin(30)
  357. command.setPrint();
  358. //质检员
  359. command.setText("质检员:" + that.gridList.qualityInspector);
  360. command.setSelectJustification(0); //居左
  361. command.setLeftMargin(30)
  362. command.setPrint();
  363. //客户签名
  364. // command.rowSpace(100);
  365. // command.setSelectJustification(0); //居左
  366. // command.setLeftMargin(30)
  367. // command.setText("客户签名:");
  368. // command.setPrint();
  369. // command.rowSpace(100);
  370. //收货单位签名或盖章
  371. command.setText("收货单位签名或盖章:");
  372. command.setSelectJustification(0); //居左
  373. command.setLeftMargin(30)
  374. command.setPrintAndFeed(150);
  375. command.setPrintAndFeedRow(10);
  376. that.isReceiptSend = true;
  377. that.prepareSend(command.getData());
  378. },
  379. //准备发送,根据每次发送字节数来处理分包数量
  380. prepareSend(buff) {
  381. console.log(buff);
  382. // let that = this
  383. // let time = that.oneTimeData
  384. // let looptime = parseInt(buff.length / time);
  385. // let lastData = parseInt(buff.length % time);
  386. // console.log(looptime + "---" + lastData)
  387. // this.looptime = looptime + 1;
  388. // this.lastData = lastData;
  389. // this.currentTime = 1;
  390. // that.Send(buff)
  391. var that = this;
  392. var time = that.oneTimeData;
  393. var looptime = parseInt(buff.length / time);
  394. var lastData = parseInt(buff.length % time); //console.log(looptime + "---" + lastData)
  395. that.setData({
  396. looptime: looptime + 1,
  397. lastData: lastData,
  398. currentTime: 1
  399. });
  400. that.Send(buff);
  401. },
  402. //查询打印机状态
  403. queryStatus() {
  404. let command = esc.jpPrinter.Query();
  405. command.getRealtimeStatusTransmission(1);
  406. },
  407. //分包发送
  408. Send(buff) {
  409. let that = this
  410. let {
  411. currentTime,
  412. looptime: loopTime,
  413. lastData,
  414. oneTimeData: onTimeData,
  415. printerNum: printNum,
  416. currentPrint
  417. } = that;
  418. let buf;
  419. let dataView;
  420. if (currentTime < loopTime) {
  421. buf = new ArrayBuffer(onTimeData)
  422. dataView = new DataView(buf)
  423. for (var i = 0; i < onTimeData; ++i) {
  424. dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
  425. }
  426. } else {
  427. buf = new ArrayBuffer(lastData)
  428. dataView = new DataView(buf)
  429. for (var i = 0; i < lastData; ++i) {
  430. dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
  431. }
  432. }
  433. console.log("第" + currentTime + "次发送数据大小为:" + buf.byteLength)
  434. let {
  435. BLEInformation
  436. } = that.Bluetooth;
  437. uni.writeBLECharacteristicValue({
  438. deviceId: BLEInformation.deviceId,
  439. serviceId: BLEInformation.writeServiceId,
  440. characteristicId: BLEInformation.writeCharaterId,
  441. value: buf,
  442. success: function(res) {
  443. if (currentTime <= loopTime) {
  444. // wx.showLoading({
  445. // title: '传输中...',
  446. // })
  447. } else {
  448. uni.showToast({
  449. title: '已打印第' + currentPrint + '张成功'
  450. });
  451. } //console.log(res)
  452. },
  453. fail: function(e) {
  454. uni.showToast({
  455. title: '打印第' + currentPrint + '张失败',
  456. icon: 'none'
  457. }); //console.log(e)
  458. },
  459. complete: function() {
  460. currentTime++;
  461. if (currentTime <= loopTime) {
  462. that.setData({
  463. currentTime: currentTime
  464. });
  465. that.Send(buff);
  466. } else {
  467. if (currentPrint == printNum) {
  468. that.setData({
  469. looptime: 0,
  470. lastData: 0,
  471. currentTime: 1,
  472. isReceiptSend: false,
  473. currentPrint: 1
  474. });
  475. } else {
  476. currentPrint++;
  477. that.setData({
  478. currentPrint: currentPrint,
  479. currentTime: 1
  480. });
  481. that.Send(buff);
  482. }
  483. }
  484. }
  485. });
  486. // plus.bluetooth.writeBLECharacteristicValue({
  487. // deviceId: BLEInformation.deviceId,
  488. // serviceId: BLEInformation.writeServiceId,
  489. // characteristicId: BLEInformation.writeCharaterId,
  490. // value: buf,
  491. // success: function(res) {
  492. // console.log(res)
  493. // },
  494. // fail: function(e) {
  495. // console.log(e)
  496. // },
  497. // complete: function() {
  498. // currentTime++
  499. // if (currentTime <= loopTime) {
  500. // that.currentTime = currentTime;
  501. // that.Send(buff)
  502. // } else {
  503. // uni.showToast({
  504. // title: '已打印第' + currentPrint + '张',
  505. // })
  506. // if (currentPrint == printNum) {
  507. // that.looptime = 0;
  508. // that.lastData = 0;
  509. // that.currentTime = 1;
  510. // that.isReceiptSend = false;
  511. // that.isLabelSend = false;
  512. // that.currentPrint = 1;
  513. // } else {
  514. // currentPrint++;
  515. // that.currentPrint = currentPrint;
  516. // that.currentTime = 1;
  517. // that.Send(buff)
  518. // }
  519. // }
  520. // }
  521. // })
  522. },
  523. buffBindChange: function(res) { //更改打印字节数
  524. let index = res.detail.value
  525. let time = this.buffSize[index]
  526. this.buffIndex = index;
  527. this.oneTimeData = time;
  528. },
  529. printNumBindChange: function(res) { //更改打印份数
  530. let index = res.detail.value
  531. let num = this.printNum[index]
  532. this.printNumIndex = index;
  533. this.printerNum = num;
  534. },
  535. }
  536. }
  537. </script>
  538. <style lang="less">
  539. .input {
  540. text-align: top;
  541. width: 90%;
  542. height: 150px;
  543. margin-left: 4%;
  544. margin-right: 4%;
  545. margin-top: 10px;
  546. margin-bottom: 12px;
  547. border: 1px solid slategray;
  548. }
  549. .receiver_info_scroll_view {
  550. width: 90%;
  551. height: 200px;
  552. margin-left: 4%;
  553. margin-right: 4%;
  554. margin-top: 10px;
  555. margin-bottom: 25px;
  556. border: 1px solid black;
  557. }
  558. .result {
  559. width: 90%;
  560. height: 150px;
  561. border: 1px solid black;
  562. margin-left: 4%;
  563. margin-bottom: 4%;
  564. margin-top: 5%;
  565. }
  566. button {
  567. width: 90%;
  568. margin-left: 5%;
  569. margin-right: 5%;
  570. }
  571. .switch {
  572. float: right;
  573. margin-right: 20px;
  574. margin-bottom: 16px;
  575. }
  576. text {
  577. color: #fff;
  578. display: block;
  579. }
  580. input {
  581. color: gainsboro;
  582. float: left;
  583. }
  584. .v_net_ssid {
  585. width: 100%;
  586. background: #fff;
  587. }
  588. .v_net_passw {
  589. width: 100%;
  590. background: antiquewhite;
  591. }
  592. .swiper {
  593. width: 100%;
  594. height: 100%;
  595. }
  596. .btn {
  597. margin-top: 50rpx;
  598. background: #22C572;
  599. }
  600. uni-button[loading][type=primary] {
  601. color: hsla(0, 0%, 100%, .6);
  602. background-color: #22C572;
  603. }
  604. </style>