sendCommand.vue 15 KB

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