reactor:【system 系统管理】role 进一步统一代码风格

pull/210/head
YunaiV 2025-09-06 20:17:38 +08:00
parent 6d47871f02
commit 8d5a6d8aa0
8 changed files with 224 additions and 206 deletions

View File

@ -1,12 +1,14 @@
import type { VbenFormSchema } from '#/adapter/form'; import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table'; import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import { CommonStatusEnum, SystemDataScopeEnum } from '@vben/constants'; import {
CommonStatusEnum,
import { z } from '#/adapter/form'; DICT_TYPE,
import { DICT_TYPE } from '@vben/constants'; SystemDataScopeEnum,
} from '@vben/constants';
import { getDictOptions } from '@vben/hooks'; import { getDictOptions } from '@vben/hooks';
import { z } from '#/adapter/form';
import { getRangePickerDefaultProps } from '#/utils'; import { getRangePickerDefaultProps } from '#/utils';
/** 新增/修改的表单 */ /** 新增/修改的表单 */
@ -155,19 +157,28 @@ export function useGridFormSchema(): VbenFormSchema[] {
fieldName: 'name', fieldName: 'name',
label: '角色名称', label: '角色名称',
component: 'Input', component: 'Input',
componentProps: {
placeholder: '请输入角色名称',
allowClear: true,
},
}, },
{ {
fieldName: 'code', fieldName: 'code',
label: '角色标识', label: '角色标识',
component: 'Input', component: 'Input',
componentProps: {
placeholder: '请输入角色标识',
allowClear: true,
},
}, },
{ {
fieldName: 'status', fieldName: 'status',
label: '角色状态', label: '角色状态',
component: 'Select', component: 'Select',
componentProps: { componentProps: {
allowClear: true,
options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'), options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
placeholder: '请选择角色状态',
allowClear: true,
}, },
}, },
{ {
@ -189,14 +200,17 @@ export function useGridColumns(): VxeTableGridOptions['columns'] {
{ {
field: 'id', field: 'id',
title: '角色编号', title: '角色编号',
minWidth: 100,
}, },
{ {
field: 'name', field: 'name',
title: '角色名称', title: '角色名称',
minWidth: 200,
}, },
{ {
field: 'type', field: 'type',
title: '角色类型', title: '角色类型',
minWidth: 100,
cellRender: { cellRender: {
name: 'CellDict', name: 'CellDict',
props: { type: DICT_TYPE.SYSTEM_ROLE_TYPE }, props: { type: DICT_TYPE.SYSTEM_ROLE_TYPE },
@ -205,18 +219,22 @@ export function useGridColumns(): VxeTableGridOptions['columns'] {
{ {
field: 'code', field: 'code',
title: '角色标识', title: '角色标识',
minWidth: 200,
}, },
{ {
field: 'sort', field: 'sort',
title: '显示顺序', title: '显示顺序',
minWidth: 100,
}, },
{ {
field: 'remark', field: 'remark',
title: '角色备注', title: '角色备注',
minWidth: 100,
}, },
{ {
field: 'status', field: 'status',
title: '角色状态', title: '角色状态',
minWidth: 100,
cellRender: { cellRender: {
name: 'CellDict', name: 'CellDict',
props: { type: DICT_TYPE.COMMON_STATUS }, props: { type: DICT_TYPE.COMMON_STATUS },
@ -225,6 +243,7 @@ export function useGridColumns(): VxeTableGridOptions['columns'] {
{ {
field: 'createTime', field: 'createTime',
title: '创建时间', title: '创建时间',
minWidth: 180,
formatter: 'formatDateTime', formatter: 'formatDateTime',
}, },
{ {

View File

@ -4,7 +4,7 @@ import type { SystemRoleApi } from '#/api/system/role';
import { ref } from 'vue'; import { ref } from 'vue';
import { DocAlert, Page, useVbenModal } from '@vben/common-ui'; import { confirm, DocAlert, Page, useVbenModal } from '@vben/common-ui';
import { downloadFileFromBlobPart, isEmpty } from '@vben/utils'; import { downloadFileFromBlobPart, isEmpty } from '@vben/utils';
import { message } from 'ant-design-vue'; import { message } from 'ant-design-vue';
@ -40,7 +40,7 @@ const [AssignMenuFormModel, assignMenuFormApi] = useVbenModal({
}); });
/** 刷新表格 */ /** 刷新表格 */
function onRefresh() { function handleRefresh() {
gridApi.query(); gridApi.query();
} }
@ -50,29 +50,43 @@ async function handleExport() {
downloadFileFromBlobPart({ fileName: '角色.xls', source: data }); downloadFileFromBlobPart({ fileName: '角色.xls', source: data });
} }
/** 编辑角色 */
function handleEdit(row: SystemRoleApi.Role) {
formModalApi.setData(row).open();
}
/** 创建角色 */ /** 创建角色 */
function handleCreate() { function handleCreate() {
formModalApi.setData(null).open(); formModalApi.setData(null).open();
} }
/** 编辑角色 */
function handleEdit(row: SystemRoleApi.Role) {
formModalApi.setData(row).open();
}
/** 删除角色 */ /** 删除角色 */
async function handleDelete(row: SystemRoleApi.Role) { async function handleDelete(row: SystemRoleApi.Role) {
const hideLoading = message.loading({ const hideLoading = message.loading({
content: $t('ui.actionMessage.deleting', [row.name]), content: $t('ui.actionMessage.deleting', [row.name]),
key: 'action_key_msg', duration: 0,
}); });
try { try {
await deleteRole(row.id as number); await deleteRole(row.id as number);
message.success({ message.success($t('ui.actionMessage.deleteSuccess', [row.name]));
content: $t('ui.actionMessage.deleteSuccess', [row.name]), handleRefresh();
key: 'action_key_msg', } finally {
}); hideLoading();
onRefresh(); }
}
/** 批量删除角色 */
async function handleDeleteBatch() {
await confirm($t('ui.actionMessage.deleteBatchConfirm'));
const hideLoading = message.loading({
content: $t('ui.actionMessage.deletingBatch'),
duration: 0,
});
try {
await deleteRoleList(checkedIds.value);
checkedIds.value = [];
message.success($t('ui.actionMessage.deleteSuccess'));
handleRefresh();
} finally { } finally {
hideLoading(); hideLoading();
} }
@ -87,23 +101,6 @@ function handleRowCheckboxChange({
checkedIds.value = records.map((item) => item.id!); checkedIds.value = records.map((item) => item.id!);
} }
/** 批量删除角色 */
async function handleDeleteBatch() {
const hideLoading = message.loading({
content: $t('ui.actionMessage.deleting'),
duration: 0,
key: 'action_process_msg',
});
try {
await deleteRoleList(checkedIds.value);
checkedIds.value = [];
message.success($t('ui.actionMessage.deleteSuccess'));
onRefresh();
} finally {
hideLoading();
}
}
/** 分配角色的数据权限 */ /** 分配角色的数据权限 */
function handleAssignDataPermission(row: SystemRoleApi.Role) { function handleAssignDataPermission(row: SystemRoleApi.Role) {
assignDataPermissionFormApi.setData(row).open(); assignDataPermissionFormApi.setData(row).open();
@ -159,9 +156,9 @@ const [Grid, gridApi] = useVbenVxeGrid({
<DocAlert title="数据权限" url="https://doc.iocoder.cn/data-permission" /> <DocAlert title="数据权限" url="https://doc.iocoder.cn/data-permission" />
</template> </template>
<FormModal @success="onRefresh" /> <FormModal @success="handleRefresh" />
<AssignDataPermissionFormModel @success="onRefresh" /> <AssignDataPermissionFormModel @success="handleRefresh" />
<AssignMenuFormModel @success="onRefresh" /> <AssignMenuFormModel @success="handleRefresh" />
<Grid table-title=""> <Grid table-title="">
<template #toolbar-tools> <template #toolbar-tools>
<TableAction <TableAction
@ -181,11 +178,11 @@ const [Grid, gridApi] = useVbenVxeGrid({
onClick: handleExport, onClick: handleExport,
}, },
{ {
label: '批量删除', label: $t('ui.actionTitle.deleteBatch'),
type: 'primary', type: 'primary',
danger: true, danger: true,
disabled: isEmpty(checkedIds),
icon: ACTION_ICON.DELETE, icon: ACTION_ICON.DELETE,
disabled: isEmpty(checkedIds),
auth: ['system:role:delete'], auth: ['system:role:delete'],
onClick: handleDeleteBatch, onClick: handleDeleteBatch,
}, },

View File

@ -76,8 +76,8 @@ const [Modal, modalApi] = useVbenModal({
try { try {
// //
await loadDeptTree(); await loadDeptTree();
toggleExpandAll(); handleExpandAll();
// , //
await formApi.setValues(await getRole(data.id)); await formApi.setValues(await getRole(data.id));
} finally { } finally {
modalApi.unlock(); modalApi.unlock();
@ -97,7 +97,7 @@ async function loadDeptTree() {
} }
/** 全选/全不选 */ /** 全选/全不选 */
function toggleSelectAll() { function handleSelectAll() {
isAllSelected.value = !isAllSelected.value; isAllSelected.value = !isAllSelected.value;
if (isAllSelected.value) { if (isAllSelected.value) {
const allIds = getAllNodeIds(deptTree.value); const allIds = getAllNodeIds(deptTree.value);
@ -108,14 +108,13 @@ function toggleSelectAll() {
} }
/** 展开/折叠所有节点 */ /** 展开/折叠所有节点 */
function toggleExpandAll() { function handleExpandAll() {
isExpanded.value = !isExpanded.value; isExpanded.value = !isExpanded.value;
// ID
expandedKeys.value = isExpanded.value ? getAllNodeIds(deptTree.value) : []; expandedKeys.value = isExpanded.value ? getAllNodeIds(deptTree.value) : [];
} }
/** 切换父子联动 */ /** 切换父子联动 */
function toggleCheckStrictly() { function handleCheckStrictly() {
isCheckStrictly.value = !isCheckStrictly.value; isCheckStrictly.value = !isCheckStrictly.value;
} }
@ -151,13 +150,13 @@ function getAllNodeIds(nodes: any[], ids: number[] = []): number[] {
</Form> </Form>
<template #prepend-footer> <template #prepend-footer>
<div class="flex flex-auto items-center"> <div class="flex flex-auto items-center">
<Checkbox :checked="isAllSelected" @change="toggleSelectAll"> <Checkbox :checked="isAllSelected" @change="handleSelectAll">
全选 全选
</Checkbox> </Checkbox>
<Checkbox :checked="isExpanded" @change="toggleExpandAll"> <Checkbox :checked="isExpanded" @change="handleExpandAll">
全部展开 全部展开
</Checkbox> </Checkbox>
<Checkbox :checked="isCheckStrictly" @change="toggleCheckStrictly"> <Checkbox :checked="isCheckStrictly" @change="handleCheckStrictly">
父子联动 父子联动
</Checkbox> </Checkbox>
</div> </div>

View File

@ -4,7 +4,7 @@ import type { Recordable } from '@vben/types';
import type { SystemMenuApi } from '#/api/system/menu'; import type { SystemMenuApi } from '#/api/system/menu';
import type { SystemRoleApi } from '#/api/system/role'; import type { SystemRoleApi } from '#/api/system/role';
import { ref } from 'vue'; import {nextTick, ref} from 'vue';
import { useVbenModal, VbenTree } from '@vben/common-ui'; import { useVbenModal, VbenTree } from '@vben/common-ui';
import { SystemMenuTypeEnum } from '@vben/constants'; import { SystemMenuTypeEnum } from '@vben/constants';
@ -80,6 +80,7 @@ const [Modal, modalApi] = useVbenModal({
await formApi.setValues(data); await formApi.setValues(data);
} finally { } finally {
await nextTick(); //
modalApi.unlock(); modalApi.unlock();
} }
}, },
@ -97,7 +98,7 @@ async function loadMenuTree() {
} }
/** 全选/全不选 */ /** 全选/全不选 */
function toggleSelectAll() { function handleSelectAll() {
isAllSelected.value = !isAllSelected.value; isAllSelected.value = !isAllSelected.value;
if (isAllSelected.value) { if (isAllSelected.value) {
const allIds = getAllNodeIds(menuTree.value); const allIds = getAllNodeIds(menuTree.value);
@ -108,9 +109,8 @@ function toggleSelectAll() {
} }
/** 展开/折叠所有节点 */ /** 展开/折叠所有节点 */
function toggleExpandAll() { function handleExpandAll() {
isExpanded.value = !isExpanded.value; isExpanded.value = !isExpanded.value;
// ID
expandedKeys.value = isExpanded.value ? getAllNodeIds(menuTree.value) : []; expandedKeys.value = isExpanded.value ? getAllNodeIds(menuTree.value) : [];
} }
@ -158,10 +158,10 @@ function getNodeClass(node: Recordable<any>) {
</Form> </Form>
<template #prepend-footer> <template #prepend-footer>
<div class="flex flex-auto items-center"> <div class="flex flex-auto items-center">
<Checkbox :checked="isAllSelected" @change="toggleSelectAll"> <Checkbox :checked="isAllSelected" @change="handleSelectAll">
全选 全选
</Checkbox> </Checkbox>
<Checkbox :checked="isExpanded" @change="toggleExpandAll"> <Checkbox :checked="isExpanded" @change="handleExpandAll">
全部展开 全部展开
</Checkbox> </Checkbox>
</div> </div>

View File

@ -1,8 +1,6 @@
import type { VbenFormSchema } from '#/adapter/form'; import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table'; import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemRoleApi } from '#/api/system/role';
import { useAccess } from '@vben/access';
import { import {
CommonStatusEnum, CommonStatusEnum,
DICT_TYPE, DICT_TYPE,
@ -13,8 +11,6 @@ import { getDictOptions } from '@vben/hooks';
import { z } from '#/adapter/form'; import { z } from '#/adapter/form';
import { getRangePickerDefaultProps } from '#/utils'; import { getRangePickerDefaultProps } from '#/utils';
const { hasAccessByCodes } = useAccess();
/** 新增/修改的表单 */ /** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] { export function useFormSchema(): VbenFormSchema[] {
return [ return [
@ -161,19 +157,28 @@ export function useGridFormSchema(): VbenFormSchema[] {
fieldName: 'name', fieldName: 'name',
label: '角色名称', label: '角色名称',
component: 'Input', component: 'Input',
componentProps: {
placeholder: '请输入角色名称',
clearable: true,
},
}, },
{ {
fieldName: 'code', fieldName: 'code',
label: '角色标识', label: '角色标识',
component: 'Input', component: 'Input',
componentProps: {
placeholder: '请输入角色标识',
clearable: true,
},
}, },
{ {
fieldName: 'status', fieldName: 'status',
label: '角色状态', label: '角色状态',
component: 'Select', component: 'Select',
componentProps: { componentProps: {
clearable: true,
options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'), options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
placeholder: '请选择角色状态',
clearable: true,
}, },
}, },
{ {
@ -189,18 +194,13 @@ export function useGridFormSchema(): VbenFormSchema[] {
} }
/** 列表的字段 */ /** 列表的字段 */
export function useGridColumns<T = SystemRoleApi.Role>( export function useGridColumns(): VxeTableGridOptions['columns'] {
onActionClick: OnActionClickFn<T>,
): VxeTableGridOptions['columns'] {
return [ return [
{ { type: 'checkbox', width: 40 },
type: 'checkbox',
width: 40,
},
{ {
field: 'id', field: 'id',
title: '角色编号', title: '角色编号',
minWidth: 200, minWidth: 100,
}, },
{ {
field: 'name', field: 'name',
@ -247,41 +247,10 @@ export function useGridColumns<T = SystemRoleApi.Role>(
formatter: 'formatDateTime', formatter: 'formatDateTime',
}, },
{ {
field: 'operation',
title: '操作', title: '操作',
width: 240, width: 240,
fixed: 'right', fixed: 'right',
align: 'center', slots: { default: 'actions' },
cellRender: {
attrs: {
nameField: 'name',
nameTitle: '角色',
onClick: onActionClick,
},
name: 'CellOperation',
options: [
{
code: 'edit',
show: hasAccessByCodes(['system:role:update']),
},
{
code: 'delete',
show: hasAccessByCodes(['system:role:delete']),
},
{
code: 'assign-data-permission',
text: '数据权限',
show: hasAccessByCodes([
'system:permission:assign-role-data-scope',
]),
},
{
code: 'assign-menu',
text: '菜单权限',
show: hasAccessByCodes(['system:permission:assign-role-menu']),
},
],
},
}, },
]; ];
} }

View File

@ -1,19 +1,15 @@
<script lang="ts" setup> <script lang="ts" setup>
import type { import type { VxeTableGridOptions } from '#/adapter/vxe-table';
OnActionClickParams,
VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { SystemRoleApi } from '#/api/system/role'; import type { SystemRoleApi } from '#/api/system/role';
import { ref } from 'vue'; import { ref } from 'vue';
import { confirm, DocAlert, Page, useVbenModal } from '@vben/common-ui'; import { confirm, DocAlert, Page, useVbenModal } from '@vben/common-ui';
import { Download, Plus } from '@vben/icons';
import { downloadFileFromBlobPart, isEmpty } from '@vben/utils'; import { downloadFileFromBlobPart, isEmpty } from '@vben/utils';
import { ElButton, ElLoading, ElMessage } from 'element-plus'; import { ElLoading, ElMessage } from 'element-plus';
import { useVbenVxeGrid } from '#/adapter/vxe-table'; import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import { import {
deleteRole, deleteRole,
deleteRoleList, deleteRoleList,
@ -44,47 +40,54 @@ const [AssignMenuFormModel, assignMenuFormApi] = useVbenModal({
}); });
/** 刷新表格 */ /** 刷新表格 */
function onRefresh() { function handleRefresh() {
gridApi.query(); gridApi.query();
} }
/** 导出表格 */ /** 导出表格 */
async function onExport() { async function handleExport() {
const data = await exportRole(await gridApi.formApi.getValues()); const data = await exportRole(await gridApi.formApi.getValues());
downloadFileFromBlobPart({ fileName: '角色.xls', source: data }); downloadFileFromBlobPart({ fileName: '角色.xls', source: data });
} }
/** 编辑角色 */
function onEdit(row: SystemRoleApi.Role) {
formModalApi.setData(row).open();
}
/** 创建角色 */ /** 创建角色 */
function onCreate() { function handleCreate() {
formModalApi.setData(null).open(); formModalApi.setData(null).open();
} }
/** 编辑角色 */
function handleEdit(row: SystemRoleApi.Role) {
formModalApi.setData(row).open();
}
/** 删除角色 */ /** 删除角色 */
async function onDelete(row: SystemRoleApi.Role) { async function handleDelete(row: SystemRoleApi.Role) {
const loadingInstance = ElLoading.service({ const loadingInstance = ElLoading.service({
text: $t('ui.actionMessage.deleting', [row.name]), text: $t('ui.actionMessage.deleting', [row.name]),
}); });
try { try {
await deleteRole(row.id as number); await deleteRole(row.id as number);
ElMessage.success($t('ui.actionMessage.deleteSuccess', [row.name])); ElMessage.success($t('ui.actionMessage.deleteSuccess', [row.name]));
onRefresh(); handleRefresh();
} finally { } finally {
loadingInstance.close(); loadingInstance.close();
} }
} }
/** 批量删除角色 */ /** 批量删除角色 */
async function onDeleteBatch() { async function handleDeleteBatch() {
await confirm('确定要批量删除该角色吗?'); await confirm($t('ui.actionMessage.deleteBatchConfirm'));
await deleteRoleList(checkedIds.value); const loadingInstance = ElLoading.service({
checkedIds.value = []; text: $t('ui.actionMessage.deletingBatch'),
ElMessage.success($t('ui.actionMessage.deleteSuccess')); });
onRefresh(); try {
await deleteRoleList(checkedIds.value);
checkedIds.value = [];
ElMessage.success($t('ui.actionMessage.deleteSuccess'));
handleRefresh();
} finally {
loadingInstance.close();
}
} }
const checkedIds = ref<number[]>([]); const checkedIds = ref<number[]>([]);
@ -97,50 +100,28 @@ function handleRowCheckboxChange({
} }
/** 分配角色的数据权限 */ /** 分配角色的数据权限 */
function onAssignDataPermission(row: SystemRoleApi.Role) { function handleAssignDataPermission(row: SystemRoleApi.Role) {
assignDataPermissionFormApi.setData(row).open(); assignDataPermissionFormApi.setData(row).open();
} }
/** 分配角色的菜单权限 */ /** 分配角色的菜单权限 */
function onAssignMenu(row: SystemRoleApi.Role) { function handleAssignMenu(row: SystemRoleApi.Role) {
assignMenuFormApi.setData(row).open(); assignMenuFormApi.setData(row).open();
} }
/** 表格操作按钮的回调函数 */
function onActionClick({ code, row }: OnActionClickParams<SystemRoleApi.Role>) {
switch (code) {
case 'assign-data-permission': {
onAssignDataPermission(row);
break;
}
case 'assign-menu': {
onAssignMenu(row);
break;
}
case 'delete': {
onDelete(row);
break;
}
case 'edit': {
onEdit(row);
break;
}
}
}
const [Grid, gridApi] = useVbenVxeGrid({ const [Grid, gridApi] = useVbenVxeGrid({
formOptions: { formOptions: {
schema: useGridFormSchema(), schema: useGridFormSchema(),
}, },
gridOptions: { gridOptions: {
columns: useGridColumns(onActionClick), columns: useGridColumns(),
height: 'auto', height: 'auto',
keepSource: true, keepSource: true,
proxyConfig: { proxyConfig: {
ajax: { ajax: {
query: async ({ page }, formValues) => { query: async ({ page }, formValues) => {
return await getRolePage({ return await getRolePage({
page: page.currentPage, pageNo: page.currentPage,
pageSize: page.pageSize, pageSize: page.pageSize,
...formValues, ...formValues,
}); });
@ -149,6 +130,7 @@ const [Grid, gridApi] = useVbenVxeGrid({
}, },
rowConfig: { rowConfig: {
keyField: 'id', keyField: 'id',
isHover: true,
}, },
toolbarConfig: { toolbarConfig: {
refresh: true, refresh: true,
@ -172,38 +154,78 @@ const [Grid, gridApi] = useVbenVxeGrid({
<DocAlert title="数据权限" url="https://doc.iocoder.cn/data-permission" /> <DocAlert title="数据权限" url="https://doc.iocoder.cn/data-permission" />
</template> </template>
<FormModal @success="onRefresh" /> <FormModal @success="handleRefresh" />
<AssignDataPermissionFormModel @success="onRefresh" /> <AssignDataPermissionFormModel @success="handleRefresh" />
<AssignMenuFormModel @success="onRefresh" /> <AssignMenuFormModel @success="handleRefresh" />
<Grid table-title=""> <Grid table-title="">
<template #toolbar-tools> <template #toolbar-tools>
<ElButton <TableAction
type="primary" :actions="[
@click="onCreate" {
v-access:code="['system:role:create']" label: $t('ui.actionTitle.create', ['角色']),
> type: 'primary',
<Plus class="mr-2 size-5" /> icon: ACTION_ICON.ADD,
{{ $t('ui.actionTitle.create', ['角色']) }} auth: ['system:role:create'],
</ElButton> onClick: handleCreate,
<ElButton },
type="primary" {
class="ml-2" label: $t('ui.actionTitle.export'),
@click="onExport" type: 'primary',
v-access:code="['system:role:export']" icon: ACTION_ICON.DOWNLOAD,
> auth: ['system:role:export'],
<Download class="mr-2 size-5" /> onClick: handleExport,
{{ $t('ui.actionTitle.export') }} },
</ElButton> {
<ElButton label: $t('ui.actionTitle.deleteBatch'),
type="danger" type: 'danger',
class="ml-2" icon: ACTION_ICON.DELETE,
@click="onDeleteBatch" disabled: isEmpty(checkedIds),
v-access:code="['system:role:delete']" auth: ['system:role:delete'],
:disabled="isEmpty(checkedIds)" onClick: handleDeleteBatch,
> },
<i class="fa-solid fa-trash-can mr-2"></i> ]"
{{ $t('ui.actionTitle.deleteBatch') }} />
</ElButton> </template>
<template #actions="{ row }">
<TableAction
:actions="[
{
label: $t('common.edit'),
type: 'primary',
link: true,
icon: ACTION_ICON.EDIT,
auth: ['system:role:update'],
onClick: handleEdit.bind(null, row),
},
{
label: $t('common.delete'),
type: 'danger',
link: true,
icon: ACTION_ICON.DELETE,
auth: ['system:role:delete'],
popConfirm: {
title: $t('ui.actionMessage.deleteConfirm', [row.name]),
confirm: handleDelete.bind(null, row),
},
},
]"
:drop-down-actions="[
{
label: '数据权限',
type: 'primary',
link: true,
auth: ['system:permission:assign-role-data-scope'],
onClick: handleAssignDataPermission.bind(null, row),
},
{
label: '菜单权限',
type: 'primary',
link: true,
auth: ['system:permission:assign-role-menu'],
onClick: handleAssignMenu.bind(null, row),
},
]"
/>
</template> </template>
</Grid> </Grid>
</Page> </Page>

View File

@ -76,8 +76,8 @@ const [Modal, modalApi] = useVbenModal({
try { try {
// //
await loadDeptTree(); await loadDeptTree();
toggleExpandAll(); handleExpandAll();
// , //
await formApi.setValues(await getRole(data.id)); await formApi.setValues(await getRole(data.id));
} finally { } finally {
modalApi.unlock(); modalApi.unlock();
@ -97,7 +97,7 @@ async function loadDeptTree() {
} }
/** 全选/全不选 */ /** 全选/全不选 */
function toggleSelectAll() { function handleSelectAll() {
isAllSelected.value = !isAllSelected.value; isAllSelected.value = !isAllSelected.value;
if (isAllSelected.value) { if (isAllSelected.value) {
const allIds = getAllNodeIds(deptTree.value); const allIds = getAllNodeIds(deptTree.value);
@ -108,14 +108,13 @@ function toggleSelectAll() {
} }
/** 展开/折叠所有节点 */ /** 展开/折叠所有节点 */
function toggleExpandAll() { function handleExpandAll() {
isExpanded.value = !isExpanded.value; isExpanded.value = !isExpanded.value;
// ID
expandedKeys.value = isExpanded.value ? getAllNodeIds(deptTree.value) : []; expandedKeys.value = isExpanded.value ? getAllNodeIds(deptTree.value) : [];
} }
/** 切换父子联动 */ /** 切换父子联动 */
function toggleCheckStrictly() { function handleCheckStrictly() {
isCheckStrictly.value = !isCheckStrictly.value; isCheckStrictly.value = !isCheckStrictly.value;
} }
@ -132,15 +131,14 @@ function getAllNodeIds(nodes: any[], ids: number[] = []): number[] {
</script> </script>
<template> <template>
<Modal title="数据权限" class="w-[40%]"> <Modal title="数据权限" class="w-2/5">
<Form class="mx-4"> <Form class="mx-4">
<template #dataScopeDeptIds="slotProps"> <template #dataScopeDeptIds="slotProps">
<!-- <Spin :spinning="deptLoading"> -->
<!-- TODO @芋艿可优化使用 antd tree原因是更原生 -->
<VbenTree <VbenTree
:tree-data="deptTree" :tree-data="deptTree"
multiple multiple
bordered bordered
:spinning="deptLoading"
:expanded="expandedKeys" :expanded="expandedKeys"
v-bind="slotProps" v-bind="slotProps"
value-field="id" value-field="id"
@ -148,20 +146,19 @@ function getAllNodeIds(nodes: any[], ids: number[] = []): number[] {
:auto-check-parent="false" :auto-check-parent="false"
:check-strictly="!isCheckStrictly" :check-strictly="!isCheckStrictly"
/> />
<!-- </Spin> -->
</template> </template>
</Form> </Form>
<template #prepend-footer> <template #prepend-footer>
<div class="flex flex-auto items-center"> <div class="flex flex-auto items-center">
<ElCheckbox :model-value="isAllSelected" @change="toggleSelectAll"> <ElCheckbox :model-value="isAllSelected" @change="handleSelectAll">
全选 全选
</ElCheckbox> </ElCheckbox>
<ElCheckbox :model-value="isExpanded" @change="toggleExpandAll"> <ElCheckbox :model-value="isExpanded" @change="handleExpandAll">
全部展开 全部展开
</ElCheckbox> </ElCheckbox>
<ElCheckbox <ElCheckbox
:model-value="isCheckStrictly" :model-value="isCheckStrictly"
@change="toggleCheckStrictly" @change="handleCheckStrictly"
> >
父子联动 父子联动
</ElCheckbox> </ElCheckbox>

View File

@ -1,10 +1,13 @@
<script lang="ts" setup> <script lang="ts" setup>
import type { SystemDeptApi } from '#/api/system/dept'; import type { Recordable } from '@vben-core/typings';
import type { SystemMenuApi } from '#/api/system/menu';
import type { SystemRoleApi } from '#/api/system/role'; import type { SystemRoleApi } from '#/api/system/role';
import { ref } from 'vue'; import {nextTick, ref} from 'vue';
import { useVbenModal, VbenTree } from '@vben/common-ui'; import { useVbenModal, VbenTree } from '@vben/common-ui';
import { SystemMenuTypeEnum } from '@vben/constants';
import { handleTree } from '@vben/utils'; import { handleTree } from '@vben/utils';
import { ElCheckbox, ElMessage } from 'element-plus'; import { ElCheckbox, ElMessage } from 'element-plus';
@ -18,7 +21,7 @@ import { useAssignMenuFormSchema } from '../data';
const emit = defineEmits(['success']); const emit = defineEmits(['success']);
const menuTree = ref<SystemDeptApi.Dept[]>([]); // const menuTree = ref<SystemMenuApi.Menu[]>([]); //
const menuLoading = ref(false); // const menuLoading = ref(false); //
const isAllSelected = ref(false); // const isAllSelected = ref(false); //
const isExpanded = ref(false); // const isExpanded = ref(false); //
@ -77,6 +80,7 @@ const [Modal, modalApi] = useVbenModal({
await formApi.setValues(data); await formApi.setValues(data);
} finally { } finally {
await nextTick(); //
modalApi.unlock(); modalApi.unlock();
} }
}, },
@ -87,14 +91,14 @@ async function loadMenuTree() {
menuLoading.value = true; menuLoading.value = true;
try { try {
const data = await getMenuList(); const data = await getMenuList();
menuTree.value = handleTree(data) as SystemDeptApi.Dept[]; menuTree.value = handleTree(data) as SystemMenuApi.Menu[];
} finally { } finally {
menuLoading.value = false; menuLoading.value = false;
} }
} }
/** 全选/全不选 */ /** 全选/全不选 */
function toggleSelectAll() { function handleSelectAll() {
isAllSelected.value = !isAllSelected.value; isAllSelected.value = !isAllSelected.value;
if (isAllSelected.value) { if (isAllSelected.value) {
const allIds = getAllNodeIds(menuTree.value); const allIds = getAllNodeIds(menuTree.value);
@ -105,9 +109,8 @@ function toggleSelectAll() {
} }
/** 展开/折叠所有节点 */ /** 展开/折叠所有节点 */
function toggleExpandAll() { function handleExpandAll() {
isExpanded.value = !isExpanded.value; isExpanded.value = !isExpanded.value;
// ID
expandedKeys.value = isExpanded.value ? getAllNodeIds(menuTree.value) : []; expandedKeys.value = isExpanded.value ? getAllNodeIds(menuTree.value) : [];
} }
@ -121,32 +124,44 @@ function getAllNodeIds(nodes: any[], ids: number[] = []): number[] {
}); });
return ids; return ids;
} }
function getNodeClass(node: Recordable<any>) {
const classes: string[] = [];
if (node.value?.type === SystemMenuTypeEnum.BUTTON) {
classes.push('inline-flex');
if (node.index % 3 >= 1) {
classes.push('!pl-0');
}
}
return classes.join(' ');
}
</script> </script>
<template> <template>
<Modal title="数据权限" class="w-[40%]"> <Modal title="数据权限" class="w-2/5">
<Form class="mx-4"> <Form class="mx-4">
<template #menuIds="slotProps"> <template #menuIds="slotProps">
<!-- <Spin :spinning="menuLoading" class="w-full"> -->
<!-- TODO @芋艿可优化使用 antd tree原因是更原生 -->
<VbenTree <VbenTree
:spinning="menuLoading"
:tree-data="menuTree" :tree-data="menuTree"
multiple multiple
bordered bordered
:expanded="expandedKeys" :expanded="expandedKeys"
:get-node-class="getNodeClass"
v-bind="slotProps" v-bind="slotProps"
value-field="id" value-field="id"
label-field="name" label-field="name"
icon-field="meta.icon"
/> />
<!-- </Spin> -->
</template> </template>
</Form> </Form>
<template #prepend-footer> <template #prepend-footer>
<div class="flex flex-auto items-center"> <div class="flex flex-auto items-center">
<ElCheckbox :model-value="isAllSelected" @change="toggleSelectAll"> <ElCheckbox :model-value="isAllSelected" @change="handleSelectAll">
全选 全选
</ElCheckbox> </ElCheckbox>
<ElCheckbox :model-value="isExpanded" @change="toggleExpandAll"> <ElCheckbox :model-value="isExpanded" @change="handleExpandAll">
全部展开 全部展开
</ElCheckbox> </ElCheckbox>
</div> </div>