dongyukun
9 天以前 5f653cd83425db29ccf514e9fd966a90c7eeab32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
          #foreach($column in $columns)
              #if ($column.createOperation || $column.updateOperation)
                  #set ($dictType = $column.dictType)
                  #set ($javaField = $column.javaField)
                  #set ($AttrName = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
                  #set ($comment = $column.columnComment)
                  #if ( $table.templateType == 2 && $column.id == $treeParentColumn.id )
                    <el-form-item label="${comment}" prop="${javaField}">
                      <TreeSelect
                        v-model="formData.${javaField}"
                        :options="${classNameVar}Tree"
                        :normalizer="normalizer"
                        placeholder="请选择${comment}"
                      />
                    </el-form-item>
                  #elseif ($column.htmlType == "input" && !$column.primaryKey)## 忽略主键,不用在表单里
                    <el-form-item label="${comment}" prop="${javaField}">
                      <el-input v-model="formData.${javaField}" placeholder="请输入${comment}" />
                    </el-form-item>
                  #elseif($column.htmlType == "imageUpload")## 图片上传
                      #set ($hasImageUploadColumn = true)
                    <el-form-item label="${comment}">
                      <ImageUpload v-model="formData.${javaField}"/>
                    </el-form-item>
                  #elseif($column.htmlType == "fileUpload")## 文件上传
                      #set ($hasFileUploadColumn = true)
                    <el-form-item label="${comment}">
                      <FileUpload v-model="formData.${javaField}"/>
                    </el-form-item>
                  #elseif($column.htmlType == "editor")## 文本编辑器
                      #set ($hasEditorColumn = true)
                    <el-form-item label="${comment}">
                      <Editor v-model="formData.${javaField}" :min-height="192"/>
                    </el-form-item>
                  #elseif($column.htmlType == "select")## 下拉框
                    <el-form-item label="${comment}" prop="${javaField}">
                      <el-select v-model="formData.${javaField}" placeholder="请选择${comment}">
                          #if ("" != $dictType)## 有数据字典
                            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.$dictType.toUpperCase())"
                                       :key="dict.value" :label="dict.label" #if ($column.javaType == "Integer" || $column.javaType == "Long"):value="parseInt(dict.value)"#else:value="dict.value"#end />
                          #else##没数据字典
                            <el-option label="请选择字典生成" value="" />
                          #end
                      </el-select>
                    </el-form-item>
                  #elseif($column.htmlType == "checkbox")## 多选框
                    <el-form-item label="${comment}" prop="${javaField}">
                      <el-checkbox-group v-model="formData.${javaField}">
                          #if ("" != $dictType)## 有数据字典
                            <el-checkbox v-for="dict in this.getDictDatas(DICT_TYPE.$dictType.toUpperCase())"
                                         :key="dict.value" #if($column.javaType == "Integer" || $column.javaType == "Long"):label="parseInt(dict.value)"#else:label="dict.value"#end>{{dict.label}}</el-checkbox>
                          #else##没数据字典
                            <el-checkbox>请选择字典生成</el-checkbox>
                          #end
                      </el-checkbox-group>
                    </el-form-item>
                  #elseif($column.htmlType == "radio")## 单选框
                    <el-form-item label="${comment}" prop="${javaField}">
                      <el-radio-group v-model="formData.${javaField}">
                          #if ("" != $dictType)## 有数据字典
                            <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.$dictType.toUpperCase())"
                                      :key="dict.value" #if($column.javaType == "Integer" || $column.javaType == "Long"):label="parseInt(dict.value)"
                                      #else:label="dict.value"#end>{{dict.label}}</el-radio>
                          #else##没数据字典
                            <el-radio label="1">请选择字典生成</el-radio>
                          #end
                      </el-radio-group>
                    </el-form-item>
                  #elseif($column.htmlType == "datetime")## 时间框
                    <el-form-item label="${comment}" prop="${javaField}">
                      <el-date-picker clearable v-model="formData.${javaField}" type="date" value-format="timestamp" placeholder="选择${comment}" />
                    </el-form-item>
                  #elseif($column.htmlType == "textarea")## 文本框
                    <el-form-item label="${comment}" prop="${javaField}">
                      <el-input v-model="formData.${javaField}" type="textarea" placeholder="请输入内容" />
                    </el-form-item>
                  #end
              #end
          #end
      </el-form>
        ## 特殊:主子表专属逻辑
        #if ( $table.templateType == 10 || $table.templateType == 12 )
          <!-- 子表的表单 -->
          <el-tabs v-model="subTabsName">
              #foreach ($subTable in $subTables)
                  #set ($index = $foreach.count - 1)
                  #set ($subClassNameVar = $subClassNameVars.get($index))
                  #set ($subSimpleClassName = $subSimpleClassNames.get($index))
                  #set ($subJoinColumn_strikeCase = $subJoinColumn_strikeCases.get($index))
                <el-tab-pane label="${subTable.classComment}" name="$subClassNameVar">
                  <${subSimpleClassName}Form ref="${subClassNameVar}FormRef" :${subJoinColumn_strikeCase}="formData.id" />
                </el-tab-pane>
              #end
          </el-tabs>
        #end
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
 
<script>
  import * as ${simpleClassName}Api from '@/api/${table.moduleName}/${table.businessName}';
  #if ($hasImageUploadColumn)
  import ImageUpload from '@/components/ImageUpload';
  #end
  #if ($hasFileUploadColumn)
  import FileUpload from '@/components/FileUpload';
  #end
  #if ($hasEditorColumn)
  import Editor from '@/components/Editor';
  #end
  ## 特殊:树表专属逻辑
  #if ( $table.templateType == 2 )
  import TreeSelect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  #end
  ## 特殊:主子表专属逻辑
  #if ( $table.templateType == 10 || $table.templateType == 12 )
      #foreach ($subSimpleClassName in $subSimpleClassNames)
      import ${subSimpleClassName}Form from './components/${subSimpleClassName}Form.vue'
      #end
  #end
  export default {
    name: "${simpleClassName}Form",
    components: {
        #if ($hasImageUploadColumn)
          ImageUpload,
        #end
        #if ($hasFileUploadColumn)
          FileUpload,
        #end
        #if ($hasEditorColumn)
          Editor,
        #end
        ## 特殊:树表专属逻辑
        #if ( $table.templateType == 2 )
          TreeSelect,
        #end
        ## 特殊:主子表专属逻辑
        #if ( $table.templateType == 10 || $table.templateType == 12 )
            #foreach ($subSimpleClassName in $subSimpleClassNames)
               ${subSimpleClassName}Form,
            #end
        #end
    },
    data() {
      return {
        // 弹出层标题
        dialogTitle: "",
        // 是否显示弹出层
        dialogVisible: false,
        // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
        formLoading: false,
        // 表单参数
        formData: {
            #foreach ($column in $columns)
                #if ($column.createOperation || $column.updateOperation)
                    #if ($column.htmlType == "checkbox")
                            $column.javaField: [],
                    #else
                            $column.javaField: undefined,
                    #end
                #end
            #end
        },
        // 表单校验
        formRules: {
            #foreach ($column in $columns)
                #if (($column.createOperation || $column.updateOperation) && !$column.nullable && !${column.primaryKey})## 创建或者更新操作 && 要求非空 && 非主键
                    #set($comment=$column.columnComment)
                        $column.javaField: [{ required: true, message: '${comment}不能为空', trigger: #if($column.htmlType == 'select')'change'#else'blur'#end }],
                #end
            #end
        },
          ## 特殊:树表专属逻辑
          #if ( $table.templateType == 2 )
             ${classNameVar}Tree: [], // 树形结构
          #end
        ## 特殊:主子表专属逻辑
        #if ( $table.templateType == 10 || $table.templateType == 12 )
        #if ( $subTables && $subTables.size() > 0 )
            /** 子表的表单 */
             subTabsName: '$subClassNameVars.get(0)'
        #end
        #end
      };
    },
    methods: {
      /** 打开弹窗 */
     async open(id) {
        this.dialogVisible = true;
        this.reset();
        // 修改时,设置数据
        if (id) {
          this.formLoading = true;
          try {
            const res = await ${simpleClassName}Api.get${simpleClassName}(id);
            this.formData = res.data;
            this.title = "修改${table.classComment}";
          } finally {
            this.formLoading = false;
          }
        }
        this.title = "新增${table.classComment}";
        ## 特殊:树表专属逻辑
        #if ( $table.templateType == 2 )
        await this.get${simpleClassName}Tree();
        #end
      },
      /** 提交按钮 */
      async submitForm() {
        // 校验主表
        await this.$refs["formRef"].validate();
          ## 特殊:主子表专属逻辑
          #if ( $table.templateType == 10 || $table.templateType == 12 )
              #if ( $subTables && $subTables.size() > 0 )
                // 校验子表
                  #foreach ($subTable in $subTables)
                      #set ($index = $foreach.count - 1)
                      #set ($subClassNameVar = $subClassNameVars.get($index))
                    try {
                      ## 代码生成后会替换为正确的 refs
                      await this.refs['${subClassNameVar}FormRef'].validate();
                    } catch (e) {
                      this.subTabsName = '${subClassNameVar}';
                      return;
                    }
                  #end
              #end
          #end
        this.formLoading = true;
        try {
          const data = this.formData;
        ## 特殊:主子表专属逻辑
        #if ( $table.templateType == 10 || $table.templateType == 12 )
        #if ( $subTables && $subTables.size() > 0 )
            // 拼接子表的数据
            #foreach ($subTable in $subTables)
                #set ($index = $foreach.count - 1)
                #set ($subClassNameVar = $subClassNameVars.get($index))
              data.${subClassNameVar}#if ( $subTable.subJoinMany)s#end = this.refs['${subClassNameVar}FormRef'].getData();
            #end
        #end
        #end
          // 修改的提交
          if (data.${primaryColumn.javaField}) {
            await ${simpleClassName}Api.update${simpleClassName}(data);
            this.#[[$modal]]#.msgSuccess("修改成功");
            this.dialogVisible = false;
            this.#[[$]]#emit('success');
            return;
          }
          // 添加的提交
          await ${simpleClassName}Api.create${simpleClassName}(data);
          this.#[[$modal]]#.msgSuccess("新增成功");
          this.dialogVisible = false;
          this.#[[$]]#emit('success');
        } finally {
          this.formLoading = false;
        }
      },
        ## 特殊:树表专属逻辑
        #if ( $table.templateType == 2 )
          /** 获得${table.classComment}树 */
         async get${simpleClassName}Tree() {
            this.${classNameVar}Tree = [];
            const res = await ${simpleClassName}Api.get${simpleClassName}List();
            const root = { id: 0, name: '顶级${table.classComment}', children: [] };
            root.children = this.handleTree(res.data, 'id', '${treeParentColumn.javaField}')
            this.${classNameVar}Tree.push(root)
          },
        #end
        ## 特殊:树表专属逻辑
        #if ( $table.templateType == 2 )
          /** 转换${table.classComment}数据结构 */
          normalizer(node) {
            if (node.children && !node.children.length) {
              delete node.children;
            }
              #if ($treeNameColumn.javaField == "name")
                return {
                  id: node.id,
                  label: node.name,
                  children: node.children
                };
              #else
                return {
                  id: node.id,
                  label: node['$treeNameColumn.javaField'],
                  children: node.children
                };
              #end
          },
        #end
      /** 表单重置 */
      reset() {
        this.formData = {
            #foreach ($column in $columns)
                #if ($column.createOperation || $column.updateOperation)
                    #if ($column.htmlType == "checkbox")
                            $column.javaField: [],
                    #else
                            $column.javaField: undefined,
                    #end
                #end
            #end
        };
        this.resetForm("formRef");
      }
    }
  };
</script>