Преглед изворни кода

1、修复切换tabs页重复加锁,解锁导致清单可能清空问题

iggy пре 2 дана
родитељ
комит
9ecbf1693b
2 измењених фајлова са 53 додато и 16 уклоњено
  1. 21 16
      JLHWEB/src/routers/index.ts
  2. 32 0
      JLHWEB/src/stores/modules/keepAlive.ts

+ 21 - 16
JLHWEB/src/routers/index.ts

@@ -11,6 +11,7 @@ import { loading } from "@/utils/loading";
 import { useKeepAliveStore } from "@/stores/modules/keepAlive";
 import { storeToRefs } from "pinia";
 import { LockBill, UnLockBill } from "@/api/modules/common";
+import { useTabsStore } from "@/stores/modules/tabs";
 
 /**
  * @description 📚 路由参数配置简介
@@ -88,11 +89,15 @@ router.beforeEach(async (to, from, next) => {
 
   // 8.如果需要加锁的路由,进入时尝试加锁
   if (to.meta.needLock && Number(to.query.id)) {
-    try {
-      await LockBill({ keyword: String(to.meta.billtype), billid: Number(to.query.id), billcode: String(to.query.code) });
-    } catch (err) {
-      to.meta.aborted = true;
-      return next(false);
+    const keepAliveStore = useKeepAliveStore();
+    const routeItem = keepAliveStore.getKeepAliveName(to.fullPath);
+    if (!routeItem) {
+      try {
+        await LockBill({ keyword: String(to.meta.billtype), billid: Number(to.query.id), billcode: String(to.query.code) });
+      } catch (err) {
+        to.meta.aborted = true;
+        return next(false);
+      }
     }
   }
 
@@ -138,17 +143,17 @@ router.afterEach(async (to, from) => {
   }
 
   // 2. 如果来源页面是需要加锁的,离开时解锁
-  if (from.meta.needLock && Number(from.query.id)) {
-    try {
-      await UnLockBill({
-        keyword: String(from.meta.billtype),
-        billid: Number(from.query.id),
-        billcode: String(from.query.code)
-      });
-    } catch (err) {
-      console.warn("解锁失败:", from.meta.billtype, from.query.id, err);
-    }
-  }
+  // if (from.meta.needLock && Number(from.query.id)) {
+  //   try {
+  //     await UnLockBill({
+  //       keyword: String(from.meta.billtype),
+  //       billid: Number(from.query.id),
+  //       billcode: String(from.query.code)
+  //     });
+  //   } catch (err) {
+  //     console.warn("解锁失败:", from.meta.billtype, from.query.id, err);
+  //   }
+  // }
 
   if (to.meta.aborted) return;
 

+ 32 - 0
JLHWEB/src/stores/modules/keepAlive.ts

@@ -2,6 +2,7 @@ import { defineStore } from "pinia";
 import { KeepAliveState } from "@/stores/interface";
 import { cloneDeep } from "lodash-es";
 import { useTabsStore } from "@/stores/modules/tabs";
+import { LockBill, UnLockBill } from "@/api/modules/common";
 
 export const useKeepAliveStore = defineStore({
   id: "geeker-keepAlive",
@@ -54,6 +55,16 @@ export const useKeepAliveStore = defineStore({
         }
         this.keepAliveList.splice(index, 1);
         // this.keepAliveList[index].times = this.keepAliveList[index].times + 1;
+
+        if (route.needLock && Number(route.billid)) {
+          UnLockBill({
+            keyword: String(route.billtype),
+            billid: Number(route.billid),
+            billcode: String(route.billcode)
+          }).catch(err => {
+            console.warn("解锁失败:", route.billtype, route.billid, err);
+          });
+        }
       }
       console.log("getKeepAliveName routeItem removeKeepAliveName :>> ", index, JSON.stringify(this.keepAliveList));
       console.log("getKeepAliveName routeItem removeKeepAliveName :>> ", this.keepAliveList);
@@ -95,6 +106,27 @@ export const useKeepAliveStore = defineStore({
         name: to.name as string,
         close: !to.meta.isAffix
       };
+
+      if (to.meta.needLock && Number(to.query.id)) {
+        tabsParams["needLock"] = to.meta.needLock;
+        tabsParams["keyword"] = String(to.meta.billtype);
+        tabsParams["billid"] = Number(to.query.id);
+        tabsParams["billcode"] = String(to.query.code);
+      }
+
+      const isEditToDetail =
+        from.fullPath.includes("/edit") && to.fullPath.includes("/detail") && from.query?.id && from.meta?.needLock;
+
+      if (isEditToDetail) {
+        UnLockBill({
+          keyword: String(from.meta.billtype),
+          billid: Number(from.query.id),
+          billcode: String(from.query.code)
+        }).catch(err => {
+          console.warn("edit->detail 解锁失败:", err);
+        });
+      }
+
       console.log("tabStore :>> ", tabStore, to);
       console.log("tabsParams router:>> ", tabsParams);
       let isReplace = false;