sendCommand.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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. }
  323. command.setPrint();
  324. //杂质
  325. command.setSelectJustification(0); //居左
  326. command.setLeftMargin(30)
  327. command.setText("杂质(%):" + that.gridList.impurity);
  328. command.setPrint();
  329. //霉变粒
  330. command.setSelectJustification(0); //居左
  331. command.setLeftMargin(30)
  332. command.setText("霉变粒(%):" + that.gridList.mildewGrain);
  333. command.setPrint();
  334. //购粮性质
  335. command.setSelectJustification(0); //居左
  336. command.setLeftMargin(30)
  337. command.setText("购粮性质:" + that.gridList.natureOfGrainPurchase);
  338. command.setPrint();
  339. //仓位号
  340. command.setSelectJustification(0); //居左
  341. command.setLeftMargin(30)
  342. command.setText("仓位号:" + that.gridList.binNumber);
  343. command.setPrint();
  344. //囤位号
  345. command.setSelectJustification(0); //居左
  346. command.setLeftMargin(30)
  347. command.setText("囤位号:" + that.gridList.storageTagNo);
  348. command.setPrint();
  349. //质检员
  350. command.setSelectJustification(0); //居左
  351. command.setLeftMargin(30)
  352. command.setText("质检员:" + that.gridList.qualityInspector);
  353. command.setPrint();
  354. //客户签名
  355. // command.rowSpace(100);
  356. // command.setSelectJustification(0); //居左
  357. // command.setLeftMargin(30)
  358. // command.setText("客户签名:");
  359. // command.setPrint();
  360. // command.rowSpace(100);
  361. //收货单位签名或盖章
  362. command.setSelectJustification(0); //居左
  363. command.setLeftMargin(30)
  364. command.setText("收货单位签名或盖章:");
  365. command.setPrintAndFeed(150);
  366. command.setPrintAndFeedRow(10);
  367. that.isReceiptSend = true;
  368. that.prepareSend(command.getData());
  369. },
  370. //准备发送,根据每次发送字节数来处理分包数量
  371. prepareSend(buff) {
  372. console.log(buff);
  373. let that = this
  374. let time = that.oneTimeData
  375. let looptime = parseInt(buff.length / time);
  376. let lastData = parseInt(buff.length % time);
  377. console.log(looptime + "---" + lastData)
  378. this.looptime = looptime + 1;
  379. this.lastData = lastData;
  380. this.currentTime = 1;
  381. that.Send(buff)
  382. },
  383. //查询打印机状态
  384. queryStatus() {
  385. let command = esc.jpPrinter.Query();
  386. command.getRealtimeStatusTransmission(1);
  387. },
  388. //分包发送
  389. Send(buff) {
  390. let that = this
  391. let {
  392. currentTime,
  393. looptime: loopTime,
  394. lastData,
  395. oneTimeData: onTimeData,
  396. printerNum: printNum,
  397. currentPrint
  398. } = that;
  399. let buf;
  400. let dataView;
  401. if (currentTime < loopTime) {
  402. buf = new ArrayBuffer(onTimeData)
  403. dataView = new DataView(buf)
  404. for (var i = 0; i < onTimeData; ++i) {
  405. dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
  406. }
  407. } else {
  408. buf = new ArrayBuffer(lastData)
  409. dataView = new DataView(buf)
  410. for (var i = 0; i < lastData; ++i) {
  411. dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
  412. }
  413. }
  414. console.log("第" + currentTime + "次发送数据大小为:" + buf.byteLength)
  415. let {
  416. BLEInformation
  417. } = that.Bluetooth;
  418. plus.bluetooth.writeBLECharacteristicValue({
  419. deviceId: BLEInformation.deviceId,
  420. serviceId: BLEInformation.writeServiceId,
  421. characteristicId: BLEInformation.writeCharaterId,
  422. value: buf,
  423. success: function(res) {
  424. console.log(res)
  425. },
  426. fail: function(e) {
  427. console.log(e)
  428. },
  429. complete: function() {
  430. currentTime++
  431. if (currentTime <= loopTime) {
  432. that.currentTime = currentTime;
  433. that.Send(buff)
  434. } else {
  435. uni.showToast({
  436. title: '已打印第' + currentPrint + '张',
  437. })
  438. if (currentPrint == printNum) {
  439. that.looptime = 0;
  440. that.lastData = 0;
  441. that.currentTime = 1;
  442. that.isReceiptSend = false;
  443. that.isLabelSend = false;
  444. that.currentPrint = 1;
  445. } else {
  446. currentPrint++;
  447. that.currentPrint = currentPrint;
  448. that.currentTime = 1;
  449. that.Send(buff)
  450. }
  451. }
  452. }
  453. })
  454. },
  455. buffBindChange: function(res) { //更改打印字节数
  456. let index = res.detail.value
  457. let time = this.buffSize[index]
  458. this.buffIndex = index;
  459. this.oneTimeData = time;
  460. },
  461. printNumBindChange: function(res) { //更改打印份数
  462. let index = res.detail.value
  463. let num = this.printNum[index]
  464. this.printNumIndex = index;
  465. this.printerNum = num;
  466. },
  467. }
  468. }
  469. </script>
  470. <style lang="less">
  471. .input {
  472. text-align: top;
  473. width: 90%;
  474. height: 150px;
  475. margin-left: 4%;
  476. margin-right: 4%;
  477. margin-top: 10px;
  478. margin-bottom: 12px;
  479. border: 1px solid slategray;
  480. }
  481. .receiver_info_scroll_view {
  482. width: 90%;
  483. height: 200px;
  484. margin-left: 4%;
  485. margin-right: 4%;
  486. margin-top: 10px;
  487. margin-bottom: 25px;
  488. border: 1px solid black;
  489. }
  490. .result {
  491. width: 90%;
  492. height: 150px;
  493. border: 1px solid black;
  494. margin-left: 4%;
  495. margin-bottom: 4%;
  496. margin-top: 5%;
  497. }
  498. button {
  499. width: 90%;
  500. margin-left: 5%;
  501. margin-right: 5%;
  502. }
  503. .switch {
  504. float: right;
  505. margin-right: 20px;
  506. margin-bottom: 16px;
  507. }
  508. text {
  509. color: #fff;
  510. display: block;
  511. }
  512. input {
  513. color: gainsboro;
  514. float: left;
  515. }
  516. .v_net_ssid {
  517. width: 100%;
  518. background: #fff;
  519. }
  520. .v_net_passw {
  521. width: 100%;
  522. background: antiquewhite;
  523. }
  524. .swiper {
  525. width: 100%;
  526. height: 100%;
  527. }
  528. .btn {
  529. margin-top: 50rpx;
  530. background: #22C572;
  531. }
  532. uni-button[loading][type=primary] {
  533. color: hsla(0, 0%, 100%, .6);
  534. background-color: #22C572;
  535. }
  536. </style>