From 4d5ea2996bd1047c206990218081a489f8e4684d Mon Sep 17 00:00:00 2001 From: xiaowang Date: Sat, 21 Mar 2026 12:37:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=97=A0=E5=BA=97=E9=93=BA=E5=90=8E=E5=8F=B0=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E5=BC=95=E5=AF=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Frontend/src/api/admin.ts | 2 +- Frontend/src/router/index.ts | 22 +++++--- Frontend/src/views/admin/AdminLoginView.vue | 30 +++++++++-- Frontend/src/views/admin/HomeView.vue | 27 +++++++--- .../service/impl/PermissionServiceImpl.java | 34 +++++++++++- .../service/impl/UserServiceImpl.java | 14 ++++- docs/architecture/项目架构分析.md | 3 ++ ...26-03-21-后台无店铺时管理员登录引导修复.md | 53 +++++++++++++++++++ 8 files changed, 162 insertions(+), 23 deletions(-) create mode 100644 docs/changes/2026-03-21-后台无店铺时管理员登录引导修复.md diff --git a/Frontend/src/api/admin.ts b/Frontend/src/api/admin.ts index dc029e98..ac6bcbce 100644 --- a/Frontend/src/api/admin.ts +++ b/Frontend/src/api/admin.ts @@ -93,7 +93,7 @@ adminApi.interceptors.response.use( export interface AdminLoginRequest { account: string password: string - shopId: number + shopId?: number } export interface AdminLoginResponse { diff --git a/Frontend/src/router/index.ts b/Frontend/src/router/index.ts index 86a5ba8e..c6057723 100644 --- a/Frontend/src/router/index.ts +++ b/Frontend/src/router/index.ts @@ -176,6 +176,17 @@ router.beforeEach((to, _from, next) => { if (isAdminRoute) { const adminToken = localStorage.getItem('adminAccessToken') || sessionStorage.getItem('adminAccessToken') const adminUserInfoStr = localStorage.getItem('adminUserInfo') + let adminUserInfo: any = null + let isBootstrapAdmin = false + + if (adminUserInfoStr) { + try { + adminUserInfo = JSON.parse(adminUserInfoStr) + isBootstrapAdmin = adminUserInfo?.type === 4 && !adminUserInfo?.shopId + } catch (parseError) { + console.error('Failed to parse admin user info in guard:', parseError) + } + } if (requiresAuth && (!adminToken || !adminUserInfoStr)) { // 清除可能存在的过期信息 @@ -186,11 +197,12 @@ router.beforeEach((to, _from, next) => { sessionStorage.removeItem('adminAccessToken') next('/admin/login') } else if (to.path === '/admin/login' && adminToken && adminUserInfoStr) { - next('/admin/dashboard') + next(isBootstrapAdmin ? '/admin/shops' : '/admin/dashboard') } else { // 检查用户权限 const permissionCode = to.meta.permissionCode as string if (permissionCode) { + const fallbackAdminPath = isBootstrapAdmin ? '/admin/shops' : '/admin' // 获取用户信息 try { const adminUserInfoStr = localStorage.getItem('adminUserInfo') @@ -206,8 +218,7 @@ router.beforeEach((to, _from, next) => { if (userPermissions.includes(permissionCode)) { next() } else { - // 权限不足,重定向到首页 - next('/admin') + next(fallbackAdminPath) } } catch (parseError) { console.error('Failed to parse user permissions:', parseError) @@ -215,7 +226,7 @@ router.beforeEach((to, _from, next) => { if (to.path === '/admin') { next() } else { - next('/admin') + next(fallbackAdminPath) } } } else { @@ -223,8 +234,7 @@ router.beforeEach((to, _from, next) => { if (to.path === '/admin') { next() } else { - // 对于其他页面,重定向到首页 - next('/admin') + next(fallbackAdminPath) } } } catch (parseError) { diff --git a/Frontend/src/views/admin/AdminLoginView.vue b/Frontend/src/views/admin/AdminLoginView.vue index fde4add5..23a16e38 100644 --- a/Frontend/src/views/admin/AdminLoginView.vue +++ b/Frontend/src/views/admin/AdminLoginView.vue @@ -14,6 +14,15 @@ layout="vertical" @finish="handleLogin" > + + @@ -96,6 +105,7 @@ const shopOptions = computed(() => value: shop.id })) ) +const hasAvailableShops = computed(() => shopOptions.value.length > 0) const formRules = { account: [ @@ -103,7 +113,15 @@ const formRules = { { pattern: /^[a-zA-Z0-9_\-@.]+$/, message: '账号只能包含数字、字母、下划线、连字符、@和点号', trigger: 'blur' } ], password: [{ required: true, message: '请输入密码', trigger: 'blur' }], - shopId: [{ required: true, message: '请选择登录店铺', trigger: 'change' }] + shopId: [{ + validator: async () => { + if (!hasAvailableShops.value || formState.shopId) { + return Promise.resolve() + } + return Promise.reject(new Error('请选择登录店铺')) + }, + trigger: 'change' + }] } as any const filterShopOption = (input: string, option: { label?: string | number } | undefined) => @@ -128,6 +146,10 @@ const loadShops = async () => { shopLoading.value = true try { shops.value = await shopApi.getShopsSimple() as unknown as ShopSimpleVO[] + if (!shops.value.length) { + formState.shopId = undefined + return + } if (!formState.shopId && shops.value.length === 1) { const [onlyShop] = shops.value if (onlyShop) { @@ -150,7 +172,7 @@ const handleLogin = async () => { const res = await adminAuthApi.login({ account: formState.account, password: formState.password, - shopId: formState.shopId as number + shopId: formState.shopId }) diff --git a/Frontend/src/views/admin/HomeView.vue b/Frontend/src/views/admin/HomeView.vue index 4dfbc2e2..579ad466 100644 --- a/Frontend/src/views/admin/HomeView.vue +++ b/Frontend/src/views/admin/HomeView.vue @@ -1,6 +1,15 @@