Merge remote-tracking branch 'remotes/origin/master'

This commit is contained in:
yangzhq68909 2025-07-24 11:02:11 +08:00
commit 8c08c1f3df
6 changed files with 348 additions and 258 deletions

View File

@ -7,7 +7,9 @@ VITE_SHOW_SOURCEMAP = true
# 是否启用读取配置文件 min # 是否启用读取配置文件 min
#VITE_WEBAPP = 'D://opt//AppUpdateTest' #VITE_WEBAPP = 'D://opt//AppUpdateTest'
VITE_WEBAPP = '' VITE_WEBAPP = ''
VITE_SERVER_BASEURL = 'http://10.75.15.249:8080/jeecg-boot' VITE_SERVER_BASEURL = 'http://10.75.15.246:8080/jeecg-boot'
#websocket连接url-测试用
VITE_WEBSOCKET_URL = 'ws://10.75.15.246:8899/mqttprod/ws/simple'
#VITE_SERVER_BASEURL = 'https://36.112.48.190/jeecg-boot' #VITE_SERVER_BASEURL = 'https://36.112.48.190/jeecg-boot'
#VITE_SERVER_BASEURL = 'https://szcx.zyyt.sinopec.com/jeecg-boot' #VITE_SERVER_BASEURL = 'https://szcx.zyyt.sinopec.com/jeecg-boot'

View File

@ -1,4 +1,5 @@
// @ts-nocheck // @ts-nocheck
import { log } from 'console'
import { randomString } from './uitls' import { randomString } from './uitls'
import { useUserStore } from '@/store/user' import { useUserStore } from '@/store/user'
@ -6,6 +7,7 @@ const baseUrl = import.meta.env.VITE_SERVER_BASEURL
class socket { class socket {
constructor() { constructor() {
console.log(77777777777777)
this.socketUrl = baseUrl this.socketUrl = baseUrl
this.socketStart = false this.socketStart = false
this.socketType = '' this.socketType = ''
@ -13,7 +15,7 @@ class socket {
this.monitorSocketClose() this.monitorSocketClose()
this.socketReceive() this.socketReceive()
} }
init(socket_type, callback?) { init(socket_type, callback ?) {
const userStore = useUserStore() const userStore = useUserStore()
const _this = this const _this = this
if (baseUrl) { if (baseUrl) {
@ -48,6 +50,7 @@ class socket {
}) })
// update-end-author:taoyan date:20220422 for: v2.4.6 websocket #3278 // update-end-author:taoyan date:20220422 for: v2.4.6 websocket #3278
uni.onSocketOpen((res) => { uni.onSocketOpen((res) => {
console.log("连接进来了--------53333333333323----")
this.socketStart = true this.socketStart = true
callback && callback() callback && callback()
console.log('WebSocket连接已打开') console.log('WebSocket连接已打开')
@ -82,7 +85,13 @@ class socket {
socketReceive() { socketReceive() {
const _this = this const _this = this
uni.onSocketMessage(function (res) { uni.onSocketMessage(function (res) {
console.log('APP:--》收到服务器内容:') console.log('APP:--》收到服务器内容:', res)
console.log('连接路径----', baseUrl)
if (res.data.startsWith('CONNECTED') || res.data.startsWith('MESSAGE') || res.data.startsWith('RECEIPT')) {
console.log('放行----------', res)
// STOMP socketTask.onMessage by
return;
}
let data = JSON.parse(res.data) let data = JSON.parse(res.data)
// console.log('', data); // console.log('', data);
_this.acceptMessage && _this.acceptMessage(data) _this.acceptMessage && _this.acceptMessage(data)
@ -90,6 +99,7 @@ class socket {
} }
// Socket // Socket
closeSocket() { closeSocket() {
console.log(9099999)
const _this = this const _this = this
uni.closeSocket() uni.closeSocket()
_this.socketStart = false _this.socketStart = false
@ -98,6 +108,12 @@ class socket {
monitorSocketClose() { monitorSocketClose() {
const _this = this const _this = this
uni.onSocketClose(function (res) { uni.onSocketClose(function (res) {
console.log('关闭222222222',res)
if (res.code = 1000) {
// STOMP socketTask.onMessage by
return;
}
console.log('WebSocket 已关闭!') console.log('WebSocket 已关闭!')
_this.socketStart = false _this.socketStart = false
setTimeout(function () { setTimeout(function () {

View File

@ -39,7 +39,7 @@
</view> </view>
</view> </view>
</view> </view>
<!-- <!--
<view class="connection-section"> <view class="connection-section">
<button v-if="!connected" @click="connectWebSocket" class="connect-btn"> <button v-if="!connected" @click="connectWebSocket" class="connect-btn">
连接WebSocket 连接WebSocket
@ -68,6 +68,13 @@
import { import {
ref ref
} from 'vue'; } from 'vue';
import {
onLaunch,
onShow,
onHide,
onLoad,
onReady
} from '@dcloudio/uni-app'
import { import {
gethomelist, gethomelist,
treeRootList, treeRootList,
@ -79,6 +86,7 @@
listAllRegions, listAllRegions,
Selecthomelist Selecthomelist
} from '@/api/devicecontrol/officedevice'; } from '@/api/devicecontrol/officedevice';
// import socketforleader from '@/common/socketforleader'
// WebSocket // WebSocket
const connected = ref(false); const connected = ref(false);
@ -100,6 +108,13 @@
url: url url: url
}); });
} }
//
onLaunch(() => {
console.log('初始化------')
})
/***-------------------------------开始数据处理------------------------**/ /***-------------------------------开始数据处理------------------------**/
const getmqttdata = async () => { const getmqttdata = async () => {
await getMqttPushClient().then(res => { await getMqttPushClient().then(res => {
@ -271,17 +286,18 @@
selectBydata.value = res.result; selectBydata.value = res.result;
console.log("selectBydata.value", selectBydata.value) console.log("selectBydata.value", selectBydata.value)
filteredFloors.value = groupByFloor(selectBydata) filteredFloors.value = groupByFloor(selectBydata)
console.log("数据分组-555---", filteredFloors.value[0].rooms) // console.log("-555---", filteredFloors.value[0].rooms)
// //
getAllswitchByids() getAllswitchByids()
} }
} }
//----------------end----------- //----------------end-----------
//---------------------------end------------- //---------------------------end-------------
// //
const handlesocketMessage = (jsonString) => { const handlesocketMessage = (jsonString) => {
console.log("1300--666666666666666---", jsonString)
try { try {
const messageObj = JSON.parse(jsonString); const messageObj = JSON.parse(jsonString);
@ -306,13 +322,12 @@
) )
})); }));
}; };
//------------------------websocket-----------
// WebSocket使/simple //------------------------websocket-----------
// const WS_URL = 'http://10.75.15.246:8899/mqttprod/ws/simple'; const WS_URL = 'wss://szcx.zyyt.sinopec.com/mqttprod/ws/simple';
const WS_URL = 'https://szcx.zyyt.sinopec.com/mqttprod/ws/simple'; // const WS_URL = 'ws://10.75.15.246:8899/mqttprod/ws/simple';
//
const connectionStatus = ref('未连接'); const connectionStatus = ref('未连接');
// STOMP // STOMP
const buildStompFrame = (command, headers = {}, body = '') => { const buildStompFrame = (command, headers = {}, body = '') => {
let frame = `${command}\n`; let frame = `${command}\n`;
@ -325,40 +340,34 @@
// WebSocket // WebSocket
const connectWebSocket = () => { const connectWebSocket = () => {
console.log(123123)
if (connected.value) return; if (connected.value) return;
console.log('正在连接WebSocket...');
connectionStatus.value = '连接中...'; connectionStatus.value = '连接中...';
//
if (socketTask) { if (socketTask) {
socketTask.close({}); socketTask.close({});
socketTask = null; socketTask = null;
} }
console.log(44444444)
// 2使uni.connectSocket API
socketTask = uni.connectSocket({ socketTask = uni.connectSocket({
url: WS_URL, url: WS_URL,
protocols: ['v12.stomp', 'v11.stomp'], // STOMP protocols: ['v12.stomp', 'v11.stomp'],
success: () => { success: () => {
console.log('开始连接WebSocket'); console.log('开始连接WebSocket');
}, },
fail: (err) => { fail: (err) => {
console.error('连接失败:', err); console.error('连接失败:', err);
connectionStatus.value = '连接失败'; connectionStatus.value = '连接失败';
messages.value.push({
type: 'error',
content: `连接失败: ${err.errMsg || '未知错误'}`,
timestamp: Date.now()
});
} }
}); });
console.log(55555555555)
// 3使uni.onSocketOpensocketTask.onOpen
uni.onSocketOpen((res) => { uni.onSocketOpen((res) => {
console.log(66666666)
console.log('WebSocket连接已打开', res); console.log('WebSocket连接已打开', res);
connected.value = true; connected.value = true;
connectionStatus.value = '连接'; connectionStatus.value = '连接中...';
// STOMP CONNECT // STOMP CONNECT
const connectFrame = buildStompFrame('CONNECT', { const connectFrame = buildStompFrame('CONNECT', {
@ -367,23 +376,15 @@
}); });
uni.sendSocketMessage({ uni.sendSocketMessage({
data: connectFrame, data: connectFrame
success: () => {
messages.value.push({
type: 'system',
content: '已发送STOMP连接请求',
timestamp: Date.now()
});
}
}); });
console.log("77777777777")
}); });
// //
socketTask.onMessage((res) => { socketTask.onMessage((res) => {
console.log("8888888888888888888")
const data = res.data; const data = res.data;
console.log('收到原始消息:', data);
// STOMP
const [commandLine, ...headerLines] = data.split('\n'); const [commandLine, ...headerLines] = data.split('\n');
const emptyLineIndex = headerLines.findIndex(line => line === ''); const emptyLineIndex = headerLines.findIndex(line => line === '');
const headers = {}; const headers = {};
@ -394,16 +395,11 @@
} }
const body = headerLines.slice(emptyLineIndex + 1).join('\n').replace(/\x00$/, ''); const body = headerLines.slice(emptyLineIndex + 1).join('\n').replace(/\x00$/, '');
console.log("999999999999999", res)
console.log("121212121212", commandLine)
switch (commandLine) { switch (commandLine) {
case 'CONNECTED': case 'CONNECTED':
connectionStatus.value = 'STOMP已连接'; connectionStatus.value = 'STOMP已连接';
messages.value.push({
type: 'system',
content: `STOMP协议连接成功版本: ${headers.version}`,
timestamp: Date.now()
});
// //
const subscribeFrame = buildStompFrame('SUBSCRIBE', { const subscribeFrame = buildStompFrame('SUBSCRIBE', {
id: 'sub-0', id: 'sub-0',
@ -411,56 +407,25 @@
}); });
socketTask.send({ socketTask.send({
data: subscribeFrame, data: subscribeFrame
success: () => {
messages.value.push({
type: 'system',
content: '已订阅消息主题',
timestamp: Date.now()
});
}
}); });
break; break;
case 'MESSAGE': case 'MESSAGE':
messages.value.push({
type: 'received',
content: `收到: ${body}`,
timestamp: Date.now()
});
handlesocketMessage(body); handlesocketMessage(body);
break; break;
case 'ERROR': case 'ERROR':
messages.value.push({ console.error('服务器错误:', body);
type: 'error',
content: `服务器错误: ${body}`,
timestamp: Date.now()
});
break; break;
default:
messages.value.push({
type: 'system',
content: `未知帧类型: ${commandLine}`,
timestamp: Date.now()
});
} }
}); });
//
//
// //
uni.onSocketError((err) => { uni.onSocketError((err) => {
console.error('WebSocket错误:', err); console.error('WebSocket错误:', err);
connectionStatus.value = '连接错误'; connectionStatus.value = '连接错误';
connected.value = false; connected.value = false;
messages.value.push({
type: 'error',
content: `连接错误: ${err.errMsg || '未知错误'}`,
timestamp: Date.now()
});
}); });
// //
@ -468,12 +433,6 @@
console.log('WebSocket连接已关闭', closeRes); console.log('WebSocket连接已关闭', closeRes);
connectionStatus.value = '已断开'; connectionStatus.value = '已断开';
connected.value = false; connected.value = false;
messages.value.push({
type: 'system',
content: `WebSocket连接已关闭: ${closeRes.code || ''} ${closeRes.reason || ''}`,
timestamp: Date.now()
});
}); });
}; };
@ -496,31 +455,19 @@
socketTask.send({ socketTask.send({
data: sendFrame, data: sendFrame,
success: () => { success: () => {
messages.value.push({
type: 'sent',
content: `发送: ${message.value}`,
timestamp: Date.now()
});
message.value = ''; message.value = '';
},
fail: (err) => {
messages.value.push({
type: 'error',
content: `发送失败: ${err.errMsg}`,
timestamp: Date.now()
});
} }
}); });
}; };
// WebSocket // WebSocket
const disconnectWebSocket = () => { const disconnectWebSocket = () => {
if (socketTask) { if (socketTask) {
// STOMP DISCONNECT // STOMP DISCONNECT
const disconnectFrame = buildStompFrame('DISCONNECT', { const disconnectFrame = buildStompFrame('DISCONNECT', {
receipt: 'disconnect-receipt' receipt: 'disconnect-receipt'
}); });
console.log(socketTask)
socketTask.send({ socketTask.send({
data: disconnectFrame, data: disconnectFrame,
success: () => { success: () => {
@ -528,14 +475,8 @@
code: 1000, code: 1000,
reason: '用户手动关闭', reason: '用户手动关闭',
success: () => { success: () => {
console.log('WebSocket已关闭');
connected.value = false; connected.value = false;
connectionStatus.value = '已断开'; connectionStatus.value = '已断开';
messages.value.push({
type: 'system',
content: '连接已安全关闭',
timestamp: Date.now()
});
} }
}); });
} }
@ -544,6 +485,7 @@
} }
onMounted(() => { onMounted(() => {
// disconnectWebSocket();
loadtree(); loadtree();
connectWebSocket(); connectWebSocket();
getmqttdata(); getmqttdata();

View File

@ -16,7 +16,7 @@
</view> </view>
</view> </view>
</view> </view>
<!-- <view class="connection-section"> <!-- <view class="connection-section">
<button v-if="!connected" @click="connectWebSocket" class="connect-btn"> <button v-if="!connected" @click="connectWebSocket" class="connect-btn">
连接WebSocket 连接WebSocket
</button> </button>
@ -54,10 +54,10 @@
listAllRegions, listAllRegions,
Selecthomelist Selecthomelist
} from '@/api/devicecontrol/officedevice'; } from '@/api/devicecontrol/officedevice';
import useWebSocketService from '@/api/devicecontrol/webSocketService' // import useWebSocketService from '@/api/devicecontrol/webSocketService'
// 0717 // 0717
const wsService = useWebSocketService() // const wsService = useWebSocketService()
const unsubscribe = ref(null) const unsubscribe = ref(null)
// WebSocket // WebSocket
const connected = ref(false); const connected = ref(false);
@ -253,7 +253,7 @@
// //
const handlesocketMessage = (jsonString) => { const handlesocketMessage = (jsonString) => {
console.log("1300-----",jsonString) console.log("1300-----", jsonString)
try { try {
const messageObj = JSON.parse(jsonString); const messageObj = JSON.parse(jsonString);
@ -280,8 +280,8 @@
}; };
//------------------------websocket----------- //------------------------websocket-----------
// const WS_URL = 'https://szcx.zyyt.sinopec.com/mqttprod/ws/simple'; const WS_URL = 'wss://szcx.zyyt.sinopec.com/mqttprod/ws/simple';
// const WS_URL = 'http://10.75.15.246:8899/mqttprod/ws/simple'; // const WS_URL = 'ws://10.75.15.246:8899/mqttprod/ws/simple';
const connectionStatus = ref('未连接'); const connectionStatus = ref('未连接');
// STOMP // STOMP
@ -296,25 +296,146 @@
// WebSocket // WebSocket
const connectWebSocket = () => { const connectWebSocket = () => {
const WS_URL = 'https://szcx.zyyt.sinopec.com/mqttprod/ws/simple' console.log(123123)
// const WS_URL = 'http://10.75.15.246:8899/mqttprod/ws/simple'; if (connected.value) return;
wsService.connectWebSocket(WS_URL, '/topic/messages')
connectionStatus.value = '连接中...';
if (socketTask) {
socketTask.close({});
socketTask = null;
} }
console.log(44444444)
socketTask = uni.connectSocket({
url: WS_URL,
protocols: ['v12.stomp', 'v11.stomp'],
success: () => {
console.log('开始连接WebSocket');
},
fail: (err) => {
console.error('连接失败:', err);
connectionStatus.value = '连接失败';
}
});
console.log(55555555555)
uni.onSocketOpen((res) => {
console.log(66666666)
console.log('WebSocket连接已打开', res);
connected.value = true;
connectionStatus.value = '连接中...';
// STOMP CONNECT
const connectFrame = buildStompFrame('CONNECT', {
'accept-version': '1.2',
'heart-beat': '10000,10000'
});
uni.sendSocketMessage({
data: connectFrame
});
});
//
socketTask.onMessage((res) => {
const data = res.data;
const [commandLine, ...headerLines] = data.split('\n');
const emptyLineIndex = headerLines.findIndex(line => line === '');
const headers = {};
for (let i = 0; i < emptyLineIndex; i++) {
const [key, value] = headerLines[i].split(':');
headers[key] = value;
}
const body = headerLines.slice(emptyLineIndex + 1).join('\n').replace(/\x00$/, '');
switch (commandLine) {
case 'CONNECTED':
connectionStatus.value = 'STOMP已连接';
//
const subscribeFrame = buildStompFrame('SUBSCRIBE', {
id: 'sub-0',
destination: '/topic/messages'
});
socketTask.send({
data: subscribeFrame
});
break;
case 'MESSAGE':
handlesocketMessage(body);
break;
case 'ERROR':
console.error('服务器错误:', body);
break;
}
});
//
uni.onSocketError((err) => {
console.error('WebSocket错误:', err);
connectionStatus.value = '连接错误';
connected.value = false;
});
//
uni.onSocketClose((closeRes) => {
console.log('WebSocket连接已关闭', closeRes);
connectionStatus.value = '已断开';
connected.value = false;
});
};
//
const sendMessage = () => {
if (!message.value.trim()) {
uni.showToast({
title: '消息不能为空',
icon: 'none'
});
return;
}
// STOMP SEND
const sendFrame = buildStompFrame('SEND', {
destination: '/app/chat',
'content-type': 'text/plain'
}, message.value);
socketTask.send({
data: sendFrame,
success: () => {
message.value = '';
}
});
};
// WebSocket // WebSocket
const disconnectWebSocket = () => { const disconnectWebSocket = () => {
wsService.disconnectWebSocket() if (socketTask) {
// STOMP DISCONNECT
const disconnectFrame = buildStompFrame('DISCONNECT', {
receipt: 'disconnect-receipt'
});
console.log(socketTask)
socketTask.send({
data: disconnectFrame,
success: () => {
socketTask.close({
code: 1000,
reason: '用户手动关闭',
success: () => {
connected.value = false;
connectionStatus.value = '已断开';
}
});
}
});
}
} }
//
unsubscribe.value = wsService.subscribe((message) => {
handlesocketMessage(message)
messages.value.push({
content: message,
type: 'received',
time: new Date().toLocaleTimeString()
})
})
onMounted(() => { onMounted(() => {
loadtree(); loadtree();
@ -322,12 +443,20 @@
getmqttdata(); getmqttdata();
getofficehomelist(); getofficehomelist();
}) })
onUnmounted(() => {
// onUnload(() => {
if (unsubscribe.value) { disconnectWebSocket();
unsubscribe.value() // //
} // uni.onSocketClose((closeRes) => {
// 使 // console.log('WebSocket', closeRes);
// connectionStatus.value = '';
// connected.value = false;
// // messages.value.push({
// // type: 'system',
// // content: `WebSocket: ${closeRes.code || ''} ${closeRes.reason || ''}`,
// // timestamp: Date.now()
// // });
// });
}) })
</script> </script>

View File

@ -64,8 +64,8 @@
//1455737215009562625 //1455737215009562625
//1455737513371377665 //1455737513371377665
//1358226526012129281 //1358226526012129281
console.log("当前登陆人信息000", userStore.userInfo.roles) // console.log("000", userStore.userInfo.roles)
console.log("当前登陆人信息222222222222", userStore.userInfo.roles.split(",")) // console.log("222222222222", userStore.userInfo.roles.split(","))
for (let a = 0; a < userStore.userInfo.roles.split(",").length; a++) { for (let a = 0; a < userStore.userInfo.roles.split(",").length; a++) {
console.log("userStore.userInfo.roles.split(", ")[a]", userStore.userInfo.roles.split(",")[a]) console.log("userStore.userInfo.roles.split(", ")[a]", userStore.userInfo.roles.split(",")[a])
@ -76,7 +76,7 @@
"1455737513371377665" || rolesid == "1455737513371377665" || rolesid ==
"1358226526012129281") { // "1358226526012129281") { //
isleader.value = true; isleader.value = true;
console.log("isleader-------", isleader.value) // console.log("isleader-------", isleader.value)
} else { } else {
isleader.value = false; isleader.value = false;
} }

View File

@ -94,6 +94,7 @@ const init = () => {
navTitle.value = params.fromUserName navTitle.value = params.fromUserName
chatto.value = chatObj.value.msgTo chatto.value = chatObj.value.msgTo
creatFriendSession(chatObj.value.msgTo) creatFriendSession(chatObj.value.msgTo)
console.log(7777777777777777777777777777)
onSocketOpen() onSocketOpen()
onSocketReceive() onSocketReceive()
getMsgList() getMsgList()