sendCommand.vue 19 KB

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