feat: demo with code gen

This commit is contained in:
dap
2024-10-06 13:03:12 +08:00
parent 41fda26248
commit 64d4878628
10 changed files with 1002 additions and 8 deletions

View File

@@ -0,0 +1,50 @@
import type { TreeForm, TreeQuery, TreeVO } from './model';
import type { ID, IDS } from '#/api/common';
import { requestClient } from '#/api/request';
/**
* 查询测试树列表
* @param params
* @returns 测试树列表
*/
export function treeList(params?: TreeQuery) {
return requestClient.get<TreeVO[]>('/demo/tree/list', { params });
}
/**
* 查询测试树详情
* @param id id
* @returns 测试树详情
*/
export function treeInfo(id: ID) {
return requestClient.get<TreeVO>(`/demo/tree/${id}`);
}
/**
* 新增测试树
* @param data
* @returns void
*/
export function treeAdd(data: TreeForm) {
return requestClient.postWithMsg<void>('/demo/tree', data);
}
/**
* 更新测试树
* @param data
* @returns void
*/
export function treeUpdate(data: TreeForm) {
return requestClient.putWithMsg<void>('/demo/tree', data);
}
/**
* 删除测试树
* @param id id
* @returns void
*/
export function treeRemove(id: ID | IDS) {
return requestClient.deleteWithMsg<void>(`/demo/tree/${id}`);
}

View File

@@ -0,0 +1,102 @@
import type { BaseEntity } from '#/api/common';
export interface TreeVO {
/**
* 主键
*/
id: number | string;
/**
* 父id
*/
parentId: number | string;
/**
* 部门id
*/
deptId: number | string;
/**
* 用户id
*/
userId: number | string;
/**
* 值
*/
treeName: string;
/**
* 版本
*/
version: number;
/**
* 子对象
*/
children: TreeVO[];
}
export interface TreeForm extends BaseEntity {
/**
* 主键
*/
id?: number | string;
/**
* 父id
*/
parentId?: number | string;
/**
* 部门id
*/
deptId?: number | string;
/**
* 用户id
*/
userId?: number | string;
/**
* 值
*/
treeName?: string;
/**
* 版本
*/
version?: number;
}
export interface TreeQuery {
/**
* 父id
*/
parentId?: number | string;
/**
* 部门id
*/
deptId?: number | string;
/**
* 用户id
*/
userId?: number | string;
/**
* 值
*/
treeName?: string;
/**
* 版本
*/
version?: number;
/**
* 日期范围参数
*/
params?: any;
}

View File

@@ -0,0 +1,106 @@
import type { FormSchemaGetter, VxeGridProps } from '#/adapter';
export const querySchema: FormSchemaGetter = () => [
{
component: 'Input',
fieldName: 'parentId',
label: '父id',
},
{
component: 'Input',
fieldName: 'deptId',
label: '部门id',
},
{
component: 'Input',
fieldName: 'userId',
label: '用户id',
},
{
component: 'Input',
fieldName: 'treeName',
label: '值',
},
{
component: 'Input',
fieldName: 'version',
label: '版本',
},
];
export const columns: VxeGridProps['columns'] = [
{
title: '主键',
field: 'id',
treeNode: true,
},
{
title: '父id',
field: 'parentId',
},
{
title: '部门id',
field: 'deptId',
},
{
title: '用户id',
field: 'userId',
},
{
title: '值',
field: 'treeName',
},
{
title: '版本',
field: 'version',
},
{
field: 'action',
fixed: 'right',
slots: { default: 'action' },
title: '操作',
width: 180,
},
];
export const modalSchema: FormSchemaGetter = () => [
{
label: '主键',
fieldName: 'id',
component: 'Input',
dependencies: {
show: () => false,
triggerFields: [''],
},
},
{
label: '父id',
fieldName: 'parentId',
component: 'TreeSelect',
rules: 'required',
},
{
label: '部门id',
fieldName: 'deptId',
component: 'Input',
rules: 'required',
},
{
label: '用户id',
fieldName: 'userId',
component: 'Input',
rules: 'required',
},
{
label: '值',
fieldName: 'treeName',
component: 'Input',
rules: 'required',
},
{
label: '版本',
fieldName: 'version',
component: 'Input',
rules: 'required',
},
];

View File

@@ -1,9 +1,147 @@
<script setup lang="ts">
import CommonSkeleton from '#/views/common';
import type { Recordable } from '@vben/types';
import { nextTick } from 'vue';
import { Page, useVbenModal, type VbenFormProps } from '@vben/common-ui';
import { listToTree } from '@vben/utils';
import { Popconfirm, Space } from 'ant-design-vue';
import { useVbenVxeGrid, type VxeGridProps } from '#/adapter';
import { treeList, treeRemove } from './api';
import { columns, querySchema } from './data';
import treeModal from './tree-modal.vue';
const formOptions: VbenFormProps = {
commonConfig: {
labelWidth: 80,
},
schema: querySchema(),
wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
};
const gridOptions: VxeGridProps = {
columns,
height: 'auto',
keepSource: true,
pagerConfig: {
enabled: false,
},
proxyConfig: {
ajax: {
query: async (_, formValues = {}) => {
const resp = await treeList({
...formValues,
});
const treeData = listToTree(resp, {
id: 'id',
pid: 'parentId',
children: 'children',
});
return { rows: treeData };
},
// 默认请求接口后展开全部 不需要可以删除这段
querySuccess: () => {
nextTick(() => {
expandAll();
});
},
},
},
rowConfig: {
isHover: true,
keyField: 'id',
},
round: true,
align: 'center',
showOverflow: true,
treeConfig: {
parentField: 'parentId',
rowField: 'id',
transform: false,
},
};
const [BasicTable, tableApi] = useVbenVxeGrid({ formOptions, gridOptions });
const [TreeModal, modalApi] = useVbenModal({
connectedComponent: treeModal,
});
function handleAdd() {
modalApi.setData({ update: false });
modalApi.open();
}
async function handleEdit(row: Recordable<any>) {
modalApi.setData({ id: row.id, update: true });
modalApi.open();
}
async function handleDelete(row: Recordable<any>) {
await treeRemove(row.id);
await tableApi.query();
}
function expandAll() {
tableApi.grid?.setAllTreeExpand(true);
}
function collapseAll() {
tableApi.grid?.setAllTreeExpand(false);
}
</script>
<template>
<div>
<CommonSkeleton />
</div>
<Page :auto-content-height="true">
<BasicTable>
<template #toolbar-actions>
<span class="pl-[7px] text-[16px]">测试树列表</span>
</template>
<template #toolbar-tools>
<Space>
<a-button @click="collapseAll">
{{ $t('pages.common.collapse') }}
</a-button>
<a-button @click="expandAll">
{{ $t('pages.common.expand') }}
</a-button>
<a-button
type="primary"
v-access:code="['demo:tree:add']"
@click="handleAdd"
>
{{ $t('pages.common.add') }}
</a-button>
</Space>
</template>
<template #action="{ row }">
<a-button
size="small"
type="link"
v-access:code="['demo:tree:edit']"
@click="handleEdit(row)"
>
{{ $t('pages.common.edit') }}
</a-button>
<Popconfirm
placement="left"
title="确认删除?"
@confirm="handleDelete(row)"
>
<a-button
danger
size="small"
type="link"
v-access:code="['demo:tree:remove']"
@click.stop=""
>
{{ $t('pages.common.delete') }}
</a-button>
</Popconfirm>
</template>
</BasicTable>
<TreeModal @reload="tableApi.query()" />
</Page>
</template>

View File

@@ -0,0 +1,104 @@
<script setup lang="ts">
import { computed, ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { $t } from '@vben/locales';
import { cloneDeep, listToTree } from '@vben/utils';
import { useVbenForm } from '#/adapter';
import { treeAdd, treeInfo, treeList, treeUpdate } from './api';
import { modalSchema } 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: {
// 默认占满两列
formItemClass: 'col-span-2',
// 默认label宽度 px
labelWidth: 80,
// 通用配置项 会影响到所有表单项
componentProps: {
class: 'w-full',
},
},
schema: modalSchema(),
showDefaultActions: false,
wrapperClass: 'grid-cols-2',
});
async function setupTreeSelect() {
const listData = await treeList();
const treeData = listToTree(listData, { id: 'id', pid: 'parentId' });
formApi.updateSchema([
{
fieldName: 'parentId',
componentProps: {
treeData,
treeLine: { showLeafIcon: false },
fieldNames: { label: 'treeName', value: 'id' },
treeDefaultExpandAll: true,
},
},
]);
}
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 };
isUpdate.value = !!id;
if (isUpdate.value && id) {
const record = await treeInfo(id);
await formApi.setValues(record);
}
await setupTreeSelect();
modalApi.modalLoading(false);
},
});
async function handleConfirm() {
try {
modalApi.modalLoading(true);
const { valid } = await formApi.validate();
if (!valid) {
return;
}
// getValues获取为一个readonly的对象 需要修改必须先深拷贝一次
const data = cloneDeep(await formApi.getValues());
await (isUpdate.value ? treeUpdate(data) : treeAdd(data));
emit('reload');
await handleCancel();
} catch (error) {
console.error(error);
} finally {
modalApi.modalLoading(false);
}
}
async function handleCancel() {
modalApi.close();
await formApi.resetForm();
}
</script>
<template>
<BasicModal :close-on-click-modal="false" :title="title" class="w-[550px]">
<BasicForm />
</BasicModal>
</template>