!42 新增vben版中的《代码生成案例》

Merge pull request !42 from JackLi/demo01
pull/43/head
芋道源码 2024-05-18 13:12:03 +00:00 committed by Gitee
commit e10a7aa22b
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
11 changed files with 656 additions and 71 deletions

View File

@ -0,0 +1,31 @@
import { defHttp } from '@/utils/http/axios'
// 查询示例联系人列表
export function getDemo01ContactPage(params) {
return defHttp.get({ url: '/infra/demo01-contact/page', params })
}
// 查询示例联系人详情
export function getDemo01Contact(id: number) {
return defHttp.get({ url: `/infra/demo01-contact/get?id=${id}` })
}
// 新增示例联系人
export function createDemo01Contact(data) {
return defHttp.post({ url: '/infra/demo01-contact/create', data })
}
// 修改示例联系人
export function updateDemo01Contact(data) {
return defHttp.put({ url: '/infra/demo01-contact/update', data })
}
// 删除示例联系人
export function deleteDemo01Contact(id: number) {
return defHttp.delete({ url: `/infra/demo01-contact/delete?id=${id}` })
}
// 导出示例联系人 Excel
export function exportDemo01Contact(params) {
return defHttp.download({ url: '/infra/demo01-contact/export-excel', params }, '示例联系人.xls')
}

View File

@ -0,0 +1,31 @@
import { defHttp } from '@/utils/http/axios'
// 查询示例分类列表
export function getDemo02CategoryPage(params) {
return defHttp.get({ url: '/infra/demo02-category/page', params })
}
// 查询示例分类详情
export function getDemo02Category(id: number) {
return defHttp.get({ url: `/infra/demo02-category/get?id=${id}` })
}
// 新增示例分类
export function createDemo02Category(data) {
return defHttp.post({ url: '/infra/demo02-category/create', data })
}
// 修改示例分类
export function updateDemo02Category(data) {
return defHttp.put({ url: '/infra/demo02-category/update', data })
}
// 删除示例分类
export function deleteDemo02Category(id: number) {
return defHttp.delete({ url: `/infra/demo02-category/delete?id=${id}` })
}
// 导出示例分类 Excel
export function exportDemo02Category(params) {
return defHttp.download({ url: '/infra/demo02-category/export-excel', params }, '示例分类.xls')
}

View File

@ -0,0 +1,59 @@
<script lang="ts" setup>
import { ref, unref } from 'vue'
import { createFormSchema, updateFormSchema } from './demo01Contact.data'
import { useI18n } from '@/hooks/web/useI18n'
import { useMessage } from '@/hooks/web/useMessage'
import { BasicForm, useForm } from '@/components/Form'
import { BasicModal, useModalInner } from '@/components/Modal'
import { createDemo01Contact, getDemo01Contact, updateDemo01Contact } from '@/api/infra/demo/demo01'
defineOptions({ name: 'Demo01ContactModal' })
const emit = defineEmits(['success', 'register'])
const { t } = useI18n()
const { createMessage } = useMessage()
const isUpdate = ref(true)
const [registerForm, { setFieldsValue, resetFields, resetSchema, validate }] = useForm({
labelWidth: 120,
baseColProps: { span: 24 },
schemas: createFormSchema,
showActionButtonGroup: false,
actionColOptions: { span: 23 }
})
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
resetFields()
setModalProps({ confirmLoading: false })
isUpdate.value = !!data?.isUpdate
if (unref(isUpdate)) {
resetSchema(updateFormSchema)
const res = await getDemo01Contact(data.record.id)
setFieldsValue({ ...res })
}
})
async function handleSubmit() {
try {
const values = await validate()
setModalProps({ confirmLoading: true })
if (unref(isUpdate)) {
await updateDemo01Contact(values)
} else {
await createDemo01Contact(values)
}
closeModal()
emit('success')
createMessage.success(t('common.saveSuccessText'))
} finally {
setModalProps({ confirmLoading: false })
}
}
</script>
<template>
<BasicModal v-bind="$attrs" :title="isUpdate ? t('action.edit') : t('action.create')" @register="registerModal" @ok="handleSubmit">
<BasicForm @register="registerForm" />
</BasicModal>
</template>

View File

@ -0,0 +1,179 @@
import type { BasicColumn, FormSchema } from '@/components/Table'
import { useRender } from '@/components/Table'
import { DICT_TYPE, getDictOptions } from '@/utils/dict'
export const columns: BasicColumn[] = [
{
title: '编号',
dataIndex: 'id',
width: 80
},
{
title: '名字',
dataIndex: 'name',
width: 160
},
{
title: '性别',
dataIndex: 'sex',
width: 80,
customRender: ({ text }) => {
return useRender.renderDict(text, DICT_TYPE.SYSTEM_USER_SEX)
}
},
{
title: '出生年',
dataIndex: 'birthday',
width: 180,
customRender: ({ text }) => {
return useRender.renderDate(text)
}
},
{
title: '简介',
dataIndex: 'description',
width: 180
},
{
title: '头像',
dataIndex: 'avatar',
width: 120,
customRender: ({ text }) => {
return useRender.renderImg(text)
}
},
{
title: '创建时间',
dataIndex: 'createTime',
width: 180,
customRender: ({ text }) => {
return useRender.renderDate(text)
}
}
]
export const searchFormSchema: FormSchema[] = [
{
label: '名字',
field: 'name',
component: 'Input',
colProps: { span: 8 }
},
{
label: '性别',
field: 'sex',
component: 'Select',
componentProps: {
options: getDictOptions(DICT_TYPE.SYSTEM_USER_SEX)
},
colProps: { span: 8 }
},
{
label: '创建时间',
field: 'createTime',
component: 'RangePicker',
colProps: { span: 8 }
}
]
export const createFormSchema: FormSchema[] = [
{
label: '编号',
field: 'id',
show: false,
component: 'Input'
},
{
label: '名字',
field: 'name',
required: true,
component: 'Input'
},
{
label: '性别',
field: 'sex',
required: true,
component: 'RadioGroup',
componentProps: {
options: getDictOptions(DICT_TYPE.SYSTEM_USER_SEX)
}
},
{
label: '出生年月',
field: 'birthday',
required: true,
component: 'DatePicker',
componentProps: {
showTime: true,
format: 'YYYY-MM-DD',
valueFormat: 'x'
}
},
{
label: '简介',
field: 'description',
required: true,
component: 'Editor'
},
{
label: '头像',
field: 'avatar',
required: true,
component: 'FileUpload',
componentProps: {
fileType: 'image',
maxCount: 1
}
}
]
export const updateFormSchema: FormSchema[] = [
{
label: '编号',
field: 'id',
show: false,
component: 'Input'
},
{
label: '名字',
field: 'name',
required: true,
component: 'Input'
},
{
label: '性别',
field: 'sex',
required: true,
component: 'RadioGroup',
componentProps: {
options: getDictOptions(DICT_TYPE.SYSTEM_USER_SEX)
}
},
{
label: '出生年',
field: 'birthday',
required: true,
component: 'DatePicker',
componentProps: {
showTime: true,
format: 'YYYY-MM-DD',
valueFormat: 'x'
}
},
{
label: '简介',
field: 'description',
required: true,
component: 'Editor'
},
{
label: '头像',
field: 'avatar',
required: true,
component: 'FileUpload',
componentProps: {
fileType: 'image',
maxCount: 1
}
}
]

View File

@ -1,17 +1,93 @@
<script lang="ts" setup> <script lang="ts" setup>
import { DocAlert } from '@/components/DocAlert' import Demo01ContactModal from './Demo01ContactModal.vue'
</script> import { columns, searchFormSchema } from './demo01Contact.data'
import { useI18n } from '@/hooks/web/useI18n'
import { useMessage } from '@/hooks/web/useMessage'
import { useModal } from '@/components/Modal'
import { BasicTable, TableAction, useTable } from '@/components/Table'
import { deleteDemo01Contact, exportDemo01Contact, getDemo01ContactPage } from '@/api/infra/demo/demo01'
import { IconEnum } from '@/enums/appEnum'
defineOptions({ name: 'Demo01Contact' })
const { t } = useI18n()
const { createConfirm, createMessage } = useMessage()
const [registerModal, { openModal }] = useModal()
const [registerTable, { getForm, reload }] = useTable({
title: '示例联系人列表',
api: getDemo01ContactPage,
columns,
formConfig: { labelWidth: 120, schemas: searchFormSchema },
useSearchForm: true,
showTableSetting: true,
showIndexColumn: false,
actionColumn: {
width: 140,
title: t('common.action'),
dataIndex: 'action',
fixed: 'right'
}
})
function handleCreate() {
openModal(true, { isUpdate: false })
}
function handleEdit(record: Recordable) {
openModal(true, { record, isUpdate: true })
}
async function handleExport() {
createConfirm({
title: t('common.exportTitle'),
iconType: 'warning',
content: t('common.exportMessage'),
async onOk() {
await exportDemo01Contact(getForm().getFieldsValue())
createMessage.success(t('common.exportSuccessText'))
}
})
}
async function handleDelete(record: Recordable) {
await deleteDemo01Contact(record.id)
createMessage.success(t('common.delSuccessText'))
reload()
}
</script>
<template> <template>
<div> <div>
<DocAlert title="代码生成(单表)" url="https://doc.iocoder.cn/new-feature/" /> <BasicTable @register="registerTable">
<template #toolbar>
<a-button danger type="link" target="_blank" href="https://github.com/yudaocode/yudao-ui-admin-vue3"> <a-button type="primary" v-auth="['infra:demo01-contact:create']" :preIcon="IconEnum.ADD" @click="handleCreate">
该功能支持 Vue3 + element-plus 版本 {{ t('action.create') }}
</a-button> </a-button>
<br /> <a-button v-auth="['infra:demo01-contact:export']" :preIcon="IconEnum.EXPORT" @click="handleExport">
<a-button type="link" target="_blank" href="https://github.com/yudaocode/yudao-ui-admin-vue3/blob/master/src/views/infra/demo/demo01/index.vue"> {{ t('action.export') }}
可参考 https://github.com/yudaocode/yudao-ui-admin-vue3/blob/master/src/views/infra/demo/demo01/index.vue pull request </a-button>
</a-button> </template>
<template #bodyCell="{ column, record }">
<template v-if="column.key === 'action'">
<TableAction
:actions="[
{ icon: IconEnum.EDIT, label: t('action.edit'), auth: 'infra:demo01-contact:update', onClick: handleEdit.bind(null, record) },
{
icon: IconEnum.DELETE,
danger: true,
label: t('action.delete'),
auth: 'infra:demo01-contact:delete',
popConfirm: {
title: t('common.delMessage'),
placement: 'left',
confirm: handleDelete.bind(null, record)
}
}
]"
/>
</template>
</template>
</BasicTable>
<Demo01ContactModal @register="registerModal" @success="reload()" />
</div> </div>
</template> </template>

View File

@ -0,0 +1,58 @@
<script lang="ts" setup>
import { ref, unref } from 'vue'
import { createFormSchema, updateFormSchema } from './demo02Category.data'
import { useI18n } from '@/hooks/web/useI18n'
import { useMessage } from '@/hooks/web/useMessage'
import { BasicForm, useForm } from '@/components/Form'
import { BasicModal, useModalInner } from '@/components/Modal'
import { createDemo02Category, getDemo02Category, updateDemo02Category } from '@/api/infra/demo/demo02'
defineOptions({ name: 'Demo02CategoryModal' })
const emit = defineEmits(['success', 'register'])
const { t } = useI18n()
const { createMessage } = useMessage()
const isUpdate = ref(true)
const [registerForm, { setFieldsValue, resetFields, resetSchema, validate }] = useForm({
labelWidth: 120,
baseColProps: { span: 24 },
schemas: createFormSchema,
showActionButtonGroup: false,
actionColOptions: { span: 23 }
})
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
resetFields()
setModalProps({ confirmLoading: false })
isUpdate.value = !!data?.isUpdate
if (unref(isUpdate)) {
resetSchema(updateFormSchema)
const res = await getDemo02Category(data.record.id)
setFieldsValue({ ...res })
}
})
async function handleSubmit() {
try {
const values = await validate()
setModalProps({ confirmLoading: true })
if (unref(isUpdate)) {
await updateDemo02Category(values)
} else {
await createDemo02Category(values)
}
closeModal()
emit('success')
createMessage.success(t('common.saveSuccessText'))
} finally {
setModalProps({ confirmLoading: false })
}
}
</script>
<template>
<BasicModal v-bind="$attrs" :title="isUpdate ? t('action.edit') : t('action.create')" @register="registerModal" @ok="handleSubmit">
<BasicForm @register="registerForm" />
</BasicModal>
</template>

View File

@ -0,0 +1,101 @@
import type { BasicColumn, FormSchema } from '@/components/Table'
import { useRender } from '@/components/Table'
import { getDemo02CategoryPage } from '@/api/infra/demo/demo02'
export const columns: BasicColumn[] = [
{
title: '编号',
dataIndex: 'id',
width: 100
},
{
title: '名字',
dataIndex: 'name',
width: 290
},
{
title: '创建时间',
dataIndex: 'createTime',
width: 120,
customRender: ({ text }) => {
return useRender.renderDate(text)
}
}
]
export const searchFormSchema: FormSchema[] = [
{
label: '名字',
field: 'name',
component: 'Input',
colProps: { span: 8 }
},
{
label: '创建时间',
field: 'createTime',
component: 'RangePicker',
colProps: { span: 8 }
}
]
export const createFormSchema: FormSchema[] = [
{
label: '编号',
field: 'id',
show: false,
component: 'Input'
},
{
label: '名字',
field: 'name',
required: true,
component: 'Input'
},
{
label: '父级编号',
field: 'parentId',
required: true,
component: 'ApiTreeSelect',
componentProps: {
api: () => getDemo02CategoryPage(null),
parentLabel: '顶级示例分类',
fieldNames: {
label: 'name',
key: 'id',
value: 'id'
},
handleTree: 'id'
}
}
]
export const updateFormSchema: FormSchema[] = [
{
label: '编号',
field: 'id',
show: false,
component: 'Input'
},
{
label: '名字',
field: 'name',
required: true,
component: 'Input'
},
{
label: '父级编号',
field: 'parentId',
required: true,
component: 'ApiTreeSelect',
componentProps: {
api: () => getDemo02CategoryPage(null),
fieldNames: {
parentLabel: '顶级示例分类',
label: 'name',
key: 'id',
value: 'id'
},
handleTree: 'id'
}
}
]

View File

@ -1,16 +1,114 @@
<script setup lang="ts"> <script lang="ts" setup>
import { DocAlert } from '@/components/DocAlert' import Demo02CategoryModal from './Demo02CategoryModal.vue'
import { columns, searchFormSchema } from './demo02Category.data'
import { useI18n } from '@/hooks/web/useI18n'
import { useMessage } from '@/hooks/web/useMessage'
import { useModal } from '@/components/Modal'
import { BasicTable, TableAction, useTable } from '@/components/Table'
import { deleteDemo02Category, exportDemo02Category, getDemo02CategoryPage } from '@/api/infra/demo/demo02'
import { IconEnum } from '@/enums/appEnum'
import { handleTree } from '@/utils/tree'
import { nextTick } from 'vue'
defineOptions({ name: 'Demo02Category' })
const { t } = useI18n()
const { createConfirm, createMessage } = useMessage()
const [registerModal, { openModal }] = useModal()
const [registerTable, { expandAll, collapseAll, getForm, reload }] = useTable({
title: '示例分类列表',
api: getList,
columns,
rowKey: 'id',
formConfig: { labelWidth: 120, schemas: searchFormSchema },
isTreeTable: true,
pagination: false,
useSearchForm: true,
showTableSetting: true,
showIndexColumn: false,
actionColumn: {
width: 140,
title: t('common.action'),
dataIndex: 'action',
fixed: 'right'
}
})
async function getList() {
const res = await getDemo02CategoryPage(getForm().getFieldsValue() as any)
return handleTree(res, 'id')
}
function onFetchSuccess() {
nextTick(expandAll)
}
function handleCreate() {
openModal(true, { isUpdate: false })
}
function handleEdit(record: Recordable) {
openModal(true, { record, isUpdate: true })
}
async function handleExport() {
createConfirm({
title: t('common.exportTitle'),
iconType: 'warning',
content: t('common.exportMessage'),
async onOk() {
await exportDemo02Category(getForm().getFieldsValue())
createMessage.success(t('common.exportSuccessText'))
}
})
}
async function handleDelete(record: Recordable) {
await deleteDemo02Category(record.id)
createMessage.success(t('common.delSuccessText'))
reload()
}
</script> </script>
<template> <template>
<div> <div>
<DocAlert title="代码生成(树表)" url="https://doc.iocoder.cn/new-feature/tree/" /> <BasicTable @register="registerTable" @fetch-success="onFetchSuccess">
<template #toolbar>
<a-button danger type="link" target="_blank" href="https://github.com/yudaocode/yudao-ui-admin-vue3"> <a-button type="primary" v-auth="['infra:demo02-category:create']" :preIcon="IconEnum.ADD" @click="handleCreate">
该功能支持 Vue3 + element-plus 版本 {{ t('action.create') }}
</a-button> </a-button>
<br /> <a-button v-auth="['infra:demo02-category:export']" :preIcon="IconEnum.EXPORT" @click="handleExport">
<a-button type="link" target="_blank" href="https://github.com/yudaocode/yudao-ui-admin-vue3/blob/master/src/views/infra/demo/demo02/index.vue"> {{ t('action.export') }}
可参考 https://github.com/yudaocode/yudao-ui-admin-vue3/blob/master/src/views/infra/demo/demo02/index.vue pull request </a-button>
</a-button> <a-button type="dashed" @click="expandAll">{{ t('component.tree.expandAll') }}</a-button>
<a-button type="dashed" @click="collapseAll">{{ t('component.tree.unExpandAll') }}</a-button>
</template>
<template #bodyCell="{ column, record }">
<template v-if="column.key === 'action'">
<TableAction
:actions="[
{
icon: IconEnum.EDIT,
label: t('action.edit'),
auth: 'infra:demo02-category:update',
onClick: handleEdit.bind(null, record)
},
{
icon: IconEnum.DELETE,
danger: true,
label: t('action.delete'),
auth: 'infra:demo02-category:delete',
popConfirm: {
title: t('common.delMessage'),
placement: 'left',
confirm: handleDelete.bind(null, record)
}
}
]"
/>
</template>
</template>
</BasicTable>
<Demo02CategoryModal @register="registerModal" @success="reload()" />
</div> </div>
</template> </template>

View File

@ -1,16 +0,0 @@
<script setup lang="ts">
import { DocAlert } from '@/components/DocAlert'
</script>
<template>
<div>
<DocAlert title="代码生成(主子表)" url="https://doc.iocoder.cn/new-feature/master-sub/" />
<a-button danger type="link" target="_blank" href="https://github.com/yudaocode/yudao-ui-admin-vue3">
该功能支持 Vue3 + element-plus 版本
</a-button>
<br />
<a-button type="link" target="_blank" href="https://github.com/yudaocode/yudao-ui-admin-vue3/blob/master/src/views/infra/demo/demo03/erp/index.vue">
可参考 https://github.com/yudaocode/yudao-ui-admin-vue3/blob/master/src/views/infra/demo/demo03/erp/index.vue pull request
</a-button>
</div>
</template>

View File

@ -1,16 +0,0 @@
<script setup lang="ts">
import { DocAlert } from '@/components/DocAlert'
</script>
<template>
<div>
<DocAlert title="代码生成(主子表)" url="https://doc.iocoder.cn/new-feature/master-sub/" />
<a-button danger type="link" target="_blank" href="https://github.com/yudaocode/yudao-ui-admin-vue3">
该功能支持 Vue3 + element-plus 版本
</a-button>
<br />
<a-button type="link" target="_blank" href="https://github.com/yudaocode/yudao-ui-admin-vue3/blob/master/src/views/infra/demo/demo03/inner/index.vue">
可参考 https://github.com/yudaocode/yudao-ui-admin-vue3/blob/master/src/views/infra/demo/demo03/inner/index.vue pull request
</a-button>
</div>
</template>

View File

@ -1,16 +0,0 @@
<script setup lang="ts">
import { DocAlert } from '@/components/DocAlert'
</script>
<template>
<div>
<DocAlert title="代码生成(主子表)" url="https://doc.iocoder.cn/new-feature/master-sub/" />
<a-button danger type="link" target="_blank" href="https://github.com/yudaocode/yudao-ui-admin-vue3">
该功能支持 Vue3 + element-plus 版本
</a-button>
<br />
<a-button type="link" target="_blank" href="https://github.com/yudaocode/yudao-ui-admin-vue3/blob/master/src/views/infra/demo/demo03/normal/index.vue">
可参考 https://github.com/yudaocode/yudao-ui-admin-vue3/blob/master/src/views/infra/demo/demo03/normal/index.vue pull request
</a-button>
</div>
</template>