diff --git a/pages/home/home.1.vue b/pages/home/home.1.vue
new file mode 100644
index 0000000..e1a4613
--- /dev/null
+++ b/pages/home/home.1.vue
@@ -0,0 +1,291 @@
+
+
+
+
+
+
+ {{userName}}
+
+ {{$t('home.message')}}
+
+
+
+ {{$t('home.exit')}}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/pages/home/home.vue b/pages/home/home.vue
index 32f9b78..342ebf4 100644
--- a/pages/home/home.vue
+++ b/pages/home/home.vue
@@ -40,6 +40,9 @@
export default {
data() {
return {
+ reconnectTimer: null,
+ websocket: null,
+ serverUrl: this.$store.getters.baseUrl,
userName: '',
// menuList: [
// {title: '库位管理', path: 'RF03', sonTree: [{title: '人工取货', path: '/pages/ftdl/man-get-goods'}, {title: '人工放货', path: '/pages/ftdl/man-load-goods'}, {title: '库存锁定/解锁', path: '/pages/ftdl/kc-manage'}]},
@@ -71,7 +74,88 @@
// }
// },
+ mounted() {
+ this.initWebSocket()
+ },
methods: {
+ initWebSocket() {
+ if (this.reconnectTimer) clearTimeout(this.reconnectTimer)
+ const wsHost = this.serverUrl.replace(/^https?:\/\//, '')
+ const sid = 'sid' + Date.now() + '_' + Math.random().toString(36).substr(2, 9)
+ const wsUrl = `ws://${wsHost}/webSocket/${sid}`
+ console.log(wsUrl, 666)
+
+ this.closeWebSocket()
+
+ // 使用 uni.connectSocket 创建连接
+ this.socketTask = uni.connectSocket({
+ url: wsUrl,
+ success: () => {
+ console.log('WebSocket 连接创建成功')
+ },
+ fail: (err) => {
+ console.error('WebSocket 连接创建失败:', err)
+ this.reconnectWebSocket()
+ }
+ })
+
+ // 监听 WebSocket 事件
+ this.socketTask.onOpen(() => {
+ console.log('WebSocket 连接已打开')
+ })
+
+ this.socketTask.onMessage((event) => {
+ try {
+ const res = JSON.parse(event.data)
+ this.handleWebSocketMessage(res)
+ } catch (error) {
+ console.error('WebSocket消息解析失败:', error)
+ }
+ })
+
+ this.socketTask.onError((error) => {
+ console.error('WebSocket 错误:', error)
+ uni.showToast({
+ title: '连接错误',
+ icon: 'none'
+ })
+ })
+
+ this.socketTask.onClose(() => {
+ console.log('WebSocket 连接已关闭')
+ this.reconnectWebSocket()
+ })
+ },
+
+ handleWebSocketMessage(res) {
+ console.log(res, 'res')
+ console.log(res.msg.data, 'res.msg.data')
+ uni.showToast({
+ title: res.msg.data,
+ icon: 'none'
+ })
+ },
+
+ closeWebSocket() {
+ if (this.socketTask) {
+ this.socketTask.close({
+ success: () => {
+ console.log('WebSocket 已关闭')
+ },
+ fail: (err) => {
+ console.error('关闭 WebSocket 失败:', err)
+ }
+ })
+ this.socketTask = null
+ }
+ },
+
+ reconnectWebSocket() {
+ if (this.reconnectTimer) clearTimeout(this.reconnectTimer)
+ this.reconnectTimer = setTimeout(() => {
+ this.initWebSocket()
+ }, 5000) // 5秒后重连
+ },
toPage1 (e) {
if (e.sonTree.length > 0) {
this.show = true
diff --git a/utils/websocket.js b/utils/websocket.js
new file mode 100644
index 0000000..bc7e1db
--- /dev/null
+++ b/utils/websocket.js
@@ -0,0 +1,99 @@
+let websock = null
+let messageCallback = null
+let errorCallback = null
+let wsUrl = ''
+
+// 接收ws后端返回的数据
+function websocketonmessage (e) {
+ messageCallback(JSON.parse(e.data))
+}
+
+/**
+ * 发起websocket连接
+ * @param {Object} agentData 需要向后台传递的参数数据
+ */
+function websocketSend (agentData) {
+ // 加延迟是为了尽量让ws连接状态变为OPEN
+ setTimeout(() => {
+ // 添加状态判断,当为OPEN时,发送消息
+ if (websock.readyState === websock.OPEN) { // websock.OPEN = 1
+ // 发给后端的数据需要字符串化
+ websock.send(JSON.stringify(agentData))
+ }
+ if (websock.readyState === websock.CLOSED) { // websock.CLOSED = 3
+ console.log('websock.readyState=3')
+ }
+ }, 500)
+}
+
+// 关闭ws连接
+function websocketclose (e) {
+ // e.code === 1000 表示正常关闭。 无论为何目的而创建, 该链接都已成功完成任务。
+ // e.code !== 1000 表示非正常关闭。
+ if (e && e.code !== 1000) {
+ uni.showToast({
+ title: 'server error',
+ icon: 'none'
+ })
+ errorCallback()
+ }
+}
+// 建立ws连接
+function websocketOpen (e) {
+ // console.log('ws连接成功')
+}
+
+// 初始化weosocket
+function initWebSocket () {
+ if (typeof (WebSocket) === 'undefined') {
+ uni.showToast({
+ title: '您的浏览器不支持WebSocket,无法获取数据',
+ icon: 'none'
+ })
+ return false
+ }
+ // ws请求完整地址
+ const requstWsUrl = wsUrl
+ websock = new WebSocket(requstWsUrl)
+
+ websock.onmessage = function (e) {
+ websocketonmessage(e)
+ }
+ websock.onopen = function () {
+ websocketOpen()
+ }
+ websock.onerror = function () {
+ }
+ websock.onclose = function (e) {
+ websocketclose(e)
+ }
+}
+
+/**
+ * 发起websocket请求函数
+ * @param {string} url ws连接地址
+ * @param {Object} agentData 传给后台的参数
+ * @param {function} successCallback 接收到ws数据,对数据进行处理的回调函数
+ * @param {function} errCallback ws连接错误的回调函数
+ */
+export function sendWebsocket (url, agentData, successCallback, errCallback) {
+ wsUrl = url
+ initWebSocket()
+ messageCallback = successCallback
+ errorCallback = errCallback
+ websocketSend(agentData)
+}
+
+/**
+ * 关闭websocket函数
+ */
+export function closeWebsocket (flag) {
+ if (flag) {
+ websock.close()
+ return
+ }
+ if (websock) {
+ websock.close() // 关闭websocket
+ websock.onclose() // 关闭websocket
+ }
+}