From f853349d1e0299d84d345e9fd2e7d4feccd32d52 Mon Sep 17 00:00:00 2001
From: fengerli <528575642@qq.com>
Date: Fri, 19 Sep 2025 09:33:56 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9mqtt=E6=A0=B9=E6=8D=AE?=
=?UTF-8?q?=E7=AB=AF=E5=8F=A3=E5=9C=B0=E5=9D=80=EF=BC=8C=E8=B5=B0ws?=
=?UTF-8?q?=E6=88=96=E8=80=85wss=E5=8D=8F=E8=AE=AE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.env.development | 4 +-
src/layout/components/Navbar.vue | 2 +-
src/utils/mqtt.ts | 100 +++++++-------
src/views/system/user/index.vue | 223 +++++++++++--------------------
4 files changed, 131 insertions(+), 198 deletions(-)
diff --git a/.env.development b/.env.development
index c6f3ed1..24e61f8 100644
--- a/.env.development
+++ b/.env.development
@@ -5,8 +5,8 @@ VITE_APP_TITLE = 云平台管理系统
VITE_APP_ENV = 'development'
# 开发环境
- VITE_APP_BASE_API = 'http://47.120.79.150/backend'
- #VITE_APP_BASE_API = 'http://192.168.2.23:8000'
+ #VITE_APP_BASE_API = 'https://fuyuanshen.com/backend'
+ VITE_APP_BASE_API = 'http://192.168.2.34:8000'
#代永飞接口
#VITE_APP_BASE_API = 'http://457102h2d6.qicp.vip:24689'
diff --git a/src/layout/components/Navbar.vue b/src/layout/components/Navbar.vue
index ebc30d0..c50d020 100644
--- a/src/layout/components/Navbar.vue
+++ b/src/layout/components/Navbar.vue
@@ -20,7 +20,7 @@

-
{{ useUserStore().nickname }}
+
{{ useUserStore().nickname }}
diff --git a/src/utils/mqtt.ts b/src/utils/mqtt.ts
index b032c2a..6d0d528 100644
--- a/src/utils/mqtt.ts
+++ b/src/utils/mqtt.ts
@@ -1,4 +1,4 @@
-import { ref, onUnmounted } from 'vue'; // 修复:导入必要的Vue API
+
import * as Paho from 'paho-mqtt';
// MQTT消息类型定义
@@ -9,61 +9,60 @@ export interface MqttMessage {
retained: boolean;
time: Date;
}
-
// 订阅选项
export interface SubscribeOptions {
qos: 0 | 1 | 2;
}
-// MQTT配置信息
-const MQTT_CONFIG = {
- // 连接地址(添加协议类型)
- protocol: 'ws', // 关键:明确协议(ws或wss)
- host: '47.120.79.150',
- port: 9083,
- // 认证信息
- username: 'admin',
- password: '#YtvpSfCNG',
- // 客户端ID(添加时间戳确保唯一性)
- clientId: `vue3-mqtt-client-${Date.now()}-${Math.random().toString(36).substring(2, 10)}`,
- // 连接选项
- cleanSession: true,
- keepAliveInterval: 60,
- reconnect: true,
+// 根据当前页面协议自动选择MQTT配置
+const getMqttConfig = () => {
+ // 检测当前页面协议(http: 或 https:)
+ const isHttps = window.location.protocol === 'https:';
+ return {
+ // 自动切换协议:https页面用wss,http页面用ws
+ protocol: isHttps ? 'wss' : 'ws',
+ host: 'www.cnxhyc.com',
+ // 自动切换端口:https对应9084,http对应9083
+ port: isHttps ? 9084 : 9083,
+ // 认证信息
+ username: 'admin',
+ password: '#YtvpSfCNG',
+ clientId: `vue3-mqtt-client-${Date.now()}-${Math.random().toString(36).substring(2, 10)}`,
+ cleanSession: true,
+ keepAliveInterval: 60,
+ reconnect: true,
+ };
};
+const MQTT_CONFIG = getMqttConfig();
// MQTT客户端组合式API
export function useMqtt() {
// 客户端实例
let client: Paho.Client | null = null;
-
// 状态管理(修复:已导入ref)
const connected = ref(false);
const connecting = ref(false);
const error = ref(null);
const messages = ref([]);
const subscribedTopics = ref([]);
-
// 事件回调
const connectCallbacks: (() => void)[] = [];
const messageCallbacks: ((message: MqttMessage) => void)[] = [];
const errorCallbacks: ((err: Error) => void)[] = [];
const disconnectCallbacks: (() => void)[] = [];
-
// 修复:移除无用的p0参数,connect方法不接受回调(通过onConnect注册)
const connect = () => {
if (connected.value || connecting.value) return;
connecting.value = true;
error.value = null;
-
+
try {
- // 创建客户端实例(添加协议参数)
client = new Paho.Client(
- MQTT_CONFIG.host,
+ MQTT_CONFIG.host,
MQTT_CONFIG.port,
MQTT_CONFIG.clientId
);
-
+
// 设置连接选项
const connectOptions: Paho.ConnectOptions = {
userName: MQTT_CONFIG.username,
@@ -71,7 +70,8 @@ export function useMqtt() {
cleanSession: MQTT_CONFIG.cleanSession,
keepAliveInterval: MQTT_CONFIG.keepAliveInterval,
reconnect: MQTT_CONFIG.reconnect,
-
+ useSSL: MQTT_CONFIG.protocol === 'wss', // 关键:根据协议自动启用 SSL
+
// 连接成功回调
onSuccess: () => {
console.log('MQTT连接成功');
@@ -79,7 +79,7 @@ export function useMqtt() {
connecting.value = false;
connectCallbacks.forEach(cb => cb()); // 触发所有连接成功回调
},
-
+
// 连接失败回调
onFailure: (err) => {
console.error('MQTT连接失败:', err);
@@ -89,7 +89,7 @@ export function useMqtt() {
errorCallbacks.forEach(cb => cb(error.value!));
}
};
-
+
// 设置客户端回调
client.onConnectionLost = (responseObject) => {
if (responseObject.errorCode !== 0) {
@@ -97,12 +97,12 @@ export function useMqtt() {
error.value = new Error(responseObject.errorMessage || '连接丢失');
errorCallbacks.forEach(cb => cb(error.value!));
}
-
+
connected.value = false;
connecting.value = false;
disconnectCallbacks.forEach(cb => cb());
};
-
+
// 消息接收回调
client.onMessageArrived = (message) => {
const newMessage: MqttMessage = {
@@ -112,11 +112,11 @@ export function useMqtt() {
retained: message.retained,
time: new Date()
};
-
+
messages.value.push(newMessage);
messageCallbacks.forEach(cb => cb(newMessage));
};
-
+
// 连接服务器
client.connect(connectOptions);
} catch (err) {
@@ -127,18 +127,18 @@ export function useMqtt() {
errorCallbacks.forEach(cb => cb(error.value!));
}
};
-
+
// 断开连接
const disconnect = () => {
if (!client || !connected.value) return;
-
+
client.disconnect();
client = null;
connected.value = false;
subscribedTopics.value = [];
disconnectCallbacks.forEach(cb => cb());
};
-
+
// 订阅主题
const subscribe = (topic: string, options: SubscribeOptions): Promise => {
return new Promise((resolve, reject) => {
@@ -146,12 +146,12 @@ export function useMqtt() {
reject(new Error('未连接到MQTT服务器'));
return;
}
-
+
if (subscribedTopics.value.includes(topic)) {
resolve();
return;
}
-
+
client.subscribe(topic, {
qos: options.qos,
onSuccess: () => {
@@ -166,7 +166,7 @@ export function useMqtt() {
});
});
};
-
+
// 取消订阅
const unsubscribe = (topic: string): Promise => {
return new Promise((resolve, reject) => {
@@ -174,12 +174,12 @@ export function useMqtt() {
reject(new Error('未连接到MQTT服务器'));
return;
}
-
+
if (!subscribedTopics.value.includes(topic)) {
resolve();
return;
}
-
+
client.unsubscribe(topic, {
onSuccess: () => {
console.log(`取消订阅主题成功: ${topic}`);
@@ -193,7 +193,7 @@ export function useMqtt() {
});
});
};
-
+
// 发布消息
const publish = (
topic: string,
@@ -205,47 +205,47 @@ export function useMqtt() {
reject(new Error('未连接到MQTT服务器'));
return;
}
-
+
if (!topic) {
reject(new Error('主题不能为空'));
return;
}
-
+
const message = new Paho.Message(
typeof payload === 'string' ? payload : payload.toString()
);
-
+
message.destinationName = topic;
message.qos = options.qos;
message.retained = options.retained ?? false;
-
+
client.send(message);
resolve();
});
};
-
+
// 事件注册
const onConnect = (callback: () => void) => {
connectCallbacks.push(callback);
};
-
+
const onMessage = (callback: (message: MqttMessage) => void) => {
messageCallbacks.push(callback);
};
-
+
const onError = (callback: (err: Error) => void) => {
errorCallbacks.push(callback);
};
-
+
const onDisconnect = (callback: () => void) => {
disconnectCallbacks.push(callback);
};
-
+
// 组件卸载时断开连接
onUnmounted(() => {
disconnect();
});
-
+
return {
connected,
connecting,
diff --git a/src/views/system/user/index.vue b/src/views/system/user/index.vue
index d12aeed..eb659af 100644
--- a/src/views/system/user/index.vue
+++ b/src/views/system/user/index.vue
@@ -5,22 +5,14 @@
-
+
-
+
@@ -31,24 +23,19 @@
-
+
-
-
+
-
+
搜索
@@ -63,15 +50,18 @@
- 新增
+ 新增
-
+
修改
-
+
删除
@@ -79,32 +69,39 @@
更多
-
+
下载模板
- 导入数据
- 导出数据
+ 导入数据
+ 导出数据
-
+
-
-
-
-
+
+
+
+
-
+
@@ -117,129 +114,85 @@
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- {{ dict.label }}
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ {{ dict.label
+ }}
+
@@ -254,18 +207,9 @@
-
+
@@ -274,7 +218,8 @@
是否更新已经存在的用户数据
仅允许导入xls、xlsx格式文件。
-
下载模板
+
下载模板
@@ -294,11 +239,9 @@ import { UserForm, UserQuery, UserVO } from '@/api/system/user/types';
import { DeptTreeVO, DeptVO } from '@/api/system/dept/types';
import { RoleVO } from '@/api/system/role/types';
import { PostQuery, PostVO } from '@/api/system/post/types';
-import { treeselect } from '@/api/system/dept';
import { globalHeaders } from '@/utils/request';
import { to } from 'await-to-js';
import { optionselect } from '@/api/system/post';
-import { hasPermi } from '@/directive/permission';
import { checkPermi } from '@/utils/permission';
const router = useRouter();
@@ -362,10 +305,7 @@ const initFormData: UserForm = {
nickName: undefined,
password: '',
phonenumber: undefined,
- email: undefined,
- sex: undefined,
status: '0',
- remark: '',
postIds: [],
roleIds: []
};
@@ -402,13 +342,6 @@ const initData: PageData = {
},
{ pattern: /^[^<>"'|\\]+$/, message: '不能包含非法字符:< > " \' \\ |', trigger: 'blur' }
],
- email: [
- {
- type: 'email',
- message: '请输入正确的邮箱地址',
- trigger: ['blur', 'change']
- }
- ],
phonenumber: [
{
pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,