jjsendCommand.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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: row;'>
  10. <button class="btn" type='primary' @tap='receiptTest' :loading='isReceiptSend'
  11. :disabled='isReceiptSend'>打印检斤单小票</button>
  12. <!-- <button type='primary' @tap='labelTest' :loading='isLabelSend' :disabled='isLabelSend'>标签测试</button> -->
  13. </view>
  14. <view style='margin-top:4%;display: flex;flex-direction: row;'>
  15. <!-- hidden='true' -->
  16. <canvas canvas-id='edit_area_canvas'
  17. :style="{width:canvasWidth+'px',height:canvasHeight+'px'}"></canvas>
  18. </view>
  19. <!-- <picker style='margin:20px' mode='selector' :range='buffSize' :value='buffIndex' @change='buffBindChange'>
  20. 当前每次发送字节数为(点击可更换):{{buffSize[buffIndex]}}
  21. </picker>
  22. <picker style='margin:20px' mode='selector' :range='printNum' :value='printNumIndex' @change='printNumBindChange'>
  23. 当前打印份数(点击可更换):{{printNum[printNumIndex]}}
  24. </picker> -->
  25. </view>
  26. </view>
  27. </template>
  28. <script>
  29. var tsc = require("../../util/ble/tsc.js");
  30. var esc = require("../../util/ble/esc.js");
  31. var encode = require("../../util/ble/encoding.js");
  32. import {
  33. mapState
  34. } from 'vuex';
  35. export default {
  36. data() {
  37. return {
  38. sendContent: "",
  39. looptime: 0,
  40. currentTime: 1,
  41. lastData: 0,
  42. oneTimeData: 0,
  43. returnResult: "",
  44. canvasWidth: 180,
  45. canvasHeight: 180,
  46. imageSrc: '../../static/img/abc_ic_star_black_16dp.png',
  47. buffSize: [],
  48. buffIndex: 0,
  49. printNum: [],
  50. printNumIndex: 0,
  51. printerNum: 1,
  52. currentPrint: 1,
  53. isReceiptSend: false,
  54. isLabelSend: false,
  55. gridList: {}
  56. };
  57. },
  58. computed: mapState(['sysinfo', 'Bluetooth']),
  59. onLoad() {
  60. let that = this;
  61. let {
  62. BLEInformation
  63. } = that.Bluetooth;
  64. uni.notifyBLECharacteristicValueChange({
  65. deviceId: BLEInformation.deviceId,
  66. serviceId: BLEInformation.notifyServiceId,
  67. characteristicId: BLEInformation.notifyCharaterId,
  68. state: true,
  69. success(res) {
  70. uni.onBLECharacteristicValueChange(function(r) {
  71. console.log(`characteristic ${r.characteristicId} has changed, now is ${r}`)
  72. })
  73. },
  74. fail: function(e) {
  75. console.log(e)
  76. },
  77. complete: function(e) {
  78. console.log(e)
  79. }
  80. })
  81. },
  82. onReady() {
  83. let list = []
  84. let numList = []
  85. let j = 0
  86. for (let i = 20; i < 200; i += 10) {
  87. list[j] = i;
  88. j++
  89. }
  90. for (let i = 1; i < 10; i++) {
  91. numList[i - 1] = i
  92. }
  93. this.buffSize = list;
  94. this.oneTimeData = list[0];
  95. this.printNum = numList;
  96. this.printerNum = numList[0];
  97. },
  98. onShow() {
  99. this.gridList = uni.getStorageSync("acquisitionInspection_print")
  100. let that = this;
  101. let width;
  102. let height;
  103. uni.getImageInfo({
  104. src: that.imageSrc,
  105. success(res) {
  106. console.log(res.width)
  107. console.log(res.height)
  108. width = res.width
  109. height = res.height
  110. that.canvasWidth = res.width;
  111. that.canvasHeight = res.height;
  112. }
  113. })
  114. const ctx = uni.createCanvasContext("edit_area_canvas", this);
  115. // if (app.globalData.platform == "android") {
  116. // ctx.translate(width, height)
  117. // ctx.rotate(180 * Math.PI / 180)
  118. // }
  119. ctx.drawImage(this.imageSrc, 0, 0, width, height);
  120. ctx.draw();
  121. },
  122. onUnload() {
  123. let that = this;
  124. let {
  125. BLEInformation
  126. } = that.Bluetooth;
  127. // uni.closeBLEConnection({
  128. // deviceId: BLEInformation.deviceId,
  129. // success: function(res) {
  130. // console.log("关闭蓝牙成功")
  131. // },
  132. // })
  133. },
  134. methods: {
  135. //获取输入内容
  136. inputEvent(e) {
  137. this.sendContent = e.detail.value;
  138. },
  139. //输入框点击发送
  140. sendData() {
  141. let data = this.sendContent + "\n"
  142. this.looptime = 0;
  143. var content = new encode.TextEncoder(
  144. 'gb18030', {
  145. NONSTANDARD_allowLegacyEncoding: true
  146. }).encode(data);
  147. this.prepareSend(content);
  148. },
  149. //打印票据数据
  150. receiptTest() {
  151. var that = this;
  152. var canvasWidth = that.canvasWidth
  153. var canvasHeight = that.canvasHeight
  154. var command = esc.jpPrinter.createNew()
  155. command.init()
  156. //编号
  157. command.bold(3); //加粗
  158. command.setFontSize(36); //字体大小
  159. command.setSelectJustification(1) //居中
  160. command.rowSpace(10);
  161. // command.rowSpace(10);
  162. command.setText(that.gridList.number.substring(that.gridList.number.length - 3));
  163. command.setPrint();
  164. command.rowSpace(60);
  165. command.bold(0); //取消加粗
  166. command.setFontSize(0); //正常字体
  167. // 标题
  168. command.bold(1); //加粗
  169. command.setFontSize(16); //字体大小
  170. command.setSelectJustification(1) //居中
  171. // command.rowSpace(10);
  172. command.setText("粮食检斤单");
  173. command.setPrint();
  174. command.rowSpace(60);
  175. command.bold(0); //取消加粗
  176. command.setFontSize(0); //正常字体
  177. // 二维码
  178. command.rowSpace(60);
  179. command.setSelectSizeOfModuleForQRCode(5);
  180. command.setSelectErrorCorrectionLevelForQRCode(7)
  181. command.setStoreQRCodeData(that.gridList.compId + '&' + that.gridList.number + '&' + that.gridList
  182. .warehouseId);
  183. command.setPrintQRCode();
  184. //编号
  185. command.setSelectJustification(0); //居左
  186. command.setLeftMargin(30)
  187. command.setText("编号:" + that.gridList.number);
  188. command.setPrint();
  189. //客户姓名
  190. command.setSelectJustification(0); //居左
  191. command.setLeftMargin(30)
  192. command.setText("客户姓名:" + that.gridList.customer);
  193. command.setPrint();
  194. //客户电话
  195. command.setSelectJustification(0); //居左
  196. command.setLeftMargin(30)
  197. command.setText("客户电话:" + that.gridList.customerPhone);
  198. command.setPrint();
  199. //车牌号
  200. command.setSelectJustification(0); //居左
  201. command.setLeftMargin(30)
  202. command.setText("车牌号:" + that.gridList.carNumber);
  203. command.setPrint();
  204. //收货单位
  205. command.setSelectJustification(0); //居左
  206. command.setLeftMargin(30)
  207. command.setText("收货单位:" + that.gridList.compName);
  208. command.setPrint();
  209. //联系人
  210. command.setSelectJustification(0); //居左
  211. command.setLeftMargin(30)
  212. command.setText("联系人:" + that.gridList.person);
  213. command.setPrint();
  214. //联系电话
  215. command.setSelectJustification(0); //居左
  216. command.setLeftMargin(30)
  217. command.setText("联系电话:" + that.gridList.personPhone);
  218. command.setPrint();
  219. //货名
  220. command.setSelectJustification(0); //居左
  221. command.setLeftMargin(30)
  222. command.setText("货名:" + that.gridList.goodsName);
  223. command.setPrint();
  224. //类型
  225. command.setSelectJustification(0); //居左
  226. command.setLeftMargin(30)
  227. command.setText("类型:" + that.gridList.type);
  228. command.setPrint();
  229. //皮重
  230. command.setSelectJustification(0); //居左
  231. command.setLeftMargin(30)
  232. if(!that.gridList.tare) that.gridList.tare = ''
  233. command.setText("皮重(公斤):" + that.gridList.tare);
  234. command.setPrint();
  235. //皮检时间
  236. command.setSelectJustification(0); //居左
  237. command.setLeftMargin(30)
  238. if(!that.gridList.tareDate) that.gridList.tareDate = ''
  239. command.setText("皮检时间:" + that.gridList.tareDate);
  240. command.setPrint();
  241. //毛重
  242. command.setSelectJustification(0); //居左
  243. command.setLeftMargin(30)
  244. command.setText("毛重(公斤):" + that.gridList.grossWeight);
  245. command.setPrint();
  246. //毛检时间
  247. command.setSelectJustification(0); //居左
  248. command.setLeftMargin(30)
  249. command.setText("毛检时间:" + that.gridList.grossDate);
  250. command.setPrint();
  251. //净重
  252. command.setSelectJustification(0); //居左
  253. command.setLeftMargin(30)
  254. if(!that.gridList.netWeight) that.gridList.netWeight = ''
  255. command.setText("净重(公斤):" + that.gridList.netWeight);
  256. command.setPrint();
  257. //收货单位签名或盖章
  258. command.setSelectJustification(0); //居左
  259. command.setLeftMargin(30)
  260. command.setText("收货单位签名或盖章:");
  261. command.setPrintAndFeed(120);
  262. command.setPrintAndFeedRow(10);
  263. that.isReceiptSend = true;
  264. that.prepareSend(command.getData());
  265. },
  266. //准备发送,根据每次发送字节数来处理分包数量
  267. prepareSend(buff) {
  268. console.log(buff);
  269. let that = this
  270. let time = that.oneTimeData
  271. let looptime = parseInt(buff.length / time);
  272. let lastData = parseInt(buff.length % time);
  273. console.log(looptime + "---" + lastData)
  274. this.looptime = looptime + 1;
  275. this.lastData = lastData;
  276. this.currentTime = 1;
  277. that.Send(buff)
  278. },
  279. //查询打印机状态
  280. queryStatus() {
  281. let command = esc.jpPrinter.Query();
  282. command.getRealtimeStatusTransmission(1);
  283. },
  284. //分包发送
  285. Send(buff) {
  286. let that = this
  287. let {
  288. currentTime,
  289. looptime: loopTime,
  290. lastData,
  291. oneTimeData: onTimeData,
  292. printerNum: printNum,
  293. currentPrint
  294. } = that;
  295. let buf;
  296. let dataView;
  297. if (currentTime < loopTime) {
  298. buf = new ArrayBuffer(onTimeData)
  299. dataView = new DataView(buf)
  300. for (var i = 0; i < onTimeData; ++i) {
  301. dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
  302. }
  303. } else {
  304. buf = new ArrayBuffer(lastData)
  305. dataView = new DataView(buf)
  306. for (var i = 0; i < lastData; ++i) {
  307. dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
  308. }
  309. }
  310. console.log("第" + currentTime + "次发送数据大小为:" + buf.byteLength)
  311. let {
  312. BLEInformation
  313. } = that.Bluetooth;
  314. plus.bluetooth.writeBLECharacteristicValue({
  315. deviceId: BLEInformation.deviceId,
  316. serviceId: BLEInformation.writeServiceId,
  317. characteristicId: BLEInformation.writeCharaterId,
  318. value: buf,
  319. success: function(res) {
  320. console.log(res)
  321. },
  322. fail: function(e) {
  323. console.log(e)
  324. },
  325. complete: function() {
  326. currentTime++
  327. if (currentTime <= loopTime) {
  328. that.currentTime = currentTime;
  329. that.Send(buff)
  330. } else {
  331. uni.showToast({
  332. title: '已打印第' + currentPrint + '张',
  333. })
  334. if (currentPrint == printNum) {
  335. that.looptime = 0;
  336. that.lastData = 0;
  337. that.currentTime = 1;
  338. that.isReceiptSend = false;
  339. that.isLabelSend = false;
  340. that.currentPrint = 1;
  341. } else {
  342. currentPrint++;
  343. that.currentPrint = currentPrint;
  344. that.currentTime = 1;
  345. that.Send(buff)
  346. }
  347. }
  348. }
  349. })
  350. },
  351. buffBindChange: function(res) { //更改打印字节数
  352. let index = res.detail.value
  353. let time = this.buffSize[index]
  354. this.buffIndex = index;
  355. this.oneTimeData = time;
  356. },
  357. printNumBindChange: function(res) { //更改打印份数
  358. let index = res.detail.value
  359. let num = this.printNum[index]
  360. this.printNumIndex = index;
  361. this.printerNum = num;
  362. },
  363. }
  364. }
  365. </script>
  366. <style lang="less">
  367. .input {
  368. text-align: top;
  369. width: 90%;
  370. height: 150px;
  371. margin-left: 4%;
  372. margin-right: 4%;
  373. margin-top: 10px;
  374. margin-bottom: 12px;
  375. border: 1px solid slategray;
  376. }
  377. .receiver_info_scroll_view {
  378. width: 90%;
  379. height: 200px;
  380. margin-left: 4%;
  381. margin-right: 4%;
  382. margin-top: 10px;
  383. margin-bottom: 25px;
  384. border: 1px solid black;
  385. }
  386. .result {
  387. width: 90%;
  388. height: 150px;
  389. border: 1px solid black;
  390. margin-left: 4%;
  391. margin-bottom: 4%;
  392. margin-top: 5%;
  393. }
  394. button {
  395. width: 90%;
  396. margin-left: 5%;
  397. margin-right: 5%;
  398. }
  399. .switch {
  400. float: right;
  401. margin-right: 20px;
  402. margin-bottom: 16px;
  403. }
  404. text {
  405. color: #fff;
  406. display: block;
  407. }
  408. input {
  409. color: gainsboro;
  410. float: left;
  411. }
  412. .v_net_ssid {
  413. width: 100%;
  414. background: #fff;
  415. }
  416. .v_net_passw {
  417. width: 100%;
  418. background: antiquewhite;
  419. }
  420. .swiper {
  421. width: 100%;
  422. height: 100%;
  423. }
  424. .btn {
  425. margin-top: 50rpx;
  426. background: #22C572;
  427. }
  428. </style>