From bbae990ed1f4574fa954753faeae159052d03fba Mon Sep 17 00:00:00 2001
From: llj <132905093+newwwwwwtree@users.noreply.github.com>
Date: 星期二, 27 一月 2026 16:14:06 +0800
Subject: [PATCH] 待我审核增加销售订单,采购订单

---
 components/blueToothConnector/blueToothConnector.vue |  170 +++++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 136 insertions(+), 34 deletions(-)

diff --git a/components/blueToothConnector/blueToothConnector.vue b/components/blueToothConnector/blueToothConnector.vue
index 9b86d0f..b8d00cf 100644
--- a/components/blueToothConnector/blueToothConnector.vue
+++ b/components/blueToothConnector/blueToothConnector.vue
@@ -2,7 +2,15 @@
 	<view>
 		<!-- 钃濈墮杩炴帴鐘舵�� MAC鍦板潃 -->
 		<view class="connection-status">
-			<view>杩炴帴鐘舵�侊細</view>
+			<view style="display: flex; flex-direction: row; gap: 20rpx;padding: 20rpx;">
+				<view style="width: 5rem;">杩炴帴璁惧: </view>
+				<view v-if="$printer.isConnected() === false">鏃�</view>
+				<view v-else="$printer.isConnected() === true"
+					style="flex: 1;display: inline-flex;flex-direction: row;justify-content: space-between;">
+					<view>{{ $printer.connectedDevice().name }}</view>
+					<view>{{ $printer.connectedDevice().address }}</view>
+				</view>
+			</view>
 		</view>
 		<!-- 鎿嶄綔鍖� -->
 		<view class="buttons">
@@ -10,19 +18,24 @@
 			<button class="btn-d" @click="closeBluetooth()">鏂紑杩炴帴</button>
 		</view>
 		<!-- 璁惧鍒楄〃 -->
-		<view v-for="(device, index) in discoveredDevices" :key="device.address">
-			<uni-card :title="device.name" :extra="connectedDeviceId === device.address?'宸茶繛鎺�':'鏈繛鎺�'"
-				style="margin: 10px;">
-				<view class="operation-zone">
-					<button class="op1" size="mini" plain @click="connectBT(device)">杩炴帴璁惧</button>
-				</view>
-			</uni-card>
-		</view>
+		<scroll-view scroll-y="true" style="height: calc(80vh - 8rem);">
+			<view v-for="(device, index) in discoveredDevices" :key="device.address">
+				<uni-card :title="device.name" :extra="connectedDeviceId === device.address?'宸茶繛鎺�':'鏈繛鎺�'"
+					style="margin: 10px;">
+					<view class="operation-zone">
+						<button class="op1" size="mini" plain @click="connectBT(device)">杩炴帴璁惧</button>
+					</view>
+				</uni-card>
+			</view>
+		</scroll-view>
 	</view>
 </template>
 
 <script>
-	import bluetoothTool from '@/plugins/BluetoothTool.js'
+    import printerMeta from "../../static/json/printerMeta.json";
+	import {
+		CommonUtils
+	} from '../../utils/common';
 	import permission from '@/plugins/permission.js'
 	import {
 		InputImage
@@ -34,12 +47,19 @@
 		FakeConnectedDevice,
 		WriteOptions,
 	} from '@psdk/frame-father';
+	import {
+		getAddress
+	} from '../../utils/auth';
+	import blueToothTool from '@/plugins/BluetoothTool.js';
+import { nextTick } from "vue";
 	export default {
-		name: "blueToothConnector",
+		name: "blueToothPrinterConnector",
 		data() {
 			return {
 				discoveredDevices: [], // 鏌ヨ鍒扮殑璁惧
-				connectedDeviceId: ""
+				connectedDeviceId: "",
+				readSuccess: false,
+				clock: null,
 			};
 		},
 		methods: {
@@ -73,19 +93,16 @@
 						console.log('start discovery devices');
 						this.discoveredDevices = [];
 						console.log(res)
-						bluetoothTool.discoveryNewDevice();
+						blueToothTool.discoveryNewDevice();
 					},
 					fail: async (e) => {
 						console.error(e)
-						switch (e.code) {
-							case "10009":
-								this.showToast("姝よ澶囦笉鏀寔璁惧鎼滅储鍔熻兘!");
-								break;
-							default:
-								console.error(e);
+						if(e.errCode == 10009) {
+							this.showToast('姝よ澶囦笉鏀寔璁惧鎼滅储鍔熻兘!');
 						}
 					}
 				})
+
 			},
 			onDevice(device) {
 				console.log("鐩戝惉瀵绘壘鍒版柊璁惧鐨勪簨浠�---------------")
@@ -107,19 +124,24 @@
 				uni.showLoading({
 					title: '杩炴帴涓�'
 				});
-				bluetoothTool.connDevice(device.address, (result) => {
-					console.log(result)
+				blueToothTool.connDevice(device.address, (result) => {
+					console.log("璁惧MAC鍦板潃: ", device.address)
+					console.log("printer锛�", vm.$printer._connectedDevice)
 					uni.hideLoading()
 					if (result) {
 						// 	// console.log(result);
-						bluetoothTool.cancelDiscovery();
+						blueToothTool.cancelDiscovery();
 						// console.log(vm.$printer)
 						vm.$printer.init(new FakeConnectedDevice());
 						vm.connectedDeviceId = device.address;
+						vm.$printer._connectedDevice.address = device.address
+						vm.$printer._connectedDevice.name = device.name
+						console.log("printer锛�", vm.$printer._connectedDevice)
 						uni.showToast({
 							icon: 'none',
 							title: '杩炴帴鎴愬姛'
 						})
+
 					} else {
 						uni.showToast({
 							icon: 'none',
@@ -130,45 +152,125 @@
 			},
 			stopSearchBT() {
 				console.log("鍋滄鎼滃闄勮繎鐨勮摑鐗欏鍥磋澶�---------------")
-				bluetoothTool.cancelDiscovery();
+				blueToothTool.cancelDiscovery();
 			},
 			closeBluetooth() {
 				console.log("鍋滄钃濈墮杩炴帴")
 				const vm = this;
 				if (vm.connectedDeviceId != '') {
-					bluetoothTool.closeBtSocket();
+					blueToothTool.closeBtSocket();
 					vm.connectedDeviceId = "";
+					vm.$printer._connectedDevice = null
 				}
+
+			},
+			showToast(msg, status = "none") {
+				uni.showToast({
+					title: msg,
+					icon: status,
+					duration: 2000
+				});
+			},
+			async checkReadSuccess(count) {
+				// 鎵归噺鎻掑叆鏃讹紝濡傛灉鍓嶄竴娆″彂閫佹垚鍔燂紝鍒欐竻闄ゅ墠涓�娆$殑鏃堕挓
+				if (this.clock != null) {
+					this.clock.stop();
+					this.clock = null;
+				}
+				let that = this
+				return new Promise((resolve, reject) => {
+					let currentCount = 0;
+					that.clock = new CommonUtils.timeClock(() => {
+						console.log(that.readSuccess)
+						currentCount++;
+						if (that.readSuccess == true) {
+							that.clock.stop();
+							resolve(true);
+						} else if (currentCount >= count) {
+							that.clock.stop();
+							reject(new Error('璇诲彇瓒呮椂'));
+						}
+					}, printerMeta['timeoutPerMillSecond']);
+
+					// 鍚姩鏃堕挓
+					that.clock.start();
+				});
+			},
+			async sendMessage(cmd) {
+				cmd = Array.from(this.uint8ArrayToSignedArray(cmd))
+				uni.showLoading({
+					title: "鍙戦�佷腑..."
+				})
+				// console.log("msg: " + cmd);
+				let that = this
+				return new Promise((resolve, reject) => {
+					const result = blueToothTool.sendByteData(cmd);
+					if (!result) {
+                        this.clock.stop();
+						reject("鍙戦�佸け璐�,璇烽噸璇�...")
+					}
+					that.checkReadSuccess(printerMeta['timeoutCount'])
+						.then((res) => {
+							if (res === true) {
+								resolve(true)
+							}
+						})
+						.catch((err) => {
+							this.clock.stop();
+							reject('璁惧寮傚父,璇锋鏌ヨ澶囩姸鎬�...')
+						})
+						.finally(() => {
+							that.readSuccess = false
+						})
+				})
+
+			},
+			uint8ArrayToSignedArray(uint8Array) {
+				let signedArray = new Array(uint8Array.length);
+				for (let i = 0; i < uint8Array.length; i++) {
+					if (uint8Array[i] >= 128) {
+						signedArray[i] = uint8Array[i] - 256;
+					} else {
+						signedArray[i] = uint8Array[i];
+					}
+				}
+				return signedArray;
 			},
 		},
 		async mounted() {
 			//#ifdef APP-PLUS
 			// 钃濈墮
-			bluetoothTool.init({
+			console.log("钃濈墮鍒濆鍖�")
+			blueToothTool.init({
 				listenBTStatusCallback: (state) => {
 					if (state == 'STATE_ON') {
 						console.log(state);
+					}
+					if (state == 'STATE_OFF') {
+						console.log(state);
+						this.closeBluetooth()
 					}
 				},
 				discoveryDeviceCallback: this.onDevice,
 				discoveryFinishedCallback: function() {
 					console.log("鎼滅储瀹屾垚");
 				},
-				readDataCallback: function(dataByteArr) {
-					/* if(that.receiveDataArr.length >= 200) {
-						that.receiveDataArr = [];
-					}
-					that.receiveDataArr.push.apply(that.receiveDataArr, dataByteArr); */
+				readDataCallback: (dataByteArr) => {
+					// if(that.receiveDataArr.length >= 200) {
+					// 	that.receiveDataArr = [];
+					// }
+					// that.receiveDataArr.push.apply(that.receiveDataArr, dataByteArr);
+					this.readSuccess = true
 					console.log("璇诲彇瀹屾垚" + dataByteArr);
 				},
 				connExceptionCallback: function(e) {
 					console.log(e);
 				}
 			});
-			
-			// 鏌ョ湅杩炴帴璁惧
-			let pariedDevices = bluetoothTool.getPairedDevices()
-			console("宸查厤瀵硅澶�", pariedDevices)
+
+			if (this.$printer.isConnected()) {
+				this.connectedDeviceId = this.$printer.connectedDevice().address
+			}
 			//#endif
 		},
 	}

--
Gitblit v1.9.1