printTag.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <template>
  2. <view class="printTag">
  3. <view class="print-nums flex flex_column align_center justify_center">
  4. <view>打印数量</view>
  5. <view class="u-ninput">
  6. <u-number-box :min="1" v-model="printNum" color="#000" bg-color="#fff" size="30" :input-height="70" :input-width="100"></u-number-box>
  7. </view>
  8. </view>
  9. <view class="shelf-info">
  10. <view class="shelf-info-title">
  11. <view>{{infoData&&infoData.shelfName || '--'}}</view>
  12. </view>
  13. <view>产品编码:{{fromPage == 'smdy'?infoData.productCode : infoData.productEntity&&infoData.productEntity.code}}</view>
  14. <view>产品名称:{{fromPage == 'smdy'?infoData.productName : infoData.productEntity&&infoData.productEntity.productName}}</view>
  15. <view>{{fromPage == 'smdy'?'实发':'库存'}}数量:{{fromPage == 'smdy'?infoData.confirmQty : infoData.qty}}</view>
  16. </view>
  17. <view class="buttons flex align_center justify_center">
  18. <view v-if="fromPage == 'bdtq'"><u-button @click="cansel" shape="circle" plain>返回列表</u-button></view>
  19. <view v-if="fromPage == 'smdy'"><u-button @click="cansel" shape="circle" plain>重新扫描</u-button></view>
  20. <view>
  21. <kk-printer ref="kkprinter" @startPrint="startPrint"></kk-printer>
  22. </view>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. import { shelfDetail } from '@/api/shelf'
  28. import kkPrinter from '@/components/kk-printer/index.vue';
  29. export default {
  30. components:{
  31. kkPrinter
  32. },
  33. data() {
  34. return {
  35. infoData: null,
  36. fromPage: '',
  37. printInfo: null,
  38. printNum: 0
  39. }
  40. },
  41. onLoad(options) {
  42. const _this = this
  43. _this.fromPage = options.page
  44. _this.infoData = options.data ? JSON.parse(options.data) : {}
  45. if(_this.fromPage == 'bdtq'){
  46. shelfDetail({ sn: _this.infoData.shelfSn }).then(res => {
  47. if(res.status == 200 && res.data){
  48. _this.infoData.shelfName = res.data.shelfName
  49. _this.printInfo = {
  50. dealerName: '剪冠汽配西安长ab路店123汽配西安长青二路',
  51. shelfName: _this.infoData.shelfName || '',
  52. productCode: _this.infoData.productEntity&&_this.infoData.productEntity.code || '',
  53. productName: _this.infoData.productEntity&&_this.infoData.productEntity.productName || '',
  54. shelfPlaceCode: _this.infoData.shelfPlaceCode || '',
  55. currentInven: 3,
  56. printDate: '2021-12-21 15:30',
  57. printUser: '张三',
  58. barCode: 'dealerSn=1033242&shelfSn=342326735950757888&productCode=RPF2340&shelfPlaceCode=A01&shelfPlaceSn=123213'
  59. }
  60. }
  61. })
  62. }
  63. _this.printNum = _this.fromPage == 'smdy' ? _this.infoData.confirmQty : _this.infoData.qty
  64. },
  65. onUnload() {
  66. this.$refs.kkprinter.closeConnect()
  67. },
  68. methods: {
  69. cansel(){
  70. uni.$emit('refreshBL')
  71. uni.navigateBack()
  72. },
  73. textFormat(command,text,maxFontNums,left,top,lightHeight){
  74. const textLen = text.length
  75. console.log(textLen,maxFontNums)
  76. if(textLen > maxFontNums){
  77. const rows = Math.ceil(textLen/maxFontNums)
  78. for(let i=0;i<rows;i++){
  79. top = top+i*lightHeight
  80. console.log(top)
  81. // 最后一行
  82. if(i==rows-1){
  83. const ltxt = text.substring(i*maxFontNums)
  84. const ltlen = ltxt.length
  85. command.setText(left+((maxFontNums-ltlen)/2)*lightHeight, top, "TSS24.BF2", 1, 1, ltxt)
  86. }else{
  87. command.setText(left, top, "TSS24.BF2", 1, 1, text.substring(i*maxFontNums,maxFontNums))
  88. }
  89. }
  90. }else{
  91. command.setText(left+((maxFontNums-textLen)/2)*lightHeight, top, "TSS24.BF2", 1, 1, text)
  92. }
  93. return top
  94. },
  95. // 打印
  96. startPrint(opt,tsc,blesdk){
  97. console.log(opt,'opt')
  98. let top = 10 // 距离顶部10点像素
  99. const left = 10 // 距离左边10点
  100. const lightHeight = 24 // 行高3mm,1mm = 8点
  101. const pageW = 40 // 页签宽度mm
  102. const pageH = 30 // 页签高度mm
  103. const maxFontNums = Math.floor((pageW*8-left*2)/lightHeight) // 每行最多字体数
  104. // 初始化打印机
  105. const command = tsc.jpPrinter.createNew()
  106. command.init()
  107. command.setSize(pageW, pageH) // 标签纸张宽高,单位mm
  108. command.setGap(2) // 标签上下间距,单位mm
  109. command.setCls() // 清除缓冲区数据
  110. // 经销商文字高度,是否换行
  111. top = this.textFormat(command,this.printInfo.dealerName,maxFontNums,left,top,lightHeight)
  112. // 数字货架名称文字
  113. top = top+lightHeight
  114. top = this.textFormat(command,this.printInfo.shelfName,Math.floor(maxFontNums*0.6),left,top,lightHeight)
  115. // 产品编码
  116. top = top+lightHeight+15
  117. command.setText(left, top, "TSS24.BF2", 1, 1, this.printInfo.productCode)
  118. // 产品名称
  119. top = top+lightHeight
  120. this.textFormat(command,this.printInfo.productName,Math.floor(maxFontNums*0.6),left,top,lightHeight)
  121. // command.setQR(50, 50, "L", 5, "A", "https://www.baidu.com")
  122. // command.setBox(5, 5, 795, 595, 1)
  123. command.setPagePrint(1,1) // 打印分数1,每个标签重发打印2次
  124. console.log("开始打印了",command.getData())
  125. blesdk.senBlData(opt.deviceId, opt.serviceId, opt.writeId, command.getData(), this.onPrintSuccess);
  126. },
  127. // 打印成功
  128. onPrintSuccess(){
  129. // uni.navigateBack()
  130. this.$refs.kkprinter.onPrintSuccess()
  131. }
  132. }
  133. }
  134. </script>
  135. <style lang="less">
  136. .printTag{
  137. background-color: #fff;
  138. width: 100vh;
  139. padding: 50rpx 30rpx;
  140. .print-nums{
  141. padding: 50rpx 30rpx;
  142. color: #999;
  143. font-size: 32rpx;
  144. > view{
  145. padding: 20rpx 0;
  146. }
  147. .u-ninput{
  148. border: 2rpx solid #eee;
  149. border-radius: 50rpx;
  150. padding: 0 12rpx;
  151. }
  152. /deep/ .u-icon-disabled{
  153. background: #fff!important;
  154. }
  155. /deep/ .u-number-input{
  156. margin: 0 2rpx;
  157. border: 2rpx solid #eee;
  158. border-top: 0;
  159. border-bottom: 0;
  160. }
  161. /deep/ .u-icon-plus, /deep/ .u-icon-minus{
  162. border-radius: 50rpx;
  163. }
  164. }
  165. .shelf-info{
  166. background-color: #f8f8f8;
  167. padding: 30rpx;
  168. border-radius: 12rpx;
  169. color: #707070;
  170. .shelf-info-title{
  171. font-size: 32rpx;
  172. color: #222222;
  173. display: flex;
  174. justify-content: space-between;
  175. margin-bottom: 10rpx;
  176. .pno{
  177. background: rgba(3, 54, 146, 0.15);
  178. border-radius: 100rpx;
  179. padding: 6rpx 20rpx;
  180. color: #033692;
  181. font-size: 24rpx;
  182. }
  183. }
  184. }
  185. .buttons{
  186. padding: 50rpx 0;
  187. > view{
  188. width: 36%;
  189. margin: 0 2%;
  190. }
  191. }
  192. }
  193. </style>