feat(@vben/web-antd): 优化用户管理
This commit is contained in:
parent
51d63fc7c2
commit
996a8972ff
@ -76,9 +76,10 @@ export namespace UserApi {
|
||||
createAt?: string;
|
||||
}
|
||||
|
||||
export interface UserState {
|
||||
export interface User {
|
||||
username?: string;
|
||||
nickName?: string;
|
||||
name: string;
|
||||
avatar?: string;
|
||||
email?: string;
|
||||
phone?: string;
|
||||
@ -112,7 +113,7 @@ export function register(data: UserApi.CreateRecord) {
|
||||
}
|
||||
|
||||
// 新建用户
|
||||
export function create(data: UserApi.CreateRecord) {
|
||||
export function createUser(data: UserApi.CreateRecord) {
|
||||
return requestClient.post('/rest/user', data);
|
||||
}
|
||||
|
||||
@ -127,32 +128,32 @@ export function queryUserList(params: any) {
|
||||
}
|
||||
|
||||
// 根据id查询用户信息
|
||||
export function userDetail(id: string) {
|
||||
export function userDetail(id: any) {
|
||||
return requestClient.get(`/rest/user/${id}`);
|
||||
}
|
||||
|
||||
// 是否启用
|
||||
export function enabled(id: string) {
|
||||
export function enabledUser(id: any) {
|
||||
return requestClient.patch(`/rest/user/${id}/toggle`);
|
||||
}
|
||||
|
||||
// 删除用户
|
||||
export function remove(id: string) {
|
||||
export function removeUser(id: string) {
|
||||
return requestClient.delete(`/rest/user/${id}`);
|
||||
}
|
||||
|
||||
// 更新用户信息
|
||||
export function update(data: UserApi.UserRecord) {
|
||||
return requestClient.patch<UserApi.Res>(`/rest/user/${data.id}`, data);
|
||||
export function updateUser(id: any, data: UserApi.UserRecord) {
|
||||
return requestClient.patch<UserApi.Res>(`/rest/user/${id}`, data);
|
||||
}
|
||||
|
||||
export function selfUpdate(data: UserApi.UserState) {
|
||||
export function selfUpdate(data: UserApi.User) {
|
||||
return requestClient.patch<UserApi.Res>(`/rest/user/self`, data);
|
||||
}
|
||||
|
||||
// 获取个人用户信息
|
||||
export function getUserInfo() {
|
||||
return requestClient.get<UserApi.UserState>('/rest/user/self');
|
||||
return requestClient.get<UserApi.User>('/rest/user/self');
|
||||
}
|
||||
|
||||
// 部门的审核员
|
||||
@ -161,5 +162,5 @@ export function deptAudit(id: string, roleId: string) {
|
||||
}
|
||||
|
||||
export function getUserDetail(id: number) {
|
||||
return requestClient.get<UserApi.UserState>(`/user/${id}`);
|
||||
return requestClient.get<UserApi.User>(`/user/${id}`);
|
||||
}
|
||||
|
197
apps/web-antd/src/views/user/data.ts
Normal file
197
apps/web-antd/src/views/user/data.ts
Normal file
@ -0,0 +1,197 @@
|
||||
import type { VbenFormSchema } from '#/adapter/form';
|
||||
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
|
||||
import type { UserApi } from '#/api';
|
||||
|
||||
import { ref } from 'vue';
|
||||
|
||||
import { getAllDeptTree, queryRoleList } from '#/api';
|
||||
import { $t } from '#/locales';
|
||||
|
||||
const treeData = ref([]);
|
||||
const optionsRoleData = ref([]);
|
||||
|
||||
queryRoleList('').then((res) => {
|
||||
optionsRoleData.value = res.records.map((item) => ({
|
||||
label: item.name,
|
||||
value: item.id,
|
||||
}));
|
||||
});
|
||||
|
||||
function transformToTreeData(data: any) {
|
||||
return data.map((item) => ({
|
||||
value: item.id,
|
||||
label: item.name,
|
||||
...(item.children && { children: transformToTreeData(item.children) }),
|
||||
}));
|
||||
}
|
||||
|
||||
// 使用示例
|
||||
getAllDeptTree(1).then((res) => {
|
||||
treeData.value = transformToTreeData(res);
|
||||
});
|
||||
export function useFormSchema(): VbenFormSchema[] {
|
||||
return [
|
||||
{
|
||||
component: 'Input',
|
||||
fieldName: 'username',
|
||||
label: '用户名称',
|
||||
rules: 'required',
|
||||
},
|
||||
{
|
||||
component: 'Input',
|
||||
fieldName: 'name',
|
||||
label: '昵称',
|
||||
rules: 'required',
|
||||
},
|
||||
{
|
||||
component: 'Input',
|
||||
fieldName: 'email',
|
||||
label: 'Email',
|
||||
rules: 'required',
|
||||
},
|
||||
{
|
||||
component: 'Input',
|
||||
fieldName: 'phone',
|
||||
label: '电话',
|
||||
rules: 'required',
|
||||
},
|
||||
{
|
||||
component: 'Input',
|
||||
fieldName: 'password',
|
||||
label: '密码',
|
||||
rules: 'required',
|
||||
},
|
||||
{
|
||||
component: 'Input',
|
||||
fieldName: 'address',
|
||||
label: '地址',
|
||||
},
|
||||
{
|
||||
component: 'RadioGroup',
|
||||
componentProps: {
|
||||
buttonStyle: 'solid',
|
||||
options: [
|
||||
{ label: $t('common.enabled'), value: true },
|
||||
{ label: $t('common.disabled'), value: false },
|
||||
],
|
||||
optionType: 'button',
|
||||
},
|
||||
defaultValue: true,
|
||||
fieldName: 'enable',
|
||||
label: '状态',
|
||||
},
|
||||
{
|
||||
component: 'Textarea',
|
||||
fieldName: 'remark',
|
||||
label: '备注',
|
||||
},
|
||||
{
|
||||
component: 'Select',
|
||||
componentProps: {
|
||||
allowClear: true,
|
||||
mode: 'multiple',
|
||||
// api: queryRoleList(''),
|
||||
options: optionsRoleData,
|
||||
class: 'w-full',
|
||||
labelField: 'name',
|
||||
valueField: 'id',
|
||||
},
|
||||
fieldName: 'roleIds',
|
||||
label: '所属角色',
|
||||
},
|
||||
{
|
||||
component: 'TreeSelect',
|
||||
componentProps: {
|
||||
allowClear: true,
|
||||
// api: getAllDeptTree(1),
|
||||
treeData,
|
||||
class: 'w-full',
|
||||
labelField: 'name',
|
||||
valueField: 'id',
|
||||
childrenField: 'children',
|
||||
},
|
||||
fieldName: 'deptId',
|
||||
label: '所属部门',
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
export function useGridFormSchema(): VbenFormSchema[] {
|
||||
return [
|
||||
{
|
||||
component: 'Input',
|
||||
fieldName: 'username',
|
||||
label: '用户名称',
|
||||
},
|
||||
{
|
||||
component: 'Select',
|
||||
componentProps: {
|
||||
allowClear: true,
|
||||
options: [
|
||||
{ label: $t('common.enabled'), value: true },
|
||||
{ label: $t('common.disabled'), value: false },
|
||||
],
|
||||
},
|
||||
fieldName: 'enableState',
|
||||
label: '状态',
|
||||
},
|
||||
// {
|
||||
// component: 'Input',
|
||||
// fieldName: 'remark',
|
||||
// label: '备注',
|
||||
// },
|
||||
// {
|
||||
// component: 'RangePicker',
|
||||
// fieldName: 'createTime',
|
||||
// label: '创建时间',
|
||||
// },
|
||||
];
|
||||
}
|
||||
|
||||
export function useColumns<T = UserApi.User>(
|
||||
onActionClick: OnActionClickFn<T>,
|
||||
onStatusChange?: (newStatus: any, row: T) => PromiseLike<boolean | undefined>,
|
||||
): VxeTableGridOptions['columns'] {
|
||||
return [
|
||||
{
|
||||
field: 'username',
|
||||
title: '用户名称',
|
||||
width: 200,
|
||||
},
|
||||
{
|
||||
cellRender: {
|
||||
attrs: { beforeChange: onStatusChange },
|
||||
name: onStatusChange ? 'CellSwitch' : 'CellTag',
|
||||
},
|
||||
field: 'enableState',
|
||||
title: '用户状态',
|
||||
width: 100,
|
||||
},
|
||||
{
|
||||
field: 'remark',
|
||||
minWidth: 100,
|
||||
title: '备注',
|
||||
},
|
||||
{
|
||||
field: 'createTime',
|
||||
slots: { default: 'createTime' },
|
||||
title: '创建时间',
|
||||
width: 200,
|
||||
},
|
||||
{
|
||||
align: 'center',
|
||||
cellRender: {
|
||||
attrs: {
|
||||
nameField: 'name',
|
||||
nameTitle: '用户',
|
||||
onClick: onActionClick,
|
||||
},
|
||||
name: 'CellOperation',
|
||||
},
|
||||
field: 'operation',
|
||||
fixed: 'right',
|
||||
title: '操作',
|
||||
width: 130,
|
||||
},
|
||||
];
|
||||
}
|
168
apps/web-antd/src/views/user/list.vue
Normal file
168
apps/web-antd/src/views/user/list.vue
Normal file
@ -0,0 +1,168 @@
|
||||
<script lang="ts" setup>
|
||||
import type { Recordable } from '@vben/types';
|
||||
|
||||
import type {
|
||||
OnActionClickParams,
|
||||
VxeTableGridOptions,
|
||||
} from '#/adapter/vxe-table';
|
||||
import type { UserApi } from '#/api';
|
||||
|
||||
import { Page, useVbenDrawer } from '@vben/common-ui';
|
||||
import { Plus } from '@vben/icons';
|
||||
|
||||
import { Button, message, Modal } from 'ant-design-vue';
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
import { useVbenVxeGrid } from '#/adapter/vxe-table';
|
||||
import { enabledUser, queryUserList, removeUser } from '#/api';
|
||||
import { $t } from '#/locales';
|
||||
|
||||
import { useColumns, useGridFormSchema } from './data';
|
||||
import Form from './modules/form.vue';
|
||||
|
||||
const [FormDrawer, formDrawerApi] = useVbenDrawer({
|
||||
connectedComponent: Form,
|
||||
destroyOnClose: true,
|
||||
});
|
||||
|
||||
const [Grid, gridApi] = useVbenVxeGrid({
|
||||
formOptions: {
|
||||
fieldMappingTime: [['createTime', ['startTime', 'endTime']]],
|
||||
schema: useGridFormSchema(),
|
||||
},
|
||||
gridOptions: {
|
||||
columns: useColumns(onActionClick, onStatusChange),
|
||||
height: 'auto',
|
||||
keepSource: true,
|
||||
proxyConfig: {
|
||||
ajax: {
|
||||
query: async ({ page }, formValues) => {
|
||||
const res = await queryUserList({
|
||||
page: page.currentPage,
|
||||
size: page.size,
|
||||
...formValues,
|
||||
});
|
||||
return {
|
||||
items: res.records,
|
||||
total: res.total,
|
||||
};
|
||||
},
|
||||
},
|
||||
},
|
||||
rowConfig: {
|
||||
keyField: 'id',
|
||||
},
|
||||
|
||||
toolbarConfig: {
|
||||
custom: true,
|
||||
export: false,
|
||||
refresh: { code: 'query' },
|
||||
search: true,
|
||||
zoom: true,
|
||||
},
|
||||
} as VxeTableGridOptions<UserApi.User>,
|
||||
});
|
||||
|
||||
function onActionClick(e: OnActionClickParams<UserApi.User>) {
|
||||
switch (e.code) {
|
||||
case 'delete': {
|
||||
onDelete(e.row);
|
||||
break;
|
||||
}
|
||||
case 'edit': {
|
||||
onEdit(e.row);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将Antd的Modal.confirm封装为promise,方便在异步函数中调用。
|
||||
* @param content 提示内容
|
||||
* @param title 提示标题
|
||||
*/
|
||||
function confirm(content: string, title: string) {
|
||||
return new Promise((reslove, reject) => {
|
||||
Modal.confirm({
|
||||
content,
|
||||
onCancel() {
|
||||
reject(new Error('已取消'));
|
||||
},
|
||||
onOk() {
|
||||
reslove(true);
|
||||
},
|
||||
title,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 状态开关即将改变
|
||||
* @param newStatus 期望改变的状态值
|
||||
* @param row 行数据
|
||||
* @returns 返回false则中止改变,返回其他值(undefined、true)则允许改变
|
||||
*/
|
||||
async function onStatusChange(newStatus: number, row: UserApi.User) {
|
||||
const status: Recordable<string> = {
|
||||
0: '禁用',
|
||||
1: '启用',
|
||||
};
|
||||
try {
|
||||
await confirm(
|
||||
`你要将${row.name}的状态切换为 【${status[newStatus.toString()]}】 吗?`,
|
||||
`切换状态`,
|
||||
);
|
||||
await enabledUser(row.id);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function onEdit(row: UserApi.User) {
|
||||
formDrawerApi.setData(row).open();
|
||||
}
|
||||
|
||||
function onDelete(row: UserApi.User) {
|
||||
const hideLoading = message.loading({
|
||||
content: $t('ui.actionMessage.deleting', [row.name]),
|
||||
duration: 0,
|
||||
key: 'action_process_msg',
|
||||
});
|
||||
removeUser(row.id)
|
||||
.then(() => {
|
||||
message.success({
|
||||
content: $t('ui.actionMessage.deleteSuccess', [row.name]),
|
||||
key: 'action_process_msg',
|
||||
});
|
||||
onRefresh();
|
||||
})
|
||||
.catch(() => {
|
||||
hideLoading();
|
||||
});
|
||||
}
|
||||
|
||||
function onRefresh() {
|
||||
gridApi.query();
|
||||
}
|
||||
|
||||
function onCreate() {
|
||||
formDrawerApi.setData({}).open();
|
||||
}
|
||||
</script>
|
||||
<template>
|
||||
<Page auto-content-height>
|
||||
<FormDrawer />
|
||||
<Grid table-title="用户列表">
|
||||
<template #toolbar-tools>
|
||||
<Button type="primary" @click="onCreate">
|
||||
<Plus class="size-5" />
|
||||
{{ $t('ui.actionTitle.create', ['用户']) }}
|
||||
</Button>
|
||||
</template>
|
||||
<template #createTime="{ row }">
|
||||
{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm') }}
|
||||
</template>
|
||||
</Grid>
|
||||
</Page>
|
||||
</template>
|
138
apps/web-antd/src/views/user/modules/form.vue
Normal file
138
apps/web-antd/src/views/user/modules/form.vue
Normal file
@ -0,0 +1,138 @@
|
||||
<script lang="ts" setup>
|
||||
import type { DataNode } from 'ant-design-vue/es/tree';
|
||||
|
||||
import type { Recordable } from '@vben/types';
|
||||
|
||||
import type { UserApi } from '#/api';
|
||||
|
||||
import { computed, ref } from 'vue';
|
||||
|
||||
import { useVbenDrawer, VbenTree } from '@vben/common-ui';
|
||||
import { IconifyIcon } from '@vben/icons';
|
||||
|
||||
import { Spin } from 'ant-design-vue';
|
||||
|
||||
import { useVbenForm } from '#/adapter/form';
|
||||
import { createUser, updateUser, userDetail } from '#/api';
|
||||
import { $t } from '#/locales';
|
||||
|
||||
import { useFormSchema } from '../data';
|
||||
|
||||
const emits = defineEmits(['success']);
|
||||
|
||||
const formData = ref<UserApi.User>();
|
||||
|
||||
const [Form, formApi] = useVbenForm({
|
||||
schema: useFormSchema(),
|
||||
showDefaultActions: false,
|
||||
});
|
||||
|
||||
const permissions = ref<DataNode[]>([]);
|
||||
const loadingPermissions = ref(false);
|
||||
|
||||
const id = ref();
|
||||
const [Drawer, drawerApi] = useVbenDrawer({
|
||||
async onConfirm() {
|
||||
const { valid } = await formApi.validate();
|
||||
if (!valid) return;
|
||||
const values = await formApi.getValues();
|
||||
drawerApi.lock();
|
||||
(id.value ? updateUser(id.value, values) : createUser(values))
|
||||
.then(() => {
|
||||
emits('success');
|
||||
drawerApi.close();
|
||||
})
|
||||
.catch(() => {
|
||||
drawerApi.unlock();
|
||||
});
|
||||
},
|
||||
async onOpenChange(isOpen) {
|
||||
if (isOpen) {
|
||||
const data = drawerApi.getData<UserApi.User>();
|
||||
await formApi.resetForm();
|
||||
if (data.id) {
|
||||
formData.value = await userDetail(data.id);
|
||||
id.value = data.id;
|
||||
formApi.setValues(data);
|
||||
} else {
|
||||
id.value = undefined;
|
||||
}
|
||||
|
||||
// if (permissions.value.length === 0) {
|
||||
// loadPermissions();
|
||||
// }
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
// async function loadPermissions() {
|
||||
// loadingPermissions.value = true;
|
||||
// try {
|
||||
// const res = await getMenuList();
|
||||
// permissions.value = res as unknown as DataNode[];
|
||||
// } finally {
|
||||
// loadingPermissions.value = false;
|
||||
// }
|
||||
// }
|
||||
|
||||
const getDrawerTitle = computed(() => {
|
||||
return formData.value?.id
|
||||
? $t('common.edit', '用户')
|
||||
: $t('common.create', '用户');
|
||||
});
|
||||
|
||||
function getNodeClass(node: Recordable<any>) {
|
||||
const classes: string[] = [];
|
||||
if (node.value?.type === 'button') {
|
||||
classes.push('inline-flex');
|
||||
if (node.index % 3 >= 1) {
|
||||
classes.push('!pl-0');
|
||||
}
|
||||
}
|
||||
|
||||
return classes.join(' ');
|
||||
}
|
||||
</script>
|
||||
<template>
|
||||
<Drawer :title="getDrawerTitle">
|
||||
<Form>
|
||||
<template #permissions="slotProps">
|
||||
<Spin :spinning="loadingPermissions" wrapper-class-name="w-full">
|
||||
<VbenTree
|
||||
:tree-data="permissions"
|
||||
multiple
|
||||
bordered
|
||||
:default-expanded-level="2"
|
||||
:get-node-class="getNodeClass"
|
||||
v-bind="slotProps"
|
||||
value-field="id"
|
||||
label-field="meta.title"
|
||||
icon-field="meta.icon"
|
||||
>
|
||||
<template #node="{ value }">
|
||||
<IconifyIcon v-if="value.meta.icon" :icon="value.meta.icon" />
|
||||
{{ $t(value.meta.title) }}
|
||||
</template>
|
||||
</VbenTree>
|
||||
</Spin>
|
||||
</template>
|
||||
</Form>
|
||||
</Drawer>
|
||||
</template>
|
||||
<style lang="css" scoped>
|
||||
:deep(.ant-tree-title) {
|
||||
.tree-actions {
|
||||
display: none;
|
||||
margin-left: 20px;
|
||||
}
|
||||
}
|
||||
|
||||
:deep(.ant-tree-title:hover) {
|
||||
.tree-actions {
|
||||
display: flex;
|
||||
flex: auto;
|
||||
justify-content: flex-end;
|
||||
margin-left: 20px;
|
||||
}
|
||||
}
|
||||
</style>
|
Loading…
Reference in New Issue
Block a user