ZaiZai vor 2 Jahren
Ursprung
Commit
3354b65fed
2 geänderte Dateien mit 58 neuen und 6 gelöschten Zeilen
  1. 57 5
      App.vue
  2. 1 1
      pages.json

+ 57 - 5
App.vue

@@ -3,12 +3,13 @@
     import website from "@/config/index";
     import userApi from '~api/user/index';
     import {getStorage, setStorage} from "@/utils/storage";
-    import {calcDate, getObjValue, isNullES} from "js-fast-way";
+    import {calcDate, getObjValue, isNullES, isString} from "js-fast-way";
+    import {getWssApiUrl} from "@/config/envApi";
+
 	export default {
         data() {
             return {
-                userInfo: {},
-                refreshLock: false,
+                userInfo: {}, refreshLock: false, isSocket: false
             }
         },
 		onLaunch: function() {
@@ -24,6 +25,7 @@
             const store = useAppStore()
             this.userInfo = store.userInfo
             this.getRefreshToken()
+            this.setInitSocket()
         },
         methods:{
             //刷新token
@@ -32,7 +34,7 @@
                 setInterval(() => {
                     const token = getStorage('token', true)
                     const date = calcDate(token.datetime, new Date().getTime())
-                    if (isNullES(date)) return
+                    if (isNullES(date)) return false;
                     if (date.seconds >= website.tokenTime && !this.refreshLock) {
                         this.refreshLock = true
                         console.log('刷新token')
@@ -66,7 +68,57 @@
                 } else {
                     return Promise.reject(res)
                 }
-            }
+            },
+            //长连接
+            setInitSocket() {
+                const _this = this
+                const timeId = setInterval(() => {
+                    if (_this.isSocket) {
+                        clearInterval(timeId)
+                    } else {
+                        _this.setAppSocket(timeId)
+                    }
+                }, 3000)
+            },
+            setAppSocket() {
+                const {user_id} = this.userInfo
+                if (user_id) this.initWebSocket(user_id)
+            },
+            initWebSocket(user_id) {
+                const _this = this
+                uni.connectSocket({
+                    url: getWssApiUrl() + user_id,
+                    complete: ()=> {
+                        console.log('websocket链接成功')
+                    }
+                });
+                uni.onSocketOpen((res) => {
+                    _this.isSocket = true
+                    console.log('WebSocket连接已打开!');
+                    _this.sendSocketMessage();
+                });
+                uni.onSocketError((res) => {
+                    _this.isSocket = false
+                    console.log('WebSocket连接打开失败,请检查!');
+                });
+                uni.onSocketClose((res) => {
+                    _this.isSocket = false
+                    console.log('WebSocket 已关闭!');
+                    //关闭后在连接
+                    //_this.setInitSocket()
+                });
+                //收到的消息
+                uni.onSocketMessage(({data}) => {
+                    const countData = isString(data) ? JSON.parse(data) : {}
+                    console.log(countData)
+                });
+            },
+            sendSocketMessage() {
+                const store = useAppStore()
+                uni.sendSocketMessage({
+                    data: store.projectId + ',' + store.contractId
+                });
+            },
         }
 	}
 </script>

+ 1 - 1
pages.json

@@ -156,7 +156,7 @@
     "tabBar": {
         "color": "#8E8E93",
         "selectedColor": "#554D84",
-        "blurEffect":"extralight",
+        "blurEffect": "extralight",
         "backgroundColor": "rgba(255,255,255,0.1)",
         "borderStyle": "black",
         "list": [