初步完成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,5 +1,9 @@
<template>
<view class="layout-root">
<view v-if="!isAuthReady" class="auth-loading-overlay" style="flex: 1; display: flex; align-items: center; justify-content: center; height: 100vh; background-color: #f5f5f5;">
<text style="color: #666; font-size: 16px;">身份鉴权中...</text>
</view>
<template v-else>
<!-- 统一遮罩层 (复刻 CRMEB: 用于所有 Overlay 状态) -->
<view
class="mobile-mask"
@@ -70,6 +74,7 @@
<AdminFooter></AdminFooter>
</view>
</view>
</template>
</view>
</template>
@@ -131,6 +136,7 @@ const SUB_W = 200
// 页面加载状态
const isPageLoading = ref(false)
const isAuthReady = ref(false)
const hasAccess = computed<boolean>(() => {
@@ -391,6 +397,7 @@ let resizeTid: any = null
onMounted(async () => {
await refreshAdminRole()
isAuthReady.value = true
initNavState()
if (props.currentPage != '') {
openRoute(props.currentPage as string)

View File

@@ -62,10 +62,18 @@ import {
openRoute
} from '@/layouts/admin/store/adminNavStore.uts'
import { state, logout } from '@/utils/store.uts'
import { clearAdminRoleCache } from '@/layouts/admin/utils/role.uts'
import { clearAdminRoleCache, getCurrentAdminRole } from '@/layouts/admin/utils/role.uts'
const showUserMenu = ref(false)
const userName = computed((): string => state.userProfile.username || state.userProfile.email || 'admin')
const userName = computed((): string => {
if (state.userProfile?.username != null && state.userProfile!.username != '') return state.userProfile!.username as string
if (state.authUser != null) {
if (state.authUser!.getString('email') != null && state.authUser!.getString('email') != '') return state.authUser!.getString('email') as string
if (state.authUser!.getString('phone') != null && state.authUser!.getString('phone') != '') return state.authUser!.getString('phone') as string
if (state.authUser!.getString('id') != null && state.authUser!.getString('id') != '') return (state.authUser!.getString('id') as string).substring(0, 8)
}
return '未知用户'
})
let hideMenuTimer: number | null = null

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
}