sendCommand.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. let that = this;
  62. let {
  63. BLEInformation
  64. } = that.Bluetooth;
  65. uni.notifyBLECharacteristicValueChange({
  66. deviceId: BLEInformation.deviceId,
  67. serviceId: BLEInformation.notifyServiceId,
  68. characteristicId: BLEInformation.notifyCharaterId,
  69. state: true,
  70. success(res) {
  71. uni.onBLECharacteristicValueChange(function(r) {
  72. console.log(`characteristic ${r.characteristicId} has changed, now is ${r}`)
  73. })
  74. },
  75. fail: function(e) {
  76. console.log(e)
  77. },
  78. complete: function(e) {
  79. console.log(e)
  80. }
  81. })
  82. },
  83. onReady() {
  84. let list = []
  85. let numList = []
  86. let j = 0
  87. for (let i = 20; i < 200; i += 10) {
  88. list[j] = i;
  89. j++
  90. }
  91. for (let i = 1; i < 10; i++) {
  92. numList[i - 1] = i
  93. }
  94. this.buffSize = list;
  95. this.oneTimeData = list[0];
  96. this.printNum = numList;
  97. this.printerNum = numList[0];
  98. },
  99. onShow() {
  100. this.gridList = uni.getStorageSync("quality_print")
  101. if(!this.gridList.compId){
  102. this.gridList.compId = uni.getStorageSync('pcUserInfo').compId
  103. }
  104. console.log(this.gridList)
  105. let that = this;
  106. let width;
  107. let height;
  108. uni.getImageInfo({
  109. src: that.imageSrc,
  110. success(res) {
  111. console.log(res.width)
  112. console.log(res.height)
  113. width = res.width
  114. height = res.height
  115. that.canvasWidth = res.width;
  116. that.canvasHeight = res.height;
  117. }
  118. })
  119. const ctx = uni.createCanvasContext("edit_area_canvas", this);
  120. // if (app.globalData.platform == "android") {
  121. // ctx.translate(width, height)
  122. // ctx.rotate(180 * Math.PI / 180)
  123. // }
  124. ctx.drawImage(this.imageSrc, 0, 0, width, height);
  125. ctx.draw();
  126. },
  127. onUnload() {
  128. let that = this;
  129. let {
  130. BLEInformation
  131. } = that.Bluetooth;
  132. // uni.closeBLEConnection({
  133. // deviceId: BLEInformation.deviceId,
  134. // success: function(res) {
  135. // console.log("关闭蓝牙成功")
  136. // },
  137. // })
  138. },
  139. methods: {
  140. goToList(){
  141. uni.navigateTo({
  142. url: '/pages/erpbusiness/quality_testing?managementType=1'
  143. })
  144. },
  145. //获取输入内容
  146. inputEvent(e) {
  147. this.sendContent = e.detail.value;
  148. },
  149. //输入框点击发送
  150. sendData() {
  151. let data = this.sendContent + "\n"
  152. this.looptime = 0;
  153. var content = new encode.TextEncoder(
  154. 'gb18030', {
  155. NONSTANDARD_allowLegacyEncoding: true
  156. }).encode(data);
  157. this.prepareSend(content);
  158. },
  159. //打印票据数据
  160. receiptTest() {
  161. var that = this;
  162. var canvasWidth = that.canvasWidth
  163. var canvasHeight = that.canvasHeight
  164. var command = esc.jpPrinter.createNew()
  165. command.init()
  166. //编号
  167. command.bold(3); //加粗
  168. command.setFontSize(36); //字体大小
  169. command.setSelectJustification(1) //居中
  170. command.rowSpace(200);
  171. // command.rowSpace(10);
  172. command.setText(that.gridList.qualityNo.substring(that.gridList.qualityNo.length - 3));
  173. command.setPrint();
  174. command.rowSpace(60);
  175. command.bold(0); //取消加粗
  176. command.setFontSize(0); //正常字体
  177. // 标题
  178. command.bold(1); //加粗
  179. command.setFontSize(16); //字体大小
  180. command.setSelectJustification(1) //居中
  181. // command.rowSpace(10);
  182. command.setText("粮食质检单");
  183. command.setPrint();
  184. command.rowSpace(60);
  185. command.bold(0); //取消加粗
  186. command.setFontSize(0); //正常字体
  187. // 二维码
  188. command.rowSpace(60);
  189. command.setSelectSizeOfModuleForQRCode(5);
  190. command.setSelectErrorCorrectionLevelForQRCode(7)
  191. command.setStoreQRCodeData(that.gridList.compId + '&' + that.gridList.qualityNo + '&' + that.gridList.warehouseId);
  192. command.setPrintQRCode();
  193. //客户姓名
  194. command.setSelectJustification(0); //居左
  195. command.setLeftMargin(30)
  196. command.setText("客户姓名:" + that.gridList.customerName);
  197. command.setPrint();
  198. //客户电话
  199. command.setSelectJustification(0); //居左
  200. command.setLeftMargin(30)
  201. command.setText("客户电话:" + that.gridList.customerPhone);
  202. command.setPrint();
  203. //车牌号
  204. command.setSelectJustification(0); //居左
  205. command.setLeftMargin(30)
  206. command.setText("车牌号:" + that.gridList.carNumber);
  207. command.setPrint();
  208. if (that.gridList.boxNo) {
  209. //箱号
  210. command.setSelectJustification(0); //居左
  211. command.setLeftMargin(30)
  212. command.setText("箱号-1:" + that.gridList.boxNo);
  213. command.setPrint();
  214. }
  215. if (that.gridList.boxNoOther) {
  216. //箱号
  217. command.setSelectJustification(0); //居左
  218. command.setLeftMargin(30)
  219. command.setText("箱号-2:" + that.gridList.boxNoOther);
  220. command.setPrint();
  221. }
  222. if (that.gridList.titleNo) {
  223. //封号
  224. command.setSelectJustification(0); //居左
  225. command.setLeftMargin(30)
  226. command.setText("封号-1:" + that.gridList.titleNo);
  227. command.setPrint();
  228. }
  229. if (that.gridList.titleNoOther) {
  230. //封号
  231. command.setSelectJustification(0); //居左
  232. command.setLeftMargin(30)
  233. command.setText("封号-2:" + that.gridList.titleNoOther);
  234. command.setPrint();
  235. }
  236. //收货单位
  237. command.setSelectJustification(0); //居左
  238. command.setLeftMargin(30)
  239. command.setText("收货单位:" + that.gridList.compName);
  240. command.setPrint();
  241. //联系人
  242. command.setSelectJustification(0); //居左
  243. command.setLeftMargin(30)
  244. command.setText("联系人:" + that.gridList.person);
  245. command.setPrint();
  246. //联系电话
  247. command.setSelectJustification(0); //居左
  248. command.setLeftMargin(30)
  249. command.setText("联系电话:" + that.gridList.personPhone);
  250. command.setPrint();
  251. //货名
  252. command.setSelectJustification(0); //居左
  253. command.setLeftMargin(30)
  254. command.setText("货名:" + that.gridList.goodsName);
  255. command.setPrint();
  256. //类型
  257. command.setSelectJustification(0); //居左
  258. command.setLeftMargin(30)
  259. command.setText("类型:" + that.gridList.type);
  260. command.setPrint();
  261. //容量
  262. command.setSelectJustification(0); //居左
  263. command.setLeftMargin(30)
  264. command.setText("容量(克/升):" + that.gridList.bulkDensity);
  265. command.setPrint();
  266. //不完善粒
  267. command.setSelectJustification(0); //居左
  268. command.setLeftMargin(30)
  269. command.setText("不完善粒(%):" + that.gridList.imperfectGrain);
  270. command.setPrint();
  271. //等级
  272. command.setSelectJustification(0); //居左
  273. command.setLeftMargin(30)
  274. command.setText("等级:" + that.gridList.grade);
  275. command.setPrint();
  276. //水分
  277. command.setSelectJustification(0); //居左
  278. command.setLeftMargin(30)
  279. command.setText("水分(%):" + that.gridList.waterContent);
  280. command.setPrint();
  281. //热损伤
  282. command.setSelectJustification(0); //居左
  283. command.setLeftMargin(30)
  284. command.setText("热损伤(%):" + that.gridList.jiaorenli);
  285. command.setPrint();
  286. //单价
  287. command.setSelectJustification(0); //居左
  288. command.setLeftMargin(30)
  289. if (that.gridList.type == '潮粮') {
  290. command.setText("单价(元/公斤):" + that.gridList.tidalGrainPrice);
  291. } else {
  292. command.setText("单价(元/公斤):" + that.gridList.dryGrainPrice);
  293. }
  294. command.setPrint();
  295. //杂质
  296. command.setSelectJustification(0); //居左
  297. command.setLeftMargin(30)
  298. command.setText("杂质(%):" + that.gridList.impurity);
  299. command.setPrint();
  300. //霉变粒
  301. command.setSelectJustification(0); //居左
  302. command.setLeftMargin(30)
  303. command.setText("霉变粒(%):" + that.gridList.mildewGrain);
  304. command.setPrint();
  305. //购粮性质
  306. command.setSelectJustification(0); //居左
  307. command.setLeftMargin(30)
  308. command.setText("购粮性质:" + that.gridList.natureOfGrainPurchase);
  309. command.setPrint();
  310. //仓位号
  311. command.setSelectJustification(0); //居左
  312. command.setLeftMargin(30)
  313. command.setText("仓位号:" + that.gridList.binNumber);
  314. command.setPrint();
  315. //囤位号
  316. command.setSelectJustification(0); //居左
  317. command.setLeftMargin(30)
  318. command.setText("囤位号:" + that.gridList.storageTagNo);
  319. command.setPrint();
  320. //质检员
  321. command.setSelectJustification(0); //居左
  322. command.setLeftMargin(30)
  323. command.setText("质检员:" + that.gridList.qualityInspector);
  324. command.setPrint();
  325. //客户签名
  326. command.rowSpace(100);
  327. command.setSelectJustification(0); //居左
  328. command.setLeftMargin(30)
  329. command.setText("客户签名:");
  330. command.setPrint();
  331. command.rowSpace(100);
  332. //收货单位签名或盖章
  333. command.setSelectJustification(0); //居左
  334. command.setLeftMargin(30)
  335. command.setText("收货单位签名或盖章:");
  336. command.setPrintAndFeed(150);
  337. command.setPrintAndFeedRow(8);
  338. that.isReceiptSend = true;
  339. that.prepareSend(command.getData());
  340. },
  341. //准备发送,根据每次发送字节数来处理分包数量
  342. prepareSend(buff) {
  343. console.log(buff);
  344. let that = this
  345. let time = that.oneTimeData
  346. let looptime = parseInt(buff.length / time);
  347. let lastData = parseInt(buff.length % time);
  348. console.log(looptime + "---" + lastData)
  349. this.looptime = looptime + 1;
  350. this.lastData = lastData;
  351. this.currentTime = 1;
  352. that.Send(buff)
  353. },
  354. //查询打印机状态
  355. queryStatus() {
  356. let command = esc.jpPrinter.Query();
  357. command.getRealtimeStatusTransmission(1);
  358. },
  359. //分包发送
  360. Send(buff) {
  361. let that = this
  362. let {
  363. currentTime,
  364. looptime: loopTime,
  365. lastData,
  366. oneTimeData: onTimeData,
  367. printerNum: printNum,
  368. currentPrint
  369. } = that;
  370. let buf;
  371. let dataView;
  372. if (currentTime < loopTime) {
  373. buf = new ArrayBuffer(onTimeData)
  374. dataView = new DataView(buf)
  375. for (var i = 0; i < onTimeData; ++i) {
  376. dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
  377. }
  378. } else {
  379. buf = new ArrayBuffer(lastData)
  380. dataView = new DataView(buf)
  381. for (var i = 0; i < lastData; ++i) {
  382. dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
  383. }
  384. }
  385. console.log("第" + currentTime + "次发送数据大小为:" + buf.byteLength)
  386. let {
  387. BLEInformation
  388. } = that.Bluetooth;
  389. plus.bluetooth.writeBLECharacteristicValue({
  390. deviceId: BLEInformation.deviceId,
  391. serviceId: BLEInformation.writeServiceId,
  392. characteristicId: BLEInformation.writeCharaterId,
  393. value: buf,
  394. success: function(res) {
  395. console.log(res)
  396. },
  397. fail: function(e) {
  398. console.log(e)
  399. },
  400. complete: function() {
  401. currentTime++
  402. if (currentTime <= loopTime) {
  403. that.currentTime = currentTime;
  404. that.Send(buff)
  405. } else {
  406. uni.showToast({
  407. title: '已打印第' + currentPrint + '张',
  408. })
  409. if (currentPrint == printNum) {
  410. that.looptime = 0;
  411. that.lastData = 0;
  412. that.currentTime = 1;
  413. that.isReceiptSend = false;
  414. that.isLabelSend = false;
  415. that.currentPrint = 1;
  416. } else {
  417. currentPrint++;
  418. that.currentPrint = currentPrint;
  419. that.currentTime = 1;
  420. that.Send(buff)
  421. }
  422. }
  423. }
  424. })
  425. },
  426. buffBindChange: function(res) { //更改打印字节数
  427. let index = res.detail.value
  428. let time = this.buffSize[index]
  429. this.buffIndex = index;
  430. this.oneTimeData = time;
  431. },
  432. printNumBindChange: function(res) { //更改打印份数
  433. let index = res.detail.value
  434. let num = this.printNum[index]
  435. this.printNumIndex = index;
  436. this.printerNum = num;
  437. },
  438. }
  439. }
  440. </script>
  441. <style lang="less">
  442. .input {
  443. text-align: top;
  444. width: 90%;
  445. height: 150px;
  446. margin-left: 4%;
  447. margin-right: 4%;
  448. margin-top: 10px;
  449. margin-bottom: 12px;
  450. border: 1px solid slategray;
  451. }
  452. .receiver_info_scroll_view {
  453. width: 90%;
  454. height: 200px;
  455. margin-left: 4%;
  456. margin-right: 4%;
  457. margin-top: 10px;
  458. margin-bottom: 25px;
  459. border: 1px solid black;
  460. }
  461. .result {
  462. width: 90%;
  463. height: 150px;
  464. border: 1px solid black;
  465. margin-left: 4%;
  466. margin-bottom: 4%;
  467. margin-top: 5%;
  468. }
  469. button {
  470. width: 90%;
  471. margin-left: 5%;
  472. margin-right: 5%;
  473. }
  474. .switch {
  475. float: right;
  476. margin-right: 20px;
  477. margin-bottom: 16px;
  478. }
  479. text {
  480. color: #fff;
  481. display: block;
  482. }
  483. input {
  484. color: gainsboro;
  485. float: left;
  486. }
  487. .v_net_ssid {
  488. width: 100%;
  489. background: #fff;
  490. }
  491. .v_net_passw {
  492. width: 100%;
  493. background: antiquewhite;
  494. }
  495. .swiper {
  496. width: 100%;
  497. height: 100%;
  498. }
  499. .btn {
  500. margin-top: 50rpx;
  501. background: #22C572;
  502. }
  503. uni-button[loading][type=primary] {
  504. color: hsla(0, 0%, 100%, .6);
  505. background-color: #22C572;
  506. }
  507. </style>