From 4794dcd8c41b42476934a2a5ca85647dddf5044b Mon Sep 17 00:00:00 2001
From: chenhaozhe <cgz@hz-kingdee.com>
Date: 星期三, 14 一月 2026 22:20:21 +0800
Subject: [PATCH] 新增消息推送 检验单据 添加 仓位扫码功能

---
 utils/WebSocketServices.js |   69 +++++++++++++++++++++++++---------
 1 files changed, 51 insertions(+), 18 deletions(-)

diff --git a/utils/WebSocketServices.js b/utils/WebSocketServices.js
index 4a86605..6e5bdb5 100644
--- a/utils/WebSocketServices.js
+++ b/utils/WebSocketServices.js
@@ -1,44 +1,60 @@
-import { CommonUtils } from "@/utils/common.js";
+import {
+    CommonUtils
+} from "@/utils/common.js";
 
 class WebSocketServices {
     constructor() {
         this.wsInstance = null; // WebSocket 瀹炰緥
         this.isConnecting = false; // 閬垮厤骞跺彂閲嶈繛
         this.isConnected = false; // 閬垮厤骞跺彂閲嶈繛
-        this.wsUrl = CommonUtils.httpFormatWs() 
+        this.wsUrl = CommonUtils.httpFormatWs()
     }
-    
+
     // 寤虹珛WebSocket杩炴帴
-    createConnect(userId) { // 浣跨敤鐢ㄦ埛鏍囪瘑 浣滀负鍚庣杩炴帴鐨勫嚟鎹�
-       
-        if(this.isConnecting || !userId || this.isConnected) {
+    createConnect(userId, userName) { // 浣跨敤鐢ㄦ埛鏍囪瘑 浣滀负鍚庣杩炴帴鐨勫嚟鎹�
+        console.log('wsUrl: ', this.wsUrl);
+        console.log('userId: ', userId);
+        console.log('userName: ', userName);
+        if (this.isConnecting || !userId || this.isConnected) {
             return
         }
-        console.log('wsUrl: ',this.wsUrl);
-        console.log('userId: ',userId);
         this.wsInstance = uni.connectSocket({
-            url: this.wsUrl,
-            header: {
-                "X-User-Id": userId
-            },
+            url: this.wsUrl + `?userId=${userId}&userName=${encodeURIComponent(userName)}`,
             success() {
                 this.isConnecting = true
             }
         })
-        
+
         // 鐩戝惉濂楁帴瀛楄繛鎺ュ缓绔�
         uni.onSocketOpen((res) => {
             console.log('[webSocket]: 濂楁帴瀛楄繛鎺ュ缓绔嬫垚鍔�');
             this.isConnecting = false
             this.isConnected = true
-            console.log('res: ',res);
+            console.log('res: ', res);
             this.wsInstance = res.socketTask
+        })
+
+        uni.onSocketMessage((res) => {
+            let message = JSON.parse(res.data)
+            console.log('message: ', message);
+            if (message.Type == 'Message') {
+                // 娑堟伅淇″彿
+                let content = JSON.parse(message.Content)
+                console.log('content: ', content);
+
+                this.showTaskTip(`鎮ㄦ湁${content.length}鏉℃秷鎭渶瑕佸鐞�!`)
+            } else if (message.Type == 'ping') {
+                // 蹇冭烦淇″彿
+                uni.sendSocketMessage({
+                    data: "pong"
+                })
+            }
         })
     }
 
     // 閲嶈繛
     reConnect(reCount = 1, limit = 3) {
-        if(reCount > limit) {
+        if (reCount > limit) {
             uni.showToast({
                 icon: 'none',
                 title: `瓒呭嚭鏈�澶ч噸杩炴鏁般�傚凡閫�鍑鸿繛鎺
@@ -50,9 +66,9 @@
             icon: 'none',
             title: `姝e湪灏濊瘯閲嶈繛锛岄噸杩炴鏁� ${reCount}`
         })
-        
-        reConnect(reCount+1, limit)
-        
+
+        reConnect(reCount + 1, limit)
+
         uni.hideToast()
     }
 
@@ -60,6 +76,23 @@
     disConnect() {
 
     }
+
+    showTaskTip(Content) {
+        console.log('Content: ', Content);
+        // #ifdef APP-PLUS || APP
+        console.log('Content2: ', Content);
+        let content = Content;
+        let options = {
+            title: "閲嶈閫氱煡",
+            cover: true, // 鏄惁瑕嗙洊涓婁竴娆$殑閫氱煡
+            when: new Date() // 閫氱煡鏄剧ず鏃堕棿
+        };
+        // TODO 璺宠浆鍒版寚瀹氶〉
+        let payload = JSON.stringify({});
+
+        plus.push.createMessage(content, payload, options);
+        // #endif
+    }
 }
 
 export default new WebSocketServices()
\ No newline at end of file

--
Gitblit v1.9.1