form.vue 4.87 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: -30px 0px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="模板名称">
            <el-input v-model="form.name" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="标识" prop="code">
            <el-input v-model="form.code" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="顺序">
            <el-input v-model="form.sort" type="number" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态">
            <el-switch
              v-model="form.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="可用"
              inactive-text="不可用"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-col>
        <!--        <el-col :span="12">-->
        <!--          <el-form-item label="创建时间" >-->
        <!--            <el-date-picker v-model="form.createTime" type="datetime"/>-->
        <!--          </el-form-item>-->
        <!--        </el-col>-->
        <!--        <el-col :span="12">-->
        <!--          <el-form-item label="更新时间" >-->
        <!--            <el-date-picker v-model="form.updateTime" type="datetime"/>-->
        <!--          </el-form-item>-->
        <!--        </el-col>-->
      </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, getByCode } from '@/api/system/permissionTemplate'
export default {
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    dictMap: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      loading: false, dialog: false,
      form: {
        id: '',
        name: '',
        code: '',
        sort: '',
        status: ''
      },
      rules: {
        code: [
          { required: true, message: '请输入标识', trigger: 'blur' }, { validator: this.validateCode, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    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()
          })
      }
    },
    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.$parent.init()
          }).catch(err => {
            this.dialog = false
            console.log(err)
          })
        } else {
          this.$notify({
            title: '警告',
            message: '信息不合法',
            type: 'warning',
            duration: 2000
          })
        }
        this.loading = false
      })
    },
    doEdit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          edit(this.form).then(() => {
            this.$notify({
              title: '修改成功',
              type: 'success',
              duration: 2500
            })
            this.dialog = false
            this.$parent.init()
          }).catch(err => {
            this.dialog = false
            console.log(err)
          })
        } else {
          this.$notify({
            title: '警告',
            message: '信息不合法',
            type: 'warning',
            duration: 2000
          })
        }
        this.loading = false
      })
    },
    doClose() {
      this.resetForm()
    },
    resetForm() {
      this.$refs['form'].resetFields()
      this.form = {
        originalCode: '',
        id: '',
        name: '',
        code: '',
        sort: '',
        status: ''
      }
    }
  }
}
</script>

<style scoped>

</style>