sendCommand.vue 15 KB

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