cuiyalong 3 жил өмнө
parent
commit
79ee8db8a6

+ 36 - 3
src/web/staticres/common-module/collection/js/visited.js

@@ -1,3 +1,29 @@
+// 实现跨tab共享sessionStorage
+// 同步已有标签页的sessionStorage数据到新开tab页
+(function () {
+  var syncKey = 'syncSessionStorage'
+  var sessKey = 'sessionStorage'
+  // 新打开一个tab标签页并通知其他标签页同步sessionStorage的数据到本标签页
+  // if (!sessionStorage.length) {
+  //   // 这个调用能触发storage事件,从而达到共享数据的目的
+  //   localStorage.setItem(syncKey, Date.now())
+  // }
+
+  window.addEventListener('storage', function (e) {
+    if (e.key === syncKey) {
+      // 已存在的标签页会收到这个事件
+      localStorage.setItem(sessKey, JSON.stringify(sessionStorage))
+      localStorage.removeItem(sessKey)
+    } else if (e.key === sessKey && !sessionStorage.length) {
+      // 新开启的标签页会收到这个事件
+      var data = JSON.parse(e.newValue)
+      for (var k in data) {
+        sessionStorage.setItem(k, data[k])
+      }
+    }
+  })
+})()
+
 function VisitedPathItem (path, search) {
   this.path = path
   this.search = search
@@ -5,12 +31,13 @@ function VisitedPathItem (path, search) {
 }
 
 function VisitedPath () {
+  this.debug = false
   this.state = {
     storageKey: 'visited-path-list',
     visitedList: [],
     cLocation: {
-      path: '', // location.path
-      search: '', // location.search
+      path: '', // location.pathname // 识别不同跳转目标的项(比如,跳转到企业画像、跳转采购单位画像、跳转三级页等的区分)
+      search: '', // location.search.replace(/\?/g, '')  // location.search去掉?(识别跳转项的唯一id)
       timestamp: 0
     }
   }
@@ -43,6 +70,9 @@ function VisitedPath () {
           break
         }
       }
+      if (this.debug) {
+        console.log(`循环了:${i + 1}次,匹配到`, list[i])
+      }
     }
     return sameIndex
   }
@@ -66,7 +96,9 @@ function VisitedPath () {
     var index = this.pathVisitedIndex(path)
     if (index >= 0) {
       // 已存在
-      var item = this.state.visitedList.splice(index, 1)
+      var itemArr = this.state.visitedList.splice(index, 1)
+      var item = itemArr[0]
+      item.timestamp = Date.now()
       this.state.visitedList.unshift(item)
     } else {
       this.state.visitedList.unshift(path)
@@ -76,6 +108,7 @@ function VisitedPath () {
 
   this.pathSave = function () {
     sessionStorage.setItem(this.state.storageKey, JSON.stringify(this.state.visitedList))
+    localStorage.setItem(this.state.storageKey, Date.now())
   }
 
   this.init()