sendCommand.vue 15 KB

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