sendCommand.vue 16 KB

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