index.vue 8.9 KB
<template>
  <div>
    <el-form :inline="true" style="text-align: right;" :model="dictDataSearchForm">
      <el-form-item>
        <el-input
          v-model="dictDataSearchForm.dictName"
          clearable
          :size="btn_size"
          placeholder="请输入字典名称"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="dictDataSearchForm.dictType"
          :size="btn_size"
          readonly
          placeholder="请输入字典类型"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="dictDataSearchForm.enableStatus"
          clearable
          :size="btn_size"
          placeholder="请选择字典状态"
        >
          <el-option
            v-for="item in $Constants.DICT_STATUS_OPTIONS"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button :size="btn_size" type="primary" @click="reset">重置</el-button>
        <el-button type="primary" :size="btn_size" :id="dictDataTableParams.btnId">搜索</el-button>
        <el-button type="primary" :size="btn_size" @click="btnClick('create')">新增字典数据</el-button>
      </el-form-item>
    </el-form>
    <d2-table
      ref="dictDataTable"
      :tableParams="dictDataTableParams"
      :searchForm="dictDataSearchForm"
      :getListApi="getListApi"
      :pageConf="pageConf"
    >
      <span slot="status" slot-scope="scope">
        <span
          class="dot"
          :class="scope.scope.enableStatus ? DICTSTATUS[scope.scope.enableStatus].split(',')[1] : ''"
        ></span>
        <span
          class="statusText"
        >{{ scope.scope.enableStatus ? DICTSTATUS[scope.scope.enableStatus].split(',')[0] : '--'}}</span>
      </span>
      <span slot="auth-button" slot-scope="scope">
        <el-button
          v-for="(j, idx) in dictDataTableParams.tableOption.btnList"
          :key="idx"
          :type="j.type || 'text'"
          :size="btn_size"
          @click="btnClick(j.pType, scope.scope)"
        >{{ j.name }}</el-button>
      </span>
    </d2-table>
    <basic-dialog
      :title="dictDataDialogTitle"
      :isVisible="dictDataVisible"
      @confirmDialog="handleConfirm"
      @closeDialog="handleClose"
    >
      <slot>
        <rule-form
          :ruleForm="dictDataForm"
          :rules="dictDataRules"
          :ruleData="dictDataData"
          ref="dictDataForm"
        ></rule-form>
      </slot>
    </basic-dialog>
  </div>
</template>

<script>
import { dictApi } from '@/api/dictManage'
export default {
  data() {
    return {
      DICTSTATUS: this.$Constants.DICT_STATUS,
      dictDataSearchForm: {
        dictType: '',
        enableStatus: ''
      },
      pageConf: {
        pageSize: 'pageSize',
        pageNum: 'pageNum',
        totalCount: 'data.count'
      },
      dictDataTableData: [
        {
          id: 1,
          dictName: 'dasf'
        }
      ],
      getListApi: dictApi.findDictData,
      dictDataTableParams: {
        numName: '字典编码',
        btnId: new Date().getTime() + parseInt(Math.random() * 1000),
        listName: 'data.data',
        tableColumns: [
          {
            label: '字典标签',
            prop: 'dictLabel'
          },
          {
            label: '字典键值',
            prop: 'dictValue'
          },
          {
            label: '字典排序',
            prop: 'dictSort'
          },
          {
            label: '备注',
            prop: 'remark'
          },
          {
            label: '字典状态',
            prop: 'enableStatus',
            addHtml: 'status'
          },
          {
            label: '创建时间',
            prop: 'createdDate'
          }
        ],
        tableOption: {
          width: 140,
          btnList: [
            {
              name: '编辑',
              pType: 'edit'
            },
            {
              name: '删除',
              pType: 'delete'
            }
          ]
        }
      },
      // 弹窗
      dictDataDialogTitle: '新增字典',
      dictDataVisible: false,
      dictDataForm: {},
      dictDataData: [
        {
          label: '字典类型:',
          prop: 'dictType',
          type: 'input',
          readonly: true
        },
        {
          label: '字典标签:',
          prop: 'dictLabel',
          type: 'input',
          maxlength: 20
        },
        {
          label: '字典键值:',
          prop: 'dictValue',
          type: 'input',
          maxlength: 50
        },
        { label: '字典排序:', prop: 'dictSort', type: 'input', maxlength: 5 },
        { label: '状态:', prop: 'enableStatus', type: 'switch' },
        {
          label: '备注:',
          prop: 'remark',
          type: 'input',
          inputText: 'textarea',
          maxlength: 200,
          showWordLimit: true
        }
      ],
      dictDataRules: {
        dictType: [{ required: true }],
        dictLabel: [
          { required: true, message: '请输入字典标签', trigger: 'blur' }
        ],
        dictValue: [
          { required: true, message: '请输入字典键值', trigger: 'blur' },
          { validator: this.$Common.checkRegExpNumber, trigger: 'blur' }
        ],
        dictSort: [
          { required: true, message: '请输入字典排序', trigger: 'blur' },
          { validator: this.$Common.checkRegExpNumber, trigger: 'blur' }
        ],
        enableStatus: [{ required: true }]
      },
      dictData: {},
      params: ''
    }
  },
  mounted() {
    this.params = this.$Common.getParams()
    console.log(this.params, 'params');
    this.dictDataSearchForm.dictType = this.params.dictType
    this.dictData = this.params
    this.$refs.dictDataTable.refreshTable()
  },
  methods: {
    // 确定
    handleConfirm() {
      this.$refs.dictDataForm.$children[0].validate(valid => {
        if (valid) {
          const params = JSON.parse(JSON.stringify(this.dictDataForm))
          params.enableStatus = this.dictDataForm.enableStatus ? 1 : 2
          let api = 'saveDictData'
          if (params.id) {
            api = 'updateDictData'
            delete params.delFlag
            delete params.disabled
            delete params.deleted
            delete params.typeDeleted
            delete params.typeDisabled
          }
          dictApi[api](params).then(res => {
            if (res.code == 0) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.handleClose()
              this.$refs.dictDataTable.refreshTable()
            } else {
              this.$message({
                type: 'error',
                message: res.message
              })
            }
          })
        }
      })
    },
    // 取消
    handleClose() {
      if (!this.dictDataVisible && this.$refs.dictDataForm) {
        this.$refs.dictDataForm.$children[0].resetFields()
      }
      this.dictDataVisible = !this.dictDataVisible
    },
    // 按钮事件
    btnClick(pType, scope) {
      switch (pType) {
        case 'create':
          this.dictDataDialogTitle = '新增字典'
          // const params = this.getInitStorage()
          this.dictDataForm = {
            dictType: this.dictData.dictType,
            enableStatus: this.dictData.enableStatus == 1
          }
          this.handleClose()
          break
        case 'edit':
          this.dictDataDialogTitle = '编辑字典'
          this.editEvent(scope)
          break
        case 'delete':
          this.deleteEvent(scope.id)
          break
      }
    },

    // 编辑触发事件
    editEvent(row) {
      this.dictDataForm = JSON.parse(JSON.stringify(row))
      this.dictDataForm.enableStatus = row.enableStatus == 1 ? true : false
      delete this.dictDataForm.createdDate
      delete this.dictDataForm.isDefault
      this.handleClose()
    },
    // 删除触发事件
    deleteEvent(id) {
      const that = this
      const tipData = {
        that: that,
        tipText: '是否删除当前记录',
        tipTitle: '提示',
        type: 'warning',
        callBack: () => {
          const params = { id }
          console.log(params)
          dictApi.deleteDictData(params).then(res => {
            if (res.code == 0) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.$refs.dictDataTable.refreshTable()
            } else {
              this.$message({
                type: 'error',
                message: res.message
              })
            }
          })
        }
      }
      this.$Common.confirmDialog(tipData)
    },
    reset() {
      this.dictDataSearchForm = {
        enableStatus: '',
        dictType: this.params.dictType
      }
      this.$nextTick(() => {
        this.$refs.dictDataTable.refreshTable()
      })
    }
  }
}
</script>