完善权限的管理和校验

This commit is contained in:
xiaowang
2026-02-26 18:44:47 +08:00
parent 2e0f615775
commit 65388cb7f9
4 changed files with 37 additions and 23 deletions
+8 -5
View File
@@ -6,7 +6,7 @@
<a-tabs v-model:activeKey="activeTab"> <a-tabs v-model:activeKey="activeTab">
<a-tab-pane key="permissions" tab="权限列表"> <a-tab-pane key="permissions" tab="权限列表">
<template #extra> <template #extra>
<a-button v-if="hasPermission('permission:create')" type="primary" @click="showAddPermissionModal"> <a-button v-if="currentLoginUser?.type === 4 || hasPermission('permission:create')" type="primary" @click="showAddPermissionModal">
<template #icon> <template #icon>
<PlusOutlined /> <PlusOutlined />
</template> </template>
@@ -28,11 +28,11 @@
</template> </template>
<template v-else-if="column.key === 'actions'"> <template v-else-if="column.key === 'actions'">
<a-space> <a-space>
<a-button v-if="hasPermission('permission:edit')" type="link" size="small" @click="showEditPermissionModal(record)"> <a-button v-if="currentLoginUser?.type === 4 || hasPermission('permission:edit')" type="link" size="small" @click="showEditPermissionModal(record)">
编辑 编辑
</a-button> </a-button>
<a-popconfirm <a-popconfirm
v-if="hasPermission('permission:delete')" v-if="currentLoginUser?.type === 4 || hasPermission('permission:delete')"
title="确定要删除这个权限吗?" title="确定要删除这个权限吗?"
@confirm="handleDeletePermission(record)" @confirm="handleDeletePermission(record)"
> >
@@ -86,7 +86,7 @@
:bordered="false" :bordered="false"
> >
<template #extra> <template #extra>
<a-button v-if="selectedUser && hasPermission('permission:assign')" type="primary" @click="handleSaveUserPermissions"> <a-button v-if="selectedUser && (currentLoginUser?.type === 4 || hasPermission('permission:assign'))" type="primary" @click="handleSaveUserPermissions">
保存权限 保存权限
</a-button> </a-button>
</template> </template>
@@ -181,7 +181,7 @@ import { DEFAULT_AVATAR } from '@/constants'
import { permissionApi, type PermissionVO, userApi, type UserVO } from '@/api/admin' import { permissionApi, type PermissionVO, userApi, type UserVO } from '@/api/admin'
import { usePermission } from '@/utils/permission' import { usePermission } from '@/utils/permission'
const { hasPermission } = usePermission() const { hasPermission, loadUserPermissions } = usePermission()
const activeTab = ref('permissions') const activeTab = ref('permissions')
const loading = ref(false) const loading = ref(false)
@@ -326,6 +326,8 @@ const handleSaveUserPermissions = async () => {
try { try {
await permissionApi.assignUserPermissions(selectedUser.value.id, selectedPermissions.value) await permissionApi.assignUserPermissions(selectedUser.value.id, selectedPermissions.value)
message.success('权限保存成功') message.success('权限保存成功')
// 重新加载当前用户的权限
await loadUserPermissions()
} catch (error) { } catch (error) {
message.error('权限保存失败') message.error('权限保存失败')
} }
@@ -393,6 +395,7 @@ const handleDeletePermission = async (record: PermissionVO) => {
onMounted(async () => { onMounted(async () => {
await fetchCurrentUser() await fetchCurrentUser()
await loadUserPermissions()
fetchPermissionList() fetchPermissionList()
fetchUserList() fetchUserList()
}) })
+6 -5
View File
@@ -11,7 +11,7 @@
style="width: 250px" style="width: 250px"
@search="handleSearch" @search="handleSearch"
/> />
<a-button v-if="hasPermission('product:create')" type="primary" @click="showAddModal"> <a-button v-if="isAdmin || hasPermission('product:create')" type="primary" @click="showAddModal">
<template #icon> <template #icon>
<PlusOutlined /> <PlusOutlined />
</template> </template>
@@ -58,11 +58,11 @@
<template v-else-if="column.key === 'actions'"> <template v-else-if="column.key === 'actions'">
<a-space> <a-space>
<a-button v-if="hasPermission('product:edit')" type="link" size="small" @click="showEditModal(record)"> <a-button v-if="isAdmin || hasPermission('product:edit')" type="link" size="small" @click="showEditModal(record)">
编辑 编辑
</a-button> </a-button>
<a-popconfirm <a-popconfirm
v-if="hasPermission('product:delete')" v-if="isAdmin || hasPermission('product:delete')"
title="确定要删除这个商品吗?" title="确定要删除这个商品吗?"
@confirm="handleDelete(record)" @confirm="handleDelete(record)"
> >
@@ -249,6 +249,7 @@ import { DEFAULT_PRODUCT_IMAGE } from '@/constants'
import { adminProductApi, type Product, adminShopApi, type Shop, adminFileApi, type ObjectStorageFileVO, adminTagApi, adminArtistApi, adminCouponApi } from '@/api/admin' import { adminProductApi, type Product, adminShopApi, type Shop, adminFileApi, type ObjectStorageFileVO, adminTagApi, adminArtistApi, adminCouponApi } from '@/api/admin'
import { usePermission } from '@/utils/permission' import { usePermission } from '@/utils/permission'
const { hasPermission, loadUserPermissions } = usePermission()
const loading = ref(false) const loading = ref(false)
const searchText = ref('') const searchText = ref('')
const productList = ref<Product[]>([]) const productList = ref<Product[]>([])
@@ -272,7 +273,6 @@ const imageUrls = ref<Record<number, string[]>>({})
const imageIdCache = ref<Record<string, string>>({}) const imageIdCache = ref<Record<string, string>>({})
const isAdmin = ref(false) const isAdmin = ref(false)
const formRef = ref<any>(null) const formRef = ref<any>(null)
const { hasPermission } = usePermission()
const pagination = reactive({ const pagination = reactive({
current: 1, current: 1,
@@ -672,7 +672,8 @@ const handleDelete = async (record: Product) => {
} }
} }
onMounted(() => { onMounted(async () => {
await loadUserPermissions()
fetchShops() fetchShops()
fetchTags() fetchTags()
fetchCoupons() fetchCoupons()
+11 -6
View File
@@ -11,7 +11,7 @@
style="width: 250px" style="width: 250px"
@search="handleSearch" @search="handleSearch"
/> />
<a-button v-if="hasPermission('shop:create')" type="primary" @click="showAddModal"> <a-button v-if="isAdmin || hasPermission('shop:create')" type="primary" @click="showAddModal">
<template #icon> <template #icon>
<PlusOutlined /> <PlusOutlined />
</template> </template>
@@ -52,11 +52,11 @@
</template> </template>
<template v-else-if="column.key === 'actions'"> <template v-else-if="column.key === 'actions'">
<a-space> <a-space>
<a-button v-if="hasPermission('shop:edit')" type="link" size="small" @click="showEditModal(record)"> <a-button v-if="isAdmin || hasPermission('shop:edit')" type="link" size="small" @click="showEditModal(record)">
编辑 编辑
</a-button> </a-button>
<a-popconfirm <a-popconfirm
v-if="hasPermission('shop:delete')" v-if="isAdmin || hasPermission('shop:delete')"
title="确定要删除这个店铺吗?" title="确定要删除这个店铺吗?"
@confirm="handleDelete(record)" @confirm="handleDelete(record)"
> >
@@ -134,7 +134,7 @@ import { DEFAULT_SHOP_IMAGE } from '@/constants'
import { adminShopApi, adminFileApi, type Shop } from '@/api/admin' import { adminShopApi, adminFileApi, type Shop } from '@/api/admin'
import { usePermission } from '@/utils/permission' import { usePermission } from '@/utils/permission'
const { hasPermission } = usePermission() const { hasPermission, loadUserPermissions } = usePermission()
const loading = ref(false) const loading = ref(false)
const searchText = ref('') const searchText = ref('')
@@ -144,6 +144,7 @@ const modalLoading = ref(false)
const isEdit = ref(false) const isEdit = ref(false)
const formRef = ref<FormInstance>() const formRef = ref<FormInstance>()
const currentShop = ref<Shop | null>(null) const currentShop = ref<Shop | null>(null)
const isAdmin = ref(false)
// 图片相关 // 图片相关
const imageFileList = ref<any[]>([]) const imageFileList = ref<any[]>([])
@@ -191,7 +192,10 @@ const getCurrentUserInfo = () => {
try { try {
const userInfoStr = localStorage.getItem('adminUserInfo') const userInfoStr = localStorage.getItem('adminUserInfo')
if (userInfoStr) { if (userInfoStr) {
return JSON.parse(userInfoStr) const userInfo = JSON.parse(userInfoStr)
// 判断用户是否为管理员(类型为4)
isAdmin.value = userInfo.type === 4
return userInfo
} }
} catch (error) { } catch (error) {
console.error('获取当前用户信息失败', error) console.error('获取当前用户信息失败', error)
@@ -422,7 +426,8 @@ const handleDelete = async (record: Shop) => {
} }
} }
onMounted(() => { onMounted(async () => {
await loadUserPermissions()
fetchShopList() fetchShopList()
}) })
</script> </script>
+12 -7
View File
@@ -11,7 +11,7 @@
style="width: 250px" style="width: 250px"
@search="handleSearch" @search="handleSearch"
/> />
<a-button v-if="hasPermission('user:create')" type="primary" @click="showAddModal"> <a-button v-if="isAdmin || hasPermission('user:create')" type="primary" @click="showAddModal">
<template #icon> <template #icon>
<PlusOutlined /> <PlusOutlined />
</template> </template>
@@ -48,14 +48,14 @@
</template> </template>
<template v-else-if="column.key === 'actions'"> <template v-else-if="column.key === 'actions'">
<a-space> <a-space>
<a-button v-if="hasPermission('user:edit')" type="link" size="small" @click="showEditModal(record)"> <a-button v-if="isAdmin || hasPermission('user:edit')" type="link" size="small" @click="showEditModal(record)">
编辑 编辑
</a-button> </a-button>
<a-button v-if="hasPermission('user:reset_password')" type="link" size="small" @click="handleResetPassword(record)"> <a-button v-if="isAdmin || hasPermission('user:reset_password')" type="link" size="small" @click="handleResetPassword(record)">
重置密码 重置密码
</a-button> </a-button>
<a-popconfirm <a-popconfirm
v-if="hasPermission('user:delete')" v-if="isAdmin || hasPermission('user:delete')"
title="确定要删除这个用户吗?" title="确定要删除这个用户吗?"
@confirm="handleDelete(record)" @confirm="handleDelete(record)"
> >
@@ -148,13 +148,16 @@ import { DEFAULT_AVATAR } from '@/constants'
import { userApi, type UserVO, adminShopApi, type Shop } from '@/api/admin' import { userApi, type UserVO, adminShopApi, type Shop } from '@/api/admin'
import { usePermission } from '@/utils/permission' import { usePermission } from '@/utils/permission'
const { hasPermission } = usePermission() const { hasPermission, loadUserPermissions } = usePermission()
const getCurrentUserInfo = () => { const getCurrentUserInfo = () => {
try { try {
const userInfoStr = localStorage.getItem('adminUserInfo') const userInfoStr = localStorage.getItem('adminUserInfo')
if (userInfoStr) { if (userInfoStr) {
return JSON.parse(userInfoStr) const userInfo = JSON.parse(userInfoStr)
// 判断用户是否为管理员(类型为4)
isAdmin.value = userInfo.type === 4
return userInfo
} }
} catch (error) { } catch (error) {
console.error('获取当前用户信息失败', error) console.error('获取当前用户信息失败', error)
@@ -170,6 +173,7 @@ const shopList = ref<Shop[]>([])
const modalVisible = ref(false) const modalVisible = ref(false)
const modalLoading = ref(false) const modalLoading = ref(false)
const isEdit = ref(false) const isEdit = ref(false)
const isAdmin = ref(false)
const formRef = ref<FormInstance>() const formRef = ref<FormInstance>()
const currentUser = ref<UserVO | null>(null) const currentUser = ref<UserVO | null>(null)
const currentUserInfo = getCurrentUserInfo() const currentUserInfo = getCurrentUserInfo()
@@ -372,7 +376,8 @@ const handleDelete = async (record: UserVO) => {
} }
} }
onMounted(() => { onMounted(async () => {
await loadUserPermissions()
fetchShopList() fetchShopList()
fetchUserList() fetchUserList()
}) })