瀏覽代碼

feat: 调整消息通知改为 webscoket 获取并调用浏览器消息通知

zhangyuhan 2 年之前
父節點
當前提交
50f69e11d9
共有 1 個文件被更改,包括 58 次插入47 次删除
  1. 58 47
      src/web/staticres/js/login.js

+ 58 - 47
src/web/staticres/js/login.js

@@ -445,57 +445,68 @@ function toReaded(ids, type, url) {
         }
     })
 }
+
+/**
+ * @date 2023/1/4 消息通知改为 webscoket 获取并调用浏览器消息通知
+ */
 // 查看是否有新消息
 function checkCounts() {
-  try {
-    Notification.requestPermission(function(status) {
-      if(status === 'granted'){
-        var str = window.location.href
-        var arr = str.split('https://')[1].split('/')[1]
-        if (arr) {
-          clearInterval(msgTimer)
-          return
+
+  /**
+   * 进行浏览器通知,判断上次通知是否与当前一致,一致则不进行重复通知
+   * @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.content
+          })
+          // 点击时桌面消息时触发
+          notify.onclick = function () {
+            try {
+              getMsgBuoyActive.istoReaded(datas.ids)
+            } catch (e) {
+              console.warn(e)
+            }
+          }
+        } else {
+          Notification.requestPermission()
         }
-        clearInterval(msgTimer)
-        msgTimer = setInterval(function () {
-          $.ajax({
-            type: 'POST',
-            url: '/jymessageCenter/lastNewMessage?t=' + Date.now(),
-            success: function (r) {
-              if (r.error_code == 1001) {
-                clearInterval(msgTimer)
-                return
-              }
-              var num = r.count
-              var datas = r.data
-              // var noMesgs = window.localStorage.setItem('noMesg', num)
-              // if (num !== noMesgs && !$.isEmptyObject(datas)) {
-              if (!$.isEmptyObject(datas) && datas !=="") {
-                var notify = new Notification(datas.title,{
-                  icon: '../images/t3_new.jpg',
-                  body: datas.content
-                })
-                // 点击时桌面消息时触发
-                notify.onclick = function () {
-                  if (r.data.isRead == 0) {
-                    toReaded(r.data.id, r.data.msg_type, r.data.link)
-                  }
-                }
-              }
-              window.localStorage.setItem('noMesg', num)
-            },
-            error: function () {
-              clearInterval(msgTimer)
+      })
+    } 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)
             }
-          })
-        }, 300000)
-      } else {
-        Notification.requestPermission()
-      }
-    })
-  } catch (e) {
-    console.log('不支持通知')
+          } catch (e) {
+            console.warn(e)
+          }
+        }
+      }).init()
+    }
   }
+
+  // 初始化
+  pushMessageScoket.init()
 }
 
 //查询用户信息,响应页面登录信息
@@ -776,7 +787,7 @@ var haslogin = function(num,kyorpn,url){
 			    window.location.href = "/?nol=2";
 				return;
 			}
-      
+
 			loginflag = true;
       // TODO login callback
       try {