This commit is contained in:
2026-02-09 23:27:16 +08:00
parent adf7457502
commit af366d76b2
8 changed files with 3282 additions and 1 deletions

View File

@@ -0,0 +1,466 @@
<template>
<div class="permission-management">
<!-- 搜索表单 -->
<a-card class="search-card" :bordered="false">
<a-form :model="searchParams" layout="inline">
<a-form-item label="关键词">
<a-input v-model:value="searchParams.keyword" placeholder="权限名称/编码" allow-clear style="width: 200px" />
</a-form-item>
<a-form-item label="类型">
<a-select v-model:value="searchParams.type" allow-clear placeholder="请选择类型" style="width: 150px">
<a-select-option value="menu">菜单</a-select-option>
<a-select-option value="api">接口</a-select-option>
<a-select-option value="button">按钮</a-select-option>
</a-select>
</a-form-item>
<a-form-item label="状态">
<a-select v-model:value="searchParams.status" allow-clear placeholder="请选择状态" style="width: 120px">
<a-select-option :value="1">启用</a-select-option>
<a-select-option :value="0">禁用</a-select-option>
</a-select>
</a-form-item>
<a-form-item>
<a-space>
<a-button type="primary" @click="handleSearch">
<template #icon><SearchOutlined /></template>
搜索
</a-button>
<a-button @click="handleReset">
<template #icon><RedoOutlined /></template>
重置
</a-button>
</a-space>
</a-form-item>
</a-form>
</a-card>
<!-- 操作按钮 -->
<a-card class="table-card" :bordered="false">
<template #title>
<a-space>
<a-button type="primary" @click="handleAdd">
<template #icon><PlusOutlined /></template>
新增
</a-button>
<a-button v-if="selectedRowKeys.length > 0" @click="handleBatchDelete" danger>
<template #icon><DeleteOutlined /></template>
批量删除
</a-button>
<a-button v-if="selectedRowKeys.length > 0" @click="handleBatchStatus">
<template #icon><CheckOutlined /></template>
批量启用
</a-button>
<a-button v-if="selectedRowKeys.length > 0" @click="handleBatchStatus(false)" danger>
<template #icon><CloseOutlined /></template>
批量禁用
</a-button>
<a-switch
v-model:checked="viewMode"
checked-children="树形"
un-checked-children="列表"
@change="handleViewModeChange"
/>
</a-space>
</template>
<!-- 树形表格 -->
<a-table
v-if="viewMode"
:columns="treeColumns"
:data-source="treeDataSource"
:loading="loading"
:pagination="false"
:row-selection="treeRowSelection"
row-key="id"
>
<template #bodyCell="{ column, record }">
<template v-if="column.key === 'name'">
<a-space>
<a-icon v-if="record.icon" :type="record.icon" />
<span>{{ record.name }}</span>
</a-space>
</template>
<template v-if="column.key === 'type'">
<a-tag :color="getTypeColor(record.type)">
{{ getTypeLabel(record.type) }}
</a-tag>
</template>
<template v-if="column.key === 'status'">
<a-tag :color="record.status === 1 ? 'success' : 'error'">
{{ record.status === 1 ? '启用' : '禁用' }}
</a-tag>
</template>
<template v-if="column.key === 'action'">
<a-space>
<a-button type="link" size="small" @click="handleAddChild(record)">添加子权限</a-button>
<a-button type="link" size="small" @click="handleEdit(record)">编辑</a-button>
<a-popconfirm title="确定要删除该权限吗?" @confirm="handleDelete(record.id)">
<a-button type="link" size="small" danger>删除</a-button>
</a-popconfirm>
</a-space>
</template>
</template>
</a-table>
<!-- 列表表格 -->
<sc-table
v-else
:columns="columns"
:data-source="dataSource"
:loading="loading"
:pagination="pagination"
:row-selection="rowSelection"
@page-change="handlePageChange"
>
<template #bodyCell="{ column, record }">
<template v-if="column.key === 'name'">
<a-space>
<a-icon v-if="record.icon" :type="record.icon" />
<span>{{ record.name }}</span>
</a-space>
</template>
<template v-if="column.key === 'type'">
<a-tag :color="getTypeColor(record.type)">
{{ getTypeLabel(record.type) }}
</a-tag>
</template>
<template v-if="column.key === 'status'">
<a-tag :color="record.status === 1 ? 'success' : 'error'">
{{ record.status === 1 ? '启用' : '禁用' }}
</a-tag>
</template>
<template v-if="column.key === 'action'">
<a-space>
<a-button type="link" size="small" @click="handleAddChild(record)">添加子权限</a-button>
<a-button type="link" size="small" @click="handleEdit(record)">编辑</a-button>
<a-popconfirm title="确定要删除该权限吗?" @confirm="handleDelete(record.id)">
<a-button type="link" size="small" danger>删除</a-button>
</a-popconfirm>
</a-space>
</template>
</template>
</sc-table>
</a-card>
<!-- 新增/编辑弹窗 -->
<a-modal
v-model:open="modalVisible"
:title="modalTitle"
:width="700"
@ok="handleSubmit"
@cancel="handleCancel"
>
<a-form ref="formRef" :model="formData" :rules="formRules" :label-col="{ span: 6 }">
<a-form-item label="上级权限" name="parent_id">
<a-tree-select
v-model:value="formData.parent_id"
:tree-data="permissionTree"
allow-clear
placeholder="请选择上级权限"
:field-names="{ label: 'name', value: 'id', children: 'children' }"
tree-default-expand-all
/>
</a-form-item>
<a-form-item label="权限名称" name="name">
<a-input v-model:value="formData.name" placeholder="请输入权限名称" />
</a-form-item>
<a-form-item label="权限编码" name="code">
<a-input v-model:value="formData.code" placeholder="请输入权限编码system.user.list" />
</a-form-item>
<a-form-item label="权限类型" name="type">
<a-select v-model:value="formData.type" placeholder="请选择权限类型">
<a-select-option value="menu">菜单</a-select-option>
<a-select-option value="api">接口</a-select-option>
<a-select-option value="button">按钮</a-select-option>
</a-select>
</a-form-item>
<a-form-item label="路由" name="route">
<a-input v-model:value="formData.route" placeholder="请输入路由,如:/admin/users" />
</a-form-item>
<a-form-item label="组件" name="component">
<a-input v-model:value="formData.component" placeholder="请输入组件路径" />
</a-form-item>
<a-form-item label="图标" name="icon">
<a-input v-model:value="formData.icon" placeholder="请输入图标名称" />
</a-form-item>
<a-form-item label="排序" name="sort">
<a-input-number v-model:value="formData.sort" :min="0" :max="9999" style="width: 100%" />
</a-form-item>
<a-form-item label="状态" name="status">
<a-radio-group v-model:value="formData.status">
<a-radio :value="1">启用</a-radio>
<a-radio :value="0">禁用</a-radio>
</a-radio-group>
</a-form-item>
<a-form-item label="元数据" name="meta">
<a-textarea v-model:value="formData.meta" placeholder="JSON格式的元数据" :rows="3" />
</a-form-item>
</a-form>
</a-modal>
</div>
</template>
<script setup>
import { ref, computed, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import {
SearchOutlined,
RedoOutlined,
PlusOutlined,
DeleteOutlined,
CheckOutlined,
CloseOutlined,
} from '@ant-design/icons-vue'
import authApi from '@/api/auth'
import scTable from '@/components/scTable/index.vue'
defineOptions({
name: 'PermissionManagement',
})
const searchParams = ref({
keyword: '',
type: undefined,
status: undefined,
})
const loading = ref(false)
const dataSource = ref([])
const treeDataSource = ref([])
const pagination = ref({
current: 1,
pageSize: 20,
total: 0,
})
const viewMode = ref(true)
const selectedRowKeys = ref([])
const rowSelection = computed(() => ({
selectedRowKeys: selectedRowKeys.value,
onChange: (keys) => {
selectedRowKeys.value = keys
},
}))
const treeRowSelection = computed(() => ({
selectedRowKeys: selectedRowKeys.value,
onChange: (keys) => {
selectedRowKeys.value = keys
},
}))
const columns = [
{ title: 'ID', dataIndex: 'id', key: 'id', width: 80 },
{ title: '权限名称', key: 'name', width: 200 },
{ title: '权限编码', dataIndex: 'code', key: 'code', width: 200 },
{ title: '类型', key: 'type', width: 100, align: 'center' },
{ title: '路由', dataIndex: 'route', key: 'route', width: 200 },
{ title: '组件', dataIndex: 'component', key: 'component', width: 200 },
{ title: '图标', dataIndex: 'icon', key: 'icon', width: 100 },
{ title: '排序', dataIndex: 'sort', key: 'sort', width: 80, align: 'center' },
{ title: '状态', key: 'status', width: 80, align: 'center' },
{ title: '创建时间', dataIndex: 'created_at', key: 'created_at', width: 180 },
{ title: '操作', key: 'action', width: 220, fixed: 'right' },
]
const treeColumns = [
{ title: '权限名称', key: 'name', width: 300 },
{ title: '权限编码', dataIndex: 'code', key: 'code', width: 200 },
{ title: '类型', key: 'type', width: 100, align: 'center' },
{ title: '路由', dataIndex: 'route', key: 'route', width: 200 },
{ title: '排序', dataIndex: 'sort', key: 'sort', width: 80, align: 'center' },
{ title: '状态', key: 'status', width: 80, align: 'center' },
{ title: '操作', key: 'action', width: 220, fixed: 'right' },
]
const permissionTree = ref([])
const modalVisible = ref(false)
const modalTitle = ref('新增权限')
const formData = ref({})
const formRef = ref(null)
const formRules = {
name: [{ required: true, message: '请输入权限名称', trigger: 'blur' }],
code: [{ required: true, message: '请输入权限编码', trigger: 'blur' }],
type: [{ required: true, message: '请选择权限类型', trigger: 'change' }],
}
const fetchData = async () => {
loading.value = true
try {
if (viewMode.value) {
const res = await authApi.permissions.tree.get()
treeDataSource.value = res.data
} else {
const res = await authApi.permissions.list.get({
page: pagination.value.current,
page_size: pagination.value.pageSize,
...searchParams.value,
})
dataSource.value = res.data.list
pagination.value.total = res.data.total
}
} catch (error) {
message.error('获取权限列表失败')
} finally {
loading.value = false
}
}
const fetchPermissionTree = async () => {
try {
const res = await authApi.permissions.tree.get()
permissionTree.value = [{ id: 0, name: '顶级权限', children: res.data }]
} catch (error) {
message.error('获取权限树失败')
}
}
const handleSearch = () => {
pagination.value.current = 1
fetchData()
}
const handleReset = () => {
searchParams.value = {
keyword: '',
type: undefined,
status: undefined,
}
pagination.value.current = 1
fetchData()
}
const handleViewModeChange = () => {
selectedRowKeys.value = []
fetchData()
}
const handlePageChange = (page) => {
pagination.value.current = page
fetchData()
}
const handleAdd = () => {
modalTitle.value = '新增权限'
formData.value = {
parent_id: 0,
status: 1,
sort: 0,
}
modalVisible.value = true
}
const handleAddChild = (record) => {
modalTitle.value = '新增子权限'
formData.value = {
parent_id: record.id,
status: 1,
sort: 0,
}
modalVisible.value = true
}
const handleEdit = (record) => {
modalTitle.value = '编辑权限'
formData.value = { ...record }
modalVisible.value = true
}
const handleSubmit = async () => {
try {
await formRef.value.validate()
if (formData.value.id) {
await authApi.permissions.edit.put(formData.value.id, formData.value)
message.success('更新成功')
} else {
await authApi.permissions.add.post(formData.value)
message.success('创建成功')
}
modalVisible.value = false
fetchData()
} catch (error) {
if (error.errorFields) {
return
}
message.error(error.message || '操作失败')
}
}
const handleCancel = () => {
modalVisible.value = false
formData.value = {}
formRef.value?.resetFields()
}
const handleDelete = async (id) => {
try {
await authApi.permissions.delete.delete(id)
message.success('删除成功')
fetchData()
} catch (error) {
message.error('删除失败')
}
}
const handleBatchDelete = async () => {
try {
await authApi.permissions.batchDelete.post({ ids: selectedRowKeys.value })
message.success('批量删除成功')
selectedRowKeys.value = []
fetchData()
} catch (error) {
message.error('批量删除失败')
}
}
const handleBatchStatus = async (status = 1) => {
try {
await authApi.permissions.batchStatus.post({ ids: selectedRowKeys.value, status })
message.success(status ? '批量启用成功' : '批量禁用成功')
selectedRowKeys.value = []
fetchData()
} catch (error) {
message.error('操作失败')
}
}
const getTypeColor = (type) => {
const colors = {
menu: 'blue',
api: 'green',
button: 'orange',
}
return colors[type] || 'default'
}
const getTypeLabel = (type) => {
const labels = {
menu: '菜单',
api: '接口',
button: '按钮',
}
return labels[type] || type
}
onMounted(() => {
fetchData()
fetchPermissionTree()
})
</script>
<style scoped lang="scss">
.permission-management {
padding: 16px;
.search-card {
margin-bottom: 16px;
}
.table-card {
:deep(.ant-card-body) {
padding: 16px;
}
}
}
</style>