You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.9 KiB

  1. <template>
  2. <div>
  3. <div class="gva-form-box">
  4. <el-form :model="formData" label-position="right" label-width="80px">
  5. {{- range .Fields}}
  6. <el-form-item label="{{.FieldDesc}}:">
  7. {{- if eq .FieldType "bool" }}
  8. <el-switch v-model="formData.{{.FieldJson}}" active-color="#13ce66" inactive-color="#ff4949" active-text="是" inactive-text="否" clearable ></el-switch>
  9. {{- end }}
  10. {{- if eq .FieldType "string" }}
  11. <el-input v-model="formData.{{.FieldJson}}" clearable placeholder="请输入" />
  12. {{- end }}
  13. {{- if eq .FieldType "int" }}
  14. {{- if .DictType }}
  15. <el-select v-model="formData.{{ .FieldJson }}" placeholder="请选择" clearable>
  16. <el-option v-for="(item,key) in {{ .DictType }}Options" :key="key" :label="item.label" :value="item.value" />
  17. </el-select>
  18. {{- else }}
  19. <el-input v-model.number="formData.{{ .FieldJson }}" clearable placeholder="请输入" />
  20. {{- end }}
  21. {{- end }}
  22. {{- if eq .FieldType "time.Time" }}
  23. <el-date-picker v-model="formData.{{ .FieldJson }}" type="date" placeholder="选择日期" clearable></el-date-picker>
  24. {{- end }}
  25. {{- if eq .FieldType "float64" }}
  26. <el-input-number v-model="formData.{{ .FieldJson }}" :precision="2" clearable></el-input-number>
  27. {{- end }}
  28. </el-form-item>
  29. {{- end }}
  30. <el-form-item>
  31. <el-button size="mini" type="primary" @click="save">保存</el-button>
  32. <el-button size="mini" type="primary" @click="back">返回</el-button>
  33. </el-form-item>
  34. </el-form>
  35. </div>
  36. </div>
  37. </template>
  38. <script>
  39. export default {
  40. name: '{{.StructName}}'
  41. }
  42. </script>
  43. <script setup>
  44. import {
  45. create{{.StructName}},
  46. update{{.StructName}},
  47. find{{.StructName}}
  48. } from '@/api/{{.PackageName}}'
  49. // 自动获取字典
  50. import { getDictFunc } from '@/utils/format'
  51. import { useRoute, useRouter } from "vue-router"
  52. import { ElMessage } from 'element-plus'
  53. import { ref } from 'vue'
  54. const route = useRoute()
  55. const router = useRouter()
  56. const type = ref('')
  57. {{- range $index, $element := .DictTypes}}
  58. const {{ $element }}Options = ref([])
  59. {{- end }}
  60. const formData = ref({
  61. {{- range .Fields}}
  62. {{- if eq .FieldType "bool" }}
  63. {{.FieldJson}}: false,
  64. {{- end }}
  65. {{- if eq .FieldType "string" }}
  66. {{.FieldJson}}: '',
  67. {{- end }}
  68. {{- if eq .FieldType "int" }}
  69. {{.FieldJson}}: {{- if .DictType }} undefined{{ else }} 0{{- end }},
  70. {{- end }}
  71. {{- if eq .FieldType "time.Time" }}
  72. {{.FieldJson}}: new Date(),
  73. {{- end }}
  74. {{- if eq .FieldType "float64" }}
  75. {{.FieldJson}}: 0,
  76. {{- end }}
  77. {{- end }}
  78. })
  79. // 初始化方法
  80. const init = async () => {
  81. // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
  82. if (route.query.id) {
  83. const res = await find{{.StructName}}({ ID: route.query.id })
  84. if (res.code === 0) {
  85. formData.value = res.data.re{{.Abbreviation}}
  86. type.value = 'update'
  87. }
  88. } else {
  89. type.value = 'create'
  90. }
  91. {{- range $index, $element := .DictTypes }}
  92. {{ $element }}Options.value = await getDictFunc('{{$element}}')
  93. {{- end }}
  94. }
  95. init()
  96. // 保存按钮
  97. const save = async() => {
  98. let res
  99. switch (type.value) {
  100. case 'create':
  101. res = await create{{.StructName}}(formData.value)
  102. break
  103. case 'update':
  104. res = await update{{.StructName}}(formData.value)
  105. break
  106. default:
  107. res = await create{{.StructName}}(formData.value)
  108. break
  109. }
  110. if (res.code === 0) {
  111. ElMessage({
  112. type: 'success',
  113. message: '创建/更改成功'
  114. })
  115. }
  116. }
  117. // 返回按钮
  118. const back = () => {
  119. router.go(-1)
  120. }
  121. </script>
  122. <style>
  123. </style>