初步完成merchant和admin不同role的展示内容逻辑2

This commit is contained in:
2026-03-12 16:07:38 +08:00
parent f19dd093bf
commit a81509e1fc
11 changed files with 124 additions and 39 deletions

View File

@@ -1,4 +1,4 @@
import { state, getCurrentUser } from '@/utils/store.uts'
import { state, getCurrentUser } from '@/utils/store.uts'
import supa from '@/components/supadb/aksupainstance.uts'
/**
@@ -38,26 +38,18 @@ export function getCurrentAdminRole(): string {
}
}
// 2. 缓存兜底:为了刷新页面时立刻渲染,读取最新的 adminRole 缓存
const cachedRole = uni.getStorageSync('adminRole')
if (cachedRole != null && cachedRole != '') {
const normCached = normalizeRole(cachedRole)
if (normCached === 'admin' || normCached === 'merchant') {
return normCached
// 2. Auth Session兜底获取Tab 隔离):
const sessionUser = supa.getSession().user
if (sessionUser != null) {
const meta = sessionUser.get("user_metadata") as UTSJSONObject | null
if (meta != null && meta.getString("role") != null) {
const metaRole = normalizeRole(meta.getString("role"))
if (metaRole === "admin" || metaRole === "merchant") return metaRole
}
}
// 兼容旧的缓存字段以防万一
const oldCached = uni.getStorageSync('admin_role')
if (oldCached != null && oldCached != '') {
const normOld = normalizeRole(oldCached)
if (normOld === 'admin' || normOld === 'merchant') {
return normOld
}
}
console.warn('[AdminRole] 未能获取到有效的管理端角色,准备安全降级...')
return 'unknown'
console.warn("[AdminRole] 未能获取到有效的管理端角色,准备安全降级...")
return "unknown"
}
/**
@@ -67,6 +59,7 @@ export function clearAdminRoleCache(): void {
// 清理 admin 专属
uni.removeStorageSync('adminRole')
uni.removeStorageSync('admin_role')
uni.removeStorageSync('merchant_id')
}
/**
@@ -92,7 +85,7 @@ export async function refreshAdminRole(): Promise<string> {
}
if (finalRole !== 'unknown') {
uni.setStorageSync('adminRole', finalRole)
// uni.setStorageSync('adminRole', finalRole) // 移除缓存耦合,强制按单例会话状态刷新
if (state.userProfile != null) {
state.userProfile!.role = finalRole
}