From ba616e9307681da4781b7610ec3c27721d793379 Mon Sep 17 00:00:00 2001
From: guifei zhu <guifeizhu@guifeideiMac.local>
Date: 星期三, 18 九月 2024 12:13:37 +0800
Subject: [PATCH] 添加mqtt通讯

---
 pages/tabBar/general.vue |   97 ++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 88 insertions(+), 9 deletions(-)

diff --git a/pages/tabBar/general.vue b/pages/tabBar/general.vue
index 7600ad3..ae71ad0 100644
--- a/pages/tabBar/general.vue
+++ b/pages/tabBar/general.vue
@@ -177,7 +177,8 @@
 					<view class="flex">
 						<text>涓崏鑽�</text>
 						<text>涓�</text>
-						<text @click="showLongName(showValue(item.herbName))" class="text-cut flex-twice">{{showValue(item.herbName)}}</text>
+						<text @click="showLongName(showValue(item.herbName))"
+							class="text-cut flex-twice">{{showValue(item.herbName)}}</text>
 						<!-- <view class="margin-lr-sm">
 							<u-tag text="鎶ヨ涓�娆�" plain size="mini" type="warning"></u-tag>
 						</view> -->
@@ -356,20 +357,98 @@
 
 		},
 		onReady() {
+			const userinfo = uni.getStorageSync('userinfo');
+			const tenantid = userinfo.loginTenantId
+			uni.getSystemInfo({
+				success: (res) => {
+					let deviceId = res.deviceId
+					if (!deviceId) {
+						deviceId = 'mobile-' + tenantid + '-' + Date.parse(new Date())
+					}
+					uni.setStorageSync(this.$constant.DEVICE_ID, 'mobile-' + tenantid + '-' + res.deviceId);
+				}
+			})
+			this.startConnect();
+
+
+
 			let month = uni.$u.timeFormat(new Date(), 'yyyy-mm')
 			this.getMonth(month)
 
 			let day = uni.$u.timeFormat(new Date(), 'yyyy-mm-dd')
-			this.getOrderList(day, day)
+			this.getOrderList(day, day);
+
 
 		},
-		methods: {
-			showLongName(longName){
-					this.$refs.uToast.show({
-						type: 'default',
-						message: longName
-					})
-				
+		methods: {
+			/* 杩炴帴MQTT */
+			async startConnect() {
+				var _this = this
+				const account = uni.getStorageSync('account');
+				const deviceid = uni.getStorageSync(this.$constant.DEVICE_ID);
+
+				if (!account) {
+
+					return false
+				}
+				let opts = {
+					// #ifdef H5
+					url: 'ws://' + this.$api.mqttBaseUrl + ':8083/mqtt',
+					// #endif
+					// #ifdef APP-PLUS
+					url: 'wx://' + this.$api.mqttBaseUrl + ':8083/mqtt',
+					// #endif
+					clientId: deviceid,
+					username: account.username,
+					password: account.password
+				}
+				if (!this.$mqttTool.client) {
+					var client = await this.$mqttTool.connect(opts);
+				}
+				//璁㈤槄鏌ヨ璁惧鐘舵�佽繑鍥炴暟鎹�
+				this.$mqttTool.subscribe({
+					topic: this.$constant.SERVICE_DOWN + '/' + deviceid + '/#',
+					qos: 0
+				}).then(res => {
+					console.error(res)
+				})
+				//璁㈤槄鍙戦�佹寚浠よ繑鍥炵粨鏋�
+				// this.$mqttTool.subscribe({
+				// 	topic: this.$constant.SERVICE_RES_EQU_CMD,
+				// 	qos: 0
+				// }).then(res => {
+				// 	console.error(res)
+				// })
+				// if (!client) {
+				// 	return false
+				// }
+
+				client.on('connect', function(res) {
+					console.error('杩炴帴鎴愬姛')
+				})
+				client.on('reconnect', function(res) {
+					console.error('閲嶆柊杩炴帴')
+				})
+				client.on('error', function(res) {
+					console.info('杩炴帴閿欒')
+				})
+				client.on('close', function(res) {
+
+					console.error('鍏抽棴鎴愬姛')
+
+				})
+				client.on('message', function(topic, message, buffer) {
+					//console.info(message)
+				})
+			},
+
+
+			showLongName(longName) {
+				this.$refs.uToast.show({
+					type: 'default',
+					message: longName
+				})
+
 			},
 			dayChange(dayInfo) { // 鐐瑰嚮鏃ユ湡
 				this.model.date = dayInfo.date

--
Gitblit v1.9.3