DictModal.vue 3.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<template>
  <a-modal
    :title="title"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="字典名称">
18
          <a-input placeholder="请输入字典名称" v-decorator="[ 'dictName', validatorRules.dictName]"/>
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="字典编码">
          <a-input placeholder="请输入字典编码" v-decorator="[ 'dictCode', validatorRules.dictCode]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="描述">
          <a-input v-decorator="[ 'description']"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
42
  import { addDict, editDict, duplicateCheck } from '@/api/api'
43 44

  export default {
45
    name: 'DictModal',
46
    data() {
47
      return {
48
        value: 1,
49
        title: '操作',
50 51 52
        visible: false,
        model: {},
        labelCol: {
53 54
          xs: { span: 24 },
          sm: { span: 5 }
55 56
        },
        wrapperCol: {
57 58
          xs: { span: 24 },
          sm: { span: 16 }
59 60 61
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
62
        validatorRules: {
63
          dictName: { rules: [{ required: true, message: '请输入字典名称!' }] },
64
          dictCode: {
65 66 67 68
            rules: [{ required: true, message: '请输入字典编码!' },
              { validator: this.validateDictCode }]
          }
        }
69 70
      }
    },
71
    created() {
72 73
    },
    methods: {
74 75 76
      validateDictCode(rule, value, callback) {
        // 重复校验
        var params = {
77 78
          tableName: 'sys_dict',
          fieldName: 'dict_code',
79
          fieldVal: value,
80 81
          dataId: this.model.id
        }
82 83
        duplicateCheck(params).then((res) => {
          if (res.success) {
84
            callback()
85
          } else {
86
            callback(res.message)
87
          }
88
        })
89
      },
90
      handleChange(value) {
91
        this.model.status = value
92
      },
93
      add() {
94
        this.edit({})
95
      },
96 97
      edit(record) {
        if (record.id) {
98
          this.visiblekey = true
99
        } else {
100
          this.visiblekey = false
101
        }
102 103 104
        this.form.resetFields()
        this.model = Object.assign({}, record)
        this.visible = true
105
        this.$nextTick(() => {
106
          this.form.setFieldsValue(pick(this.model, 'dictName', 'dictCode', 'description'))
107
        })
108 109
      },
      // 确定
110
      handleOk() {
111
        const that = this
112 113 114
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
115 116 117 118 119 120
            that.confirmLoading = true
            values.dictName = (values.dictName || '').trim()
            values.dictCode = (values.dictCode || '').trim()
            values.description = (values.description || '').trim()
            let formData = Object.assign(this.model, values)
            let obj
121
            console.log(formData)
122
            if (!this.model.id) {
123
              obj = addDict(formData)
124
            } else {
125
              obj = editDict(formData)
126
            }
127 128
            obj.then((res) => {
              if (res.success) {
129 130
                that.$message.success(res.message)
                that.$emit('ok')
131
              } else {
132
                that.$message.warning(res.message)
133 134
              }
            }).finally(() => {
135 136
              that.confirmLoading = false
              that.close()
137 138 139 140 141
            })
          }
        })
      },
      // 关闭
142
      handleCancel() {
143
        this.close()
144
      },
145
      close() {
146 147 148
        this.$emit('close')
        this.visible = false
      }
149 150 151
    }
  }
</script>