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,60 @@
import type { DemoForm, DemoQuery, DemoVO } from './model';
import type { ID, IDS, PageResult } from '#/api/common';
import { commonExport } from '#/api/helper';
import { requestClient } from '#/api/request';
/**
*
* @param params
* @returns
*/
export function demoList(params?: DemoQuery) {
return requestClient.get<PageResult<DemoVO>>('/demo/demo/list', { params });
}
/**
*
* @param params
* @returns
*/
export function demoExport(params?: DemoQuery) {
return commonExport('/demo/demo/export', params ?? {});
}
/**
*
* @param id id
* @returns
*/
export function demoInfo(id: ID) {
return requestClient.get<DemoVO>(`/demo/demo/${id}`);
}
/**
*
* @param data
* @returns void
*/
export function demoAdd(data: DemoForm) {
return requestClient.postWithMsg<void>('/demo/demo', data);
}
/**
*
* @param data
* @returns void
*/
export function demoUpdate(data: DemoForm) {
return requestClient.putWithMsg<void>('/demo/demo', data);
}
/**
*
* @param id id
* @returns void
*/
export function demoRemove(id: ID | IDS) {
return requestClient.deleteWithMsg<void>(`/demo/demo/${id}`);
}

View File

@ -0,0 +1,82 @@
import type { BaseEntity, PageQuery } from '#/api/common';
export interface DemoVO {
/**
*
*/
id: number | string;
/**
*
*/
orderNum: number;
/**
* key键
*/
testKey: string;
/**
*
*/
value: string;
/**
*
*/
version: number;
}
export interface DemoForm extends BaseEntity {
/**
*
*/
id?: number | string;
/**
*
*/
orderNum?: number;
/**
* key键
*/
testKey?: string;
/**
*
*/
value?: string;
/**
*
*/
version?: number;
}
export interface DemoQuery extends PageQuery {
/**
*
*/
orderNum?: number;
/**
* key键
*/
testKey?: string;
/**
*
*/
value?: string;
/**
*
*/
version?: number;
/**
*
*/
params?: any;
}

View File

@ -0,0 +1,91 @@
import type { FormSchemaGetter, VxeGridProps } from '#/adapter';
export const querySchema: FormSchemaGetter = () => [
{
component: 'Input',
fieldName: 'orderNum',
label: '排序号',
},
{
component: 'Input',
fieldName: 'testKey',
label: 'key键',
},
{
component: 'Input',
fieldName: 'value',
label: '值',
},
{
component: 'Input',
fieldName: 'version',
label: '版本',
},
];
export const columns: VxeGridProps['columns'] = [
{ type: 'checkbox', width: 60 },
{
title: '主键',
field: 'id',
},
{
title: '排序号',
field: 'orderNum',
},
{
title: 'key键',
field: 'testKey',
},
{
title: '值',
field: 'value',
},
{
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: '排序号',
fieldName: 'orderNum',
component: 'Input',
rules: 'required',
},
{
label: 'key键',
fieldName: 'testKey',
component: 'Input',
rules: 'required',
},
{
label: '值',
fieldName: 'value',
component: 'Input',
rules: 'required',
},
{
label: '版本',
fieldName: 'version',
component: 'Input',
rules: 'required',
},
];

View File

@ -0,0 +1,87 @@
<script setup lang="ts">
import { computed, ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { $t } from '@vben/locales';
import { cloneDeep } from '@vben/utils';
import { useVbenForm } from '#/adapter';
import { demoAdd, demoInfo, demoUpdate } 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',
});
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 demoInfo(id);
await formApi.setValues(record);
}
modalApi.modalLoading(false);
},
});
async function handleConfirm() {
try {
modalApi.modalLoading(true);
const { valid } = await formApi.validate();
if (!valid) {
return;
}
// getValuesreadonly
const data = cloneDeep(await formApi.getValues());
await (isUpdate.value ? demoUpdate(data) : demoAdd(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>

View File

@ -1,9 +1,183 @@
<script setup lang="ts">
import CommonSkeleton from '#/views/common';
import type { Recordable } from '@vben/types';
import { ref } from 'vue';
import { Page, useVbenModal, type VbenFormProps } from '@vben/common-ui';
import { Modal, Popconfirm, Space } from 'ant-design-vue';
import dayjs from 'dayjs';
import { useVbenVxeGrid, type VxeGridProps } from '#/adapter';
import { downloadExcel } from '#/utils/file/download';
import { demoExport, demoList, demoRemove } from './api';
import { columns, querySchema } from './data';
import demoModal from './demo-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 = {
checkboxConfig: {
//
highlight: true,
//
reserve: true,
//
// trigger: 'row',
},
columns,
height: 'auto',
keepSource: true,
pagerConfig: {},
proxyConfig: {
ajax: {
query: async ({ page }, formValues = {}) => {
//
if (formValues?.createTime) {
formValues.params = {
beginTime: dayjs(formValues.createTime[0]).format(
'YYYY-MM-DD 00:00:00',
),
endTime: dayjs(formValues.createTime[1]).format(
'YYYY-MM-DD 23:59:59',
),
};
Reflect.deleteProperty(formValues, 'createTime');
} else {
Reflect.deleteProperty(formValues, 'params');
}
return await demoList({
pageNum: page.currentPage,
pageSize: page.pageSize,
...formValues,
});
},
},
},
rowConfig: {
isHover: true,
keyField: 'id',
},
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 [DemoModal, modalApi] = useVbenModal({
connectedComponent: demoModal,
});
function handleAdd() {
modalApi.setData({});
modalApi.open();
}
async function handleEdit(record: Recordable<any>) {
modalApi.setData({ id: record.id });
modalApi.open();
}
async function handleDelete(row: Recordable<any>) {
await demoRemove(row.id);
await tableApi.query();
}
function handleMultiDelete() {
const rows = tableApi.grid.getCheckboxRecords();
const ids = rows.map((row: any) => row.id);
Modal.confirm({
title: '提示',
okType: 'danger',
content: `确认删除选中的${ids.length}条记录吗?`,
onOk: async () => {
await demoRemove(ids);
await tableApi.query();
checked.value = 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
v-access:code="['demo:demo:export']"
@click="downloadExcel(demoExport, '测试单表数据', {})"
>
{{ $t('pages.common.export') }}
</a-button>
<a-button
:disabled="!checked"
danger
type="primary"
v-access:code="['demo:demo:remove']"
@click="handleMultiDelete"
>
{{ $t('pages.common.delete') }}
</a-button>
<a-button
type="primary"
v-access:code="['demo:demo:add']"
@click="handleAdd"
>
{{ $t('pages.common.add') }}
</a-button>
</Space>
</template>
<template #action="{ row }">
<a-button
size="small"
type="link"
v-access:code="['demo:demo: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:demo:remove']"
@click.stop=""
>
{{ $t('pages.common.delete') }}
</a-button>
</Popconfirm>
</template>
</BasicTable>
<DemoModal @reload="tableApi.query()" />
</Page>
</template>

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;
}
// getValuesreadonly
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>