eForm.ftl 5.6 KB
<template>
  <el-dialog :append-to-body="true" :close-on-click-modal="false" :visible.sync="dialog" :title="isAdd ? '新增' : '编辑'" width="960px" @closed="doClose">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px" style="padding: 30px;margin: -20px 0">
      <el-row>
<#if columns??>
  <#list columns as column>
  <#if column.changeColumnName != '${pkChangeColName}'>
    <#if column.columnName != 'images' && column.columnName != 'create_time' && column.columnName != 'update_time'>
        <el-col :span="12">
          <el-form-item label="<#if column.columnComment != ''>${column.columnComment}<#else>${column.changeColumnName}</#if>" <#if column.isNullable =
       'NO'>prop="${column.changeColumnName}"</#if>>
          <#if column.columnName = 'code'>
            <el-input v-model="form.${column.changeColumnName}" prop="code"/>
          <#elseif column.columnType = 'Timestamp'>
            <el-date-picker v-model="form.${column.changeColumnName}" type="datetime"/>
          <#elseif column.columnName = 'image'>
            <images-upload ref="upload" :limit="5" :image.sync="form.image" :images.sync="form.images" upload-entity="${changeClassName}" />
          <#else>
            <el-input v-model="form.${column.changeColumnName}"/>
          </#if>
          </el-form-item>
        </el-col>
    </#if>
  </#if>
  </#list>
</#if>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, edit } from '@/api/${changeClassName}'
<#if columns??>
<#list columns as column>
<#if column.columnName == 'code'>
import { getByCode } from '@/api/${changeClassName}'
</#if>
</#list>
</#if>
import ImagesUpload from '@/components/ImagesUpload/index'
export default {
  components: { ImagesUpload },
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    dictMap: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      loading: false, dialog: false,
      form: {
<#if columns??>
    <#list columns as column>
        ${column.changeColumnName}: ''<#if column_has_next>,</#if>
    </#list>
</#if>
      },
      rules: {
        <#if columns??>
        <#list columns as column>
        <#if column.columnName == 'code'>
        code: [
          { required: true, message: '请输入标识', trigger: 'blur' }, { validator: this.validateCode, trigger: 'blur' }
        ],
        </#if>
        </#list>
        </#if>
<#function filter columns>
<#local result = []>
<#list columns as column>
<#if column.columnKey != 'PRI' && column.isNullable = 'NO'>
<#local result = result + [column]>
</#if>
</#list>
<#return result>
</#function>
<#assign filteredData = filter(columns)>
<#list filteredData as column>
        ${column.changeColumnName}: [
          { required: true, message: '${column.columnComment}不能为空', trigger: 'blur' }
        ]<#sep>,</#sep>
</#list>
      }
    }
  },
  methods: {
    <#if columns??>
    <#list columns as column>
    <#if column.columnName == 'code'>
    validateCode(rule, value, callback) {
      // 当为编辑状态且code未改变时不进行校验
      if (!this.isAdd && this.form.originalCode === value) {
        callback()
      } else {
        getByCode(value)
          .then(res => {
            typeof (res) === 'undefined' || res.id === null || this.form.id === res.id
              ? callback()
              : callback(new Error('该code已存在!'))
          })
          .catch((err) => {
            console.log(err)
            callback()
          })
      }
    },
    </#if>
    </#list>
    </#if>
    cancel() {
      this.dialog = false
    },
    doSubmit() {
      if (this.isAdd) {
        this.doAdd()
      } else this.doEdit()
    },
    doAdd() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          add(this.form).then(() => {
            this.$notify({
              title: '添加成功',
              type: 'success',
              duration: 2500
            })
            this.dialog = false
            this.loading = false
            this.$parent.init()
          }).catch(err => {
            this.loading = false
            console.log(err)
          })
        } else {
          this.$notify({
            title: '警告',
            message: '信息不合法',
            type: 'warning',
            duration: 2000
          })
        }
      })
    },
    doEdit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          edit(this.form).then(() => {
            this.$notify({
              title: '修改成功',
              type: 'success',
              duration: 2500
            })
            this.loading = false
            this.dialog = false
            this.$parent.init()
          }).catch(err => {
            console.log(err)
            this.loading = false
          })
        } else {
          this.$notify({
            title: '警告',
            message: '信息不合法',
            type: 'warning',
            duration: 2000
          })
        }
      })
    },
    doClose() {
      this.resetForm()
    },
    resetForm() {
      this.$refs['form'].resetFields()
      this.form = {
<#if columns??>
    <#list columns as column>
        <#if column.columnName == 'code'>
        originalCode: '',
        </#if>
        ${column.changeColumnName}: ''<#if column_has_next>,</#if>
    </#list>
</#if>
      }
    }
  }
}
</script>

<style scoped>

</style>