printTag.vue 6.5 KB

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