Browse Source

Merge branch 'feature/v4.7.61' of ssh://192.168.3.207:10022/qmx/jy into feature/v4.7.61

wangchuanjin 2 years ago
parent
commit
607a5a6ebf
1 changed files with 67 additions and 47 deletions
  1. 67 47
      src/web/staticres/js/login.js

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

@@ -445,57 +445,77 @@ 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
-        }
-        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)
-                  }
-                }
+
+  /**
+   * 进行浏览器通知,判断上次通知是否与当前一致,一致则不进行重复通知
+   * @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;
               }
-              window.localStorage.setItem('noMesg', num)
-            },
-            error: function () {
-              clearInterval(msgTimer)
+            } catch (e) {
+              console.warn(e)
             }
-          })
-        }, 300000)
-      } else {
-        Notification.requestPermission()
-      }
-    })
-  } catch (e) {
-    console.log('不支持通知')
+          }
+        } 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()
 }
 
 //查询用户信息,响应页面登录信息
@@ -776,7 +796,7 @@ var haslogin = function(num,kyorpn,url){
 			    window.location.href = "/?nol=2";
 				return;
 			}
-      
+
 			loginflag = true;
       // TODO login callback
       try {