Merge branch 'zxrapp' into 'master'

7.24张祥瑞

See merge request cxcxt/jeecguniapp!29
This commit is contained in:
张 祥瑞 2025-07-24 09:39:10 +08:00
commit 4543e9d5d1
6 changed files with 348 additions and 258 deletions

View File

@ -7,7 +7,9 @@ VITE_SHOW_SOURCEMAP = true
# 是否启用读取配置文件 min
#VITE_WEBAPP = 'D://opt//AppUpdateTest'
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://szcx.zyyt.sinopec.com/jeecg-boot'

View File

@ -1,140 +1,156 @@
// @ts-nocheck
import { log } from 'console'
import { randomString } from './uitls'
import { useUserStore } from '@/store/user'
const baseUrl = import.meta.env.VITE_SERVER_BASEURL
class socket {
constructor() {
this.socketUrl = baseUrl
this.socketStart = false
this.socketType = ''
this.monitorSocketError()
this.monitorSocketClose()
this.socketReceive()
}
init(socket_type, callback?) {
constructor() {
console.log(77777777777777)
this.socketUrl = baseUrl
this.socketStart = false
this.socketType = ''
this.monitorSocketError()
this.monitorSocketClose()
this.socketReceive()
}
init(socket_type, callback ?) {
const userStore = useUserStore()
const _this = this
if (baseUrl) {
if (this.socketStart) {
console.log('webSocket已经启动了')
} else {
_this.socketType = socket_type
let url =
this.socketUrl.replace('https://', 'wss://').replace('http://', 'ws://') +
'/' +
socket_type +
'/' +
userStore.userInfo.userid +
'_app'
if (socket_type == 'eoaNewChatSocket') {
let randomMessageId = randomString(6)
url =
this.socketUrl.replace('https://', 'wss://').replace('http://', 'ws://') +
'/eoaNewChatSocket/' +
userStore.userInfo.userid +
'/' +
randomMessageId
}
console.log('启动this.socketUrl连接地址', url)
// update-begin-author:taoyan date:20220422 for:v2.4.6 websocket #3278
const _this = this
if (baseUrl) {
if (this.socketStart) {
console.log('webSocket已经启动了')
} else {
_this.socketType = socket_type
let url =
this.socketUrl.replace('https://', 'wss://').replace('http://', 'ws://') +
'/' +
socket_type +
'/' +
userStore.userInfo.userid +
'_app'
if (socket_type == 'eoaNewChatSocket') {
let randomMessageId = randomString(6)
url =
this.socketUrl.replace('https://', 'wss://').replace('http://', 'ws://') +
'/eoaNewChatSocket/' +
userStore.userInfo.userid +
'/' +
randomMessageId
}
console.log('启动this.socketUrl连接地址', url)
// update-begin-author:taoyan date:20220422 for:v2.4.6 websocket #3278
let token = userStore.userInfo.token
uni.connectSocket({
url: url,
method: 'GET',
protocols: [token],
})
// update-end-author:taoyan date:20220422 for: v2.4.6 websocket #3278
uni.onSocketOpen((res) => {
this.socketStart = true
callback && callback()
console.log('WebSocket连接已打开')
})
/* setTimeout(() => {
_this.getHeartbeat();
}, 5000); */
}
} else {
console.log('config/baseUrl socketUrl为空')
}
}
// Socket
send(data, callback) {
let token = userStore.userInfo.token
uni.connectSocket({
url: url,
method: 'GET',
protocols: [token],
})
// update-end-author:taoyan date:20220422 for: v2.4.6 websocket #3278
uni.onSocketOpen((res) => {
console.log("连接进来了--------53333333333323----")
this.socketStart = true
callback && callback()
console.log('WebSocket连接已打开')
})
/* setTimeout(() => {
_this.getHeartbeat();
}, 5000); */
}
} else {
console.log('config/baseUrl socketUrl为空')
}
}
// Socket
send(data, callback) {
const userStore = useUserStore()
const _this = this
if (userStore.userInfo.userid) {
data.userUid = userStore.userInfo.userid
}
console.log(data)
uni.sendSocketMessage({
data: JSON.stringify(data),
success: () => {
callback && callback(true)
},
fail: () => {
callback && callback(false)
},
})
}
// Socket
socketReceive() {
const _this = this
uni.onSocketMessage(function (res) {
console.log('APP:--》收到服务器内容:')
let data = JSON.parse(res.data)
// console.log('', data);
_this.acceptMessage && _this.acceptMessage(data)
})
}
// Socket
closeSocket() {
const _this = this
uni.closeSocket()
_this.socketStart = false
}
// Socket
monitorSocketClose() {
const _this = this
uni.onSocketClose(function (res) {
console.log('WebSocket 已关闭!')
_this.socketStart = false
setTimeout(function () {
_this.init(_this.socketType)
}, 3000)
})
}
// Socket
monitorSocketError() {
const _this = this
uni.onSocketError(function (res) {
_this.socketStart = false
console.log('WebSocket连接打开失败请检查')
})
}
//
getHeartbeat() {
const _this = this
if (userStore.userInfo.userid) {
data.userUid = userStore.userInfo.userid
}
console.log(data)
uni.sendSocketMessage({
data: JSON.stringify(data),
success: () => {
callback && callback(true)
},
fail: () => {
callback && callback(false)
},
})
}
// Socket
socketReceive() {
const _this = this
uni.onSocketMessage(function (res) {
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)
// console.log('', data);
_this.acceptMessage && _this.acceptMessage(data)
})
}
// Socket
closeSocket() {
console.log(9099999)
const _this = this
uni.closeSocket()
_this.socketStart = false
}
// Socket
monitorSocketClose() {
const _this = this
uni.onSocketClose(function (res) {
console.log('关闭222222222',res)
if (res.code = 1000) {
// STOMP socketTask.onMessage by
return;
}
console.log('WebSocket 已关闭!')
_this.socketStart = false
setTimeout(function () {
_this.init(_this.socketType)
}, 3000)
})
}
// Socket
monitorSocketError() {
const _this = this
uni.onSocketError(function (res) {
_this.socketStart = false
console.log('WebSocket连接打开失败请检查')
})
}
//
getHeartbeat() {
const userStore = useUserStore()
const _this = this
this.send(
{
type: '心跳',
userUid: userStore.userInfo.userid,
},
(val) => {
setTimeout(() => {
if (val) {
// _this.getHeartbeat();
} else {
if (!_this.socketStart) {
// _this.init();
}
}
}, 10000)
},
)
}
const _this = this
this.send(
{
type: '心跳',
userUid: userStore.userInfo.userid,
},
(val) => {
setTimeout(() => {
if (val) {
// _this.getHeartbeat();
} else {
if (!_this.socketStart) {
// _this.init();
}
}
}, 10000)
},
)
}
}
const mySocket = new socket()
export default mySocket
export default mySocket

View File

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

View File

@ -16,7 +16,7 @@
</view>
</view>
</view>
<!-- <view class="connection-section">
<!-- <view class="connection-section">
<button v-if="!connected" @click="connectWebSocket" class="connect-btn">
连接WebSocket
</button>
@ -54,10 +54,10 @@
listAllRegions,
Selecthomelist
} from '@/api/devicecontrol/officedevice';
import useWebSocketService from '@/api/devicecontrol/webSocketService'
// import useWebSocketService from '@/api/devicecontrol/webSocketService'
// 0717
const wsService = useWebSocketService()
// const wsService = useWebSocketService()
const unsubscribe = ref(null)
// WebSocket
const connected = ref(false);
@ -253,7 +253,7 @@
//
const handlesocketMessage = (jsonString) => {
console.log("1300-----",jsonString)
console.log("1300-----", jsonString)
try {
const messageObj = JSON.parse(jsonString);
@ -280,8 +280,8 @@
};
//------------------------websocket-----------
// const WS_URL = 'https://szcx.zyyt.sinopec.com/mqttprod/ws/simple';
// 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 = 'ws://10.75.15.246:8899/mqttprod/ws/simple';
const connectionStatus = ref('未连接');
// STOMP
@ -296,25 +296,146 @@
// WebSocket
const connectWebSocket = () => {
const WS_URL = 'https://szcx.zyyt.sinopec.com/mqttprod/ws/simple'
// const WS_URL = 'http://10.75.15.246:8899/mqttprod/ws/simple';
wsService.connectWebSocket(WS_URL, '/topic/messages')
}
console.log(123123)
if (connected.value) return;
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
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(() => {
loadtree();
@ -322,12 +443,20 @@
getmqttdata();
getofficehomelist();
})
onUnmounted(() => {
//
if (unsubscribe.value) {
unsubscribe.value()
}
// 使
onUnload(() => {
disconnectWebSocket();
// //
// 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>

View File

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

View File

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