Эх сурвалжийг харах

feat: 调整到 msgbox 函数中判断通知

zhangyuhan 2 жил өмнө
parent
commit
04574d3049

+ 74 - 2
src/web/staticres/common-module/ad/js/msgbuoy.js

@@ -8,7 +8,7 @@
 
 var getMsgBuoyActive = {
   // 版本号
-  version: '0.0.1',
+  version: '0.0.2',
   platform: '', // work_bench、wx、app、pc
   msgids: '',
   floatDOM: '',
@@ -23,6 +23,7 @@ var getMsgBuoyActive = {
         this.getBuoyMsgAjax()
       }
     } else {
+      this.listenNotification()
       this.getBuoyMsgAjax()
     }
   },
@@ -384,8 +385,79 @@ var getMsgBuoyActive = {
     } else if (isAndroid) {
       return 'Android'
     }
-  }
+  },
+  /**
+   * @date 2023/1/4 消息通知改为 webscoket 获取并调用浏览器消息通知
+   */
+  listenNotification () {
+    /**
+     * 进行浏览器通知,判断上次通知是否与当前一致,一致则不进行重复通知
+     * @param datas - 信息数据
+     */
+    function sendNotification (datas) {
+      var waitShowMessageId = localStorage.getItem('notification-login-clear-info') || -1
+      if (waitShowMessageId === datas.id) {
+        return console.warn('重复消息ID,不进行重复通知')
+      }
+      try {
+        Notification.requestPermission(function(status) {
+          if(status === 'granted'){
+            localStorage.setItem('notification-login-clear-info', datas.id)
+            var notify = new Notification(datas.title,{
+              icon: '/images/t3_new.jpg',
+              body: datas.show_content || datas.content || ''
+            })
+            // 点击时桌面消息时触发
+            notify.onclick = function () {
+              try {
+                getMsgBuoyActive.istoReaded(datas.id)
+                notify.close()
+                switch (getMsgBuoyActive.platformOS()) {
+                  case 'pc':
+                    location.href = '/swordfish/frontPage/messageCenter/sess/index'
+                    break;
+                  case 'work_bench':
+                    window.open('/swordfish/frontPage/messageCenter/sess/index')
+                    break;
+                }
+              } catch (e) {
+                console.warn(e)
+              }
+            }
+          } else {
+            Notification.requestPermission()
+          }
+        })
+      } catch (e) {
+        console.warn('不支持通知')
+      }
+    }
 
+    // 定义 scoket 信息
+    var pushMessageScoket = {
+      url: "ws"+(!isIE9&&"https:"==document.location.protocol?"s":"")+"://"+window.location.host,
+      init: function () {
+        if (typeof CommonWebScoket === 'function') {
+          this._scoket = new CommonWebScoket(this.url + '/webscoket/getBuoyMsg', '', {
+            ajaxReq: function() {},
+            complete: function (data, type) {
+              try {
+                var pushMessage = JSON.parse(data)
+                if (pushMessage.id) {
+                  sendNotification(pushMessage)
+                }
+              } catch (e) {
+                console.warn(e)
+              }
+            }
+          }).init()
+        }
+      }
+    }
+
+    // 初始化
+    pushMessageScoket.init()
+  }
 }
 
 // 初始化

+ 3 - 65
src/web/staticres/js/login.js

@@ -451,71 +451,9 @@ function toReaded(ids, type, url) {
  */
 // 查看是否有新消息
 function checkCounts() {
-
-  /**
-   * 进行浏览器通知,判断上次通知是否与当前一致,一致则不进行重复通知
-   * @param datas - 信息数据
-   */
-  function sendNotification (datas) {
-    var waitShowMessageId = localStorage.getItem('notification-login-clear-info') || -1
-    if (waitShowMessageId === datas.id) {
-      return console.warn('重复消息ID,不进行重复通知')
-    }
-    try {
-      Notification.requestPermission(function(status) {
-        if(status === 'granted'){
-          localStorage.setItem('notification-login-clear-info', datas.id)
-          var notify = new Notification(datas.title,{
-            icon: '../images/t3_new.jpg',
-            body: datas.show_content || datas.content || ''
-          })
-          // 点击时桌面消息时触发
-          notify.onclick = function () {
-            try {
-              getMsgBuoyActive.istoReaded(datas.id)
-              notify.close()
-              switch (getMsgBuoyActive.platformOS()) {
-                case 'pc':
-                  location.href = '/swordfish/frontPage/messageCenter/sess/index'
-                  break;
-                case 'work_bench':
-                  window.open('/swordfish/frontPage/messageCenter/sess/index')
-                  break;
-              }
-            } catch (e) {
-              console.warn(e)
-            }
-          }
-        } else {
-          Notification.requestPermission()
-        }
-      })
-    } catch (e) {
-      console.warn('不支持通知')
-    }
-  }
-
-  // 定义 scoket 信息
-  var pushMessageScoket = {
-    url: "ws"+(!isIE9&&"https:"==document.location.protocol?"s":"")+"://"+window.location.host,
-    init: function () {
-      this._scoket = new CommonWebScoket(this.url + '/webscoket/getBuoyMsg', '', {
-        complete: function (data, type) {
-          try {
-            var pushMessage = JSON.parse(data)
-            if (pushMessage.id) {
-              sendNotification(pushMessage)
-            }
-          } catch (e) {
-            console.warn(e)
-          }
-        }
-      }).init()
-    }
-  }
-
-  // 初始化
-  pushMessageScoket.init()
+  try {
+    getMsgBuoyActive.listenNotification()
+  } catch (e) {}
 }
 
 //查询用户信息,响应页面登录信息