index.vue 10.3 KB
<template>
  <div class="system-container">
    <div class="container">
      <search-form
        class="searchTop border-shadow"
        :formModel="searchForm"
        :formList="dictDataFormList"
      >
        <slot name="form-slot">
          <el-button
            type="primary"
            :size="btn_size"
            icon="el-icon-search"
            :id="tableParams.btnId"
          >搜索</el-button>
          <el-button
            type="primary"
            :size="btn_size"
            icon="el-icon-refresh"
            @click="refresh"
          >重置</el-button>
        </slot>
      </search-form>
      <div class="table-btn">
        <el-button
          type="primary"
          :size="btn_size"
          icon="el-icon-plus"
          @click="btnClick('create')"
        >新增</el-button>
      </div>
      <basic-table
        :tableParams="tableParams"
        :getListApi="getListApi"
        :beforeSearchFn="beforeSearchFn"
        :searchForm="searchForm"
        :hasNum="true"
        ref="dictDataTable"
      >
        <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="table-button"
          slot-scope="scope"
        >
          <el-button
            v-for="(j, idx) in tableParams.tableOption.btnList"
            :key="idx"
            :type="j.type"
            size="mini"
            plain
            @click="btnClick(j.pType, scope.scope)"
          >{{j.name}}</el-button>
        </span>
      </basic-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>
  </div>
</template>

<script>
import { getDictDataList, addDictData, editDictData, deleteDictData, exportDictData } from 'services/dictionary';

export default {
  data() {
    return {
      DICTSTATUS: this.$Constants.DICT_STATUS,
      storageParams: {},
      searchForm: {
        name: ''
      },
      dictDataFormList: [
        {
          type: 'input',
          inputType: 'text',
          prop: 'name',
          readonly: true,
          placeholder: '请输入字典名称'
        },
        {
          type: 'input',
          inputType: 'text',
          prop: 'tag',
          placeholder: '请输入字典标签'
        },
        {
          type: 'select',
          prop: 'status',
          placeholder: '请选择字典状态',
          options: [
            {
              label: '启用',
              value: 1
            }, {
              label: '停用',
              value: 2
            }
          ]
        },
        {
          type: 'daterange',
          prop: 'daterange',
          pickerOptions: {
            disabledDate(time) {
              return time.getTime() > Date.now() || time.getTime() < new Date('2017-12-31').getTime()
            }
          }
        }
      ],
      tableParams: {
        numName: '字典编码',
        // mutiSelectName: 'dictDataData',
        // mutiSelectUniqueKey: 'id',
        btnId: new Date().getTime() + parseInt(Math.random() * 1000),
        listName: 'list',
        tableColumns: [
          {
            //   label: '字典编码',
            //   prop: 'id'
            // }, {
            label: '字典标签',
            prop: 'dictLabel'
          }, {
            label: '字典键值',
            prop: 'dictValue'
          }, {
            label: '字典排序',
            prop: 'dictSort'
          }, {
            label: '备注',
            prop: 'remark'
          }, {
            label: '字典状态',
            prop: 'enableStatus',
            addHtml: 'status'
          }, {
            label: '创建时间',
            prop: 'createdDate'
          }
        ],
        tableOption: {
          width: 200,
          btnList: [
            {
              type: 'primary',
              name: '编辑',
              pType: 'edit'
            },
            {
              type: 'danger',
              name: '删除',
              pType: 'delete'
            }
          ]
        }
      },
      getListApi: getDictDataList,
      // 弹窗
      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' }],
        dictSort: [{ required: true, message: '请输入字典排序', trigger: 'blur' },
        { validator: this.$Common.checkRegExpNumber, trigger: 'blur' }],
        enableStatus: [{ required: true }]
      }
    }
  },
  mounted() {
    let params = this.$route.params
    if ((JSON.stringify(params) === '{}' || !params)) {
      params = this.getInitStorage()
      this.storageParams = params
    }
    if (params) {
      this.searchForm.name = params.row.dictName
      // this.dictDataForm.dictType = params.row.dictType
      // this.dictDataForm.enableStatus = params.row.enableStatus == 1
    }
  },
  methods: {
    getInitStorage() {
      const getStorageParams = localStorage.getItem('dictionaryParams')
      if (getStorageParams) {
        return JSON.parse(getStorageParams)
      } else {
        return ''
      }
    },
    // 重置搜索条件
    refresh() {
      let name = '';
      if (this.$route.params.row) {
        name = this.$route.params.row.dictName;
      } else if (this.storageParams.row) {
        name = this.storageParams.row.dictName;
      }
      this.searchForm = {
        name
      }
    },
    beforeSearchFn() {
      return new Promise((resolve, reject) => {
        let getDictType = '';
        setTimeout(() => {
          if (this.$route.params.row) {
            getDictType = this.$route.params.row.dictType;
          } else if (this.storageParams.row) {
            getDictType = this.storageParams.row.dictType;
          }

          this.searchForm.type = getDictType;

          if (this.searchForm.daterange) {
            this.searchForm.sDate = this.searchForm.daterange[0]
            this.searchForm.eDate = this.searchForm.daterange[1]
            delete this.searchForm.daterange
          } else {
            if (this.searchForm.sDate) delete this.searchForm.sDate && delete this.searchForm.eDate
          }
          resolve()
        }, 100)
      })
    },
    // 确定
    handleConfirm() {
      this.$refs.dictDataForm.$children[0].validate((valid) => {
        if (valid) {
          let params = JSON.parse(JSON.stringify(this.dictDataForm))
          params.enableStatus = this.dictDataForm.enableStatus ? 1 : 2
          let api = addDictData
          if (params.id) {
            api = editDictData
            delete params.delFlag
            delete params.disabled
            delete params.deleted
            delete params.typeDeleted
            delete params.typeDisabled
          }
          api(params).then(res => {
            if (res.code == 200) {
              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: params.row.dictType,
            enableStatus: params.row.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: 'error',
        callBack: () => {
          const params = {
            dataId: id
          }
          deleteDictData(params).then(res => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.$refs.dictDataTable.refreshTable()
            } else {
              this.$message({
                type: 'error',
                message: res.message
              })
            }
          })
        }
      }
      this.$Common.confirmDialog(tipData)
    }
  }
}
</script>

<style>
.el-table__indent {
  padding-left: 16px;
}
</style>