lilei %!s(int64=3) %!d(string=hai) anos
pai
achega
fdbb993d91
Modificáronse 2 ficheiros con 115 adicións e 61 borrados
  1. 46 56
      components/kk-printer/index.vue
  2. 69 5
      pages/common/printTag/printTag.vue

+ 46 - 56
components/kk-printer/index.vue

@@ -6,10 +6,10 @@
 		<view class="kk-shadow" :class="isShowSearch?'show':''" @tap="handleSearchClose">
 			<view class="kk-modal" @tap.stop="doNothing">
 				<view class="kk-search-device">
-					<view class="kk-filter-wrap">
+					<!-- <view class="kk-filter-wrap">
 						<view class="filter-title">根据SRRI过滤设备</view>
 						<slider @change="handleSRRIChange" max='-20' min='-100' value="-95" show-value/>
-					</view>
+					</view> -->
 					<view class="kk-filter-wrap">
 						<view class="filter-title">根据蓝牙名过滤</view>
 						<input type="text"  placeholder-class="kk-placeholder-class" placeholder="请输入蓝牙名字或设备ID搜索" v-model="filterName" />
@@ -31,26 +31,30 @@
 							<view class="empty-text">~ 无可搜索到的设备 ~</view>
 						</view>
 						<view class="" v-else>
-							<view class="kk-devices-item" v-for="(item,index) in filterDeviceList" :key="index" @tap="handleConnectDevice(item)">
-								<view class="name">
-									<text>设备名称:</text>
-									<text>{{item.name?item.name:'未命名'}}</text>
-								</view>
-								<view class="rssi">
-									<text>信号强度:</text>
-									<text>({{Math.max(0, item.RSSI + 100)}}%)</text>
-								</view>
-								<view class="deviceid">
-									<text>设备ID:</text>
-									<text>{{item.deviceId}}</text>
+							<view class="kk-devices-item flex align_center" v-for="(item,index) in filterDeviceList" :key="index" @tap="handleConnectDevice(item)">
+								<view>
+									<view class="name">
+										<text>设备名称:</text>
+										<text>{{item.name?item.name:'未命名'}}</text>
+									</view>
+									<view class="rssi">
+										<text>信号强度:</text>
+										<text>({{Math.max(0, item.RSSI + 100)}}%)</text>
+									</view>
+									<view class="deviceid">
+										<text>设备ID:</text>
+										<text>{{item.deviceId}}</text>
+									</view>
 								</view>
-								<view class="advmac" v-if="item.advMac">
-									<text>advMac:</text>
-									<text>{{item.advMac}}</text>
+								<view>
+									<u-icon :size="60" color="#55aaff" name="plus-circle"></u-icon>
 								</view>
 							</view>
 						</view>
 					</view>
+					<view style="margin-top: 20upx;">
+						<view style="text-align: center;padding: 20upx;border:2upx solid #eee;border-radius: 15upx;" @tap="handleSearchClose">取消打印</view>
+					</view>
 				</view>
 			</view>
 		</view>
@@ -59,7 +63,7 @@
 
 <script>
 	import tsc from '@/components/kk-printer/printer/tsc.js'
-	import esc from '@/components/kk-printer/printer/esc.js'
+	// import esc from '@/components/kk-printer/printer/esc.js'
 	import * as blesdk from './utils/bluetoolth';
 	import util from './utils/util.js';
 	export default {
@@ -98,10 +102,6 @@
 				type:String,
 				default:'打印中...'
 			},
-			bufferData:{
-				type:String,
-				require:true
-			},
 			roomcode:{
 				type:String, 
 				require:true
@@ -123,12 +123,10 @@
 				let devices = this.devicesList;
 				let name = this.filterName;
 				let rssi = this.filterRSSI;
-				console.log(devices,name,rssi)
 				//按RSSI过滤
 				let filterDevices1 = devices.filter((item)=>{
 					return item.RSSI > rssi
 				})
-				console.log(filterDevices1)
 				// 按名字过滤
 				let filterDevices2
 				if(name!=''){
@@ -153,7 +151,6 @@
 			
 		},
 		beforeDestroy(){
-			
 			this.stopSearchBtnTap();
 		},
 		methods:{
@@ -167,9 +164,13 @@
 					//获取已连接设备
 					blesdk.getConnectedBluetoothDevices().then((res)=>{
 						//若没有已连接设备,弹框搜索设备
-						console.log(res,this.deviceId,this.serviceId,this.writeId,this.bufferData,this.onPrintSuccess)
+						console.log(res,this.deviceId,this.serviceId,this.writeId,this.onPrintSuccess)
 						if(res.devices.length == 0){
 							this.isShowSearch = true
+							this.devicesList = []
+							if(this.deviceId){
+								this.closeConnect(this.deviceId)
+							}
 						}else{
 							const lastDevice = this.$store.state.vuex_lastBuleDevice
 							if(lastDevice && res.devices[0].deviceId == lastDevice.deviceId){
@@ -178,25 +179,13 @@
 								this.writeId = lastDevice.writeId
 								this.readId = lastDevice.readId
 							}
-							let datalen=20;
-							if (plus.os.name != 'Android')
-							{         
-							  datalen=180;
-							}
 							this.isPrinting = true;
 							this.$nextTick(()=>{
-								var command = tsc.jpPrinter.createNew()
-								command.init()
-								command.setSize(40, 30) // 标签纸张宽高,单位mm
-								command.setGap(2) // 标签上下间距,单位mm
-								command.setCls() // 清除缓冲区数据
-								command.setText(50, 10, "TSS24.BF2", 1, 1, "打印测试")
-								command.setQR(50, 50, "L", 5, "A", "https://www.baidu.com")
-								command.setBox(5, 5, 795, 595, 1)
-								command.setPagePrint(1,2) // 打印分数1,每个标签重发打印2次
-								console.log("开始打印了")
-								blesdk.senBlData(this.deviceId, this.serviceId, this.writeId, command.getData(), this.onPrintSuccess);
-								this.isPrinting = false;
+								this.$emit("startPrint",{
+									deviceId: this.deviceId,
+									serviceId: this.serviceId,
+									writeId: this.writeId
+								},tsc,blesdk)
 							})
 						}
 					}).catch((err)=>{
@@ -228,12 +217,11 @@
 			},
 			//点击连接设备
 			handleConnectDevice(device){
+				const _this = this
 				let deviceId = device.deviceId;
 				let name = device.name;
 				this.deviceId = deviceId;
 				console.log('deviceId',this.deviceId)
-				// uni.setStorageSync('k_curDeviceID',deviceId);
-				// uni.setStorageSync('k_curDeviceName',name);plus.bluetooth.onBluetoothDeviceFound
 				uni.onBLEConnectionStateChange(function(res){
 					console.log('连接',res)
 					if(res.connected){
@@ -241,6 +229,7 @@
 							verticalAlign:'center'
 						})
 					}else{
+						_this.closeConnect(deviceId)
 						plus.nativeUI.toast('设备'+ res.deviceId + '已断开连接',{
 							verticalAlign:'center'
 						})
@@ -276,7 +265,6 @@
 			},
 			onPrintSuccess(){
 				this.isPrinting = false;
-				console.log({roomcode:this.roomcode,CustName:this.CustName,id:this.roomid})
 				console.log('打印成功')
 				// this.$emit('onPrintSuccess')
 			},
@@ -285,8 +273,8 @@
 				this.isPrinting = false;
 			},
 			closeConnect(){
-				blesdk.closeBLEConnection()
-			}
+				blesdk.closeBLEConnection(this.deviceId)
+			},
 		}
 	}
 </script>
@@ -313,7 +301,7 @@
 				z-index: 10000;
 				.kk-modal{
 					width:680upx;
-					height: 80%;
+					height: 60%;
 					padding:24upx;
 					box-sizing: border-box;
 					overflow-y: auto;
@@ -327,7 +315,7 @@
 						height: 100%;
 						.kk-filter-wrap{
 							width:100%;
-							height: 160upx;
+							height: 200upx;
 							display: flex;
 							flex-direction: column;
 							justify-content: flex-start;
@@ -358,9 +346,9 @@
 							align-items: center;
 							&>view{
 								flex:1 1 auto;
-								height: 100upx;
-								line-height: 100upx;
-								border-radius: 16upx;
+								height: 80upx;
+								line-height: 80upx;
+								border-radius: 100upx;
 								text-align: center;
 								color:#ffffff;
 								&.confirm-btn{
@@ -410,9 +398,11 @@
 								&:nth-last-child(1){
 									border-bottom: none;
 								}
-								&>view{
-									width:100%;
-									font-size: 30upx;
+								> view{
+									&:first-child{
+										flex-grow: 1;
+										width: 80%;
+									}
 								}
 							}
 						}

+ 69 - 5
pages/common/printTag/printTag.vue

@@ -19,7 +19,7 @@
 			<view v-if="fromPage == 'bdtq'"><u-button @click="cansel" shape="circle" plain>返回列表</u-button></view>
 			<view v-if="fromPage == 'smdy'"><u-button @click="cansel" shape="circle" plain>重新扫描</u-button></view>
 			<view>
-				<kk-printer ref="kkprinter" :bufferData="bufferData" @onPrintSuccess="onPrintSuccess"></kk-printer>
+				<kk-printer ref="kkprinter" @startPrint="startPrint"></kk-printer>
 			</view>
 		</view>
 	</view>
@@ -35,11 +35,23 @@
 			return {
 				part: null,
 				fromPage: '',
-				bufferData:''
+				printInfo: null
 			}
 		},
 		onLoad(opts) {
 			this.fromPage = opts.from
+			this.printInfo = {
+				dealerName: '剪冠汽配西安长ab路店123汽配西安长青二路',
+				shelfName: '西安车领主常青二路数字货架',
+				productCode: 'JO-21026E',
+				productName: '箭牌机油滤清器 10款进口大众途锐...',
+				shelfPlaceCode: 'A01',
+				currentInven: 3,
+				printDate: '2021-12-21 15:30',
+				printUser: '张三',
+				barCode: 'dealerSn=1033242&shelfSn=342326735950757888&productCode=RPF2340&shelfPlaceCode=A01&shelfPlaceSn=123213'
+			}
+			console.log(this.printInfo.shelfName.length,'=====')
 		},
 		onUnload() {
 			this.$refs.kkprinter.closeConnect()
@@ -48,13 +60,65 @@
 			cansel(){
 				uni.navigateBack()
 			},
+			textFormat(command,text,maxFontNums,left,top,lightHeight){
+				const textLen = text.length
+				console.log(textLen,maxFontNums)
+				if(textLen > maxFontNums){
+					const rows = Math.ceil(textLen/maxFontNums)
+					for(let i=0;i<rows;i++){
+						top = top+i*lightHeight
+						console.log(top)
+						// 最后一行
+						if(i==rows-1){
+							const ltxt = text.substring(i*maxFontNums)
+							const ltlen = ltxt.length
+							command.setText(left+((maxFontNums-ltlen)/2)*lightHeight, top, "TSS24.BF2", 1, 1, ltxt)
+						}else{
+							command.setText(left, top, "TSS24.BF2", 1, 1, text.substring(i*maxFontNums,maxFontNums))
+						}
+					}
+				}else{
+					command.setText(left+((maxFontNums-textLen)/2)*lightHeight, top, "TSS24.BF2", 1, 1, text)
+				}
+				return top
+			},
 			// 打印
-			onPrint(opt){
-				this.bufferData = strCmd;
+			startPrint(opt,tsc,blesdk){
+				console.log(opt,'opt')
+				let top = 10 // 距离顶部10点像素
+				const left = 10 // 距离左边10点
+				const lightHeight = 24 // 行高3mm,1mm = 8点
+				const pageW = 40 // 页签宽度mm
+				const pageH = 30 // 页签高度mm
+				const maxFontNums = Math.floor((pageW*8-left*2)/lightHeight) // 每行最多字体数
+				// 初始化打印机
+				const command = tsc.jpPrinter.createNew()
+				command.init()
+				command.setSize(pageW, pageH) // 标签纸张宽高,单位mm
+				command.setGap(2) // 标签上下间距,单位mm
+				command.setCls() // 清除缓冲区数据
+				// 经销商文字高度,是否换行
+				top = this.textFormat(command,this.printInfo.dealerName,maxFontNums,left,top,lightHeight)
+				// 数字货架名称文字
+				top = top+lightHeight
+				top = this.textFormat(command,this.printInfo.shelfName,Math.floor(maxFontNums*0.6),left,top,lightHeight)
+				// 产品编码
+				top = top+lightHeight+15
+				command.setText(left, top, "TSS24.BF2", 1, 1, this.printInfo.productCode)
+				// 产品名称
+				top = top+lightHeight
+				this.textFormat(command,this.printInfo.productName,Math.floor(maxFontNums*0.6),left,top,lightHeight)
+				
+				// command.setQR(50, 50, "L", 5, "A", "https://www.baidu.com")
+				// command.setBox(5, 5, 795, 595, 1)
+				command.setPagePrint(1,1) // 打印分数1,每个标签重发打印2次
+				console.log("开始打印了",command.getData())
+				blesdk.senBlData(opt.deviceId, opt.serviceId, opt.writeId, command.getData(), this.onPrintSuccess);
 			},
 			// 打印成功
 			onPrintSuccess(){
-				uni.navigateBack()
+				// uni.navigateBack()
+				this.$refs.kkprinter.onPrintSuccess()
 			}
 		}
 	}