Browse Source

顶部选择项目时,使用localStorage缓存项目ID,以便下次进入时默认选择该项目

niuheng 2 years ago
parent
commit
74fa874a99
4 changed files with 30 additions and 2 deletions
  1. 2 0
      src/components/frame/pageRight.vue
  2. 6 0
      src/logicConfig/index.ts
  3. 12 2
      src/utils/authUtils.ts
  4. 10 0
      src/utils/toolUtils.ts

+ 2 - 0
src/components/frame/pageRight.vue

@@ -57,6 +57,7 @@ import { mapState } from "vuex";
 import { sessionStore } from "@/store/sessionStore";
 import { logicConfig } from "@/logicConfig";
 import uploadFile from "@/components/uploadFile";
+import { toolUtils } from "@/utils/toolUtils";
 export default {
   props: [],
   data() {
@@ -68,6 +69,7 @@ export default {
       set(value) {
         this.$store.commit("layout/setprojectId", value.id);
         this.$store.commit("updateSelectProject", value);
+        toolUtils.updateLocalStorageProject(value.id);
       },
       get() {
         return this.$store.state.selectProject;

+ 6 - 0
src/logicConfig/index.ts

@@ -32,4 +32,10 @@ export class logicConfig {
     public static get resultObj() {
         return this._resultObj;
     }
+    
+    //localStorage中存储项目id的key名称
+    private static _localStorKeyToSelProject = 'selProjectId';
+    public static get localStorKeyToSelProject() {
+        return this._localStorKeyToSelProject;
+    }
 }

+ 12 - 2
src/utils/authUtils.ts

@@ -2,6 +2,7 @@ import vueStore from "@/store";
 import { sessionStore } from "@/store/sessionStore";
 import { logicConfig } from "@/logicConfig";
 import { userController } from "@/controller/userController";
+import { toolUtils } from "./toolUtils";
 
 export class authUtils {
   /**
@@ -71,8 +72,17 @@ export class authUtils {
     });
     vueStore.commit("updateProjects", projects);
     vueStore.commit("layout/setProjects", projects);
-    vueStore.commit("layout/setprojectId", projects[0].id);
-    vueStore.commit("updateSelectProject", projects[0]);
+    //从localStorage中取出缓存的项目ID
+    var oldProjectId = window.localStorage.getItem(logicConfig.localStorKeyToSelProject) || projects[0].id;
+    //判断该用户是否具有缓存的项目ID的权限
+    var defaultProject = projects.filter(_c => { 
+      return _c.id == oldProjectId;
+    })[0];
+    if(!defaultProject)
+      defaultProject = projects[0];
+    vueStore.commit("layout/setprojectId", defaultProject.id);
+    vueStore.commit("updateSelectProject", defaultProject);
+    toolUtils.updateLocalStorageProject(defaultProject.id);
 
     next();
 

+ 10 - 0
src/utils/toolUtils.ts

@@ -3,6 +3,8 @@
  * nh 2021.12.23
  */
 
+import { logicConfig } from "@/logicConfig";
+
 export class toolUtils {
   /**
    * 拼接完整的http请求地址
@@ -100,4 +102,12 @@ export class toolUtils {
       message: reason
     };
   };
+
+  /**
+   * 更新localStorage中存储的项目id
+   * @param projectId 项目id
+   */
+  static updateLocalStorageProject(projectId: string) { 
+    window.localStorage.setItem(logicConfig.localStorKeyToSelProject, projectId);
+  }
 }