ca910c582f
F1 闸门②行为用例: routeOf() 实跑 producer, 取样自 adminPages.superAdminOnly(不写死 menuKey), 含取样非空自检与"普通页 meta 不许出现 superAdminOnly"的反向自检。 F2 守卫断言从松散 /superAdminOnly/ 收紧为锚在 `if (to.meta.superAdminOnly && !session.isSuperAdmin)` 真实语句上; 原 /isSuperAdmin/ 那条已被锚定式覆盖且被 index.ts:57 预先满足, 不再单列。 F3 新增 companyScoped ↔ COMPANY_SCOPED_PAGES 双向对账(>=8 页自检)与 17 条声明路径存在性 (existsSync 作断言不作守卫, >=17 自检)——Task 7 护栏 G1 的分母掉一行不再无人报警。 F4 src/types/vue-router.d.ts 补 superAdminOnly?: boolean, producer/consumer 的键从字符串约定 变为编译期契约(控制方已追认, 与 task-29.test.ts 那条同批)。 F5 剪枝的角色实参改用 store 自己的 isSuperAdmin getter, 与守卫同一谓词; task-29 正则未受影响。 变异自证 4 轮 + 3 轮加强(注入→红→从原始字节还原并 sha256 复核): routeOf 删抄送行→F1 红; 去一个 companyScoped: true→F3 对账红(含分母保持 8 的方向一/虚增一行的方向二); 清单路径改名→F3 存在性红; 守卫条件改名→F2 红(且"删语句只留注释 token"这一形状旧断言恒绿、新断言红)。 node --test 聚焦 6 文件 37/37; vue-tsc --noEmit rc=0; npm test 1701/1701(基线 1698 + 新增 3 支)。
52 lines
1.7 KiB
TypeScript
52 lines
1.7 KiB
TypeScript
import { defineStore } from 'pinia'
|
|
import { fetchAdminMenuTree, fetchCurrentUser, logout } from '@/api/session'
|
|
import { joinAdminPath } from '@/config/app'
|
|
import type { AdminMenuNode, AdminUser } from '@/types/admin'
|
|
import { isSuperAdminRole } from '@/types/admin'
|
|
import { firstVisiblePath, pruneSuperAdminOnly } from '@/router/helpers'
|
|
import { canonicalizeMenuTree } from '@/router/menu-route-map'
|
|
|
|
export const useAdminSessionStore = defineStore('admin-session', {
|
|
state: () => ({
|
|
user: null as AdminUser | null,
|
|
menuTree: [] as AdminMenuNode[],
|
|
initialized: false,
|
|
loading: false,
|
|
error: '',
|
|
}),
|
|
getters: {
|
|
isSuperAdmin: (state) => isSuperAdminRole(state.user?.role),
|
|
firstVisible: (state): string => {
|
|
return firstVisiblePath(state.menuTree)
|
|
},
|
|
},
|
|
actions: {
|
|
async initialize() {
|
|
if (this.initialized || this.loading) return
|
|
this.loading = true
|
|
this.error = ''
|
|
try {
|
|
this.user = await fetchCurrentUser()
|
|
// 剪枝是角色内在闸门的第三处(注册表 + 守卫 + 菜单树),必须在 this.user 赋值之后。
|
|
// 角色判定用 store 自己的 isSuperAdmin getter:守卫读的就是它,两处各算一次会靠约定对齐。
|
|
this.menuTree = pruneSuperAdminOnly(
|
|
canonicalizeMenuTree(await fetchAdminMenuTree()),
|
|
this.isSuperAdmin,
|
|
)
|
|
this.initialized = true
|
|
} catch (error) {
|
|
this.error = error instanceof Error ? error.message : '后台初始化失败'
|
|
throw error
|
|
} finally {
|
|
this.loading = false
|
|
}
|
|
},
|
|
async signOut() {
|
|
await logout()
|
|
this.$reset()
|
|
window.location.assign(joinAdminPath('login'))
|
|
},
|
|
},
|
|
})
|
|
|