feat: 角色授权

This commit is contained in:
dap 2024-10-06 11:06:56 +08:00
parent ba73a23d4f
commit 87f9ff34b1
4 changed files with 273 additions and 24 deletions

View File

@ -62,7 +62,7 @@ export function roleOptionSelect(params?: any) {
}
export function roleAllocatedList(params: any) {
return requestClient.get(Api.roleAllocatedList, params);
return requestClient.get(Api.roleAllocatedList, { params });
}
/**

View File

@ -1,4 +1,4 @@
import type { FormSchemaGetter } from '#/adapter';
import type { FormSchemaGetter, VxeGridProps } from '#/adapter';
export const querySchema: FormSchemaGetter = () => [
{
@ -12,3 +12,30 @@ export const querySchema: FormSchemaGetter = () => [
label: '手机号码',
},
];
export const columns: VxeGridProps['columns'] = [
{ type: 'checkbox', width: 60 },
{
title: '用户账号',
field: 'userName',
},
{
title: '用户昵称',
field: 'nickName',
},
{
title: '邮箱',
field: 'email',
},
{
title: '手机号',
field: 'phonenumber',
},
{
field: 'action',
fixed: 'right',
slots: { default: 'action' },
title: '操作',
width: 180,
},
];

View File

@ -1,42 +1,163 @@
<script setup lang="ts">
import type { Recordable } from '@vben/types';
import { ref } from 'vue';
import { useRoute } from 'vue-router';
import { Page } from '@vben/common-ui';
import { Page, useVbenDrawer, type VbenFormProps } from '@vben/common-ui';
import { Card } from 'ant-design-vue';
import { Modal, Popconfirm, Space } from 'ant-design-vue';
import { useVbenForm } from '#/adapter';
import { useVbenVxeGrid, type VxeGridProps } from '#/adapter';
import {
roleAllocatedList,
roleAuthCancel,
roleAuthCancelAll,
} from '#/api/system/role';
import { querySchema } from './data';
import { columns, querySchema } from './data';
import roleAssignDrawer from './role-assign-drawer.vue';
const route = useRoute();
const roleId = route.params.roleId as string;
const [QueryForm] = useVbenForm({
//
collapsed: false,
//
const formOptions: VbenFormProps = {
commonConfig: {
//
componentProps: {
class: 'w-full',
},
labelWidth: 80,
},
schema: querySchema(),
//
showCollapseButton: true,
submitButtonOptions: {
text: '查询',
},
wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
};
const gridOptions: VxeGridProps = {
checkboxConfig: {
//
highlight: true,
//
reserve: true,
//
// trigger: 'row',
},
columns,
height: 'auto',
keepSource: true,
pagerConfig: {},
proxyConfig: {
ajax: {
query: async ({ page }, formValues = {}) => {
return await roleAllocatedList({
pageNum: page.currentPage,
pageSize: page.pageSize,
roleId,
...formValues,
});
},
},
},
rowConfig: {
isHover: true,
keyField: 'userId',
},
round: true,
align: 'center',
showOverflow: true,
};
const checked = ref(false);
const [BasicTable, tableApi] = useVbenVxeGrid({
formOptions,
gridOptions,
gridEvents: {
checkboxChange: (e: any) => {
checked.value = e.records.length > 0;
},
checkboxAll: (e: any) => {
checked.value = e.records.length > 0;
},
},
});
const [RoleAssignDrawer, drawerApi] = useVbenDrawer({
connectedComponent: roleAssignDrawer,
});
function handleAdd() {
drawerApi.setData({});
drawerApi.open();
}
/**
* 取消授权 一条记录
*/
async function handleAuthCancel(record: Recordable<any>) {
await roleAuthCancel({ userId: record.userId, roleId });
await tableApi.query();
}
/**
* 批量取消授权
*/
function handleMultipleAuthCancel() {
const rows = tableApi.grid.getCheckboxRecords();
const ids = rows.map((row: any) => row.userId);
Modal.confirm({
title: '提示',
okType: 'danger',
content: `确认取消选中的${ids.length}条授权记录吗?`,
onOk: async () => {
await roleAuthCancelAll(roleId, ids);
await tableApi.query();
checked.value = false;
tableApi.grid.clearCheckboxRow();
},
});
}
</script>
<template>
<Page>
<Card class="flex-1">
<QueryForm />
ID: {{ roleId }}
</Card>
<Page :auto-content-height="true">
<BasicTable>
<template #toolbar-actions>
<span class="pl-[7px] text-[16px]">已分配的用户列表</span>
</template>
<template #toolbar-tools>
<Space>
<a-button
:disabled="!checked"
danger
type="primary"
v-access:code="['system:role:remove']"
@click="handleMultipleAuthCancel"
>
取消授权
</a-button>
<a-button
type="primary"
v-access:code="['system:role:add']"
@click="handleAdd"
>
{{ $t('pages.common.add') }}
</a-button>
</Space>
</template>
<template #action="{ row }">
<Popconfirm
:title="`是否取消授权用户[${row.userName} - ${row.nickName}]?`"
placement="left"
@confirm="handleAuthCancel(row)"
>
<a-button
danger
size="small"
type="link"
v-access:code="['system:role:remove']"
@click.stop=""
>
取消授权
</a-button>
</Popconfirm>
</template>
</BasicTable>
<RoleAssignDrawer @reload="tableApi.query()" />
</Page>
</template>

View File

@ -0,0 +1,101 @@
<script setup lang="ts">
import { ref } from 'vue';
import { useRoute } from 'vue-router';
import { useVbenDrawer } from '@vben/common-ui';
import {
useVbenVxeGrid,
type VbenFormProps,
type VxeGridProps,
} from '#/adapter';
import { roleSelectAll, roleUnallocatedList } from '#/api/system/role';
import { columns, querySchema } from './data';
const emit = defineEmits<{ reload: [] }>();
const [BasicDrawer, drawerApi] = useVbenDrawer({
onConfirm: handleSubmit,
onCancel: handleReset,
});
const route = useRoute();
const roleId = route.params.roleId as string;
const formOptions: VbenFormProps = {
commonConfig: {
labelWidth: 80,
},
schema: querySchema(),
wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3',
};
const gridOptions: VxeGridProps = {
checkboxConfig: {
//
highlight: true,
//
reserve: true,
//
trigger: 'row',
},
columns: columns?.filter((item) => item.field !== 'action'),
height: 'auto',
keepSource: true,
pagerConfig: {},
proxyConfig: {
ajax: {
query: async ({ page }, formValues = {}) => {
return await roleUnallocatedList({
pageNum: page.currentPage,
pageSize: page.pageSize,
roleId,
...formValues,
});
},
},
},
rowConfig: {
isHover: true,
keyField: 'userId',
},
round: true,
align: 'center',
showOverflow: true,
};
const checked = ref(false);
const [BasicTable, tableApi] = useVbenVxeGrid({
formOptions,
gridOptions,
gridEvents: {
checkboxChange: (e: any) => {
checked.value = e.records.length > 0;
},
checkboxAll: (e: any) => {
checked.value = e.records.length > 0;
},
},
});
async function handleSubmit() {
const records = tableApi.grid.getCheckboxRecords();
const userIds = records.map((item) => item.userId);
if (userIds.length > 0) {
await roleSelectAll(roleId, userIds);
}
handleReset();
emit('reload');
}
function handleReset() {
drawerApi.close();
}
</script>
<template>
<BasicDrawer class="w-[800px]" title="选择用户">
<BasicTable />
</BasicDrawer>
</template>