后台管理页面权限管理优化,权限管理细化
This commit is contained in:
@@ -146,7 +146,7 @@ export const adminDashboardApi = {
|
||||
}
|
||||
|
||||
export const adminProductApi = {
|
||||
listProducts: (params: { page: number; size: number; keyword?: string }) => {
|
||||
listProducts: (params: { page: number; size: number; keyword?: string; shopId?: number }) => {
|
||||
return adminApi.get<ProductListResponse>('/admin/product/list', { params })
|
||||
},
|
||||
createProduct: (data: CreateProductRequest) => {
|
||||
@@ -188,7 +188,7 @@ export interface CreateShopRequest {
|
||||
}
|
||||
|
||||
export const adminShopApi = {
|
||||
listShops: (params: { page: number; size: number; keyword?: string }) => {
|
||||
listShops: (params: { page: number; size: number; keyword?: string; shopId?: number }) => {
|
||||
return adminApi.get<ShopListResponse>('/admin/shops/list', { params })
|
||||
},
|
||||
createShop: (data: CreateShopRequest) => {
|
||||
|
||||
@@ -29,7 +29,7 @@ export const permissionApi = {
|
||||
if (userId) {
|
||||
return api.get<PermissionVO[]>(`/admin/permission/user/${userId}`)
|
||||
}
|
||||
return api.get<PermissionVO[]>('/permission/list/user/current')
|
||||
return api.get<PermissionVO[]>('/admin/permission/user/current')
|
||||
},
|
||||
|
||||
listAllPermissions: () => {
|
||||
|
||||
@@ -28,6 +28,7 @@ export interface ListUsersParams {
|
||||
page: number
|
||||
size: number
|
||||
keyword?: string
|
||||
shopId?: number
|
||||
}
|
||||
|
||||
export interface CreateUserRequest {
|
||||
|
||||
@@ -71,31 +71,31 @@ const router = createRouter({
|
||||
path: 'dashboard',
|
||||
name: 'admin-dashboard',
|
||||
component: () => import('../views/admin/DashboardView.vue'),
|
||||
meta: { requiresAuth: true, isAdmin: true, hideNav: true }
|
||||
meta: { requiresAuth: true, isAdmin: true, hideNav: true, permissionCode: 'dashboard:view' }
|
||||
},
|
||||
{
|
||||
path: 'users',
|
||||
name: 'admin-users',
|
||||
component: () => import('../views/admin/UsersView.vue'),
|
||||
meta: { requiresAuth: true, isAdmin: true, hideNav: true }
|
||||
meta: { requiresAuth: true, isAdmin: true, hideNav: true, permissionCode: 'user:view' }
|
||||
},
|
||||
{
|
||||
path: 'products',
|
||||
name: 'admin-products',
|
||||
component: () => import('../views/admin/ProductsView.vue'),
|
||||
meta: { requiresAuth: true, isAdmin: true, hideNav: true }
|
||||
meta: { requiresAuth: true, isAdmin: true, hideNav: true, permissionCode: 'product:view' }
|
||||
},
|
||||
{
|
||||
path: 'shops',
|
||||
name: 'admin-shops',
|
||||
component: () => import('../views/admin/ShopsView.vue'),
|
||||
meta: { requiresAuth: true, isAdmin: true, hideNav: true }
|
||||
meta: { requiresAuth: true, isAdmin: true, hideNav: true, permissionCode: 'shop:view' }
|
||||
},
|
||||
{
|
||||
path: 'permissions',
|
||||
name: 'admin-permissions',
|
||||
component: () => import('../views/admin/PermissionsView.vue'),
|
||||
meta: { requiresAuth: true, isAdmin: true, hideNav: true }
|
||||
meta: { requiresAuth: true, isAdmin: true, hideNav: true, permissionCode: 'permission:view' }
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -18,35 +18,14 @@
|
||||
theme="dark"
|
||||
@click="handleMenuClick"
|
||||
>
|
||||
<a-menu-item key="/admin/dashboard">
|
||||
<a-menu-item
|
||||
v-for="item in menuItems"
|
||||
:key="item.key"
|
||||
>
|
||||
<template #icon>
|
||||
<DashboardOutlined />
|
||||
<component :is="item.icon" />
|
||||
</template>
|
||||
<span>仪表盘</span>
|
||||
</a-menu-item>
|
||||
<a-menu-item key="/admin/users">
|
||||
<template #icon>
|
||||
<UserOutlined />
|
||||
</template>
|
||||
<span>用户管理</span>
|
||||
</a-menu-item>
|
||||
<a-menu-item key="/admin/products">
|
||||
<template #icon>
|
||||
<ShopOutlined />
|
||||
</template>
|
||||
<span>商品管理</span>
|
||||
</a-menu-item>
|
||||
<a-menu-item key="/admin/shops">
|
||||
<template #icon>
|
||||
<HomeOutlined />
|
||||
</template>
|
||||
<span>店铺管理</span>
|
||||
</a-menu-item>
|
||||
<a-menu-item key="/admin/permissions">
|
||||
<template #icon>
|
||||
<SafetyCertificateOutlined />
|
||||
</template>
|
||||
<span>权限管理</span>
|
||||
<span>{{ item.label }}</span>
|
||||
</a-menu-item>
|
||||
</a-menu>
|
||||
</a-layout-sider>
|
||||
@@ -100,7 +79,7 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, computed, onMounted } from 'vue'
|
||||
import { ref, computed, onMounted, watch } from 'vue'
|
||||
import { useRouter, useRoute } from 'vue-router'
|
||||
import {
|
||||
MenuFoldOutlined,
|
||||
@@ -117,14 +96,44 @@ import { message, Modal } from 'ant-design-vue'
|
||||
import { userApi, type UserVO } from '@/api/user'
|
||||
import { authApi } from '@/api/auth'
|
||||
import { DEFAULT_AVATAR } from '@/constants'
|
||||
import { usePermission } from '@/utils/permission'
|
||||
|
||||
const router = useRouter()
|
||||
const route = useRoute()
|
||||
|
||||
const collapsed = ref(false)
|
||||
const userInfo = ref<UserVO | null>(null)
|
||||
const { loadUserPermissions, hasPermission, clearPermissions } = usePermission()
|
||||
|
||||
const selectedKeys = computed(() => [route.path])
|
||||
const menuItems = [
|
||||
{
|
||||
key: '/admin/dashboard',
|
||||
icon: DashboardOutlined,
|
||||
label: '仪表盘'
|
||||
},
|
||||
{
|
||||
key: '/admin/users',
|
||||
icon: UserOutlined,
|
||||
label: '用户管理'
|
||||
},
|
||||
{
|
||||
key: '/admin/products',
|
||||
icon: ShopOutlined,
|
||||
label: '商品管理'
|
||||
},
|
||||
{
|
||||
key: '/admin/shops',
|
||||
icon: HomeOutlined,
|
||||
label: '店铺管理'
|
||||
},
|
||||
{
|
||||
key: '/admin/permissions',
|
||||
icon: SafetyCertificateOutlined,
|
||||
label: '权限管理'
|
||||
}
|
||||
]
|
||||
|
||||
const selectedKeys = ref<string[]>([route.path])
|
||||
|
||||
const toggleCollapsed = () => {
|
||||
collapsed.value = !collapsed.value
|
||||
@@ -150,7 +159,10 @@ const handleLogout = () => {
|
||||
} finally {
|
||||
localStorage.removeItem('accessToken')
|
||||
localStorage.removeItem('refreshToken')
|
||||
localStorage.removeItem('adminAccessToken')
|
||||
sessionStorage.removeItem('accessToken')
|
||||
sessionStorage.removeItem('adminAccessToken')
|
||||
clearPermissions()
|
||||
message.success('已退出登录')
|
||||
router.push('/auth')
|
||||
}
|
||||
@@ -167,8 +179,15 @@ const fetchUserInfo = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
fetchUserInfo()
|
||||
watch(() => route.path, (newPath) => {
|
||||
selectedKeys.value = [newPath]
|
||||
}, { immediate: true })
|
||||
|
||||
onMounted(async () => {
|
||||
await Promise.all([
|
||||
fetchUserInfo(),
|
||||
loadUserPermissions()
|
||||
])
|
||||
})
|
||||
</script>
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@
|
||||
@search="handleUserSearch"
|
||||
/>
|
||||
<a-list
|
||||
:data-source="userList"
|
||||
:data-source="filteredUserList"
|
||||
:loading="userLoading"
|
||||
item-layout="horizontal"
|
||||
size="small"
|
||||
@@ -72,7 +72,7 @@
|
||||
<a-avatar :src="item.avatar || DEFAULT_AVATAR" />
|
||||
</template>
|
||||
<template #title>{{ item.nickname }}</template>
|
||||
<template #description>{{ item.account }}</template>
|
||||
<template #description>{{ item.account }} - {{ getUserTypeText(item.type) }}</template>
|
||||
</a-list-item-meta>
|
||||
</a-list-item>
|
||||
</template>
|
||||
@@ -228,6 +228,7 @@ const selectedUser = ref<UserVO | null>(null)
|
||||
const assignLoading = ref(false)
|
||||
const selectedPermissions = ref<number[]>([])
|
||||
const activeCollapseKeys = ref<string[]>(['商品管理', '店铺管理', '用户管理', '权限管理'])
|
||||
const currentLoginUser = ref<UserVO | null>(null)
|
||||
|
||||
const groupedPermissions = computed(() => {
|
||||
const groups: Record<string, PermissionVO[]> = {}
|
||||
@@ -250,6 +251,20 @@ const getUserTypeText = (type: number) => {
|
||||
return map[type] || '未知'
|
||||
}
|
||||
|
||||
const filteredUserList = computed(() => {
|
||||
if (!currentLoginUser.value) return []
|
||||
return userList.value.filter(user => user.type < currentLoginUser.value!.type)
|
||||
})
|
||||
|
||||
const fetchCurrentUser = async () => {
|
||||
try {
|
||||
const res = await userApi.getUserInfo()
|
||||
currentLoginUser.value = res as unknown as UserVO
|
||||
} catch (error) {
|
||||
console.error('获取当前用户信息失败:', error)
|
||||
}
|
||||
}
|
||||
|
||||
const fetchPermissionList = async () => {
|
||||
loading.value = true
|
||||
try {
|
||||
@@ -373,7 +388,8 @@ const handleDeletePermission = async (record: PermissionVO) => {
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
onMounted(async () => {
|
||||
await fetchCurrentUser()
|
||||
fetchPermissionList()
|
||||
fetchUserList()
|
||||
})
|
||||
|
||||
@@ -170,14 +170,33 @@ const columns = [
|
||||
{ title: '操作', key: 'actions', width: 150, fixed: 'right' as const }
|
||||
]
|
||||
|
||||
const getCurrentUserInfo = () => {
|
||||
try {
|
||||
const userInfoStr = localStorage.getItem('adminUserInfo')
|
||||
if (userInfoStr) {
|
||||
return JSON.parse(userInfoStr)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取当前用户信息失败', error)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
const fetchProductList = async () => {
|
||||
loading.value = true
|
||||
try {
|
||||
const res = await adminProductApi.listProducts({
|
||||
const currentUser = getCurrentUserInfo()
|
||||
const params: any = {
|
||||
page: pagination.current,
|
||||
size: pagination.pageSize,
|
||||
keyword: searchText.value
|
||||
})
|
||||
}
|
||||
|
||||
if (currentUser && currentUser.type !== 4 && currentUser.shopId) {
|
||||
params.shopId = currentUser.shopId
|
||||
}
|
||||
|
||||
const res = await adminProductApi.listProducts(params)
|
||||
productList.value = res.list || []
|
||||
pagination.total = res.total || 0
|
||||
} catch (error) {
|
||||
|
||||
@@ -146,14 +146,33 @@ const columns = [
|
||||
{ title: '操作', key: 'actions', width: 150, fixed: 'right' as const }
|
||||
]
|
||||
|
||||
const getCurrentUserInfo = () => {
|
||||
try {
|
||||
const userInfoStr = localStorage.getItem('adminUserInfo')
|
||||
if (userInfoStr) {
|
||||
return JSON.parse(userInfoStr)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取当前用户信息失败', error)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
const fetchShopList = async () => {
|
||||
loading.value = true
|
||||
try {
|
||||
const res = await adminShopApi.listShops({
|
||||
const currentUser = getCurrentUserInfo()
|
||||
const params: any = {
|
||||
page: pagination.current,
|
||||
size: pagination.pageSize,
|
||||
keyword: searchText.value
|
||||
})
|
||||
}
|
||||
|
||||
if (currentUser && currentUser.type !== 4 && currentUser.shopId) {
|
||||
params.shopId = currentUser.shopId
|
||||
}
|
||||
|
||||
const res = await adminShopApi.listShops(params)
|
||||
shopList.value = res.list || []
|
||||
pagination.total = res.total || 0
|
||||
} catch (error) {
|
||||
|
||||
@@ -177,14 +177,33 @@ const getUserTypeColor = (type: number) => {
|
||||
return map[type] || 'default'
|
||||
}
|
||||
|
||||
const getCurrentUserInfo = () => {
|
||||
try {
|
||||
const userInfoStr = localStorage.getItem('adminUserInfo')
|
||||
if (userInfoStr) {
|
||||
return JSON.parse(userInfoStr)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取当前用户信息失败', error)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
const fetchUserList = async () => {
|
||||
loading.value = true
|
||||
try {
|
||||
const res = await userApi.listUsers({
|
||||
const currentUser = getCurrentUserInfo()
|
||||
const params: any = {
|
||||
page: pagination.current,
|
||||
size: pagination.pageSize,
|
||||
keyword: searchText.value
|
||||
})
|
||||
}
|
||||
|
||||
if (currentUser && currentUser.type !== 4 && currentUser.shopId) {
|
||||
params.shopId = currentUser.shopId
|
||||
}
|
||||
|
||||
const res = await userApi.listUsers(params)
|
||||
userList.value = res.list || []
|
||||
pagination.total = res.total || 0
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user