Przeglądaj źródła

fix: 修复缓存数据结构不一样的问题

zhangyuhan 1 rok temu
rodzic
commit
2c5ff4c91f

+ 5 - 2
src/store/user.js

@@ -13,6 +13,7 @@ import {
   getIdentityList
 } from '@/api/modules'
 import { formatKeywordsList, formatVipKeywordsList } from '@/utils/format'
+import VisitedModelTransform from '@/utils/mixins/tran-visited'
 
 const vtMap = {
   free: 'f',
@@ -144,10 +145,12 @@ export default {
     refreshVisited (state) {
       const expiresTime = 3 * 24 * 60 * 60 * 1000
       const now = Date.now()
-      const list = JSON.parse(localStorage.getItem('visited-path-list')) || []
-      state.visitedList = list.filter(item => {
+      const cacheList = JSON.parse(localStorage.getItem('visited-path-list')) || []
+      const TransformModel = new VisitedModelTransform(cacheList)
+      const visitedList = TransformModel.list.filter(item => {
         return now - item.timestamp <= expiresTime
       })
+      state.visitedList = TransformModel.transform(visitedList)
       localStorage.setItem('visited-path-list', JSON.stringify(state.visitedList))
     },
     // 访问添加 visitedList

+ 47 - 0
src/utils/mixins/tran-visited.js

@@ -0,0 +1,47 @@
+/**
+ * 当前所有已查看页面匹配规则
+ * @type {{project: string, ent: string, issued: string, content: string, buyer: string}}
+ */
+const Rules = {
+  content: 'article/content/*',
+  issued: 'article/issued',
+  ent: 'ent(_ser)?_portrait',
+  buyer: 'client_portrayal',
+  project: 'client_follow_detail'
+}
+
+/**
+ * 旧数据模型转换处理,同时兼容新旧模型使用
+ */
+class VisitedModelTransform {
+  constructor (list = []) {
+    this.list = list
+    return this
+  }
+
+  checkPathRules (path) {
+    let result = path
+    for (const type in Rules) {
+      if (new RegExp(Rules[type]).test(path)) {
+        result = type
+        break
+      }
+    }
+    return result
+  }
+
+  transformItem (item) {
+    return {
+      id: item?.id || item?.search.replace(/(id|sid|entName)=/, ''),
+      type: item?.type || this.checkPathRules(item?.path),
+      path: item?.path,
+      timestamp: item?.timestamp
+    }
+  }
+
+  transform (list = this.list) {
+    return list.map(this.transformItem.bind(this))
+  }
+}
+
+export default VisitedModelTransform

+ 6 - 3
src/utils/mixins/visited.js

@@ -1,4 +1,5 @@
 import { mapState } from 'vuex'
+import VisitedModelTransform from '@/utils/mixins/tran-visited'
 
 class VisitedPathItem {
   constructor (path, search) {
@@ -8,6 +9,8 @@ class VisitedPathItem {
   }
 }
 
+const TranModel = new VisitedModelTransform()
+
 export const mixinVisited = {
   computed: {
     ...mapState({
@@ -19,7 +22,7 @@ export const mixinVisited = {
   },
   methods: {
     createPathItem (path, search) {
-      return new VisitedPathItem(path, search)
+      return TranModel.transformItem(new VisitedPathItem(path, search))
     },
     // 从判断 path 是否访问过
     // 参数path为 VisitedPathItem new 出来的实例
@@ -38,8 +41,8 @@ export const mixinVisited = {
       return sameIndex
     },
     comparePath (basePath, newPath) {
-      const pathSame = basePath.path === newPath.path
-      const searchSame = basePath.search === newPath.search
+      const pathSame = basePath.type === newPath.type
+      const searchSame = basePath.id === newPath.id
 
       return pathSame && searchSame
     },