完善权限的管理和校验
This commit is contained in:
@@ -21,7 +21,10 @@ import BottomNav from './components/BottomNav.vue'
|
||||
const route = useRoute()
|
||||
|
||||
const isHiddenNav = computed(() => {
|
||||
return route.meta.hideNav as boolean
|
||||
// 检查当前路由及其所有父路由是否有 hideNav: true
|
||||
const result = route.matched.some(r => r.meta.hideNav)
|
||||
console.log('isHiddenNav:', result, 'matched routes:', route.matched)
|
||||
return result
|
||||
})
|
||||
</script>
|
||||
|
||||
|
||||
@@ -248,7 +248,7 @@ export interface CreateUserRequest {
|
||||
|
||||
export const userApi = {
|
||||
getUserInfo: () => {
|
||||
return adminApi.get<UserVO>('/admin/user/info')
|
||||
return adminApi.get<UserVO>('/user/info')
|
||||
},
|
||||
listUsers: (params: { page: number; size: number; keyword?: string; shopId?: number }) => {
|
||||
return adminApi.get<UserListResponse>('/admin/user/list', { params })
|
||||
@@ -307,12 +307,28 @@ export const adminFileApi = {
|
||||
}
|
||||
}
|
||||
|
||||
export interface PermissionVO {
|
||||
id: number
|
||||
permissionCode: string
|
||||
permissionName: string
|
||||
permissionGroup: string
|
||||
description: string
|
||||
createTime: string
|
||||
updateTime: string
|
||||
}
|
||||
|
||||
export const permissionApi = {
|
||||
listAllPermissions: () => {
|
||||
return adminApi.get<PermissionVO[]>('/admin/permission/list/all')
|
||||
},
|
||||
getUserPermissions: () => {
|
||||
getUserPermissions: (userId?: number) => {
|
||||
if (userId) {
|
||||
return adminApi.get<PermissionVO[]>(`/admin/permission/user/${userId}`)
|
||||
}
|
||||
return adminApi.get<PermissionVO[]>('/admin/permission/user/current')
|
||||
},
|
||||
assignUserPermissions: (userId: number, permissionIds: number[]) => {
|
||||
return adminApi.post(`/admin/permission/assign/${userId}`, { permissionIds })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -71,7 +71,8 @@ const router = createRouter({
|
||||
{
|
||||
path: '',
|
||||
name: 'admin',
|
||||
redirect: '/admin/dashboard'
|
||||
component: () => import('../views/admin/HomeView.vue'),
|
||||
meta: { requiresAuth: true, isAdmin: true, hideNav: true }
|
||||
},
|
||||
{
|
||||
path: 'dashboard',
|
||||
@@ -113,16 +114,100 @@ router.beforeEach((to, _from, next) => {
|
||||
const requiresAuth = to.meta.requiresAuth as boolean
|
||||
|
||||
if (isAdminRoute) {
|
||||
const adminToken = localStorage.getItem('adminAccessToken') || sessionStorage.getItem('adminAccessToken')
|
||||
|
||||
if (requiresAuth && !adminToken) {
|
||||
next('/admin/login')
|
||||
} else if (to.path === '/admin/login' && adminToken) {
|
||||
next('/admin/dashboard')
|
||||
const adminToken = localStorage.getItem('adminAccessToken') || sessionStorage.getItem('adminAccessToken')
|
||||
|
||||
console.log('Admin route check:', { to: to.path, adminToken: !!adminToken, requiresAuth })
|
||||
|
||||
if (requiresAuth && !adminToken) {
|
||||
console.log('No token, redirecting to login')
|
||||
next('/admin/login')
|
||||
} else if (to.path === '/admin/login' && adminToken) {
|
||||
console.log('Token exists, redirecting from login to dashboard')
|
||||
next('/admin/dashboard')
|
||||
} else {
|
||||
// 检查用户权限
|
||||
const permissionCode = to.meta.permissionCode as string
|
||||
if (permissionCode) {
|
||||
// 获取用户信息
|
||||
try {
|
||||
const adminUserInfoStr = localStorage.getItem('adminUserInfo')
|
||||
console.log('Admin user info:', adminUserInfoStr)
|
||||
if (adminUserInfoStr) {
|
||||
try {
|
||||
const adminUserInfo = JSON.parse(adminUserInfoStr)
|
||||
console.log('Parsed admin user info:', adminUserInfo)
|
||||
// 管理员用户(type === 4)拥有所有权限
|
||||
if (adminUserInfo.type === 4) {
|
||||
console.log('Admin user, allowing access')
|
||||
next()
|
||||
}
|
||||
// 店长用户(type === 2)拥有所有权限
|
||||
else if (adminUserInfo.type === 2) {
|
||||
console.log('Shop manager user, allowing access')
|
||||
next()
|
||||
}
|
||||
// 美甲师用户(type === 3)需要检查权限
|
||||
else if (adminUserInfo.type === 3) {
|
||||
console.log('美甲师 user, checking permissions')
|
||||
// 获取用户权限
|
||||
const userPermissionsStr = localStorage.getItem('userPermissions')
|
||||
console.log('User permissions:', userPermissionsStr)
|
||||
if (userPermissionsStr) {
|
||||
const userPermissions = JSON.parse(userPermissionsStr)
|
||||
// 检查用户是否拥有该权限
|
||||
if (userPermissions.includes(permissionCode)) {
|
||||
console.log('User has required permission:', permissionCode)
|
||||
next()
|
||||
} else {
|
||||
console.log('User missing required permission:', permissionCode)
|
||||
// 权限不足,检查是否是首页
|
||||
if (to.path === '/admin') {
|
||||
// 如果是首页,允许访问(作为默认页面)
|
||||
console.log('Allowing access to home as default page')
|
||||
next()
|
||||
} else {
|
||||
// 对于其他页面,重定向到首页
|
||||
next('/admin')
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.log('No user permissions found')
|
||||
// 没有权限信息,允许访问首页临时,直到权限加载完成
|
||||
if (to.path === '/admin') {
|
||||
console.log('Allowing access to home to load permissions')
|
||||
next()
|
||||
} else {
|
||||
// 对于其他页面,重定向到首页
|
||||
next('/admin')
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.log('Unknown user type, redirecting to login')
|
||||
// 未知用户类型,重定向到登录页
|
||||
next('/admin/login')
|
||||
}
|
||||
} catch (parseError) {
|
||||
console.error('Failed to parse user info:', parseError)
|
||||
// 解析失败,允许访问所有页面临时,直到权限加载完成
|
||||
console.log('Allowing temporary access due to parse error')
|
||||
next()
|
||||
}
|
||||
} else {
|
||||
console.log('No user info found, redirecting to login')
|
||||
// 没有用户信息,重定向到登录页
|
||||
next('/admin/login')
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('解析用户信息失败:', error)
|
||||
// 解析失败,重定向到登录页
|
||||
next('/admin/login')
|
||||
}
|
||||
} else {
|
||||
// 没有权限要求,直接通过
|
||||
next()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
} else {
|
||||
const token = localStorage.getItem('accessToken') || sessionStorage.getItem('accessToken')
|
||||
|
||||
if (requiresAuth && !token) {
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
@click="handleMenuClick"
|
||||
>
|
||||
<a-menu-item
|
||||
v-for="item in menuItems"
|
||||
v-for="item in filteredMenuItems"
|
||||
:key="item.key"
|
||||
>
|
||||
<template #icon>
|
||||
@@ -102,38 +102,70 @@ const route = useRoute()
|
||||
|
||||
const collapsed = ref(false)
|
||||
const userInfo = ref<UserVO | null>(null)
|
||||
const { loadUserPermissions, hasPermission, clearPermissions } = usePermission()
|
||||
const { loadUserPermissions, hasPermission, clearPermissions, userPermissions } = usePermission()
|
||||
|
||||
const menuItems = [
|
||||
{
|
||||
key: '/admin',
|
||||
icon: HomeOutlined,
|
||||
label: '首页',
|
||||
permissionCode: ''
|
||||
},
|
||||
{
|
||||
key: '/admin/dashboard',
|
||||
icon: DashboardOutlined,
|
||||
label: '仪表盘'
|
||||
label: '仪表盘',
|
||||
permissionCode: 'dashboard:view'
|
||||
},
|
||||
{
|
||||
key: '/admin/users',
|
||||
icon: UserOutlined,
|
||||
label: '用户管理'
|
||||
label: '用户管理',
|
||||
permissionCode: 'user:view'
|
||||
},
|
||||
{
|
||||
key: '/admin/products',
|
||||
icon: ShopOutlined,
|
||||
label: '商品管理'
|
||||
label: '商品管理',
|
||||
permissionCode: 'product:view'
|
||||
},
|
||||
{
|
||||
key: '/admin/shops',
|
||||
icon: HomeOutlined,
|
||||
label: '店铺管理'
|
||||
label: '店铺管理',
|
||||
permissionCode: 'shop:view'
|
||||
},
|
||||
{
|
||||
key: '/admin/permissions',
|
||||
icon: SafetyCertificateOutlined,
|
||||
label: '权限管理'
|
||||
label: '权限管理',
|
||||
permissionCode: 'permission:view'
|
||||
}
|
||||
]
|
||||
|
||||
const selectedKeys = ref<string[]>([route.path])
|
||||
|
||||
const filteredMenuItems = computed(() => {
|
||||
// 访问 userPermissions 以建立依赖关系
|
||||
userPermissions.value
|
||||
// 管理员用户(type === 4)拥有所有权限
|
||||
if (userInfo.value?.type === 4) {
|
||||
return menuItems
|
||||
}
|
||||
// 其他用户类型根据实际权限过滤菜单
|
||||
return menuItems.filter(item => {
|
||||
// 空权限码的菜单项(如首页)总是可见
|
||||
if (!item.permissionCode) {
|
||||
return true
|
||||
}
|
||||
// 美甲师用户(type === 3)不显示仪表盘
|
||||
if (userInfo.value?.type === 3 && item.key === '/admin/dashboard') {
|
||||
return false
|
||||
}
|
||||
return hasPermission(item.permissionCode)
|
||||
})
|
||||
})
|
||||
|
||||
const toggleCollapsed = () => {
|
||||
collapsed.value = !collapsed.value
|
||||
}
|
||||
@@ -191,10 +223,12 @@ watch(() => route.path, (newPath) => {
|
||||
}, { immediate: true })
|
||||
|
||||
onMounted(async () => {
|
||||
await Promise.all([
|
||||
fetchUserInfo(),
|
||||
loadUserPermissions()
|
||||
])
|
||||
console.log('AdminLayout mounted')
|
||||
await fetchUserInfo()
|
||||
console.log('User info fetched:', userInfo.value)
|
||||
await loadUserPermissions()
|
||||
console.log('Permissions loaded:', userPermissions.value)
|
||||
console.log('Filtered menu items:', filteredMenuItems.value)
|
||||
})
|
||||
</script>
|
||||
|
||||
@@ -290,6 +324,8 @@ onMounted(async () => {
|
||||
background: #f0f2f5;
|
||||
min-height: calc(100vh - 64px);
|
||||
transition: margin-left 0.2s;
|
||||
position: relative;
|
||||
z-index: 1;
|
||||
}
|
||||
|
||||
.admin-layout :deep(.ant-layout-sider-collapsed) + .ant-layout .content {
|
||||
|
||||
@@ -80,21 +80,33 @@ const handleLogin = async () => {
|
||||
try {
|
||||
await formRef.value?.validate()
|
||||
loading.value = true
|
||||
console.log('开始登录...')
|
||||
|
||||
const res = await adminAuthApi.login({
|
||||
account: formState.account,
|
||||
password: formState.password
|
||||
})
|
||||
|
||||
console.log('登录响应:', res)
|
||||
|
||||
const { accessToken, refreshToken, userInfo } = res as any
|
||||
|
||||
console.log('解析登录数据:', { accessToken, refreshToken, userInfo })
|
||||
|
||||
localStorage.setItem('adminAccessToken', accessToken)
|
||||
localStorage.setItem('adminRefreshToken', refreshToken)
|
||||
localStorage.setItem('adminUserInfo', JSON.stringify(userInfo))
|
||||
sessionStorage.setItem('adminAccessToken', accessToken)
|
||||
|
||||
console.log('存储登录信息完成')
|
||||
message.success('登录成功')
|
||||
router.push('/admin/dashboard')
|
||||
console.log('准备跳转到首页...')
|
||||
router.push('/admin').catch(err => {
|
||||
console.error('跳转到首页失败:', err)
|
||||
// 尝试使用window.location进行跳转
|
||||
window.location.href = '/admin'
|
||||
})
|
||||
console.log('跳转命令已执行')
|
||||
} catch (error: any) {
|
||||
console.error('登录失败:', error)
|
||||
message.error(error.message || '登录失败,请检查用户名和密码')
|
||||
|
||||
@@ -6,12 +6,12 @@
|
||||
<a-tabs v-model:activeKey="activeTab">
|
||||
<a-tab-pane key="permissions" tab="权限列表">
|
||||
<template #extra>
|
||||
<a-button type="primary" @click="showAddPermissionModal">
|
||||
<template #icon>
|
||||
<PlusOutlined />
|
||||
</template>
|
||||
新增权限
|
||||
</a-button>
|
||||
<a-button v-if="hasPermission('permission:create')" type="primary" @click="showAddPermissionModal">
|
||||
<template #icon>
|
||||
<PlusOutlined />
|
||||
</template>
|
||||
新增权限
|
||||
</a-button>
|
||||
</template>
|
||||
|
||||
<a-table
|
||||
@@ -28,18 +28,19 @@
|
||||
</template>
|
||||
<template v-else-if="column.key === 'actions'">
|
||||
<a-space>
|
||||
<a-button type="link" size="small" @click="showEditPermissionModal(record)">
|
||||
编辑
|
||||
</a-button>
|
||||
<a-popconfirm
|
||||
title="确定要删除这个权限吗?"
|
||||
@confirm="handleDeletePermission(record)"
|
||||
>
|
||||
<a-button type="link" size="small" danger>
|
||||
删除
|
||||
</a-button>
|
||||
</a-popconfirm>
|
||||
</a-space>
|
||||
<a-button v-if="hasPermission('permission:edit')" type="link" size="small" @click="showEditPermissionModal(record)">
|
||||
编辑
|
||||
</a-button>
|
||||
<a-popconfirm
|
||||
v-if="hasPermission('permission:delete')"
|
||||
title="确定要删除这个权限吗?"
|
||||
@confirm="handleDeletePermission(record)"
|
||||
>
|
||||
<a-button type="link" size="small" danger>
|
||||
删除
|
||||
</a-button>
|
||||
</a-popconfirm>
|
||||
</a-space>
|
||||
</template>
|
||||
</template>
|
||||
</a-table>
|
||||
@@ -85,7 +86,7 @@
|
||||
:bordered="false"
|
||||
>
|
||||
<template #extra>
|
||||
<a-button v-if="selectedUser" type="primary" @click="handleSaveUserPermissions">
|
||||
<a-button v-if="selectedUser && hasPermission('permission:assign')" type="primary" @click="handleSaveUserPermissions">
|
||||
保存权限
|
||||
</a-button>
|
||||
</template>
|
||||
@@ -99,13 +100,13 @@
|
||||
/>
|
||||
|
||||
<a-spin :spinning="assignLoading">
|
||||
<a-collapse v-model:activeKey="activeCollapseKeys">
|
||||
<a-collapse-panel
|
||||
v-for="(permissions, group) in groupedPermissions"
|
||||
:key="group"
|
||||
:header="group"
|
||||
>
|
||||
<a-checkbox-group v-model:value="selectedPermissions">
|
||||
<a-checkbox-group v-model:value="selectedPermissions">
|
||||
<a-collapse v-model:activeKey="activeCollapseKeys">
|
||||
<a-collapse-panel
|
||||
v-for="(permissions, group) in groupedPermissions"
|
||||
:key="group"
|
||||
:header="group"
|
||||
>
|
||||
<a-space direction="vertical" style="width: 100%">
|
||||
<a-checkbox
|
||||
v-for="permission in permissions"
|
||||
@@ -118,9 +119,9 @@
|
||||
</span>
|
||||
</a-checkbox>
|
||||
</a-space>
|
||||
</a-checkbox-group>
|
||||
</a-collapse-panel>
|
||||
</a-collapse>
|
||||
</a-collapse-panel>
|
||||
</a-collapse>
|
||||
</a-checkbox-group>
|
||||
</a-spin>
|
||||
</template>
|
||||
<a-empty v-else description="请先选择一个用户" />
|
||||
@@ -177,8 +178,10 @@ import { message } from 'ant-design-vue'
|
||||
import type { FormInstance } from 'ant-design-vue'
|
||||
import { PlusOutlined } from '@ant-design/icons-vue'
|
||||
import { DEFAULT_AVATAR } from '@/constants'
|
||||
import { permissionApi, type PermissionVO } from '@/api/permission'
|
||||
import { userApi, type UserVO } from '@/api/user'
|
||||
import { permissionApi, type PermissionVO, userApi, type UserVO } from '@/api/admin'
|
||||
import { usePermission } from '@/utils/permission'
|
||||
|
||||
const { hasPermission } = usePermission()
|
||||
|
||||
const activeTab = ref('permissions')
|
||||
const loading = ref(false)
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
style="width: 250px"
|
||||
@search="handleSearch"
|
||||
/>
|
||||
<a-button type="primary" @click="showAddModal">
|
||||
<a-button v-if="hasPermission('product:create')" type="primary" @click="showAddModal">
|
||||
<template #icon>
|
||||
<PlusOutlined />
|
||||
</template>
|
||||
@@ -58,10 +58,11 @@
|
||||
|
||||
<template v-else-if="column.key === 'actions'">
|
||||
<a-space>
|
||||
<a-button type="link" size="small" @click="showEditModal(record)">
|
||||
<a-button v-if="hasPermission('product:edit')" type="link" size="small" @click="showEditModal(record)">
|
||||
编辑
|
||||
</a-button>
|
||||
<a-popconfirm
|
||||
v-if="hasPermission('product:delete')"
|
||||
title="确定要删除这个商品吗?"
|
||||
@confirm="handleDelete(record)"
|
||||
>
|
||||
@@ -246,6 +247,7 @@ import type { FormInstance, UploadProps } from 'ant-design-vue'
|
||||
import { PlusOutlined } from '@ant-design/icons-vue'
|
||||
import { DEFAULT_PRODUCT_IMAGE } from '@/constants'
|
||||
import { adminProductApi, type Product, adminShopApi, type Shop, adminFileApi, type ObjectStorageFileVO, adminTagApi, adminArtistApi, adminCouponApi } from '@/api/admin'
|
||||
import { usePermission } from '@/utils/permission'
|
||||
|
||||
const loading = ref(false)
|
||||
const searchText = ref('')
|
||||
@@ -270,6 +272,7 @@ const imageUrls = ref<Record<number, string[]>>({})
|
||||
const imageIdCache = ref<Record<string, string>>({})
|
||||
const isAdmin = ref(false)
|
||||
const formRef = ref<any>(null)
|
||||
const { hasPermission } = usePermission()
|
||||
|
||||
const pagination = reactive({
|
||||
current: 1,
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
style="width: 250px"
|
||||
@search="handleSearch"
|
||||
/>
|
||||
<a-button type="primary" @click="showAddModal">
|
||||
<a-button v-if="hasPermission('shop:create')" type="primary" @click="showAddModal">
|
||||
<template #icon>
|
||||
<PlusOutlined />
|
||||
</template>
|
||||
@@ -52,10 +52,11 @@
|
||||
</template>
|
||||
<template v-else-if="column.key === 'actions'">
|
||||
<a-space>
|
||||
<a-button type="link" size="small" @click="showEditModal(record)">
|
||||
<a-button v-if="hasPermission('shop:edit')" type="link" size="small" @click="showEditModal(record)">
|
||||
编辑
|
||||
</a-button>
|
||||
<a-popconfirm
|
||||
v-if="hasPermission('shop:delete')"
|
||||
title="确定要删除这个店铺吗?"
|
||||
@confirm="handleDelete(record)"
|
||||
>
|
||||
@@ -131,6 +132,9 @@ import type { FormInstance } from 'ant-design-vue'
|
||||
import { PlusOutlined, UploadOutlined } from '@ant-design/icons-vue'
|
||||
import { DEFAULT_SHOP_IMAGE } from '@/constants'
|
||||
import { adminShopApi, adminFileApi, type Shop } from '@/api/admin'
|
||||
import { usePermission } from '@/utils/permission'
|
||||
|
||||
const { hasPermission } = usePermission()
|
||||
|
||||
const loading = ref(false)
|
||||
const searchText = ref('')
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
style="width: 250px"
|
||||
@search="handleSearch"
|
||||
/>
|
||||
<a-button type="primary" @click="showAddModal">
|
||||
<a-button v-if="hasPermission('user:create')" type="primary" @click="showAddModal">
|
||||
<template #icon>
|
||||
<PlusOutlined />
|
||||
</template>
|
||||
@@ -48,13 +48,14 @@
|
||||
</template>
|
||||
<template v-else-if="column.key === 'actions'">
|
||||
<a-space>
|
||||
<a-button type="link" size="small" @click="showEditModal(record)">
|
||||
<a-button v-if="hasPermission('user:edit')" type="link" size="small" @click="showEditModal(record)">
|
||||
编辑
|
||||
</a-button>
|
||||
<a-button type="link" size="small" @click="handleResetPassword(record)">
|
||||
<a-button v-if="hasPermission('user:reset_password')" type="link" size="small" @click="handleResetPassword(record)">
|
||||
重置密码
|
||||
</a-button>
|
||||
<a-popconfirm
|
||||
v-if="hasPermission('user:delete')"
|
||||
title="确定要删除这个用户吗?"
|
||||
@confirm="handleDelete(record)"
|
||||
>
|
||||
@@ -145,6 +146,9 @@ import type { FormInstance } from 'ant-design-vue'
|
||||
import { PlusOutlined } from '@ant-design/icons-vue'
|
||||
import { DEFAULT_AVATAR } from '@/constants'
|
||||
import { userApi, type UserVO, adminShopApi, type Shop } from '@/api/admin'
|
||||
import { usePermission } from '@/utils/permission'
|
||||
|
||||
const { hasPermission } = usePermission()
|
||||
|
||||
const getCurrentUserInfo = () => {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user