Skip to content

Commit bbc3761

Browse files
committed
代码生成:增加 tree 树形的示例
1 parent 3d0c4f4 commit bbc3761

File tree

7 files changed

+159
-478
lines changed

7 files changed

+159
-478
lines changed

src/api/infra/demo/demo02/index.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import request from '@/config/axios'
2+
3+
export interface Demo02CategoryVO {
4+
id: number
5+
name: string
6+
parentId: number
7+
}
8+
9+
// 查询示例分类列表
10+
export const getDemo02CategoryList = async (params) => {
11+
return await request.get({ url: `/infra/demo02-category/list`, params })
12+
}
13+
14+
// 查询示例分类详情
15+
export const getDemo02Category = async (id: number) => {
16+
return await request.get({ url: `/infra/demo02-category/get?id=` + id })
17+
}
18+
19+
// 新增示例分类
20+
export const createDemo02Category = async (data: Demo02CategoryVO) => {
21+
return await request.post({ url: `/infra/demo02-category/create`, data })
22+
}
23+
24+
// 修改示例分类
25+
export const updateDemo02Category = async (data: Demo02CategoryVO) => {
26+
return await request.put({ url: `/infra/demo02-category/update`, data })
27+
}
28+
29+
// 删除示例分类
30+
export const deleteDemo02Category = async (id: number) => {
31+
return await request.delete({ url: `/infra/demo02-category/delete?id=` + id })
32+
}
33+
34+
// 导出示例分类 Excel
35+
export const exportDemo02Category = async (params) => {
36+
return await request.download({ url: `/infra/demo02-category/export-excel`, params })
37+
}

src/api/infra/demo02/index.ts

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/views/infra/demo02/DemoStudentForm.vue renamed to src/views/infra/demo/demo02/Demo02CategoryForm.vue

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,30 @@
66
:rules="formRules"
77
label-width="100px"
88
v-loading="formLoading"
9-
/>
10-
<!-- 子表的表单 -->
11-
<el-tabs v-model="subTabsName">
12-
<el-tab-pane label="学生联系人" name="demoStudentContact">
13-
<DemoStudentContactForm ref="demoStudentContactFormRef" :student-id="formData.id" />
14-
</el-tab-pane>
15-
<el-tab-pane label="学生地址" name="demoStudentAddress">
16-
<DemoStudentAddressForm ref="demoStudentAddressFormRef" :student-id="formData.id" />
17-
</el-tab-pane>
18-
</el-tabs>
9+
>
10+
<el-form-item label="名字" prop="name">
11+
<el-input v-model="formData.name" placeholder="请输入名字" />
12+
</el-form-item>
13+
<el-form-item label="父级编号" prop="parentId">
14+
<el-tree-select
15+
v-model="formData.parentId"
16+
:data="demo02CategoryTree"
17+
:props="defaultProps"
18+
check-strictly
19+
default-expand-all
20+
placeholder="请选择父级编号"
21+
/>
22+
</el-form-item>
23+
</el-form>
1924
<template #footer>
2025
<el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
2126
<el-button @click="dialogVisible = false">取 消</el-button>
2227
</template>
2328
</Dialog>
2429
</template>
2530
<script setup lang="ts">
26-
import * as DemoStudentApi from '@/api/infra/demo02'
27-
import DemoStudentContactForm from './DemoStudentContactForm.vue'
28-
import DemoStudentAddressForm from './DemoStudentAddressForm.vue'
31+
import * as Demo02CategoryApi from '@/api/infra/demo/demo02'
32+
import { defaultProps, handleTree } from '@/utils/tree'
2933
3034
const { t } = useI18n() // 国际化
3135
const message = useMessage() // 消息弹窗
@@ -35,15 +39,16 @@ const dialogTitle = ref('') // 弹窗的标题
3539
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
3640
const formType = ref('') // 表单的类型:create - 新增;update - 修改
3741
const formData = ref({
38-
id: undefined
42+
id: undefined,
43+
name: undefined,
44+
parentId: undefined
45+
})
46+
const formRules = reactive({
47+
name: [{ required: true, message: '名字不能为空', trigger: 'blur' }],
48+
parentId: [{ required: true, message: '父级编号不能为空', trigger: 'blur' }]
3949
})
40-
const formRules = reactive({})
4150
const formRef = ref() // 表单 Ref
42-
43-
/** 子表的表单 */
44-
const subTabsName = ref('demoStudentContact')
45-
const demoStudentContactFormRef = ref()
46-
const demoStudentAddressFormRef = ref()
51+
const demo02CategoryTree = ref() // 树形结构
4752
4853
/** 打开弹窗 */
4954
const open = async (type: string, id?: number) => {
@@ -55,11 +60,12 @@ const open = async (type: string, id?: number) => {
5560
if (id) {
5661
formLoading.value = true
5762
try {
58-
formData.value = await DemoStudentApi.getDemoStudent(id)
63+
formData.value = await Demo02CategoryApi.getDemo02Category(id)
5964
} finally {
6065
formLoading.value = false
6166
}
6267
}
68+
await getDemo02CategoryTree()
6369
}
6470
defineExpose({ open }) // 提供 open 方法,用于打开弹窗
6571
@@ -68,31 +74,15 @@ const emit = defineEmits(['success']) // 定义 success 事件,用于操作成
6874
const submitForm = async () => {
6975
// 校验表单
7076
await formRef.value.validate()
71-
// 校验子表单
72-
try {
73-
await demoStudentContactFormRef.value.validate()
74-
} catch (e) {
75-
subTabsName.value = 'demoStudentContact'
76-
return
77-
}
78-
try {
79-
await demoStudentAddressFormRef.value.validate()
80-
} catch (e) {
81-
subTabsName.value = 'demoStudentAddress'
82-
return
83-
}
8477
// 提交请求
8578
formLoading.value = true
8679
try {
87-
const data = formData.value as unknown as DemoStudentApi.DemoStudentVO
88-
// 拼接子表的数据
89-
data.demoStudentContacts = demoStudentContactFormRef.value.getData()
90-
data.demoStudentAddress = demoStudentAddressFormRef.value.getData()
80+
const data = formData.value as unknown as Demo02CategoryApi.Demo02CategoryVO
9181
if (formType.value === 'create') {
92-
await DemoStudentApi.createDemoStudent(data)
82+
await Demo02CategoryApi.createDemo02Category(data)
9383
message.success(t('common.createSuccess'))
9484
} else {
95-
await DemoStudentApi.updateDemoStudent(data)
85+
await Demo02CategoryApi.updateDemo02Category(data)
9686
message.success(t('common.updateSuccess'))
9787
}
9888
dialogVisible.value = false
@@ -106,8 +96,19 @@ const submitForm = async () => {
10696
/** 重置表单 */
10797
const resetForm = () => {
10898
formData.value = {
109-
id: undefined
99+
id: undefined,
100+
name: undefined,
101+
parentId: undefined
110102
}
111103
formRef.value?.resetFields()
112104
}
113-
</script>
105+
106+
/** 获得示例分类树 */
107+
const getDemo02CategoryTree = async () => {
108+
demo02CategoryTree.value = []
109+
const data = await Demo02CategoryApi.getDemo02CategoryList()
110+
const root: Tree = { id: 0, name: '顶级示例分类', children: [] }
111+
root.children = handleTree(data, 'id', 'parentId')
112+
demo02CategoryTree.value.push(root)
113+
}
114+
</script>

0 commit comments

Comments
 (0)