form.vue.tpl 3.6 KB

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