diff --git a/components/supadb/aksupainstance.uts b/components/supadb/aksupainstance.uts
index 8ce98ec3..9887dfa6 100644
--- a/components/supadb/aksupainstance.uts
+++ b/components/supadb/aksupainstance.uts
@@ -26,7 +26,7 @@ export function checkConnection() {
}
// 兼容 supaReady Promise
-export const supaReady = Promise.resolve(true)
+export const supaReady = supaInstance.hydrateSessionFromStorage()
// 如果有其他需要导出的函数,可以这样导出:
export function initializeSupabase(url: string, key: string) {
diff --git a/layouts/admin/AdminLayout.uvue b/layouts/admin/AdminLayout.uvue
index a48a0313..66dfd4ad 100644
--- a/layouts/admin/AdminLayout.uvue
+++ b/layouts/admin/AdminLayout.uvue
@@ -1,5 +1,9 @@
+
+ 身份鉴权中...
+
+
+
@@ -131,6 +136,7 @@ const SUB_W = 200
// 页面加载状态
const isPageLoading = ref(false)
+const isAuthReady = ref(false)
const hasAccess = computed(() => {
@@ -391,6 +397,7 @@ let resizeTid: any = null
onMounted(async () => {
await refreshAdminRole()
+ isAuthReady.value = true
initNavState()
if (props.currentPage != '') {
openRoute(props.currentPage as string)
diff --git a/layouts/admin/components/AdminHeader.uvue b/layouts/admin/components/AdminHeader.uvue
index 3a2748eb..4f0939f4 100644
--- a/layouts/admin/components/AdminHeader.uvue
+++ b/layouts/admin/components/AdminHeader.uvue
@@ -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
diff --git a/layouts/admin/utils/role.uts b/layouts/admin/utils/role.uts
index e66151c5..9689ff10 100644
--- a/layouts/admin/utils/role.uts
+++ b/layouts/admin/utils/role.uts
@@ -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 {
}
if (finalRole !== 'unknown') {
- uni.setStorageSync('adminRole', finalRole)
+ // uni.setStorageSync('adminRole', finalRole) // 移除缓存耦合,强制按单例会话状态刷新
if (state.userProfile != null) {
state.userProfile!.role = finalRole
}
diff --git a/pages/mall/admin/product/product-management/edit.uvue b/pages/mall/admin/product/product-management/edit.uvue
index 4649f97c..4efa8086 100644
--- a/pages/mall/admin/product/product-management/edit.uvue
+++ b/pages/mall/admin/product/product-management/edit.uvue
@@ -1,4 +1,4 @@
-
+