GenInfoForm.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <script setup lang="ts">
  2. import { onMounted, PropType, reactive, ref, watch } from 'vue'
  3. import { required } from '@/utils/formRules'
  4. import { Form } from '@/components/Form'
  5. import { handleTree } from '@/utils/tree'
  6. import { ElTreeSelect } from 'element-plus'
  7. import { useForm } from '@/hooks/web/useForm'
  8. import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
  9. import { listSimpleMenusApi } from '@/api/system/menu'
  10. import { CodegenTableVO } from '@/api/infra/codegen/types'
  11. import { FormSchema } from '@/types/form'
  12. const props = defineProps({
  13. genInfo: {
  14. type: Object as PropType<Nullable<CodegenTableVO>>,
  15. default: () => null
  16. }
  17. })
  18. const menuProps = {
  19. checkStrictly: true,
  20. children: 'children',
  21. label: 'name',
  22. value: 'id'
  23. }
  24. const rules = reactive({
  25. templateType: [required],
  26. scene: [required],
  27. moduleName: [required],
  28. businessName: [required],
  29. businessPackage: [required],
  30. className: [required],
  31. classComment: [required]
  32. })
  33. const templateTypeOptions = getIntDictOptions(DICT_TYPE.INFRA_CODEGEN_TEMPLATE_TYPE)
  34. const sceneOptions = getIntDictOptions(DICT_TYPE.INFRA_CODEGEN_SCENE)
  35. const treeRef = ref<InstanceType<typeof ElTreeSelect>>()
  36. const menuOptions = ref<any>([]) // 树形结构
  37. const getTree = async () => {
  38. const res = await listSimpleMenusApi()
  39. menuOptions.value = handleTree(res)
  40. }
  41. const schema = reactive<FormSchema[]>([
  42. {
  43. label: '生成模板',
  44. field: 'templateType',
  45. component: 'Select',
  46. componentProps: {
  47. options: templateTypeOptions
  48. },
  49. colProps: {
  50. span: 12
  51. }
  52. },
  53. {
  54. label: '生成场景',
  55. field: 'scene',
  56. component: 'Select',
  57. componentProps: {
  58. options: sceneOptions
  59. },
  60. colProps: {
  61. span: 12
  62. }
  63. },
  64. {
  65. label: '模块名',
  66. field: 'moduleName',
  67. component: 'Input',
  68. labelMessage: '模块名,即一级目录,例如 system、infra、tool 等等',
  69. colProps: {
  70. span: 12
  71. }
  72. },
  73. {
  74. label: '业务名',
  75. field: 'businessName',
  76. component: 'Input',
  77. labelMessage: '业务名,即二级目录,例如 user、permission、dict 等等',
  78. colProps: {
  79. span: 12
  80. }
  81. },
  82. {
  83. label: '类名称',
  84. field: 'className',
  85. component: 'Input',
  86. labelMessage: '类名称(首字母大写),例如SysUser、SysMenu、SysDictData 等等',
  87. colProps: {
  88. span: 12
  89. }
  90. },
  91. {
  92. label: '类描述',
  93. field: 'classComment',
  94. component: 'Input',
  95. labelMessage: '用作类描述,例如 用户',
  96. colProps: {
  97. span: 12
  98. }
  99. },
  100. {
  101. label: '上级菜单',
  102. field: 'parentMenuId',
  103. componentProps: {
  104. optionsSlot: true
  105. },
  106. labelMessage: '分配到指定菜单下,例如 系统管理',
  107. colProps: {
  108. span: 12
  109. }
  110. }
  111. ])
  112. const { register, methods, elFormRef } = useForm({
  113. schema
  114. })
  115. const parentMenuId = ref<number>()
  116. //子组件像父组件传值
  117. const emit = defineEmits(['menu'])
  118. const handleNodeClick = () => {
  119. emit('menu', parentMenuId.value)
  120. }
  121. // ========== 初始化 ==========
  122. onMounted(async () => {
  123. await getTree()
  124. })
  125. watch(
  126. () => props.genInfo,
  127. (genInfo) => {
  128. if (!genInfo) return
  129. const { setValues } = methods
  130. setValues(genInfo)
  131. },
  132. {
  133. deep: true,
  134. immediate: true
  135. }
  136. )
  137. defineExpose({
  138. elFormRef,
  139. getFormData: methods.getFormData
  140. })
  141. </script>
  142. <template>
  143. <Form :rules="rules" @register="register">
  144. <template #parentMenuId>
  145. <el-tree-select
  146. v-model="parentMenuId"
  147. ref="treeRef"
  148. node-key="id"
  149. :props="menuProps"
  150. :data="menuOptions"
  151. check-strictly
  152. @change="handleNodeClick"
  153. />
  154. </template>
  155. </Form>
  156. </template>