Files
medical-mall/pages/mall/admin/docs/ADMIN_GUARD_STRATEGY.md
2026-02-05 10:11:09 +08:00

55 lines
2.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Admin 模块统一权限守卫策略
本文档遵循 `@docs/AGENT_PROJECT_SPEC.md` 规范,旨在说明 Admin 模块前端页面的权限守卫实现策略、覆盖范围及待办事项。
## 1. 统一守卫策略
根据决策Admin 模块采用**统一布局守卫**策略。
- **守卫落点**`@/layouts/admin/AdminLayout.uvue`
- **触发时机**`onMounted` 生命周期钩子
所有使用 `<AdminLayout>` 组件包裹的页面,都会自动继承此安全策略,无需在各自页面内部重复编写守卫逻辑。
## 2. 守卫行为
守卫逻辑严格分为两步,符合前端“快速失败”和依赖方向原则:
1. **登录检查(无 IO**
- 调用 `services/analytics/authGuard.uts` 中的 `ensureAnalyticsLogin()`
- **行为**:如果用户未登录(无有效 session则中断页面加载并自动跳转到登录页`/pages/user/login`),同时附带 `redirect` 参数,以便登录后能返回原页面。
2. **角色检查(依赖本地 Profile**
- 调用 `utils/store.uts` 中的 `await getCurrentUser()` 获取当前用户的 `profile`,该 `profile` 中包含了从 `ak_users` 同步的 `role` 字段。
- **行为**:检查 `role` 是否为 `'admin'``'analytics'` 之一。如果不是,则:
- `uni.showToast({ title: '权限不足' })`
- 800ms 后 `uni.switchTab({ url: '/pages/mall/consumer/index' })`(按决策 B 跳转到消费者首页)。
只有当登录和角色检查都通过后,`AdminLayout` 才会继续执行其内部的导航和组件渲染逻辑。
## 3. 覆盖范围
### 已确认被统一守卫覆盖的页面:
- `pages/mall/admin/index_new.uvue` (通过其使用的 `AdminLayout`)
- `pages/mall/admin/product/product-statistics/index.uvue`
- `pages/mall/admin/user/Statistic.uvue`
- `pages/mall/admin/order/order-statistics/index.uvue` (本次已改造)
- 其他所有在模板中使用了 `<AdminLayout>` 的页面。
### **例外与风险**:未被统一守卫覆盖的 Admin 页面
以下在 `pages.json` 中注册的 `admin` 路由,由于是独立的“裸”页面,**不会**被 `AdminLayout` 的统一守卫保护,存在安全风险:
- `pages/mall/admin/index`
- `pages/mall/admin/user-detail`
- `pages/mall/admin/merchant-detail`
- `pages/mall/admin/system-monitor`
## 4. 待办事项
为实现 Admin 模块权限的全覆盖,建议后续处理上述例外页面:
- **方案 1推荐**:将这些独立的页面改造为使用 `<AdminLayout>` 包裹,使其自动纳入统一守卫范围。
- **方案 2临时**:在这些页面的 `onLoad` 钩子中,手动加入与 `AdminLayout` 中相同的守卫逻辑代码。