Selaa lähdekoodia

fix: 后端路由覆盖静态路由时继承 rank + 修复 NULL rank SQL

1. handleWholeMenus 去重时,若 backstage 路由没有 rank,
   从被覆盖的静态路由继承 rank,避免 ascending 自动赋值
2. 提供 fix_null_ranks.sql 修复数据库中 rank 为 NULL 的菜单

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
skyline 1 viikko sitten
vanhempi
säilyke
395a8b6143

+ 12 - 0
docs/database/fix_null_ranks.sql

@@ -0,0 +1,12 @@
+-- 为 rank 为 NULL 的菜单设置默认排序值
+-- 按模块分组,模块内按 id 升序分配 rank
+
+UPDATE t_menu t
+JOIN (
+  SELECT id,
+    ROW_NUMBER() OVER (PARTITION BY parent_id ORDER BY id) AS new_rank
+  FROM t_menu
+  WHERE deleted = 0 AND `rank` IS NULL
+) r ON t.id = r.id
+SET t.`rank` = r.new_rank
+WHERE t.`rank` IS NULL;

+ 8 - 1
haha-admin-web/src/store/modules/permission.ts

@@ -29,7 +29,14 @@ export const usePermissionStore = defineStore("pure-permission", {
       const merged = this.constantMenus.concat(routes);
       const pathMap = new Map<string, any>();
       for (const r of merged as any[]) {
-        if (!pathMap.has(r.path) || r.meta?.backstage) {
+        if (!pathMap.has(r.path)) {
+          pathMap.set(r.path, r);
+        } else if (r.meta?.backstage) {
+          // 后端路由覆盖静态路由时,若后端无 rank 则继承静态的 rank
+          const existing = pathMap.get(r.path);
+          if (r.meta?.rank == null && existing?.meta?.rank != null) {
+            r.meta.rank = existing.meta.rank;
+          }
           pathMap.set(r.path, r);
         }
       }