index.vue 8.95 KB
<template>
  <div>
    <el-form :inline="true" style="text-align: right;" :model="dictSearchForm">
      <el-form-item label="字典名称:">
        <el-input
          v-model="dictSearchForm.dictName"
          clearable
          :size="btn_size"
          placeholder="请输入字典名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="字典类型:">
        <el-input
          v-model="dictSearchForm.dictType"
          clearable
          :size="btn_size"
          placeholder="请输入字典类型"
        ></el-input>
      </el-form-item>
      <el-form-item label="字典状态:">
        <el-select
          v-model="dictSearchForm.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="dictTableParams.btnId">查询</el-button>
        <el-button type="primary" :size="btn_size" @click="btnClick('create')">新增字典</el-button>
      </el-form-item>
    </el-form>
    <d2-table
      ref="dictTable"
      :hasNum="false"
      :tableParams="dictTableParams"
      :searchForm="dictSearchForm"
      :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 dictTableParams.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="dictionaryDialogTitle"
      :isVisible="dictionaryVisible"
      @confirmDialog="handleConfirm"
      @closeDialog="handleClose"
    >
      <slot>
        <rule-form
          :ruleForm="dictionaryForm"
          :rules="dictionaryRules"
          :ruleData="dictionaryData"
          ref="dictionaryForm"
        ></rule-form>
      </slot>
    </basic-dialog>
  </div>
</template>

<script>
import { dictApi } from '@/api/dictManage'
export default {
  data() {
    return {
      DICTSTATUS: this.$Constants.DICT_STATUS,
      dictSearchForm: {
        enableStatus: ''
      },
      pageConf: {
        pageSize: 'pageSize',
        pageNum: 'pageNum',
        totalCount: 'data.count'
      },
      tableData: [
        {
          id: 1,
          dictName: 'dsfd'
        }
      ],
      getListApi: dictApi.findDictType,
      dictTableParams: {
        btnId: new Date().getTime() + parseInt(Math.random() * 1000),
        listName: 'data.data',
        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'
            }
          ]
        }
      },
      // 弹窗
      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: {
    // 确定
    handleConfirm() {
      console.log(this.dictionaryForm)
      this.$refs.dictionaryForm.$children[0].validate(valid => {
        if (valid) {
          const params = JSON.parse(JSON.stringify(this.dictionaryForm))
          params.enableStatus = this.dictionaryForm.enableStatus ? 1 : 2
          console.log(params)
          let api = 'saveDictType'
          if (params.id) api = 'updateDictType'
          dictApi[api](params).then(res => {
            if (res.code == 0) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.$refs.dictTable.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':
          window.location.href = `./dictionaryData.html?dictType=${scope.dictType}&enableStatus=${scope.enableStatus}`
          window.parent.postMessage({
            type: 'location-change',
            url: `/dictionaryData.html?dictType=${scope.dictType}&enableStatus=${scope.enableStatus}`
          }, '*');
          // this.$root.to(
          //   [
          //     '账户中心',
          //     '商户账户管理',
          //     '字典管理',
          //     '字典类型管理',
          //     '*字典数据'
          //   ],
          //   { dictType: scope.dictType, enableStatus: scope.enableStatus }
          // )
          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: 'warning',
        callBack: () => {
          const params = { id }
          console.log(params)
          dictApi.deleteDictType(params).then(res => {
            if (res.code == 0) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.$refs.dictTable.refreshTable()
            } else {
              this.$message({
                type: 'error',
                message: res.message
              })
            }
          })
        }
      }
      this.$Common.confirmDialog(tipData)
    },
    reset() {
      this.dictSearchForm = {
        enableStatus: ''
      }
      this.$nextTick(() => {
        this.$refs.dictTable.refreshTable()
      })
    }
  }
}
</script>

<style>
.el-table .cell {
  line-height: 16px !important;
}
</style>