index.vue 9.13 KB
<template>
  <div class="system-container">
    <div class="container">
      <search-form
        class="searchTop border-shadow"
        :formModel="searchForm"
        :formList="dictionaryFormList"
      >
        <span
          class="inline-b"
          slot="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>
        </span>
      </search-form>
      <h3>字典管理
        <div class="pull-right">
          <el-button
            type="primary"
            :size="btn_size"
            icon="el-icon-plus"
            @click="btnClick('create')"
          >新增</el-button>
        </div>
      </h3>
      <basic-table
        ref="dictManage"
        :tableParams="tableParams"
        :getListApi="getListApi"
        :beforeSearchFn="beforeSearchFn"
        :searchForm="searchForm"
        :hasNum="false"
      >
        <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 || 'text'"
            :size="btn_size"
            @click="btnClick(j.pType, scope.scope)"
          >{{j.name}}</el-button>
        </span>
      </basic-table>
      <basic-dialog
        :title="dictionaryDialogTitle"
        :isVisible="dictionaryVisible"
        @confirmDialog="handleConfirm"
        @closeDialog="handleClose"
      >
        <slot>
          <rule-form
            :ruleForm="dictionaryForm"
            :rules="dictionaryRules"
            :ruleData="dictionaryData"
            ref='dictionaryForm'
          ></rule-form>
        </slot>
      </basic-dialog>
    </div>
  </div>
</template>

<script>
import { getDictList, addDict, editDict, deleteDict } from 'services/dictionary';
export default {
  data() {
    return {
      DICTSTATUS: this.$Constants.DICT_STATUS,
      searchForm: {},
      searchDataForm: {},
      dictionaryFormList: [
        {
          type: 'input',
          inputType: 'text',
          prop: 'name',
          placeholder: '请输入字典名称'
        },
        {
          type: 'input',
          inputType: 'text',
          prop: 'type',
          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: {
        // mutiSelectName: 'dictionary',
        // mutiSelectUniqueKey: 'id',
        btnId: new Date().getTime() + parseInt(Math.random() * 1000),
        listName: 'list',
        tableColumns: [
          {
            label: '字典主键',
            prop: 'id'
          }, {
            label: '字典名称',
            prop: 'dictName'
          }, {
            label: '字典类型',
            prop: 'dictType'
          }, {
            label: '字典备注',
            prop: 'remark'
          }, {
            label: '字典状态',
            prop: 'enableStatus',
            addHtml: 'status'
          }, {
            label: '创建时间',
            prop: 'createdDate'
          }
        ],
        tableOption: {
          width: 150,
          btnList: [
            {
              name: '列表',
              pType: 'list'
            },
            {
              name: '编辑',
              pType: 'edit'
            },
            {
              name: '删除',
              pType: 'delete'
            }
          ]
        }
      },
      getListApi: getDictList,
      // 弹窗
      dictionaryDialogTitle: '新增字典',
      dictionaryVisible: false,
      dictionaryForm: {
        enableStatus: true
      },
      dictionaryData: [
        { label: '字典名称:', prop: 'dictName', type: 'input', maxlength: 20 },
        { label: '字典类型:', prop: 'dictType', type: 'input', maxlength: 50, readonly: false },
        { label: '状态:', prop: 'enableStatus', type: 'switch' },
        { label: '备注:', prop: 'remark', type: 'input', inputText: 'textarea', maxlength: 200, showWordLimit: true }
      ],
      dictionaryRules: {
        dictName: [{ required: true, message: '请输入字典名称', trigger: blur }],
        dictType: [{ required: true, message: '请输入字典类型', trigger: blur }],
        enableStatus: [{ required: true }]
      }
    }
  },
  methods: {
    // 重置搜索条件
    refresh() {
      this.searchForm = {}
      this.searchDataForm = {}
      this.$refs.dictManage.refreshTable()
    },
    beforeSearchFn() {
      return new Promise((resolve, reject) => {
        console.log(this.searchForm)
        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()
      })
    },
    // 确定
    handleConfirm() {
      console.log(this.dictionaryForm)
      this.$refs.dictionaryForm.$children[0].validate((valid) => {
        if (valid) {
          let params = JSON.parse(JSON.stringify(this.dictionaryForm))
          params.enableStatus = this.dictionaryForm.enableStatus ? 1 : 2
          console.log(params)
          let api = addDict
          if (params.id) api = editDict
          api(params).then(res => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.$refs.dictManage.refreshTable()
              this.handleClose()
            } else {
              this.$message({
                type: 'error',
                message: res.message
              })
            }
          })
        }
      })
    },
    // 取消
    handleClose() {
      if (!this.dictionaryVisible && this.$refs.dictionaryForm) {
        this.$refs.dictionaryForm.$children[0].resetFields()
      }
      this.dictionaryVisible = !this.dictionaryVisible
    },
    // 按钮事件
    btnClick(pType, scope) {
      console.log(pType, scope)
      switch (pType) {
        case 'create':
          this.dictionaryDialogTitle = '新增字典'
          this.dictionaryForm = {
            enableStatus: true
          }
          this.dictionaryData.map(item => {
            if (item.prop == 'dictType') {
              item.readonly = false
            }
          })
          this.handleClose()
          break
        case 'edit':
          this.dictionaryDialogTitle = '编辑字典'
          this.dictionaryData.map(item => {
            if (item.prop == 'dictType') {
              item.readonly = true
            }
          })
          this.editEvent(scope)
          break
        case 'delete':
          this.deleteEvent(scope.id)
          break
        case 'list':
          console.log('list', scope)
          const listData = { row: scope };
          localStorage.setItem('dictionaryParams', JSON.stringify(listData));
          this.$router.push({ name: 'dictionaryData', params: listData });
          break
      }
    },

    // 编辑触发时间
    editEvent(row) {
      this.dictionaryForm = JSON.parse(JSON.stringify(row))
      this.dictionaryForm.enableStatus = row.enableStatus == 1 ? true : false
      delete this.dictionaryForm.createdDate
      this.handleClose()
    },
    // 删除触发事件
    deleteEvent(id) {
      const that = this
      const tipData = {
        that: that,
        tipText: '是否删除当前记录',
        tipTitle: '提示',
        type: 'error',
        callBack: () => {
          const params = {
            typeId: id
          }
          deleteDict(params).then(res => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.$refs.dictManage.refreshTable()
            } else {
              this.$message({
                type: 'error',
                message: res.message
              })
            }
          })

        }
      }
      this.$Common.confirmDialog(tipData)
    }
  }
}
</script>