Parcourir la source

封装蓝牙打印

jf il y a 5 ans
Parent
commit
361cb54368
3 fichiers modifiés avec 125 ajouts et 71 suppressions
  1. 16 11
      ghsApp/src/admin/home/order.vue
  2. 70 36
      ghsApp/src/mixins/BLE.js
  3. 39 24
      ghsApp/src/pagesOrder/print.vue

+ 16 - 11
ghsApp/src/admin/home/order.vue

@@ -96,14 +96,16 @@
                   class="admin-button-com bule"
                   :class="s.disable === 1 ? 'active' : ''"
                 >
-                  {{
-                    s.type === "item"
-                      ? "改花材"
-                      : s.type === "selfGet"
-                      ? "自取"
-                      : "发货"
-                  }}
-                </button>
+                  <!--{{-->
+                    <!--s.type === "item"-->
+                      <!--? "改花材"-->
+                      <!--: s.type === "selfGet"-->
+                      <!--? "自取"-->
+                      <!--: "发货"-->
+                  <!--}}-->
+																	{{ s.type === "item" ? "改花材" : s.type === "selfGet" ? "自取" :s.type === "print" ? "打印" :s.type === "debtPay" ? "延期收款" :s.type === "hasPay" ? "已收款" : "发货" }}
+
+																</button>
               </view>
             </div>
           </div>
@@ -149,6 +151,7 @@ import AppSearchModule from "@/components/module/app-search";
 import DorpdownSelect from "@/components/module/dorpdownSelect";
 import AppAvatarModule from "@/components/module/app-avatar";
 import rangeDatePick from "@/components/pyh-rdtpicker/pyh-rdtpicker.vue";
+import BLE from "@/mixins/BLE";
 export default {
   name: "order-list",
   components: {
@@ -161,7 +164,7 @@ export default {
     DorpdownSelect,
     AppAvatarModule,
   },
-  mixins: [list],
+  mixins: [list,BLE],
   computed: { ...mapGetters(["getLoginInfo", "getDictionariesInfo"]) },
   data() {
     return {
@@ -322,8 +325,10 @@ export default {
             id: item.id,
           },
         });
-      }
-    },
+      }else if (s.type === "print") {
+							this.autoLinkBle();
+						}
+		},
     selectSussess(val) {
       // this._list();
       console.log(val);

+ 70 - 36
ghsApp/src/mixins/BLE.js

@@ -4,9 +4,19 @@ var esc = require("@/pagesOrder/js/gprint/esc.js");
 export default {
 	data() {
 		return {
+			nameBLE:'未连接',
+			devices: [],
+			currDev: null,
+			isAutoBle: false,
+			connId: "",
+			piaojuText: ['测试测试测试测试傻老大傻老大','测试测试测试测试傻老大傻老大','测试测试测试测试傻老大傻老大'],
+		};
+	},
+	onShow() {
+		if(uni.getStorageSync('BLE')){
+			this.nameBLE = uni.getStorageSync('BLE').name;
 		}
 	},
-	onShow() {},
 	methods: {
 		//断开蓝牙链接
 		destroyed() {
@@ -20,6 +30,7 @@ export default {
 		//初始化蓝牙
 		searchBle() {
 			var that = this;
+			that.isAutoBle = false;
 			uni.openBluetoothAdapter({
 				success(res) {
 					console.log(res,"打开 蓝牙模块");
@@ -55,27 +66,56 @@ export default {
 				console.log(devices,'设备列表');
 				var re = JSON.parse(JSON.stringify(devices));
 				let name = re.devices[0].name?re.devices[0].name:'未知设备';
-				let deviceId = re.devices[0].deviceId;
-				that.devices.push({name: name,deviceId: deviceId,services: []});
-				// that.autoLinkBle();
-				// if (name != "未知设备" && name != "") {
-				// 	let deviceId = re.devices[0].deviceId;
-				// 	that.devices.push({name: name,deviceId: deviceId,services: []});
-				// }
+				if (name != "未知设备" && name != "") {
+					let deviceId = re.devices[0].deviceId;
+					that.devices.push({name: name,deviceId: deviceId,services: []});
+					// that.$nextTick(()=>{
+					// 	that.$util.unique(that.devices);
+					// })
+
+				}
 			});
 		},
-		//自动链接蓝牙
+		//自动链接蓝牙-初始化蓝牙模块
 		autoLinkBle(){
 			let self = this;
-			// let deviceId = uni.getStorageSync('BLE').deviceId;
+			if(!uni.getStorageSync('BLE')){
+				uni.showModal({
+					title: '提示', content: '请先连接蓝牙',
+					confirmText: "去连接",
+					success: function (res) {
+						if (res.confirm) {
+							uni.navigateTo({url:'/pagesOrder/print'});
+						}
+					}
+				});
+				return;
+			}
+			uni.showLoading({title: '打印中...'});
+			self.isAutoBle = true;
 			let deviceId = uni.getStorageSync('BLE');
-			this.onConn(deviceId);
-			// this.devices.forEach((ele)=>{
-			// 	if(ele.deviceId==deviceId){
-			// 		this.senBleLabel();
-			// 		this.onConn(ele);
-			// 	}
-			// })
+			//初始化蓝牙模块
+			uni.openBluetoothAdapter({
+				success(res) {
+					self.autoLinkBleFun(deviceId);
+				}
+			});
+		},
+		//自动链接蓝牙
+		autoLinkBleFun(item) {
+			var that = this;
+			let deviceId = item.deviceId;
+			uni.createBLEConnection({
+				deviceId: deviceId,
+				complete(res) {
+					if (res.errMsg == "createBLEConnection:ok") {
+						uni.setStorageSync('BLE',item);
+						setTimeout(function() {
+							that.getBLEServices(deviceId);
+						}, 2000);
+					}
+				}
+			});
 		},
 		stopFindBule() {
 			console.log("停止搜寻附近的蓝牙外围设备---------------");
@@ -93,7 +133,7 @@ export default {
 				deviceId: deviceId,
 				complete(res) {
 					if (res.errMsg == "createBLEConnection:ok") {
-						uni.setStorageSync('BLE',item);
+						that.nameBLE = item.name;
 						console.log("连接蓝牙-[" + item.name + "]--成功");
 						that.$msg("连接蓝牙-[" + item.name + "]--成功");
 						that.connId = deviceId;
@@ -133,30 +173,23 @@ export default {
 							success(res) {
 								var re = JSON.parse(JSON.stringify(res));
 
-								console.log(
-									"deviceId = [" + deviceId + "]  serviceId = [" + serviceId + "]"
-								);
+								console.log("deviceId = [" + deviceId + "]  serviceId = [" + serviceId + "]");
 								for (var c = 0; c < re.characteristics.length; c++) {
 									if (re.characteristics[c].properties.write == true) {
 										let uuid = re.characteristics[c].uuid;
-										console.log(
-											" deviceId = [" +
-											deviceId +
-											"]  serviceId = [" +
-											serviceId +
-											"] characteristics=[" +
-											uuid
-										);
-
+										console.log(" deviceId = [" +deviceId +"]  serviceId = [" +serviceId +"] characteristics=[" +uuid);
+										//是否是直接打印
+										if(that.isAutoBle){that.senBleLabel();return}
 										for (var index in that.devices) {
 											if (that.devices[index].deviceId == deviceId) {
-												that.devices[index].services.push({
-													serviceId: serviceId,
-													characteristicId: uuid
-												});
+												that.devices[index].services.push({serviceId: serviceId,characteristicId: uuid});
+												that.$nextTick(()=>{
+													uni.setStorageSync('BLE',that.devices[index]);
+												})
 												break;
 											}
 										}
+
 										console.log(JSON.stringify(that.devices));
 									}
 								}
@@ -195,6 +228,7 @@ export default {
 				for (var j = 0; j < data.length; j++) {
 					dataView.setUint8(j, data[j]);
 				}
+				uni.hideLoading();
 				uni.writeBLECharacteristicValue({
 					deviceId,
 					serviceId,
@@ -209,7 +243,7 @@ export default {
 			realWriteData(sendloop, i);
 		},
 		senBleLabel() {
-			let currDev = uni.getStorageSync('BLE').deviceId;
+			let currDev = uni.getStorageSync('BLE');
 			//标签模式
 			let deviceId = currDev.deviceId;
 			let serviceId = currDev.services[0].serviceId;
@@ -231,7 +265,7 @@ export default {
 			this.senBlData(deviceId, serviceId, characteristicId, command.getData());
 		},
 		billPrint() {
-			let currDev = uni.getStorageSync('BLE').deviceId;
+			let currDev = uni.getStorageSync('BLE');
 			//票据模式
 			let deviceId = currDev.deviceId;
 			let serviceId = currDev.services[0].serviceId;

+ 39 - 24
ghsApp/src/pagesOrder/print.vue

@@ -1,36 +1,53 @@
+<style lang="scss" scoped>
+			.center{
+				padding: 30rpx;
+				.connected-title{
+					text{color: #2079ff;}
+				}
+				.seek-btn{
+					margin: 30rpx 0;
+					width: 60%;
+					height: 80rpx;
+					color: #FFFFFF;
+					background: #169bd5;
+					border-radius: 20rpx;
+				}
+				.ble-list{
+					width: 65%;
+					height: 80rpx;
+					text{color: #2079ff;}
+				}
+			}
+</style>
+
 <template>
-	<view>
-		<button @click="searchBle">搜索蓝牙打印机</button>
-		<view style="margin-top: 30upx;" :key="index" v-for="(item, index) in devices">
-			<button style="width: 400upx; color: #0081FF;" @click="onConn(item)">
-				{{ item.name }}
-			</button>
+	<view class="center">
+		<view class="flex connected-title">已连接蓝牙:<text>{{nameBLE}}</text></view>
+		<view class="flex-center seek-btn" @click="searchBle">搜索蓝牙打印机</view>
+		<!--<button @click="searchBle">搜索蓝牙打印机</button>-->
+		<!--<view style="margin-top: 30upx;" :key="index" v-for="(item, index) in devices">-->
+		<view class="flex-space ble-list" :key="index" v-for="(item, index) in devices" @click="onConn(item)">
+				{{item.name}} <text>连接</text>
 		</view>
-		<button style="margin-top: 100upx;" @click="senBleLabel()">标签打印</button>
-		<textarea
-			@blur="bindTextAreaBlur"
-			auto-height
-			placeholder-style="color:#F76260"
-			placeholder="请输入票据信息"
-			v-model="piaojuText"
-		/>
-		<button style="margin-top: 100upx;" @click="billPrint()">票据打印</button>
+		<!--<button style="margin-top: 100upx;" @click="senBleLabel()">标签打印</button>-->
+		<!--<textarea @blur="bindTextAreaBlur" auto-height placeholder-style="color:#F76260" placeholder="请输入票据信息" v-model="piaojuText"/>-->
+		<!--<button style="margin-top: 100upx;" @click="billPrint()">票据打印</button>-->
 	</view>
 </template>
 
 <script>
 	import BLE from "@/mixins/BLE";
-var tsc = require("./js/gprint/tsc.js");
-var esc = require("./js/gprint/esc.js");
+// var tsc = require("./js/gprint/tsc.js");
+// var esc = require("./js/gprint/esc.js");
 export default {
 	mixins: [BLE],
 	data() {
 		return {
-			devices: [],
-			currDev: null,
-			connId: "",
-			piaojuText: "",
-			deviceId:'6CE888E4-6BF4-2D2A-60AE-07480F6A39A6',
+			// devices: [],
+			// currDev: null,
+			// connId: "",
+			// piaojuText: "",
+			// deviceId:'6CE888E4-6BF4-2D2A-60AE-07480F6A39A6',
 		};
 	},
 	onLoad: function() {},
@@ -267,5 +284,3 @@ export default {
 };
 </script>
 
-<style>
-</style>