chris 3 lat temu
rodzic
commit
cef681a2a5

+ 7 - 1
src/store/modules/common.js

@@ -17,6 +17,7 @@ export default {
     // 主入口标签页
     mainTabs: [],
     mainTabsActiveName: '',
+    // 审批状态
     approveStates: [
       {
         code: null, value: '全部'
@@ -36,7 +37,9 @@ export default {
       {
         code: '4', value: '审批不通过'
       }
-    ]
+    ],
+    // 消息集合
+    msgCollection: []
   },
   mutations: {
     updateDocumentClientHeight (state, height) {
@@ -65,6 +68,9 @@ export default {
     },
     updateMainTabsActiveName (state, name) {
       state.mainTabsActiveName = name
+    },
+    updateMsgCollection (state, lst) {
+      state.msgCollection = lst
     }
   }
 }

+ 7 - 9
src/store/modules/websocket.js

@@ -1,7 +1,7 @@
 export default {
   namespaced: true,
   state: {
-    websock: null,
+    websock: {},
     url: '',
     lockReconnect: false, // 是否真正建立连接
     timeout: 30 * 1000, // 30秒一次心跳
@@ -18,7 +18,7 @@ export default {
   mutations: {
     WEBSOCKET_INIT (state, url) {
       if (!state || !url) return
-      const that = this
+      let that = this
       state.websock = new WebSocket(url)
       state.url = url
       state.websock.onopen = () => {
@@ -34,15 +34,13 @@ export default {
           return
         }
         state.message = callBack.data
-        // state.message = Object.assign([], callBack.data)
-        // this.$store.state.websocket.message = JSON.parse(JSON.stringify(callBack.data))
       }
       state.websock.onerror = () => { // e错误
-        // console.log(e)
+        console.log('onerror')
         that.commit('websocket/reconnect')
       }
       state.websock.onclose = () => { // e关闭
-        // console.log(e)
+        console.log('onclose')
         that.commit('websocket/reconnect')
       }
     },
@@ -57,7 +55,7 @@ export default {
       state.lockReconnect = true
       // 没连接上会一直重连,设置延迟避免请求过多
       state.timeoutnum && clearTimeout(state.timeoutnum)
-      state.timeoutnum = setTimeout(function () {
+      state.timeoutnum = setTimeout(() => {
         // 新连接
         that.commit('websocket/WEBSOCKET_INIT', state.url)
         state.lockReconnect = false
@@ -75,7 +73,7 @@ export default {
       let that = this
       state.timeoutObj && clearTimeout(state.timeoutObj)
       state.serverTimeoutObj && clearTimeout(state.serverTimeoutObj)
-      state.timeoutObj = setTimeout(function () {
+      state.timeoutObj = setTimeout(() => {
         // 这里发送一个心跳,后端收到后,返回一个心跳消息,
         // console.log(state.websock)
         if (state.websock.readyState === 1) { // 如果连接正常
@@ -83,7 +81,7 @@ export default {
         } else { // 否则重连
           that.commit('websocket/reconnect')
         }
-        state.serverTimeoutObj = setTimeout(function () {
+        state.serverTimeoutObj = setTimeout(() => {
           // 超时关闭
           state.websock.close()
         }, state.timeout)

+ 42 - 29
src/views/common/msg.vue

@@ -24,12 +24,13 @@
           return
         }
         const h = this.$createElement
-        let json = JSON.parse(data)
-        console.log('json = ' + JSON.stringify(json))
-        if (json) {
-          this.$emit('noticeAdded', json.length)
-          if (json.length <= 5) {
-            json.forEach((item) => {
+        let newMsgCollections = JSON.parse(data)
+        console.log('newMsgCollections = ' + JSON.stringify(newMsgCollections))
+        if (newMsgCollections) {
+          this.updateMsgCollections(newMsgCollections)
+          this.$emit('noticeAdded', this.$store.state.common.msgCollection.length)
+          if (newMsgCollections.length <= 5) {
+            newMsgCollections.forEach((item) => {
               this.$notify({
                 title: item.title,
                 message: h('div', {}, [
@@ -41,30 +42,32 @@
             })
           }
         }
-      // const newmsg = JSON.parse(newstr)
-      // if (newstr && newstr.indexOf('[') === -1) {
-      //   // 收到信消息
-      //   this.$notification.open({
-      //     message: newmsg.objName,
-      //     description: newmsg.title
-      //   })
-        // // 刷新消息
-        // this.$store.dispatch('websocket/WEBSOCKET_SEND', 'refreshMessage')
-      // } else {
-      //   this.noticeData = []
-      //   this.messageData = []
-      //   this.msgCount = newmsg.length
-      //   for (var i = 0; i < newmsg.length; i++) {
-      //     var m = newmsg[i]
-      //     m.createDate = m.objName + ' · ' + this.nowTimeAgo(m.createDate)
-      //     if (m.messageType.indexOf('Notice') !== -1) {
-      //       this.noticeData.push(m)
-      //     }
-      //     if (m.messageType.indexOf('Message') !== -1) {
-      //       this.messageData.push(m)
-      //     }
-      //   }
+        // 刷新消息
+        this.$store.dispatch('websocket/WEBSOCKET_SEND', 'refreshMessage')
+    // const newmsg = JSON.parse(newstr)
+    // if (newstr && newstr.indexOf('[') === -1) {
+    //   // 收到信消息
+    //   this.$notification.open({
+    //     message: newmsg.objName,
+    //     description: newmsg.title
+    //   })
+      // // 刷新消息
+      // this.$store.dispatch('websocket/WEBSOCKET_SEND', 'refreshMessage')
+    // } else {
+    //   this.noticeData = []
+    //   this.messageData = []
+    //   this.msgCount = newmsg.length
+    //   for (var i = 0; i < newmsg.length; i++) {
+    //     var m = newmsg[i]
+    //     m.createDate = m.objName + ' · ' + this.nowTimeAgo(m.createDate)
+    //     if (m.messageType.indexOf('Notice') !== -1) {
+    //       this.noticeData.push(m)
+    //     }
+    //     if (m.messageType.indexOf('Message') !== -1) {
+    //       this.messageData.push(m)
+    //     }
     //   }
+  //   }
       }
     },
     // mounted () {
@@ -105,6 +108,16 @@
         } else {
           return '未知消息类型'
         }
+      },
+      // 更新通知列表
+      updateMsgCollections (newCollections) {
+        if (!newCollections || newCollections.length === 0) return
+        newCollections.forEach((item) => {
+          // 判断是否包含当前通知
+          if (this.$store.state.common.msgCollection.findIndex(item1 => item1.noticeId === item.noticeId) === -1) {
+            this.$store.state.common.msgCollection.push(item)
+          }
+        })
       }
     }
   }

+ 4 - 0
src/views/main.vue

@@ -59,6 +59,10 @@
       orgId: {
         get () { return this.$store.state.user.orgId },
         set (val) { this.$store.commit('user/updateOrgId', val) }
+      },
+      msgCollection: {
+        get () { return this.$store.state.common.msgCollection },
+        set (val) { this.$store.commit('common/updateMsgCollection', val) }
       }
     },
     created () {

+ 2 - 1
src/views/modules/msg-center/notice.vue

@@ -118,7 +118,8 @@
       },
       // 获取数据列表
       getNoticeList () {
-        let msg = JSON.parse(this.$store.state.websocket.message)
+        console.log('msg = ' + JSON.stringify(this.$store.state.common.msgCollection))
+        let msg = this.$store.state.common.msgCollection
         if (!msg) return
         this.dataList = msg
         if (this.dataForm.title) {