初始化
This commit is contained in:
@@ -0,0 +1,205 @@
|
||||
<template>
|
||||
<div class="form-page">
|
||||
<!-- 页面头部 -->
|
||||
<el-page-header @back="handleBack" :title="isEdit ? '编辑角色' : '新增角色'" />
|
||||
<el-divider />
|
||||
|
||||
<!-- 表单卡片 -->
|
||||
<el-card class="form-card" shadow="never">
|
||||
<el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
|
||||
<!-- 基本信息 -->
|
||||
<el-divider content-position="left">
|
||||
<span class="form-section-title">基本信息</span>
|
||||
</el-divider>
|
||||
|
||||
<el-form-item label="角色名称" prop="roleName">
|
||||
<el-input v-model="form.roleName" placeholder="请输入角色名称" />
|
||||
</el-form-item>
|
||||
<el-form-item label="角色编码" prop="roleCode">
|
||||
<el-input v-model="form.roleCode" placeholder="请输入角色编码" :disabled="isEdit" />
|
||||
</el-form-item>
|
||||
<el-form-item label="所属系统" prop="systemId">
|
||||
<el-select v-model="form.systemId" placeholder="请选择系统" style="width: 100%">
|
||||
<el-option
|
||||
v-for="sys in systemList"
|
||||
:key="sys.systemId"
|
||||
:label="sys.systemName"
|
||||
:value="sys.systemId"
|
||||
/>
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
<el-form-item label="状态" prop="status">
|
||||
<el-switch v-model="form.status" :active-value="1" :inactive-value="0" active-text="启用" inactive-text="禁用" />
|
||||
</el-form-item>
|
||||
|
||||
<!-- 其他信息 -->
|
||||
<el-divider content-position="left">
|
||||
<span class="form-section-title">其他信息</span>
|
||||
</el-divider>
|
||||
|
||||
<el-form-item label="描述">
|
||||
<el-input v-model="form.description" type="textarea" :rows="3" placeholder="请输入描述" />
|
||||
</el-form-item>
|
||||
|
||||
<!-- 按钮区域 -->
|
||||
<el-divider />
|
||||
<div class="form-footer">
|
||||
<el-button @click="handleBack">取消</el-button>
|
||||
<el-button type="primary" :loading="submitLoading" @click="handleSubmit">确定</el-button>
|
||||
</div>
|
||||
</el-form>
|
||||
</el-card>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { reactive, ref, onMounted } from 'vue'
|
||||
import { useRoute, useRouter } from 'vue-router'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import type { FormInstance, FormRules } from 'element-plus'
|
||||
import request from '@/utils/request'
|
||||
|
||||
interface SystemItem {
|
||||
id: string
|
||||
systemId: string
|
||||
systemName: string
|
||||
}
|
||||
|
||||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
|
||||
const isEdit = ref(false)
|
||||
const editId = ref<string>()
|
||||
const submitLoading = ref(false)
|
||||
const formRef = ref<FormInstance>()
|
||||
|
||||
/** 系统列表(下拉用) */
|
||||
const systemList = ref<SystemItem[]>([])
|
||||
|
||||
/** 表单数据 */
|
||||
const form = reactive({
|
||||
roleName: '',
|
||||
roleCode: '',
|
||||
systemId: undefined as string | undefined,
|
||||
description: '',
|
||||
status: 1,
|
||||
})
|
||||
|
||||
/** 校验规则 */
|
||||
const rules: FormRules = {
|
||||
roleName: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
|
||||
roleCode: [{ required: true, message: '请输入角色编码', trigger: 'blur' }],
|
||||
systemId: [{ required: true, message: '请选择所属系统', trigger: 'change' }],
|
||||
status: [{ required: true, message: '请选择状态', trigger: 'change' }],
|
||||
}
|
||||
|
||||
/** 加载系统列表 */
|
||||
async function fetchSystemList() {
|
||||
try {
|
||||
const res = await request.get('/zgapi/v1/auth/system/list')
|
||||
systemList.value = res.data ?? []
|
||||
} catch {
|
||||
systemList.value = []
|
||||
}
|
||||
}
|
||||
|
||||
/** 加载已有数据(编辑模式) */
|
||||
async function loadData() {
|
||||
const id = route.params.id as string
|
||||
if (!id) {
|
||||
isEdit.value = false
|
||||
return
|
||||
}
|
||||
isEdit.value = true
|
||||
editId.value = id
|
||||
try {
|
||||
const res = await request.get(`/zgapi/v1/admin/role/${editId.value}`)
|
||||
const data = res.data
|
||||
if (data) {
|
||||
Object.assign(form, {
|
||||
roleName: data.roleName ?? '',
|
||||
roleCode: data.roleCode ?? '',
|
||||
systemId: data.systemId,
|
||||
description: data.description ?? '',
|
||||
status: data.status ?? 1,
|
||||
})
|
||||
setTimeout(() => formRef.value?.clearValidate(), 0)
|
||||
}
|
||||
} catch {
|
||||
ElMessage.error('加载角色信息失败')
|
||||
router.back()
|
||||
}
|
||||
}
|
||||
|
||||
function handleBack() {
|
||||
router.back()
|
||||
}
|
||||
|
||||
async function handleSubmit() {
|
||||
const valid = await formRef.value?.validate().catch(() => false)
|
||||
if (!valid) return
|
||||
|
||||
submitLoading.value = true
|
||||
try {
|
||||
if (isEdit.value) {
|
||||
await request.put(`/zgapi/v1/admin/role/${editId.value}`, form)
|
||||
ElMessage.success('编辑成功')
|
||||
} else {
|
||||
await request.post('/zgapi/v1/admin/role', form)
|
||||
ElMessage.success('新增成功')
|
||||
}
|
||||
router.back()
|
||||
} catch {
|
||||
// 错误由拦截器处理
|
||||
} finally {
|
||||
submitLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
await fetchSystemList()
|
||||
await loadData()
|
||||
setTimeout(() => formRef.value?.clearValidate(), 0)
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.form-page {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0;
|
||||
}
|
||||
|
||||
:deep(.el-page-header) {
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
:deep(.el-page-header + .el-divider) {
|
||||
margin: 0 0 20px 0;
|
||||
}
|
||||
|
||||
.form-card {
|
||||
max-width: 700px;
|
||||
margin: 0 auto;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.form-section-title {
|
||||
font-size: 15px;
|
||||
font-weight: 600;
|
||||
color: #303133;
|
||||
}
|
||||
|
||||
.form-footer {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.form-card { max-width: 100%; }
|
||||
.form-footer { flex-direction: column; }
|
||||
.form-footer .el-button { width: 100%; }
|
||||
:deep(.el-form-item__label) { width: 70px !important; }
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,315 @@
|
||||
<template>
|
||||
<div class="page-wrapper">
|
||||
<!-- 搜索区域 -->
|
||||
<el-card shadow="never" class="search-card">
|
||||
<template #header>
|
||||
<div class="card-header-title">
|
||||
<el-icon><Search /></el-icon>
|
||||
<span>搜索条件</span>
|
||||
</div>
|
||||
</template>
|
||||
<el-form :model="query" inline>
|
||||
<el-form-item label="角色名称">
|
||||
<el-input v-model="query.roleName" placeholder="请输入角色名称" clearable />
|
||||
</el-form-item>
|
||||
<el-form-item label="所属系统">
|
||||
<el-select v-model="query.systemId" placeholder="全部" clearable style="width: 180px">
|
||||
<el-option
|
||||
v-for="sys in systemList"
|
||||
:key="sys.systemId"
|
||||
:label="sys.systemName"
|
||||
:value="sys.systemId"
|
||||
/>
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
<el-form-item>
|
||||
<el-button type="primary" @click="handleSearch">
|
||||
<el-icon><Search /></el-icon>
|
||||
搜索
|
||||
</el-button>
|
||||
<el-button @click="handleReset">重置</el-button>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
</el-card>
|
||||
|
||||
<!-- 操作栏 -->
|
||||
<div class="toolbar">
|
||||
<el-button type="primary" @click="handleAdd">
|
||||
<el-icon><Plus /></el-icon>
|
||||
新增角色
|
||||
</el-button>
|
||||
</div>
|
||||
|
||||
<!-- 表格 -->
|
||||
<el-card shadow="never">
|
||||
<el-table :data="tableData" border stripe v-loading="loading" style="width: 100%">
|
||||
<el-table-column prop="roleName" label="角色名称" width="140" />
|
||||
<el-table-column prop="roleCode" label="角色编码" width="140" />
|
||||
<el-table-column prop="systemName" label="所属系统" width="150" />
|
||||
<el-table-column prop="description" label="描述" min-width="160" show-overflow-tooltip />
|
||||
<el-table-column prop="status" label="状态" width="90" align="center">
|
||||
<template #default="{ row }">
|
||||
<el-tag :type="row.status === 1 ? 'success' : 'danger'" effect="dark" size="small">
|
||||
{{ row.status === 1 ? '启用' : '禁用' }}
|
||||
</el-tag>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="操作" width="310" align="center" class-name="ops-col">
|
||||
<template #default="{ row }">
|
||||
<div class="actions">
|
||||
<el-button type="primary" size="small" plain @click="handleEdit(row)">编辑</el-button>
|
||||
<el-button type="danger" size="small" plain @click="handleDelete(row)">删除</el-button>
|
||||
<el-button type="success" size="small" plain @click="handleAssignMenu(row)">分配菜单</el-button>
|
||||
</div>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<template #empty>
|
||||
<el-empty description="暂无角色数据" :image-size="100" />
|
||||
</template>
|
||||
</el-table>
|
||||
|
||||
<div class="pagination-wrapper">
|
||||
<el-pagination
|
||||
v-model:current-page="query.page"
|
||||
v-model:page-size="query.pageSize"
|
||||
:total="total"
|
||||
:page-sizes="[10, 20, 50, 100]"
|
||||
layout="total, sizes, prev, pager, next, jumper"
|
||||
background
|
||||
@current-change="fetchData"
|
||||
@size-change="fetchData"
|
||||
/>
|
||||
</div>
|
||||
</el-card>
|
||||
|
||||
<!-- 分配菜单弹窗 -->
|
||||
<el-dialog
|
||||
v-model="menuVisible"
|
||||
title="分配菜单"
|
||||
width="500px"
|
||||
:close-on-click-modal="false"
|
||||
>
|
||||
<el-tree
|
||||
ref="menuTreeRef"
|
||||
:data="menuTreeData"
|
||||
node-key="id"
|
||||
show-checkbox
|
||||
default-expand-all
|
||||
:props="{ label: 'menuName', children: 'children' }"
|
||||
style="max-height: 400px; overflow-y: auto"
|
||||
/>
|
||||
<template #footer>
|
||||
<el-button @click="menuVisible = false">取消</el-button>
|
||||
<el-button type="primary" :loading="menuSubmitLoading" @click="submitAssignMenu">确定</el-button>
|
||||
</template>
|
||||
</el-dialog>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { reactive, ref } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||||
import { Search, Plus } from '@element-plus/icons-vue'
|
||||
import type { ElTree } from 'element-plus'
|
||||
import request from '@/utils/request'
|
||||
|
||||
/** 角色类型 */
|
||||
interface RoleItem {
|
||||
id?: string
|
||||
roleName: string
|
||||
roleCode: string
|
||||
systemId: string | undefined
|
||||
systemName?: string
|
||||
description: string
|
||||
status: number
|
||||
}
|
||||
|
||||
interface SystemItem {
|
||||
id: string
|
||||
systemId: string
|
||||
systemName: string
|
||||
}
|
||||
|
||||
interface MenuNode {
|
||||
id: string
|
||||
menuName: string
|
||||
children?: MenuNode[]
|
||||
}
|
||||
|
||||
// --- 查询 ---
|
||||
const query = reactive({
|
||||
page: 1,
|
||||
pageSize: 10,
|
||||
roleName: '',
|
||||
systemId: undefined as string | undefined,
|
||||
})
|
||||
|
||||
const tableData = ref<RoleItem[]>([])
|
||||
const total = ref(0)
|
||||
const loading = ref(false)
|
||||
const systemList = ref<SystemItem[]>([])
|
||||
|
||||
async function fetchSystemList() {
|
||||
try {
|
||||
const res = await request.get('/zgapi/v1/auth/system/list')
|
||||
systemList.value = res.data ?? []
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchData() {
|
||||
loading.value = true
|
||||
try {
|
||||
const params: Record<string, unknown> = {
|
||||
page: query.page,
|
||||
pageSize: query.pageSize,
|
||||
}
|
||||
if (query.roleName) params.roleName = query.roleName
|
||||
if (query.systemId != null) params.systemId = query.systemId
|
||||
|
||||
const res = await request.post('/zgapi/v1/admin/role/page', params)
|
||||
tableData.value = res.data?.records ?? []
|
||||
total.value = res.data?.total ?? 0
|
||||
} catch {
|
||||
// ignore
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function handleSearch() {
|
||||
query.page = 1
|
||||
fetchData()
|
||||
}
|
||||
|
||||
function handleReset() {
|
||||
query.roleName = ''
|
||||
query.systemId = undefined
|
||||
query.page = 1
|
||||
fetchData()
|
||||
}
|
||||
|
||||
// --- 新增/编辑(路由跳转到独立表单页)---
|
||||
const router = useRouter()
|
||||
|
||||
function handleAdd() {
|
||||
router.push('/admin/role/form')
|
||||
}
|
||||
|
||||
function handleEdit(row: RoleItem) {
|
||||
router.push('/admin/role/form/' + row.id)
|
||||
}
|
||||
|
||||
function handleDelete(row: RoleItem) {
|
||||
ElMessageBox.confirm(`确认删除角色"${row.roleName}"吗?`, '删除确认', {
|
||||
type: 'warning',
|
||||
confirmButtonText: '确认',
|
||||
cancelButtonText: '取消',
|
||||
}).then(async () => {
|
||||
try {
|
||||
await request.delete(`/zgapi/v1/admin/role/${row.id}`)
|
||||
ElMessage.success('删除成功')
|
||||
fetchData()
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}).catch(() => {})
|
||||
}
|
||||
|
||||
// --- 分配菜单 ---
|
||||
const menuVisible = ref(false)
|
||||
const menuTreeData = ref<MenuNode[]>([])
|
||||
const menuSubmitLoading = ref(false)
|
||||
const menuRoleId = ref<string>()
|
||||
const menuTreeRef = ref<InstanceType<typeof ElTree>>()
|
||||
|
||||
async function handleAssignMenu(row: RoleItem) {
|
||||
menuRoleId.value = row.id
|
||||
const sysId = row.systemId
|
||||
if (!sysId) {
|
||||
ElMessage.warning('该角色未关联系统,无法分配菜单')
|
||||
return
|
||||
}
|
||||
menuVisible.value = true
|
||||
menuTreeData.value = []
|
||||
|
||||
// 并行加载菜单树 + 已分配的菜单ID
|
||||
try {
|
||||
const [treeRes, checkedRes] = await Promise.all([
|
||||
request.get('/zgapi/v1/admin/menu/tree', { params: { systemId: sysId } }),
|
||||
request.get(`/zgapi/v1/admin/role/${row.id}/menu`),
|
||||
])
|
||||
menuTreeData.value = treeRes.data ?? []
|
||||
// 预选已分配的菜单
|
||||
const checkedIds: string[] = checkedRes.data ?? []
|
||||
// 等 DOM 更新后设置勾选
|
||||
await new Promise(r => setTimeout(r, 50))
|
||||
menuTreeRef.value?.setCheckedKeys(checkedIds)
|
||||
} catch {
|
||||
ElMessage.error('加载菜单树失败')
|
||||
}
|
||||
}
|
||||
|
||||
async function submitAssignMenu() {
|
||||
const checkedKeys = menuTreeRef.value?.getCheckedKeys() as string[]
|
||||
const halfCheckedKeys = menuTreeRef.value?.getHalfCheckedKeys() as string[]
|
||||
const menuIds = [...checkedKeys, ...halfCheckedKeys]
|
||||
|
||||
menuSubmitLoading.value = true
|
||||
try {
|
||||
await request.post(`/zgapi/v1/admin/role/${menuRoleId.value}/menu`, menuIds)
|
||||
ElMessage.success('分配菜单成功')
|
||||
menuVisible.value = false
|
||||
} catch {
|
||||
// ignore
|
||||
} finally {
|
||||
menuSubmitLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化
|
||||
fetchData()
|
||||
fetchSystemList()
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.page-wrapper {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.card-header-title {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
font-weight: 600;
|
||||
color: #303133;
|
||||
}
|
||||
|
||||
.toolbar {
|
||||
display: flex;
|
||||
justify-content: flex-start;
|
||||
}
|
||||
|
||||
.pagination-wrapper {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
margin-top: 20px;
|
||||
}
|
||||
|
||||
.actions {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
flex-wrap: wrap;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.page-wrapper { gap: 10px; }
|
||||
.page-toolbar { flex-direction: column; gap: 10px; }
|
||||
.page-toolbar .el-select { width: 100% !important; }
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user