fix: 一些基本功能

This commit is contained in:
2025-03-07 08:18:45 +08:00
parent 638462295d
commit 4cdb8351d4
453 changed files with 13336 additions and 24197 deletions

View File

@@ -0,0 +1,69 @@
import { requestClient } from '#/api/request';
const prefix = 'role/';
/**
* 分页查询用户列表
* @param data
*/
export async function getRoleList(data: any) {
return requestClient.get<any>(`${prefix}list`, { params: data });
}
/**
* 分页查询用户列表
* @param data
*/
export async function getRoleOption(data: any) {
return requestClient.get<any>(`${prefix}option`, { params: data });
}
/**
* 获取详情
* @param id
*/
export async function getRoleInfo(id: number) {
return requestClient.get<any>(`${prefix}detail`, { params: { id } });
}
/**
* 查询菜单下拉框
* @param data
*/
export async function getMenuIdsByRoleIds(data: any) {
return requestClient.get<any>(`${prefix}get-menu-ids-by-role-ids`, {
params: {
role_id: data.id,
},
});
}
/**
* 编辑角色
* @param data
*/
export async function saveRoleMenu(data: Record<string, any>) {
return requestClient.post<any>(`${prefix}save-role-menu`, data);
}
/**
* 新增角色
* @param data
*/
export async function createRole(data: Record<string, any>) {
return requestClient.post<any>(`${prefix}create`, data);
}
/**
* 编辑角色
* @param data
*/
export async function updateRole(data: Record<string, any>) {
return requestClient.post<any>(`${prefix}update`, data);
}
/**
* 删除角色
* @param data
*/
export async function deleteRole(data: Record<string, any>) {
return requestClient.post<any>(`${prefix}delete`, data);
}

View File

@@ -0,0 +1,141 @@
<script lang="ts" setup>
import { ref } from 'vue';
import { useVbenDrawer } from '@vben/common-ui';
import { Button, message, Tree } from 'ant-design-vue';
import { $t } from '#/locales';
import { getAllNodeIds, getLeafNodeIds } from '#/util/tool';
import { getMenuTreeOption } from '#/views/system/menu/api';
import { getMenuIdsByRoleIds, saveRoleMenu } from '../api';
import {Icon} from "#/components/icon";
const record = ref();
const treeRef = ref();
const treeData = ref([]);
const isExpand = ref(false);
// 勾选的key
const checkedKeys = ref([]);
// 提交的勾选的key,会进行特殊处理,包含半勾状态的父节点halfCheckedKeys
const submitCheckedKeys = ref<any>([]);
// 所有叶子节点key
const leafKeys = ref<any>([]);
// 所有节点key
const allNodeIds = ref([]);
// 当前展开的key
const currentExpandedKeys = ref([]);
/**
* api请求成功回调
*/
const handleFetchSuccess = () => {
getMenuIdsByRoleIds({
id: record.value.id,
// appCode: props.appCode,
}).then((res: any) => {
// 设置的勾选节点只能为叶子节点
checkedKeys.value = res.filter((item: any) => {
return leafKeys.value.includes(item);
});
submitCheckedKeys.value = res;
});
};
const [Drawer, DrawerApi] = useVbenDrawer({
onOpenChange(isOpen) {
record.value = isOpen ? DrawerApi.getData()?.record : {};
if (isOpen) {
DrawerApi.setState({
loading: true,
});
getMenuTreeOption({
filterByUser: 1,
})
.then((res) => {
treeData.value = res;
leafKeys.value = getLeafNodeIds(res);
allNodeIds.value = getAllNodeIds(res);
handleFetchSuccess();
})
.finally(() => {
DrawerApi.setState({
loading: false,
});
});
}
},
onConfirm() {
const menus = submitCheckedKeys.value.map((item: any) => {
return item;
});
DrawerApi.setState({
loading: true,
confirmLoading: true,
});
saveRoleMenu({
role_id: record.value.id,
menu_id: menus,
})
.then(() => {
message.success('保存成功');
DrawerApi.close();
})
.finally(() => {
DrawerApi.setState({
loading: false,
confirmLoading: false,
});
});
},
});
/**
* 点击复选框触发处理
* @param mCheckedKeys
*/
const handleCheck = (mCheckedKeys: any, e: any) => {
checkedKeys.value = mCheckedKeys;
// 提交的时候需要将半选的父节点也提交上
submitCheckedKeys.value = [...mCheckedKeys, ...e.halfCheckedKeys];
};
// 展开折叠事件
const handleExpand = (expandedKeys: any) => {
currentExpandedKeys.value = expandedKeys;
};
// 展开折叠按钮事件
const handleExpandAndCollapse = () => {
isExpand.value = !isExpand.value;
currentExpandedKeys.value = isExpand.value ? allNodeIds.value : [];
};
defineExpose(DrawerApi);
</script>
<template>
<div>
<Drawer class="w-[60%]" title="授权菜单">
<Button type="primary" @click="handleExpandAndCollapse">
{{ isExpand ? '折叠' : '展开' }}
</Button>
<Tree
ref="treeRef"
v-model:checked-keys="checkedKeys"
:expanded-keys="currentExpandedKeys"
:field-names="{
title: 'title',
key: 'id',
}"
:show-line="true"
:tree-data="treeData"
checkable
style="margin: 20px auto"
@check="handleCheck"
@expand="handleExpand"
>
<template #title="{ title, icon }">
<Icon :icon="icon" />
{{ $t(title) }}
</template>
</Tree>
</Drawer>
</div>
</template>

View File

@@ -0,0 +1,62 @@
<script lang="ts" setup>
import { ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { message } from 'ant-design-vue';
import { useVbenForm } from '#/adapter/form';
import { createRole, updateRole } from '#/views/system/role/api';
import { modalFormProps } from '#/views/system/role/config/form';
defineOptions({
name: 'FormModelDemo',
});
const isUpdate = ref(false);
const gridApi = ref();
const [Form, formApi] = useVbenForm(modalFormProps);
const [Modal, modalApi] = useVbenModal({
fullscreenButton: false,
draggable: true,
onCancel() {
modalApi.close();
},
onConfirm: async () => {
formApi.validate().then(async (e: any) => {
if (e.valid) {
const values = await formApi.getValues();
modalApi.setState({ loading: true, confirmLoading: true });
const submitApi = isUpdate.value ? updateRole : createRole;
submitApi(values)
.then(() => {
message.success('保存成功');
gridApi.value?.reload();
modalApi.close();
})
.finally(() => {
modalApi.setState({ loading: false, confirmLoading: false });
});
}
});
await formApi.validateAndSubmitForm();
},
onOpenChange(isOpen: boolean) {
gridApi.value = isOpen ? modalApi.getData()?.gridApi : null;
if (isOpen) {
const { values, update } = modalApi.getData<Record<string, any>>();
if (values) {
isUpdate.value = update;
formApi.setValues(values);
}
}
},
});
</script>
<template>
<Modal :title="`${isUpdate === true ? '编辑' : '新增'}角色`" class="w-[30%]">
<Form />
</Modal>
</template>

View File

@@ -0,0 +1,53 @@
import type { VbenFormProps } from '#/adapter/form';
export const modalFormProps: VbenFormProps = {
wrapperClass: 'grid-cols-12', // 24栅格,
commonConfig: {
formItemClass: 'col-span-12',
// 所有表单项
componentProps: {
class: 'w-full',
},
},
// handleSubmit: onSubmit,
layout: 'horizontal',
schema: [
{
component: 'VbenInput',
fieldName: 'id',
label: 'ID',
dependencies: {
show: false,
triggerFields: ['id'],
},
},
{
component: 'VbenInput',
componentProps: {
placeholder: '请输入角色昵称',
},
fieldName: 'name',
label: '昵称',
rules: 'required',
},
{
component: 'VbenInput',
componentProps: {
placeholder: '请输入角色代码',
},
fieldName: 'value',
label: '角色代码',
rules: 'required',
},
{
component: 'VbenInput',
componentProps: {
placeholder: '请输入角色说明',
},
fieldName: 'desc',
label: '角色说明',
rules: 'required',
},
],
showDefaultActions: false,
};

View File

@@ -0,0 +1,35 @@
import type { VbenFormProps } from '#/adapter/form';
export const formOptions: VbenFormProps = {
// 默认展开
collapsed: false,
schema: [
{
component: 'VbenInput',
componentProps: {
placeholder: '输入名称',
},
defaultValue: '',
fieldName: 'name',
label: '角色名称',
},
{
component: 'VbenInput',
componentProps: {
placeholder: '输入角色代码',
},
defaultValue: '',
fieldName: 'value',
label: '角色代码',
},
],
// 控制表单是否显示折叠按钮
showCollapseButton: true,
submitButtonOptions: {
content: '查询',
},
// 是否在字段值改变时提交表单
submitOnChange: true,
// 按下回车时是否提交表单
submitOnEnter: false,
};

View File

@@ -0,0 +1,73 @@
import type { VxeGridProps } from '#/adapter/vxe-table';
import { getRoleList } from '#/views/system/role/api';
interface RowType {
id: string;
nick_name: string;
email: string;
avatar: string;
roles: { name: string }[];
open_id: string;
code: string;
platform_id: string;
phone: string;
desc: string;
created_at: string;
}
export const gridOptions: VxeGridProps<RowType> = {
checkboxConfig: {
highlight: true,
labelField: '',
},
columnConfig: {
useKey: true,
},
rowConfig: {
useKey: true,
},
columns: [
{ type: 'checkbox', width: 60 },
{ field: 'id', align: 'left', title: 'ID', width: 100 },
{ field: 'name', align: 'left', title: '名称' },
{ field: 'value', title: '角色代码' },
{ field: 'desc', title: '备注' },
{ field: 'created_at', title: '创建时间' },
{ type: 'html', title: '操作', slots: { default: 'action' } },
],
keepSource: true,
pagerConfig: {},
proxyConfig: {
ajax: {
// 请求后端接口方法
query: async ({ page }, formValues) => {
return await getRoleList({
page: page.currentPage,
pageSize: page.pageSize,
...formValues,
});
},
},
},
height: 'auto',
border: false,
toolbarConfig: {
// 是否显示搜索表单控制按钮
// @ts-ignore 正式环境时有完整的类型声明
search: true,
refresh: true, // 刷新
print: false, // 打印
export: false, // 导出
// custom: true, // 自定义列
zoom: true, // 最大化最小化
slots: {
buttons: 'toolbar-buttons',
},
custom: {
// 自定义列-图标
icon: 'vxe-icon-menu',
},
},
showOverflow: false,
};

View File

@@ -0,0 +1,159 @@
<script lang="ts" setup>
import type { VxeGridListeners } from '#/adapter/vxe-table';
import { ref } from 'vue';
import { Page, useVbenModal } from '@vben/common-ui';
import { Button, Image, message } from 'ant-design-vue';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { TableAction } from '#/components/table-action';
import { deleteRole } from './api';
import FormModalDemo from './components/modal.vue';
import { formOptions } from './config/search';
import { gridOptions } from './config/table';
import AuthMenu from "#/views/system/role/components/auth-menu.vue";
const hasTopTableDropDownActions = ref(false);
const gridEvents: VxeGridListeners<any> = {
checkboxChange() {
// eslint-disable-next-line no-use-before-define
const records = gridApi.grid.getCheckboxRecords();
hasTopTableDropDownActions.value = records.length > 0;
},
checkboxAll() {
// eslint-disable-next-line no-use-before-define
const records = gridApi.grid.getCheckboxRecords();
hasTopTableDropDownActions.value = records.length > 0;
},
};
const [Grid, gridApi] = useVbenVxeGrid({
formOptions,
gridOptions,
gridEvents,
});
const [FormModal, formModalApi] = useVbenModal({
connectedComponent: FormModalDemo,
});
const showModal = (data = {}, isUpdate = false) => {
formModalApi.setData({
// 表单值
values: {
id: data?.id,
name: data?.name,
value: data?.value,
desc: data?.desc,
},
update: isUpdate,
gridApi,
});
formModalApi.open();
};
// 授权菜单
const authMenuRef = ref();
const handleAuthMenu = (record: any) => {
authMenuRef.value.setData({
record,
});
authMenuRef.value.open();
};
const deleteApi = (row: any) => {
let ids = [];
if (row) {
ids.push(row);
} else {
ids = gridApi.grid.getCheckboxRecords().map((item) => item.id);
}
deleteRole({ ids }).then(() => {
message.success('删除成功!');
gridApi.reload();
});
};
</script>
<template>
<Page auto-content-height title="角色管理">
<FormModal />
<AuthMenu ref="authMenuRef" />
<Grid>
<template #toolbar-buttons>
<TableAction
:actions="[
{
label: '新增',
type: 'primary',
icon: 'ant-design:plus-outlined',
// auth: ['超级角色', 'sys:user:save'],
onClick: showModal.bind(null),
},
]"
:drop-down-actions="[
{
label: '删除',
icon: 'ant-design:delete-outlined',
ifShow: hasTopTableDropDownActions,
// auth: ['超级角色', 'sys:user:save'],
popConfirm: {
title: '确定删除吗?',
confirm: deleteApi.bind(null, false),
},
},
]"
>
<template #more>
<Button style="margin-left: 16px">
批量操作
<Icon icon="ant-design:down-outlined" />
</Button>
</template>
</TableAction>
</template>
<template #avatar="{ row }">
<Image :src="row.avatar" height="30" width="30" />
</template>
<template #toolbar-tools></template>
<template #action="{ row }">
<TableAction
:actions="[
{
label: '编辑',
type: 'link',
icon: 'uil:edit',
size: 'small',
// auth: ['admin', 'sys:role:detail'],
onClick: showModal.bind(null, row, true),
},
{
label: '授权菜单',
type: 'link',
icon: 'arcticons:microsoft-authenticator',
size: 'small',
// auth: ['admin', 'sys:role:detail'],
onClick: handleAuthMenu.bind(null, row),
},
]"
:drop-down-actions="[
{
label: '删除',
type: 'link',
icon: 'ant-design:delete-outlined',
size: 'small',
// auth: ['admin', 'sys:role:detail'],
popConfirm: {
title: '确定删除吗?',
confirm: deleteApi.bind(null, row.id),
},
},
]"
/>
</template>
</Grid>
</Page>
</template>