chore: 移除测试菜单
This commit is contained in:
@@ -1,224 +0,0 @@
|
|||||||
import type { FormSchemaGetter } from '#/adapter/form';
|
|
||||||
import type { VxeGridProps } from '#/adapter/vxe-table';
|
|
||||||
|
|
||||||
import { getDictOptions } from '#/utils/dict';
|
|
||||||
import { DictEnum } from '@vben/constants';
|
|
||||||
import { getPopupContainer } from '@vben/utils';
|
|
||||||
import { Tag } from 'ant-design-vue';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* authScopeOptions user也会用到
|
|
||||||
*/
|
|
||||||
export const authScopeOptions = [
|
|
||||||
{ color: 'green', label: '全部数据权限', value: '1' },
|
|
||||||
{ color: 'default', label: '自定数据权限', value: '2' },
|
|
||||||
{ color: 'orange', label: '本部门数据权限', value: '3' },
|
|
||||||
{ color: 'cyan', label: '本部门及以下数据权限', value: '4' },
|
|
||||||
{ color: 'error', label: '仅本人数据权限', value: '5' },
|
|
||||||
{ color: 'default', label: '部门及以下或本人数据权限', value: '6' },
|
|
||||||
];
|
|
||||||
|
|
||||||
export const querySchema: FormSchemaGetter = () => [
|
|
||||||
{
|
|
||||||
component: 'Input',
|
|
||||||
fieldName: 'roleName',
|
|
||||||
label: '角色名称',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
component: 'Input',
|
|
||||||
fieldName: 'roleKey',
|
|
||||||
label: '权限字符',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
component: 'Select',
|
|
||||||
componentProps: {
|
|
||||||
options: getDictOptions(DictEnum.SYS_NORMAL_DISABLE),
|
|
||||||
},
|
|
||||||
fieldName: 'status',
|
|
||||||
label: '状态',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
component: 'RangePicker',
|
|
||||||
fieldName: 'createTime',
|
|
||||||
label: '创建时间',
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
export const columns: VxeGridProps['columns'] = [
|
|
||||||
{ type: 'checkbox', width: 60 },
|
|
||||||
{
|
|
||||||
title: '角色名称',
|
|
||||||
field: 'roleName',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: '权限字符',
|
|
||||||
field: 'roleKey',
|
|
||||||
slots: {
|
|
||||||
default: ({ row }) => {
|
|
||||||
return <Tag color="processing">{row.roleKey}</Tag>;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: '数据权限',
|
|
||||||
field: 'dataScope',
|
|
||||||
slots: {
|
|
||||||
default: ({ row }) => {
|
|
||||||
const found = authScopeOptions.find(
|
|
||||||
(item) => item.value === row.dataScope,
|
|
||||||
);
|
|
||||||
if (found) {
|
|
||||||
return <Tag color={found.color}>{found.label}</Tag>;
|
|
||||||
}
|
|
||||||
return <Tag>{row.dataScope}</Tag>;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: '排序',
|
|
||||||
field: 'roleSort',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: '状态',
|
|
||||||
field: 'status',
|
|
||||||
slots: { default: 'status' },
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: '创建时间',
|
|
||||||
field: 'createTime',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
field: 'action',
|
|
||||||
fixed: 'right',
|
|
||||||
slots: { default: 'action' },
|
|
||||||
title: '操作',
|
|
||||||
width: 180,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
export const drawerSchema: FormSchemaGetter = () => [
|
|
||||||
{
|
|
||||||
component: 'Input',
|
|
||||||
dependencies: {
|
|
||||||
show: () => false,
|
|
||||||
triggerFields: [''],
|
|
||||||
},
|
|
||||||
fieldName: 'roleId',
|
|
||||||
label: '角色ID',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
component: 'Input',
|
|
||||||
fieldName: 'roleName',
|
|
||||||
label: '角色名称',
|
|
||||||
rules: 'required',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
component: 'Input',
|
|
||||||
fieldName: 'roleKey',
|
|
||||||
help: '如: test simpleUser等',
|
|
||||||
label: '权限标识',
|
|
||||||
rules: 'required',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
component: 'InputNumber',
|
|
||||||
fieldName: 'roleSort',
|
|
||||||
label: '角色排序',
|
|
||||||
rules: 'required',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
component: 'Select',
|
|
||||||
componentProps: {
|
|
||||||
allowClear: false,
|
|
||||||
options: getDictOptions(DictEnum.SYS_NORMAL_DISABLE),
|
|
||||||
getPopupContainer,
|
|
||||||
},
|
|
||||||
defaultValue: '0',
|
|
||||||
fieldName: 'status',
|
|
||||||
help: '修改后, 拥有该角色的用户将自动下线.',
|
|
||||||
label: '角色状态',
|
|
||||||
rules: 'required',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
component: 'Radio',
|
|
||||||
dependencies: {
|
|
||||||
show: () => false,
|
|
||||||
triggerFields: [''],
|
|
||||||
},
|
|
||||||
fieldName: 'menuCheckStrictly',
|
|
||||||
label: '菜单权限',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
component: 'Input',
|
|
||||||
defaultValue: [],
|
|
||||||
fieldName: 'menuIds',
|
|
||||||
label: '菜单权限',
|
|
||||||
formItemClass: 'col-span-2',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
component: 'Textarea',
|
|
||||||
defaultValue: '',
|
|
||||||
fieldName: 'remark',
|
|
||||||
formItemClass: 'items-baseline col-span-2',
|
|
||||||
label: '备注',
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
export const authModalSchemas: FormSchemaGetter = () => [
|
|
||||||
{
|
|
||||||
component: 'Input',
|
|
||||||
dependencies: {
|
|
||||||
show: () => false,
|
|
||||||
triggerFields: [''],
|
|
||||||
},
|
|
||||||
fieldName: 'roleId',
|
|
||||||
label: '角色ID',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
component: 'Radio',
|
|
||||||
dependencies: {
|
|
||||||
show: () => false,
|
|
||||||
triggerFields: [''],
|
|
||||||
},
|
|
||||||
fieldName: 'deptCheckStrictly',
|
|
||||||
label: 'deptCheckStrictly',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
component: 'Input',
|
|
||||||
componentProps: {
|
|
||||||
disabled: true,
|
|
||||||
},
|
|
||||||
fieldName: 'roleName',
|
|
||||||
label: '角色名称',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
component: 'Input',
|
|
||||||
componentProps: {
|
|
||||||
disabled: true,
|
|
||||||
},
|
|
||||||
fieldName: 'roleKey',
|
|
||||||
label: '权限标识',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
component: 'Select',
|
|
||||||
componentProps: {
|
|
||||||
allowClear: false,
|
|
||||||
getPopupContainer,
|
|
||||||
options: authScopeOptions,
|
|
||||||
},
|
|
||||||
fieldName: 'dataScope',
|
|
||||||
help: '更改后需要用户重新登录才能生效',
|
|
||||||
label: '权限范围',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
component: 'TreeSelect',
|
|
||||||
defaultValue: [],
|
|
||||||
dependencies: {
|
|
||||||
show: (values) => values.dataScope === '2',
|
|
||||||
triggerFields: ['dataScope'],
|
|
||||||
},
|
|
||||||
fieldName: 'deptIds',
|
|
||||||
formItemClass: 'items-baseline',
|
|
||||||
help: '更改后立即生效',
|
|
||||||
label: '部门权限',
|
|
||||||
},
|
|
||||||
];
|
|
@@ -1,239 +0,0 @@
|
|||||||
<script setup lang="ts">
|
|
||||||
import type { VxeGridProps } from '#/adapter/vxe-table';
|
|
||||||
import type { VbenFormProps } from '@vben/common-ui';
|
|
||||||
import type { Recordable } from '@vben/types';
|
|
||||||
|
|
||||||
import { useVbenVxeGrid, vxeCheckboxChecked } from '#/adapter/vxe-table';
|
|
||||||
import {
|
|
||||||
roleChangeStatus,
|
|
||||||
roleExport,
|
|
||||||
roleList,
|
|
||||||
roleRemove,
|
|
||||||
} from '#/api/system/role';
|
|
||||||
import { TableSwitch } from '#/components/table';
|
|
||||||
import { commonDownloadExcel } from '#/utils/file/download';
|
|
||||||
import { useAccess } from '@vben/access';
|
|
||||||
import { Page, useVbenDrawer, useVbenModal } from '@vben/common-ui';
|
|
||||||
import { getVxePopupContainer } from '@vben/utils';
|
|
||||||
import {
|
|
||||||
Dropdown,
|
|
||||||
Menu,
|
|
||||||
MenuItem,
|
|
||||||
Modal,
|
|
||||||
Popconfirm,
|
|
||||||
Space,
|
|
||||||
} from 'ant-design-vue';
|
|
||||||
import { computed } from 'vue';
|
|
||||||
import { useRouter } from 'vue-router';
|
|
||||||
|
|
||||||
import { columns, querySchema } from './data';
|
|
||||||
import roleAuthModal from './role-auth-modal.vue';
|
|
||||||
import roleDrawer from './role-drawer.vue';
|
|
||||||
|
|
||||||
const formOptions: VbenFormProps = {
|
|
||||||
commonConfig: {
|
|
||||||
labelWidth: 80,
|
|
||||||
componentProps: {
|
|
||||||
allowClear: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
schema: querySchema(),
|
|
||||||
wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
|
|
||||||
// 日期选择格式化
|
|
||||||
fieldMappingTime: [
|
|
||||||
[
|
|
||||||
'createTime',
|
|
||||||
['params[beginTime]', 'params[endTime]'],
|
|
||||||
['YYYY-MM-DD 00:00:00', 'YYYY-MM-DD 23:59:59'],
|
|
||||||
],
|
|
||||||
],
|
|
||||||
};
|
|
||||||
|
|
||||||
const gridOptions: VxeGridProps = {
|
|
||||||
checkboxConfig: {
|
|
||||||
// 高亮
|
|
||||||
highlight: true,
|
|
||||||
// 翻页时保留选中状态
|
|
||||||
reserve: true,
|
|
||||||
// 点击行选中
|
|
||||||
// trigger: 'row',
|
|
||||||
checkMethod: ({ row }) => row.roleId !== 1,
|
|
||||||
},
|
|
||||||
columns,
|
|
||||||
height: 'auto',
|
|
||||||
keepSource: true,
|
|
||||||
pagerConfig: {},
|
|
||||||
proxyConfig: {
|
|
||||||
ajax: {
|
|
||||||
query: async ({ page }, formValues = {}) => {
|
|
||||||
return await roleList({
|
|
||||||
pageNum: page.currentPage,
|
|
||||||
pageSize: page.pageSize,
|
|
||||||
...formValues,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
rowConfig: {
|
|
||||||
isHover: true,
|
|
||||||
keyField: 'roleId',
|
|
||||||
},
|
|
||||||
id: 'system-role-index',
|
|
||||||
};
|
|
||||||
|
|
||||||
const [BasicTable, tableApi] = useVbenVxeGrid({
|
|
||||||
formOptions,
|
|
||||||
gridOptions,
|
|
||||||
});
|
|
||||||
const [RoleDrawer, drawerApi] = useVbenDrawer({
|
|
||||||
connectedComponent: roleDrawer,
|
|
||||||
});
|
|
||||||
|
|
||||||
function handleAdd() {
|
|
||||||
drawerApi.setData({});
|
|
||||||
drawerApi.open();
|
|
||||||
}
|
|
||||||
|
|
||||||
async function handleEdit(record: Recordable<any>) {
|
|
||||||
drawerApi.setData({ id: record.roleId });
|
|
||||||
drawerApi.open();
|
|
||||||
}
|
|
||||||
|
|
||||||
async function handleDelete(row: Recordable<any>) {
|
|
||||||
await roleRemove(row.roleId);
|
|
||||||
await tableApi.query();
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleMultiDelete() {
|
|
||||||
const rows = tableApi.grid.getCheckboxRecords();
|
|
||||||
const ids = rows.map((row: any) => row.roleId);
|
|
||||||
Modal.confirm({
|
|
||||||
title: '提示',
|
|
||||||
okType: 'danger',
|
|
||||||
content: `确认删除选中的${ids.length}条记录吗?`,
|
|
||||||
onOk: async () => {
|
|
||||||
await roleRemove(ids);
|
|
||||||
await tableApi.query();
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleDownloadExcel() {
|
|
||||||
commonDownloadExcel(roleExport, '角色数据', tableApi.formApi.form.values, {
|
|
||||||
fieldMappingTime: formOptions.fieldMappingTime,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const { hasAccessByCodes, hasAccessByRoles } = useAccess();
|
|
||||||
|
|
||||||
const isSuperAdmin = computed(() => hasAccessByRoles(['superadmin']));
|
|
||||||
|
|
||||||
const [RoleAuthModal, authModalApi] = useVbenModal({
|
|
||||||
connectedComponent: roleAuthModal,
|
|
||||||
});
|
|
||||||
|
|
||||||
function handleAuthEdit(record: Recordable<any>) {
|
|
||||||
authModalApi.setData({ id: record.roleId });
|
|
||||||
authModalApi.open();
|
|
||||||
}
|
|
||||||
|
|
||||||
const router = useRouter();
|
|
||||||
function handleAssignRole(record: Recordable<any>) {
|
|
||||||
router.push(`/system/role-assign/${record.roleId}`);
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<template>
|
|
||||||
<Page :auto-content-height="true">
|
|
||||||
<BasicTable table-title="角色列表">
|
|
||||||
<template #toolbar-tools>
|
|
||||||
<Space>
|
|
||||||
<a-button
|
|
||||||
v-access:code="['system:role:export']"
|
|
||||||
@click="handleDownloadExcel"
|
|
||||||
>
|
|
||||||
{{ $t('pages.common.export') }}
|
|
||||||
</a-button>
|
|
||||||
<a-button
|
|
||||||
:disabled="!vxeCheckboxChecked(tableApi)"
|
|
||||||
danger
|
|
||||||
type="primary"
|
|
||||||
v-access:code="['system:role:remove']"
|
|
||||||
@click="handleMultiDelete"
|
|
||||||
>
|
|
||||||
{{ $t('pages.common.delete') }}
|
|
||||||
</a-button>
|
|
||||||
<a-button
|
|
||||||
type="primary"
|
|
||||||
v-access:code="['system:role:add']"
|
|
||||||
@click="handleAdd"
|
|
||||||
>
|
|
||||||
{{ $t('pages.common.add') }}
|
|
||||||
</a-button>
|
|
||||||
</Space>
|
|
||||||
</template>
|
|
||||||
<template #status="{ row }">
|
|
||||||
<TableSwitch
|
|
||||||
v-model="row.status"
|
|
||||||
:api="() => roleChangeStatus(row)"
|
|
||||||
:disabled="
|
|
||||||
row.roleId === 1 ||
|
|
||||||
row.roleKey === 'admin' ||
|
|
||||||
!hasAccessByCodes(['system:role:edit'])
|
|
||||||
"
|
|
||||||
:reload="() => tableApi.query()"
|
|
||||||
/>
|
|
||||||
</template>
|
|
||||||
<template #action="{ row }">
|
|
||||||
<!-- 租户管理员不可修改admin角色 防止误操作 -->
|
|
||||||
<!-- 超级管理员可通过租户切换来操作租户管理员角色 -->
|
|
||||||
<template
|
|
||||||
v-if="!row.superAdmin && (row.roleKey !== 'admin' || isSuperAdmin)"
|
|
||||||
>
|
|
||||||
<Space>
|
|
||||||
<ghost-button
|
|
||||||
v-access:code="['system:role:edit']"
|
|
||||||
@click.stop="handleEdit(row)"
|
|
||||||
>
|
|
||||||
{{ $t('pages.common.edit') }}
|
|
||||||
</ghost-button>
|
|
||||||
<Popconfirm
|
|
||||||
:get-popup-container="getVxePopupContainer"
|
|
||||||
placement="left"
|
|
||||||
title="确认删除?"
|
|
||||||
@confirm="handleDelete(row)"
|
|
||||||
>
|
|
||||||
<ghost-button
|
|
||||||
danger
|
|
||||||
v-access:code="['system:role:remove']"
|
|
||||||
@click.stop=""
|
|
||||||
>
|
|
||||||
{{ $t('pages.common.delete') }}
|
|
||||||
</ghost-button>
|
|
||||||
</Popconfirm>
|
|
||||||
</Space>
|
|
||||||
<Dropdown
|
|
||||||
:get-popup-container="getVxePopupContainer"
|
|
||||||
placement="bottomRight"
|
|
||||||
>
|
|
||||||
<template #overlay>
|
|
||||||
<Menu>
|
|
||||||
<MenuItem key="1" @click="handleAuthEdit(row)">
|
|
||||||
数据权限
|
|
||||||
</MenuItem>
|
|
||||||
<MenuItem key="2" @click="handleAssignRole(row)">
|
|
||||||
分配用户
|
|
||||||
</MenuItem>
|
|
||||||
</Menu>
|
|
||||||
</template>
|
|
||||||
<a-button size="small" type="link">
|
|
||||||
{{ $t('pages.common.more') }}
|
|
||||||
</a-button>
|
|
||||||
</Dropdown>
|
|
||||||
</template>
|
|
||||||
</template>
|
|
||||||
</BasicTable>
|
|
||||||
<RoleDrawer @reload="tableApi.query()" />
|
|
||||||
<RoleAuthModal @reload="tableApi.query()" />
|
|
||||||
</Page>
|
|
||||||
</template>
|
|
@@ -1,116 +0,0 @@
|
|||||||
<script setup lang="ts">
|
|
||||||
import { useVbenForm } from '#/adapter/form';
|
|
||||||
import { roleDataScope, roleDeptTree, roleInfo } from '#/api/system/role';
|
|
||||||
import { TreeSelectPanel } from '#/components/tree';
|
|
||||||
import { useVbenModal } from '@vben/common-ui';
|
|
||||||
import { cloneDeep } from '@vben/utils';
|
|
||||||
import { ref } from 'vue';
|
|
||||||
|
|
||||||
import { authModalSchemas } from './data';
|
|
||||||
|
|
||||||
const emit = defineEmits<{ reload: [] }>();
|
|
||||||
|
|
||||||
const [BasicForm, formApi] = useVbenForm({
|
|
||||||
commonConfig: {
|
|
||||||
componentProps: {
|
|
||||||
class: 'w-full',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
layout: 'vertical',
|
|
||||||
schema: authModalSchemas(),
|
|
||||||
showDefaultActions: false,
|
|
||||||
});
|
|
||||||
|
|
||||||
const deptTree = ref<any[]>([]);
|
|
||||||
async function setupDeptTree(id: number | string) {
|
|
||||||
const resp = await roleDeptTree(id);
|
|
||||||
formApi.setFieldValue('deptIds', resp.checkedKeys);
|
|
||||||
// 设置菜单信息
|
|
||||||
deptTree.value = resp.depts;
|
|
||||||
}
|
|
||||||
|
|
||||||
const [BasicModal, modalApi] = useVbenModal({
|
|
||||||
fullscreenButton: false,
|
|
||||||
onCancel: handleCancel,
|
|
||||||
onConfirm: handleConfirm,
|
|
||||||
onOpenChange: async (isOpen) => {
|
|
||||||
if (!isOpen) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
modalApi.modalLoading(true);
|
|
||||||
|
|
||||||
const { id } = modalApi.getData() as { id: number | string };
|
|
||||||
|
|
||||||
setupDeptTree(id);
|
|
||||||
const record = await roleInfo(id);
|
|
||||||
await formApi.setValues(record);
|
|
||||||
|
|
||||||
modalApi.modalLoading(false);
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 这里拿到的是一个数组ref
|
|
||||||
*/
|
|
||||||
const deptSelectRef = ref();
|
|
||||||
|
|
||||||
async function handleConfirm() {
|
|
||||||
try {
|
|
||||||
modalApi.modalLoading(true);
|
|
||||||
const { valid } = await formApi.validate();
|
|
||||||
if (!valid) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// formApi.getValues拿到的是一个readonly对象,不能直接修改,需要cloneDeep
|
|
||||||
const data = cloneDeep(await formApi.getValues());
|
|
||||||
// 不为自定义权限的话 删除部门id
|
|
||||||
if (data.dataScope === '2') {
|
|
||||||
const deptIds = deptSelectRef.value?.[0]?.getCheckedKeys() ?? [];
|
|
||||||
data.deptIds = deptIds;
|
|
||||||
} else {
|
|
||||||
data.deptIds = [];
|
|
||||||
}
|
|
||||||
await roleDataScope(data);
|
|
||||||
emit('reload');
|
|
||||||
await handleCancel();
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
} finally {
|
|
||||||
modalApi.modalLoading(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function handleCancel() {
|
|
||||||
modalApi.close();
|
|
||||||
await formApi.resetForm();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 通过回调更新 无法通过v-model
|
|
||||||
* @param value 菜单选择是否严格模式
|
|
||||||
*/
|
|
||||||
function handleCheckStrictlyChange(value: boolean) {
|
|
||||||
formApi.setFieldValue('deptCheckStrictly', value);
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<template>
|
|
||||||
<BasicModal
|
|
||||||
:close-on-click-modal="false"
|
|
||||||
class="min-h-[600px] w-[550px]"
|
|
||||||
title="分配权限"
|
|
||||||
>
|
|
||||||
<BasicForm>
|
|
||||||
<template #deptIds="slotProps">
|
|
||||||
<TreeSelectPanel
|
|
||||||
ref="deptSelectRef"
|
|
||||||
v-bind="slotProps"
|
|
||||||
:check-strictly="formApi.form.values.deptCheckStrictly"
|
|
||||||
:expand-all-on-init="true"
|
|
||||||
:tree-data="deptTree"
|
|
||||||
@check-strictly-change="handleCheckStrictlyChange"
|
|
||||||
/>
|
|
||||||
</template>
|
|
||||||
</BasicForm>
|
|
||||||
</BasicModal>
|
|
||||||
</template>
|
|
@@ -1,144 +0,0 @@
|
|||||||
<script setup lang="ts">
|
|
||||||
import { computed, nextTick, ref } from 'vue';
|
|
||||||
|
|
||||||
import { useVbenDrawer } from '@vben/common-ui';
|
|
||||||
import { $t } from '@vben/locales';
|
|
||||||
import { cloneDeep, eachTree } from '@vben/utils';
|
|
||||||
|
|
||||||
import { useVbenForm } from '#/adapter/form';
|
|
||||||
import { menuTreeSelect, roleMenuTreeSelect } from '#/api/system/menu';
|
|
||||||
import { roleAdd, roleInfo, roleUpdate } from '#/api/system/role';
|
|
||||||
import { MenuSelectTable } from '#/components/tree';
|
|
||||||
|
|
||||||
import { drawerSchema } from './data';
|
|
||||||
|
|
||||||
const emit = defineEmits<{ reload: [] }>();
|
|
||||||
|
|
||||||
const isUpdate = ref(false);
|
|
||||||
const title = computed(() => {
|
|
||||||
return isUpdate.value ? $t('pages.common.edit') : $t('pages.common.add');
|
|
||||||
});
|
|
||||||
|
|
||||||
const [BasicForm, formApi] = useVbenForm({
|
|
||||||
commonConfig: {
|
|
||||||
componentProps: {
|
|
||||||
class: 'w-full',
|
|
||||||
},
|
|
||||||
formItemClass: 'col-span-1',
|
|
||||||
},
|
|
||||||
layout: 'vertical',
|
|
||||||
schema: drawerSchema(),
|
|
||||||
showDefaultActions: false,
|
|
||||||
wrapperClass: 'grid-cols-2 gap-x-4',
|
|
||||||
});
|
|
||||||
|
|
||||||
const menuTree = ref<any[]>([]);
|
|
||||||
async function setupMenuTree(id?: number | string) {
|
|
||||||
if (id) {
|
|
||||||
const resp = await roleMenuTreeSelect(id);
|
|
||||||
const menus = resp.menus;
|
|
||||||
// i18n处理
|
|
||||||
eachTree(menus, (node) => {
|
|
||||||
node.label = $t(node.label);
|
|
||||||
});
|
|
||||||
// 设置菜单信息
|
|
||||||
menuTree.value = resp.menus;
|
|
||||||
// keys依赖于menu 需要先加载menu
|
|
||||||
await nextTick();
|
|
||||||
await formApi.setFieldValue('menuIds', resp.checkedKeys);
|
|
||||||
} else {
|
|
||||||
const resp = await menuTreeSelect();
|
|
||||||
// i18n处理
|
|
||||||
eachTree(resp, (node) => {
|
|
||||||
node.label = $t(node.label);
|
|
||||||
});
|
|
||||||
// 设置菜单信息
|
|
||||||
menuTree.value = resp;
|
|
||||||
// keys依赖于menu 需要先加载menu
|
|
||||||
await nextTick();
|
|
||||||
await formApi.setFieldValue('menuIds', []);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const [BasicDrawer, drawerApi] = useVbenDrawer({
|
|
||||||
onCancel: handleCancel,
|
|
||||||
onConfirm: handleConfirm,
|
|
||||||
async onOpenChange(isOpen) {
|
|
||||||
if (!isOpen) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
drawerApi.drawerLoading(true);
|
|
||||||
const { id } = drawerApi.getData() as { id?: number | string };
|
|
||||||
isUpdate.value = !!id;
|
|
||||||
|
|
||||||
if (isUpdate.value && id) {
|
|
||||||
const record = await roleInfo(id);
|
|
||||||
await formApi.setValues(record);
|
|
||||||
}
|
|
||||||
// init菜单 注意顺序要放在赋值record之后 内部watch会依赖record
|
|
||||||
await setupMenuTree(id);
|
|
||||||
|
|
||||||
drawerApi.drawerLoading(false);
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 这里拿到的是一个数组ref
|
|
||||||
*/
|
|
||||||
const menuSelectRef = ref<InstanceType<typeof MenuSelectTable>>();
|
|
||||||
|
|
||||||
async function handleConfirm() {
|
|
||||||
try {
|
|
||||||
drawerApi.drawerLoading(true);
|
|
||||||
const { valid } = await formApi.validate();
|
|
||||||
console.log(menuSelectRef.value);
|
|
||||||
if (!valid) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// 这个用于提交
|
|
||||||
const menuIds = menuSelectRef.value?.getCheckedKeys() ?? [];
|
|
||||||
// formApi.getValues拿到的是一个readonly对象,不能直接修改,需要cloneDeep
|
|
||||||
const data = cloneDeep(await formApi.getValues());
|
|
||||||
data.menuIds = menuIds;
|
|
||||||
await (isUpdate.value ? roleUpdate(data) : roleAdd(data));
|
|
||||||
emit('reload');
|
|
||||||
await handleCancel();
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
} finally {
|
|
||||||
drawerApi.drawerLoading(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function handleCancel() {
|
|
||||||
drawerApi.close();
|
|
||||||
await formApi.resetForm();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 通过回调更新 无法通过v-model
|
|
||||||
* @param value 菜单选择是否严格模式
|
|
||||||
*/
|
|
||||||
function handleMenuCheckStrictlyChange(value: boolean) {
|
|
||||||
formApi.setFieldValue('menuCheckStrictly', value);
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<template>
|
|
||||||
<BasicDrawer :close-on-click-modal="false" :title="title" class="w-[800px]">
|
|
||||||
<BasicForm>
|
|
||||||
<template #menuIds="slotProps">
|
|
||||||
<div class="h-[600px] w-full">
|
|
||||||
<!-- check-strictly为readonly 不能通过v-model绑定 -->
|
|
||||||
<MenuSelectTable
|
|
||||||
ref="menuSelectRef"
|
|
||||||
:checked-keys="slotProps.value"
|
|
||||||
:association="formApi.form.values.menuCheckStrictly"
|
|
||||||
:menus="menuTree"
|
|
||||||
@update:association="handleMenuCheckStrictlyChange"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
</BasicForm>
|
|
||||||
</BasicDrawer>
|
|
||||||
</template>
|
|
Reference in New Issue
Block a user