sendCommand.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. this.$store.commit('BLEInformationSet', '');
  157. uni.showToast({
  158. title: '请重新打印质检小票',
  159. icon: 'none'
  160. })
  161. },
  162. goToList() {
  163. uni.navigateTo({
  164. url: '/pages/erpbusiness/quality_testing?managementType=1'
  165. })
  166. },
  167. //获取输入内容
  168. inputEvent(e) {
  169. this.sendContent = e.detail.value;
  170. },
  171. //输入框点击发送
  172. sendData() {
  173. let data = this.sendContent + "\n"
  174. this.looptime = 0;
  175. var content = new encode.TextEncoder(
  176. 'gb18030', {
  177. NONSTANDARD_allowLegacyEncoding: true
  178. }).encode(data);
  179. this.prepareSend(content);
  180. },
  181. //打印票据数据
  182. receiptTest() {
  183. console.log(this.gridList)
  184. var that = this;
  185. var canvasWidth = that.canvasWidth
  186. var canvasHeight = that.canvasHeight
  187. var command = esc.jpPrinter.createNew()
  188. command.init()
  189. //编号
  190. // command.rowSpace(10);
  191. if (that.gridList.qualityNo) {
  192. command.bold(3); //加粗
  193. command.setFontSize(36); //字体大小
  194. command.setSelectJustification(1) //居中
  195. command.rowSpace(200);
  196. command.setText(that.gridList.qualityNo.substring(that.gridList.qualityNo.length - 3));
  197. } else {
  198. uni.showToast('编号错误!')
  199. }
  200. command.setPrint();
  201. command.rowSpace(60);
  202. command.bold(0); //取消加粗
  203. command.setFontSize(0); //正常字体
  204. // 标题
  205. command.bold(1); //加粗
  206. command.setFontSize(16); //字体大小
  207. command.setSelectJustification(1) //居中
  208. // command.rowSpace(10);
  209. command.setText("粮食质检单");
  210. command.setPrint();
  211. command.rowSpace(60);
  212. command.bold(0); //取消加粗
  213. command.setFontSize(0); //正常字体
  214. // 二维码
  215. command.rowSpace(60);
  216. command.setSelectSizeOfModuleForQRCode(5);
  217. command.setSelectErrorCorrectionLevelForQRCode(7)
  218. command.setStoreQRCodeData(that.gridList.compId + '&' + that.gridList.qualityNo + '&' + that.gridList
  219. .warehouseId);
  220. command.setPrintQRCode();
  221. //客户姓名
  222. command.setSelectJustification(0); //居左
  223. command.setLeftMargin(30)
  224. command.setText("客户姓名:" + that.gridList.customerName);
  225. command.setPrint();
  226. //客户身份证
  227. command.setSelectJustification(0); //居左
  228. command.setLeftMargin(30)
  229. command.setText("客户身份证:" + that.gridList.customerNumberCard);
  230. command.setPrint();
  231. //客户电话
  232. command.setSelectJustification(0); //居左
  233. command.setLeftMargin(30)
  234. command.setText("客户电话:" + that.gridList.customerPhone);
  235. command.setPrint();
  236. //车牌号
  237. command.setSelectJustification(0); //居左
  238. command.setLeftMargin(30)
  239. command.setText("车牌号:" + that.gridList.carNumber);
  240. command.setPrint();
  241. if (that.gridList.boxNo) {
  242. //箱号
  243. command.setSelectJustification(0); //居左
  244. command.setLeftMargin(30)
  245. command.setText("箱号-1:" + that.gridList.boxNo);
  246. command.setPrint();
  247. }
  248. if (that.gridList.boxNoOther) {
  249. //箱号
  250. command.setSelectJustification(0); //居左
  251. command.setLeftMargin(30)
  252. command.setText("箱号-2:" + that.gridList.boxNoOther);
  253. command.setPrint();
  254. }
  255. if (that.gridList.titleNo) {
  256. //封号
  257. command.setSelectJustification(0); //居左
  258. command.setLeftMargin(30)
  259. command.setText("封号-1:" + that.gridList.titleNo);
  260. command.setPrint();
  261. }
  262. if (that.gridList.titleNoOther) {
  263. //封号
  264. command.setSelectJustification(0); //居左
  265. command.setLeftMargin(30)
  266. command.setText("封号-2:" + that.gridList.titleNoOther);
  267. command.setPrint();
  268. }
  269. //仓位号
  270. command.setSelectJustification(0); //居左
  271. command.setLeftMargin(30)
  272. command.setText("仓位号:" + that.gridList.binNumber);
  273. command.setPrint();
  274. //收货单位
  275. command.setSelectJustification(0); //居左
  276. command.setLeftMargin(30)
  277. command.setText("收货单位:" + that.gridList.compName);
  278. command.setPrint();
  279. //联系人
  280. command.setSelectJustification(0); //居左
  281. command.setLeftMargin(30)
  282. command.setText("联系人:" + that.gridList.person);
  283. command.setPrint();
  284. //联系电话
  285. command.setSelectJustification(0); //居左
  286. command.setLeftMargin(30)
  287. command.setText("联系电话:" + that.gridList.personPhone);
  288. command.setPrint();
  289. //货名
  290. command.setSelectJustification(0); //居左
  291. command.setLeftMargin(30)
  292. command.setText("货名:" + that.gridList.goodsName);
  293. command.setPrint();
  294. //类型
  295. command.setSelectJustification(0); //居左
  296. command.setLeftMargin(30)
  297. command.setText("类型:" + that.gridList.type);
  298. command.setPrint();
  299. //容量
  300. command.setSelectJustification(0); //居左
  301. command.setLeftMargin(30)
  302. command.setText("容量(克/升):" + that.gridList.bulkDensity);
  303. command.setPrint();
  304. //不完善粒
  305. command.setSelectJustification(0); //居左
  306. command.setLeftMargin(30)
  307. command.setText("不完善粒(%):" + that.gridList.imperfectGrain);
  308. command.setPrint();
  309. //等级
  310. command.setSelectJustification(0); //居左
  311. command.setLeftMargin(30)
  312. command.setText("等级:" + that.gridList.grade);
  313. command.setPrint();
  314. //水分
  315. command.setSelectJustification(0); //居左
  316. command.setLeftMargin(30)
  317. command.setText("水分(%):" + that.gridList.waterContent);
  318. command.setPrint();
  319. //热损伤
  320. command.setSelectJustification(0); //居左
  321. command.setLeftMargin(30)
  322. command.setText("热损伤(%):" + that.gridList.jiaorenli);
  323. command.setPrint();
  324. //单价
  325. command.setSelectJustification(0); //居左
  326. command.setLeftMargin(30)
  327. if (that.gridList.type == '潮粮') {
  328. command.setText("单价(元/公斤):" + that.gridList.tidalGrainPrice);
  329. } else {
  330. command.setText("单价(元/公斤):" + that.gridList.dryGrainPrice);
  331. }
  332. command.setPrint();
  333. //杂质
  334. command.setSelectJustification(0); //居左
  335. command.setLeftMargin(30)
  336. command.setText("杂质(%):" + that.gridList.impurity);
  337. command.setPrint();
  338. //霉变粒
  339. command.setSelectJustification(0); //居左
  340. command.setLeftMargin(30)
  341. command.setText("霉变粒(%):" + that.gridList.mildewGrain);
  342. command.setPrint();
  343. //购粮性质
  344. command.setSelectJustification(0); //居左
  345. command.setLeftMargin(30)
  346. command.setText("购粮性质:" + that.gridList.natureOfGrainPurchase);
  347. command.setPrint();
  348. //仓位号
  349. command.setSelectJustification(0); //居左
  350. command.setLeftMargin(30)
  351. command.setText("仓位号:" + that.gridList.binNumber);
  352. command.setPrint();
  353. //囤位号
  354. command.setSelectJustification(0); //居左
  355. command.setLeftMargin(30)
  356. command.setText("囤位号:" + that.gridList.storageTagNo);
  357. command.setPrint();
  358. //质检员
  359. command.setSelectJustification(0); //居左
  360. command.setLeftMargin(30)
  361. command.setText("质检员:" + that.gridList.qualityInspector);
  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.setSelectJustification(0); //居左
  372. command.setLeftMargin(30)
  373. command.setText("收货单位签名或盖章:");
  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. },
  392. //查询打印机状态
  393. queryStatus() {
  394. let command = esc.jpPrinter.Query();
  395. command.getRealtimeStatusTransmission(1);
  396. },
  397. //分包发送
  398. Send(buff) {
  399. let that = this
  400. let {
  401. currentTime,
  402. looptime: loopTime,
  403. lastData,
  404. oneTimeData: onTimeData,
  405. printerNum: printNum,
  406. currentPrint
  407. } = that;
  408. let buf;
  409. let dataView;
  410. if (currentTime < loopTime) {
  411. buf = new ArrayBuffer(onTimeData)
  412. dataView = new DataView(buf)
  413. for (var i = 0; i < onTimeData; ++i) {
  414. dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
  415. }
  416. } else {
  417. buf = new ArrayBuffer(lastData)
  418. dataView = new DataView(buf)
  419. for (var i = 0; i < lastData; ++i) {
  420. dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
  421. }
  422. }
  423. console.log("第" + currentTime + "次发送数据大小为:" + buf.byteLength)
  424. let {
  425. BLEInformation
  426. } = that.Bluetooth;
  427. plus.bluetooth.writeBLECharacteristicValue({
  428. deviceId: BLEInformation.deviceId,
  429. serviceId: BLEInformation.writeServiceId,
  430. characteristicId: BLEInformation.writeCharaterId,
  431. value: buf,
  432. success: function(res) {
  433. console.log(res)
  434. },
  435. fail: function(e) {
  436. console.log(e)
  437. },
  438. complete: function() {
  439. currentTime++
  440. if (currentTime <= loopTime) {
  441. that.currentTime = currentTime;
  442. that.Send(buff)
  443. } else {
  444. uni.showToast({
  445. title: '已打印第' + currentPrint + '张',
  446. })
  447. if (currentPrint == printNum) {
  448. that.looptime = 0;
  449. that.lastData = 0;
  450. that.currentTime = 1;
  451. that.isReceiptSend = false;
  452. that.isLabelSend = false;
  453. that.currentPrint = 1;
  454. } else {
  455. currentPrint++;
  456. that.currentPrint = currentPrint;
  457. that.currentTime = 1;
  458. that.Send(buff)
  459. }
  460. }
  461. }
  462. })
  463. },
  464. buffBindChange: function(res) { //更改打印字节数
  465. let index = res.detail.value
  466. let time = this.buffSize[index]
  467. this.buffIndex = index;
  468. this.oneTimeData = time;
  469. },
  470. printNumBindChange: function(res) { //更改打印份数
  471. let index = res.detail.value
  472. let num = this.printNum[index]
  473. this.printNumIndex = index;
  474. this.printerNum = num;
  475. },
  476. }
  477. }
  478. </script>
  479. <style lang="less">
  480. .input {
  481. text-align: top;
  482. width: 90%;
  483. height: 150px;
  484. margin-left: 4%;
  485. margin-right: 4%;
  486. margin-top: 10px;
  487. margin-bottom: 12px;
  488. border: 1px solid slategray;
  489. }
  490. .receiver_info_scroll_view {
  491. width: 90%;
  492. height: 200px;
  493. margin-left: 4%;
  494. margin-right: 4%;
  495. margin-top: 10px;
  496. margin-bottom: 25px;
  497. border: 1px solid black;
  498. }
  499. .result {
  500. width: 90%;
  501. height: 150px;
  502. border: 1px solid black;
  503. margin-left: 4%;
  504. margin-bottom: 4%;
  505. margin-top: 5%;
  506. }
  507. button {
  508. width: 90%;
  509. margin-left: 5%;
  510. margin-right: 5%;
  511. }
  512. .switch {
  513. float: right;
  514. margin-right: 20px;
  515. margin-bottom: 16px;
  516. }
  517. text {
  518. color: #fff;
  519. display: block;
  520. }
  521. input {
  522. color: gainsboro;
  523. float: left;
  524. }
  525. .v_net_ssid {
  526. width: 100%;
  527. background: #fff;
  528. }
  529. .v_net_passw {
  530. width: 100%;
  531. background: antiquewhite;
  532. }
  533. .swiper {
  534. width: 100%;
  535. height: 100%;
  536. }
  537. .btn {
  538. margin-top: 50rpx;
  539. background: #22C572;
  540. }
  541. uni-button[loading][type=primary] {
  542. color: hsla(0, 0%, 100%, .6);
  543. background-color: #22C572;
  544. }
  545. </style>