From e00da17ae2adaecf1cdf2d5128e357a50607bc90 Mon Sep 17 00:00:00 2001
From: 罗广辉 <guanghui.luo@foxmail.com>
Date: Fri, 27 Feb 2026 08:40:37 +0800
Subject: [PATCH] feat: 权限获取调整
---
uniapps/work-app/src/utils/websocket.js | 40 ++++++++++++++++++++++++----------------
1 files changed, 24 insertions(+), 16 deletions(-)
diff --git a/uniapps/work-app/src/utils/websocket.js b/uniapps/work-app/src/utils/websocket.js
index 9706abd..c9a5a2a 100644
--- a/uniapps/work-app/src/utils/websocket.js
+++ b/uniapps/work-app/src/utils/websocket.js
@@ -1,10 +1,11 @@
-// WebSocket服务管理
+import { useGlobalWS } from "@/hooks/useGlobalWS.js";// WebSocket服务管理
class WebSocketService {
constructor() {
this.socketTask = null
this.connected = false
this.url = ''
this.userId = ''
+ this.token = ''
this.onMessageCallback = null
this.onOpenCallback = null
this.onCloseCallback = null
@@ -13,14 +14,15 @@
}
// 初始化WebSocket连接
- init(userId, url) {
+ init(userId, url, token) {
if (!url) {
console.error('WebSocket初始化失败:缺少url')
return
}
this.userId = userId || ''
- this.url = url + encodeURIComponent(this.userId)
+ this.token = token || ''
+ this.url = url
this.connect()
}
@@ -29,15 +31,15 @@
try {
// 关闭现有连接
this.close()
-
// 使用uni-app的WebSocket API
this.socketTask = uni.connectSocket({
url: this.url,
+ protocols: [this.token],
success: () => {
- console.log('WebSocket连接已请求')
+ // console.log('WebSocket连接已请求')
},
fail: (error) => {
- console.error('WebSocket连接请求失败:', error)
+ // console.error('WebSocket连接请求失败:', error)
if (this.onErrorCallback) {
this.onErrorCallback(error)
}
@@ -47,7 +49,7 @@
// 监听连接打开
this.socketTask.onOpen(() => {
this.connected = true
- console.log('WebSocket已连接,userId:', this.userId)
+ // console.log('WebSocket已连接,userId:', this.userId)
this.startPing()
if (this.onOpenCallback) {
this.onOpenCallback()
@@ -57,7 +59,7 @@
// 监听连接关闭
this.socketTask.onClose(() => {
this.connected = false
- console.log('WebSocket已关闭')
+ // console.log('WebSocket已关闭')
this.stopPing()
if (this.onCloseCallback) {
this.onCloseCallback()
@@ -66,7 +68,7 @@
// 监听连接错误
this.socketTask.onError((error) => {
- console.error('WebSocket错误:', error)
+ // console.error('WebSocket错误:', error)
this.connected = false
this.stopPing()
if (this.onErrorCallback) {
@@ -78,16 +80,16 @@
this.socketTask.onMessage((res) => {
try {
const message = JSON.parse(res.data)
- console.log('收到WebSocket消息:', message)
+ // console.log('收到WebSocket消息:', message)
if (this.onMessageCallback) {
this.onMessageCallback(message)
}
} catch (error) {
- console.error('解析WebSocket消息失败:', error)
+ // console.error('解析WebSocket消息失败:', error)
}
})
} catch (error) {
- console.error('WebSocket连接失败:', error)
+ // console.error('WebSocket连接失败:', error)
if (this.onErrorCallback) {
this.onErrorCallback(error)
}
@@ -112,10 +114,11 @@
this.socketTask.send({
data: JSON.stringify(message),
success: () => {
- console.log('WebSocket消息发送成功:', message)
+ // console.log('WebSocket消息发送成功:', message)
+ useGlobalWS();
},
fail: (error) => {
- console.error('WebSocket消息发送失败:', error)
+ // console.error('WebSocket消息发送失败:', error)
}
})
}
@@ -146,10 +149,10 @@
code: 1000,
reason: '正常关闭',
success: () => {
- console.log('WebSocket已关闭')
+ // console.log('WebSocket已关闭')
},
fail: (error) => {
- console.error('WebSocket关闭失败:', error)
+ // console.error('WebSocket关闭失败:', error)
}
})
this.socketTask = null
@@ -163,6 +166,11 @@
this.onMessageCallback = callback
}
+ // 获取当前消息回调(用于检查是否已设置)
+ getOnMessageCallback() {
+ return this.onMessageCallback
+ }
+
// 设置连接打开回调
setOnOpenCallback(callback) {
this.onOpenCallback = callback
--
Gitblit v1.9.3