修复:修复无店铺后台登录引导
This commit is contained in:
@@ -93,7 +93,7 @@ adminApi.interceptors.response.use(
|
||||
export interface AdminLoginRequest {
|
||||
account: string
|
||||
password: string
|
||||
shopId: number
|
||||
shopId?: number
|
||||
}
|
||||
|
||||
export interface AdminLoginResponse {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -14,6 +14,15 @@
|
||||
layout="vertical"
|
||||
@finish="handleLogin"
|
||||
>
|
||||
<a-alert
|
||||
v-if="!shopLoading && !hasAvailableShops"
|
||||
type="warning"
|
||||
show-icon
|
||||
message="当前暂无可选店铺"
|
||||
description="如果你使用的是平台管理员账号,可以直接登录后台先创建第一家店铺;其他后台账号仍需先绑定店铺。"
|
||||
style="margin-bottom: 16px"
|
||||
/>
|
||||
|
||||
<a-form-item label="用户名" name="account">
|
||||
<a-input
|
||||
v-model:value="formState.account"
|
||||
@@ -37,10 +46,10 @@
|
||||
<a-select
|
||||
v-model:value="formState.shopId"
|
||||
size="large"
|
||||
placeholder="请选择登录店铺"
|
||||
:placeholder="hasAvailableShops ? '请选择登录店铺' : '当前暂无可选店铺'"
|
||||
:loading="shopLoading"
|
||||
:options="shopOptions"
|
||||
:disabled="shopLoading || shopOptions.length === 0"
|
||||
:disabled="shopLoading || !hasAvailableShops"
|
||||
show-search
|
||||
:filter-option="filterShopOption"
|
||||
/>
|
||||
@@ -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
|
||||
})
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,15 @@
|
||||
<template>
|
||||
<div class="home">
|
||||
<a-page-header title="欢迎使用后台管理系统" :sub-title="welcomeMessage" />
|
||||
|
||||
<a-alert
|
||||
v-if="isBootstrapAdmin"
|
||||
class="bootstrap-alert"
|
||||
type="warning"
|
||||
show-icon
|
||||
message="当前处于建店引导态"
|
||||
description="系统里还没有可用店铺。当前账号已允许登录后台先创建第一家店铺,其他后台模块会保持不可用,创建完成后请重新登录并选择店铺。"
|
||||
/>
|
||||
|
||||
<a-card class="time-card">
|
||||
<a-statistic title="当前时间" :value="currentTime" />
|
||||
@@ -90,6 +99,8 @@ const welcomeMessage = computed(() => {
|
||||
}
|
||||
})
|
||||
|
||||
const isBootstrapAdmin = computed(() => userInfo.value?.type === 4 && !userInfo.value?.shopId)
|
||||
|
||||
const groupedPermissions = computed(() => {
|
||||
const groups: Record<string, PermissionVO[]> = {}
|
||||
userPermissions.value.forEach(permission => {
|
||||
@@ -140,13 +151,9 @@ const quickActions = computed(() => {
|
||||
permissionCode: 'permission:view'
|
||||
}
|
||||
]
|
||||
|
||||
// 根据用户类型过滤操作
|
||||
if (userInfo.value?.type === 3) { // 美甲师
|
||||
return actions.filter(item => ['products', 'users'].includes(item.key))
|
||||
}
|
||||
|
||||
return actions
|
||||
|
||||
const grantedPermissions = new Set(userPermissions.value.map(permission => permission.permissionCode))
|
||||
return actions.filter(item => grantedPermissions.has(item.permissionCode))
|
||||
})
|
||||
|
||||
const getUserTypeText = (type: number | undefined) => {
|
||||
@@ -226,6 +233,10 @@ onMounted(() => {
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
|
||||
.bootstrap-alert {
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
|
||||
.info-card {
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
@@ -320,4 +331,4 @@ onMounted(() => {
|
||||
color: #1890ff;
|
||||
background: #f0f2f5;
|
||||
}
|
||||
</style>
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user