exsendCommand.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 type='primary' class="btn" @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("exWarehousing_print")
  100. console.log(this.gridList)
  101. let that = this;
  102. let width;
  103. let height;
  104. uni.getImageInfo({
  105. src: that.imageSrc,
  106. success(res) {
  107. console.log(res.width)
  108. console.log(res.height)
  109. width = res.width
  110. height = res.height
  111. that.canvasWidth = res.width;
  112. that.canvasHeight = res.height;
  113. }
  114. })
  115. const ctx = uni.createCanvasContext("edit_area_canvas", this);
  116. // if (app.globalData.platform == "android") {
  117. // ctx.translate(width, height)
  118. // ctx.rotate(180 * Math.PI / 180)
  119. // }
  120. ctx.drawImage(this.imageSrc, 0, 0, width, height);
  121. ctx.draw();
  122. },
  123. onUnload() {
  124. let that = this;
  125. let {
  126. BLEInformation
  127. } = that.Bluetooth;
  128. // uni.closeBLEConnection({
  129. // deviceId: BLEInformation.deviceId,
  130. // success: function(res) {
  131. // console.log("关闭蓝牙成功")
  132. // },
  133. // })
  134. },
  135. methods: {
  136. //获取输入内容
  137. inputEvent(e) {
  138. this.sendContent = e.detail.value;
  139. },
  140. //输入框点击发送
  141. sendData() {
  142. let data = this.sendContent + "\n"
  143. this.looptime = 0;
  144. var content = new encode.TextEncoder(
  145. 'gb18030', {
  146. NONSTANDARD_allowLegacyEncoding: true
  147. }).encode(data);
  148. this.prepareSend(content);
  149. },
  150. //打印票据数据
  151. receiptTest() {
  152. var that = this;
  153. var canvasWidth = that.canvasWidth
  154. var canvasHeight = that.canvasHeight
  155. var command = esc.jpPrinter.createNew()
  156. command.init()
  157. // 标题
  158. command.bold(1); //加粗
  159. command.setFontSize(16); //字体大小
  160. command.setSelectJustification(1) //居中
  161. // command.rowSpace(10);
  162. command.setText("出库检斤单");
  163. command.setPrint();
  164. command.rowSpace(60);
  165. command.bold(0); //取消加粗
  166. command.setFontSize(0); //正常字体
  167. command.setSelectJustification(0); //居左
  168. command.setLeftMargin(30)
  169. command.setText("时间:" + that.gridList.updateDate);
  170. command.setPrint();
  171. command.setSelectJustification(0); //居左
  172. command.setLeftMargin(30)
  173. command.setText("送货单位:" + that.gridList.seller);
  174. command.setPrint();
  175. command.setSelectJustification(0); //居左
  176. command.setLeftMargin(30)
  177. command.setText("收货单位:" + that.gridList.buyer);
  178. command.setPrint();
  179. command.setSelectJustification(0); //居左
  180. command.setLeftMargin(30)
  181. command.setText("货名:" + that.gridList.goodsName);
  182. command.setPrint();
  183. command.setSelectJustification(0); //居左
  184. command.setLeftMargin(30)
  185. command.setText("质检员:" + that.gridList.backOffice);
  186. command.setPrint();
  187. command.setSelectJustification(0); //居左
  188. command.setLeftMargin(30)
  189. command.setText("毛重(吨)" + that.gridList.grossWeight);
  190. command.setPrint();
  191. command.setSelectJustification(0); //居左
  192. command.setLeftMargin(30)
  193. command.setText("皮重(吨)" + that.gridList.tare);
  194. command.setPrint();
  195. command.setSelectJustification(0); //居左
  196. command.setLeftMargin(30)
  197. command.setText("净重(吨)" + that.gridList.netWeight);
  198. command.setPrint();
  199. command.setSelectJustification(0); //居左
  200. command.setLeftMargin(30)
  201. command.setText("运输方式:" + that.gridList.outType);
  202. command.setPrint();
  203. if(that.gridList.outType=="集装箱船"||taht.gridList.outType=="汽运"){
  204. command.setSelectJustification(0); //居左
  205. command.setLeftMargin(30)
  206. command.setText("车牌号:" + that.gridList.carNo);
  207. command.setPrint();
  208. }else if(that.gridList.outType=="火运"){
  209. command.setSelectJustification(0); //居左
  210. command.setLeftMargin(30)
  211. command.setText("车号:" + that.gridList.carNo);
  212. command.setPrint();
  213. }else if(that.gridList.outType=="散船"){
  214. command.setSelectJustification(0); //居左
  215. command.setLeftMargin(30)
  216. command.setText("船名:" + that.gridList.shipName);
  217. command.setPrint();
  218. }
  219. if (that.gridList.outType=="集装箱船"||that.gridList.outType=="汽运") {
  220. //箱号
  221. command.setSelectJustification(0); //居左
  222. command.setLeftMargin(30)
  223. command.setText("箱号-1:" + that.gridList.boxNo);
  224. command.setPrint();
  225. //箱号
  226. command.setSelectJustification(0); //居左
  227. command.setLeftMargin(30)
  228. command.setText("箱号-2:" + that.gridList.boxNoOther);
  229. command.setPrint();
  230. }else if(that.gridList.outType=="火运"){
  231. command.setSelectJustification(0); //居左
  232. command.setLeftMargin(30)
  233. command.setText("车厢号-1:" + that.gridList.wingNumber);
  234. command.setPrint();
  235. command.setSelectJustification(0); //居左
  236. command.setLeftMargin(30)
  237. command.setText("车厢号-2:" + that.gridList.wingNumberOther);
  238. command.setPrint();
  239. }else if(that.gridList.outType=="散船"){
  240. command.setSelectJustification(0); //居左
  241. command.setLeftMargin(30)
  242. command.setText("船次:" + that.gridList.shipNumber);
  243. command.setPrint();
  244. }
  245. //客户签名
  246. command.rowSpace(100);
  247. command.setSelectJustification(0); //居左
  248. command.setLeftMargin(30)
  249. command.setText("客户签名:");
  250. command.setPrint();
  251. command.rowSpace(100);
  252. //收货单位签名或盖章
  253. command.setSelectJustification(0); //居左
  254. command.setLeftMargin(30)
  255. command.setText("收货单位签名或盖章:");
  256. command.setPrintAndFeed(150);
  257. command.setPrintAndFeedRow(8);
  258. that.isReceiptSend = true;
  259. that.prepareSend(command.getData());
  260. },
  261. //准备发送,根据每次发送字节数来处理分包数量
  262. prepareSend(buff) {
  263. console.log(buff);
  264. let that = this
  265. let time = that.oneTimeData
  266. let looptime = parseInt(buff.length / time);
  267. let lastData = parseInt(buff.length % time);
  268. console.log(looptime + "---" + lastData)
  269. this.looptime = looptime + 1;
  270. this.lastData = lastData;
  271. this.currentTime = 1;
  272. that.Send(buff)
  273. },
  274. //查询打印机状态
  275. queryStatus() {
  276. let command = esc.jpPrinter.Query();
  277. command.getRealtimeStatusTransmission(1);
  278. },
  279. //分包发送
  280. Send(buff) {
  281. let that = this
  282. let {
  283. currentTime,
  284. looptime: loopTime,
  285. lastData,
  286. oneTimeData: onTimeData,
  287. printerNum: printNum,
  288. currentPrint
  289. } = that;
  290. let buf;
  291. let dataView;
  292. if (currentTime < loopTime) {
  293. buf = new ArrayBuffer(onTimeData)
  294. dataView = new DataView(buf)
  295. for (var i = 0; i < onTimeData; ++i) {
  296. dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
  297. }
  298. } else {
  299. buf = new ArrayBuffer(lastData)
  300. dataView = new DataView(buf)
  301. for (var i = 0; i < lastData; ++i) {
  302. dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
  303. }
  304. }
  305. console.log("第" + currentTime + "次发送数据大小为:" + buf.byteLength)
  306. let {
  307. BLEInformation
  308. } = that.Bluetooth;
  309. plus.bluetooth.writeBLECharacteristicValue({
  310. deviceId: BLEInformation.deviceId,
  311. serviceId: BLEInformation.writeServiceId,
  312. characteristicId: BLEInformation.writeCharaterId,
  313. value: buf,
  314. success: function(res) {
  315. console.log(res)
  316. },
  317. fail: function(e) {
  318. console.log(e)
  319. },
  320. complete: function() {
  321. currentTime++
  322. if (currentTime <= loopTime) {
  323. that.currentTime = currentTime;
  324. that.Send(buff)
  325. } else {
  326. uni.showToast({
  327. title: '已打印第' + currentPrint + '张',
  328. })
  329. if (currentPrint == printNum) {
  330. that.looptime = 0;
  331. that.lastData = 0;
  332. that.currentTime = 1;
  333. that.isReceiptSend = false;
  334. that.isLabelSend = false;
  335. that.currentPrint = 1;
  336. } else {
  337. currentPrint++;
  338. that.currentPrint = currentPrint;
  339. that.currentTime = 1;
  340. that.Send(buff)
  341. }
  342. }
  343. }
  344. })
  345. },
  346. buffBindChange: function(res) { //更改打印字节数
  347. let index = res.detail.value
  348. let time = this.buffSize[index]
  349. this.buffIndex = index;
  350. this.oneTimeData = time;
  351. },
  352. printNumBindChange: function(res) { //更改打印份数
  353. let index = res.detail.value
  354. let num = this.printNum[index]
  355. this.printNumIndex = index;
  356. this.printerNum = num;
  357. },
  358. }
  359. }
  360. </script>
  361. <style lang="less">
  362. .input {
  363. text-align: top;
  364. width: 90%;
  365. height: 150px;
  366. margin-left: 4%;
  367. margin-right: 4%;
  368. margin-top: 10px;
  369. margin-bottom: 12px;
  370. border: 1px solid slategray;
  371. }
  372. .receiver_info_scroll_view {
  373. width: 90%;
  374. height: 200px;
  375. margin-left: 4%;
  376. margin-right: 4%;
  377. margin-top: 10px;
  378. margin-bottom: 25px;
  379. border: 1px solid black;
  380. }
  381. .result {
  382. width: 90%;
  383. height: 150px;
  384. border: 1px solid black;
  385. margin-left: 4%;
  386. margin-bottom: 4%;
  387. margin-top: 5%;
  388. }
  389. button {
  390. width: 90%;
  391. margin-left: 5%;
  392. margin-right: 5%;
  393. }
  394. .switch {
  395. float: right;
  396. margin-right: 20px;
  397. margin-bottom: 16px;
  398. }
  399. text {
  400. color: #fff;
  401. display: block;
  402. }
  403. input {
  404. color: gainsboro;
  405. float: left;
  406. }
  407. .v_net_ssid {
  408. width: 100%;
  409. background: #fff;
  410. }
  411. .v_net_passw {
  412. width: 100%;
  413. background: antiquewhite;
  414. }
  415. .swiper {
  416. width: 100%;
  417. height: 100%;
  418. }
  419. .btn{
  420. margin-top: 50rpx;
  421. background: #22C572;
  422. }
  423. uni-button[loading][type=primary] {
  424. color: hsla(0,0%,100%,.6);
  425. background-color: #22C572;
  426. }
  427. </style>