Files
medical-mall/layouts/admin/utils/role.uts

52 lines
1.9 KiB
Plaintext
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 role-based access control
export function getCurrentAdminRole(): string {
// 从本地存储获取当前 role。为不影响全局优先读 admin_role默认降级到读全局 role/userInfo再默认
// 根据实际系统中的 token/userInfo 结构可灵活调整
const roleType = uni.getStorageSync('admin_role') as string
if (roleType && typeof roleType === 'string' && roleType.length > 0) {
return roleType
}
// 检查是否有关联 merchant_id 或者是存了全局 role
const merchantId = uni.getStorageSync('merchant_id')
if (merchantId) return 'merchant'
const globalRole = uni.getStorageSync('role') as string
if (globalRole && typeof globalRole === 'string' && globalRole.length > 0) {
return globalRole
}
return 'admin' // 默认返回 admin 作为兜底(兼容原有全部显示逻辑)
}
// 获取不同 role 允许访问的顶级模块 ID (主侧边栏的顶层菜单 id)
export function getVisibleTopMenuIds(role: string): string[] {
if (role === 'admin') {
return ['home', 'user', 'order', 'product', 'marketing', 'distribution', 'kefu', 'finance', 'cms', 'decoration', 'app', 'setting', 'maintain']
}
if (role === 'merchant') {
// merchant: 只能看到 主页、订单、商品、营销、财务
return ['home', 'order', 'product', 'marketing', 'finance']
}
// 其他 role: 安全兜底
return ['home']
}
// 判断是否有权限访问某模块或路由
export function hasAdminModuleAccess(moduleId: string | undefined): boolean {
if (!moduleId) return true // 没有指定的通常认为是公共的,比如一些通用组件
const role = getCurrentAdminRole()
if (role === 'admin') return true
// 对于 merchant 角色,允许其访问的顶级 menu id 及其所属路由
if (role === 'merchant') {
const allowed = ['home', 'order', 'product', 'marketing', 'finance']
return allowed.includes(moduleId)
}
return false
}