From a81509e1fcfdcd651797f222d1c62c20650e14e8 Mon Sep 17 00:00:00 2001 From: huangzhenbao <17818024429@163.com> Date: Thu, 12 Mar 2026 16:07:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=AE=8C=E6=88=90merchant?= =?UTF-8?q?=E5=92=8Cadmin=E4=B8=8D=E5=90=8Crole=E7=9A=84=E5=B1=95=E7=A4=BA?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E9=80=BB=E8=BE=912?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/supadb/aksupainstance.uts | 2 +- layouts/admin/AdminLayout.uvue | 7 ++++ layouts/admin/components/AdminHeader.uvue | 12 +++++- layouts/admin/utils/role.uts | 33 ++++++--------- .../product/product-management/edit.uvue | 6 +-- .../product/product-management/index.uvue | 4 +- pages/user/login.uvue | 4 +- process.py | 35 ++++++++++++++++ search_user_id.py | 14 +++++++ uni_modules/ak-req/ak-req.uts | 41 +++++++++++++++---- utils/store.uts | 5 +++ 11 files changed, 124 insertions(+), 39 deletions(-) create mode 100644 process.py create mode 100644 search_user_id.py 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 @@ -