index.vue 19.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
<template>
  <div class="box">
    <div class="innerBox dictContnet">
      <search-top 
        :searchForm="searchForm" 
        :searchData="searchData"
        @search="handleSearch"
        @reset="handleReset"
        >
      </search-top>
      <div>
        <permission-button btnType="all" :btnData="btnData" @btnClick="btnClick"></permission-button>
      </div>
      <el-table
        v-loading="loading"
        style="magin: 0 10px"
        :data="dicTableData"
        ref="multipleTable"
        tooltip-effect="dark"
        @selection-change="handleSelectionChange"
        @row-click="handleSelect"
        :header-cell-style="{background:'#f5f7fa'}">
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          v-for="(item, index) in column"
          :key="index"
          :prop="item.prop"
          :label="item.label"
          :width="item.width"
          border="true"
          show-overflow-tooltip
          
          >
            <template slot-scope="scope">
              <template v-if="item.prop =='dataStatus'">
                <span :class="dicTableData[scope.$index][item.prop] == '1' ? 'dot green' : 'dot gray'"></span>
                <span class="statusText">{{dicTableData[scope.$index][item.prop] == '1' ? '正常' : '停用'}}</span>
              </template>
              <template v-else>
                  {{dicTableData[scope.$index][item.prop]?dicTableData[scope.$index][item.prop]:'--'}}
                </template>
            </template>
          </el-table-column>
          <!-- 操作按钮 -->
        <el-table-column
            fixed="right"
            label="操作"
            
            show-overflow-tooltip
            width="100">
            <template slot-scope="scope">
                <el-button v-for="(i, index) in button" type='text'  :key='index' @click="i.callback(scope.$index, scope.row)">{{i.label}}
                </el-button>
            </template>
          </el-table-column>
          <!-- <el-table-column 
            width="170"
             fixed="right" prop="btn" label="操作">
            <template slot-scope="scope">
              <permission-button
                btnType="single"
                :rowIndex="String(scope.$index)"
                :btnData="listBtnData"
                @btnClick="listBtnClick"
              ></permission-button>
            </template>
          </el-table-column> -->
        </el-table>
        <!-- 分页 -->
        <el-pagination
          style="padding: 20px"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageIndex"
          :page-sizes="[100, 200, 300, 400]"
          :page-size="pageSize"
          layout="total, slot, ->, prev, pager, next, sizes, jumper"
          :total="total">
          <span class="el-pagination__total">第{{pageIndex}}/{{Math.ceil(total/pageSize)}}页</span>
        </el-pagination>
        <!-- 弹窗 -->
        <add-dialog 
          :isVisible="isVisible"
          :isDisable="isDisable" 
          :title="title" 
          @hideDialog="hideDialog"
          @comfrim-click="handleDictionary">
          <div slot="center">
            <el-form :model="dictionaryForm" :rules="rules" ref="dictionaryForm" label-width="100px" label-position="left">
              <el-form-item label="字典类型:" prop="dictType">
                <el-input v-model="dictionaryForm.dictType" :disabled="true"></el-input>
              </el-form-item>
              <el-form-item label="字典标签:" prop="dataValue">
                <el-input v-model="dictionaryForm.dataValue"></el-input>
              </el-form-item>
              <el-form-item label="字典编码:" prop="id" >
                <el-input v-model="dictionaryForm.id" :disabled="true"></el-input>
              </el-form-item>
              <el-form-item label="字典键值:" prop="dataKey">
                <el-input v-model="dictionaryForm.dataKey"></el-input>
              </el-form-item>
              <el-form-item label="字典顺序:" prop="sort">
                <el-input v-model="dictionaryForm.sort"></el-input>
              </el-form-item>
              <el-form-item label="状态:" prop="dataStatus">
                <el-switch v-model="dictionaryForm.dataStatus" :active-value="1" :inactive-value="2"></el-switch>
              </el-form-item>
              <el-form-item label="备注:" prop="remark">
                <el-input type="textarea" :rows="4" v-model="dictionaryForm.remark"></el-input>
              </el-form-item>
            </el-form>
          </div>
        </add-dialog>
    </div>
  </div>
</template>

<script>
const rulesForm = "dictionaryForm"
import AddDialog from "@/components/AddDialog"
import store from "@/store";
import SearchTop from '@/components/SearchTop'
import permissionButton from "@/components/PermissionButton"
import { getDictDataList, createDictData, updateDictData, deleteDictData, exportDictData } from '@/api/system'
export default {
  name: "DictionaryManage",
  components: {
    SearchTop,
    AddDialog,
    permissionButton
  },
  data() {
    const _this = this
    return {
      dicId: '',
      dicType: '',
      dicName: '',
      // 分页
      pageIndex: 1,
      pageSize: 10,
      total: 400,

      isVisible: false,
      isDisable: false,
      title: '新增字典',
      // 复选框
      multipleSelection: [],
      selectAll: [],
      selectSum: 0,
      // form表单绑定的值
      searchForm: {
        name: '',
        number: '',
        time: '',
        option: ''
      },
      searchFormAsyc: {
        dataValue: undefined,
        dictType: undefined,
        dataStatus: undefined,
        beginDate: undefined,
        endDate: undefined,
      },
      // select组件的option选项
      optionValue: [
        { value: '1', label: '正常' },
        { value: '2', label: '停用' },
        // { value: '3', label: '注销' },
      ],
      // 表单信息;modelValue的值需与searchForm的属性一一对应。
      searchData: [
        { modelValue: 'name', label: '字典名称:', type: 'input', disabled : true},
        { modelValue: 'number', label: '字典标签:', type: 'input' },
        { modelValue: 'time', label: '创建时间:', type: 'time' },
        { modelValue: 'option', label: '字典状态:', type: 'select', optionValue: '' }
      ],
      // 弹窗model
      dictionaryForm: {
        dictType: '',
        dataValue: '',
        id: '',
        dataKey: '',
        sort: '',
        dataStatus: 1,
        remark: ''
      },
      // 表单验证
      rules: {
        dictType: [{ required: true, msg: '请输入字典类型',  trigger: 'blur' }],
        dataValue: [{ required: true, msg: '请输入字典标签', trigger: 'blur' ,max: 50,validator: this.$Common.checkRegExpName}],
        dataKey: [{ required: true, msg: '请输入键值', trigger: 'blur',max: 50,validator: this.$Common.checkRegExpName }],
        sort: [{ required: true, msg: '请输入字典顺序', trigger: 'blur',max: 20,validator: this.$Common.checkRegExpNumber }],
        dataStatus: [{ required: true }],
        remark: [{min: 0, max: 200,message: '长度在 0 到 200 个字符', trigger: 'blur' }]
      },
      modeForm: {},
      // table数据
      dicTableData: [],
      loading:true,
      column: [
        { label: '字典编码', prop: 'id', width: 100},
        { label: '字典标签', prop: 'dataValue', sortable: true },
        { label: '字典键值', prop: 'dataKey' },
        { label: '字典排序', prop: 'sort' },
        { label: '备注', prop: 'remark' },
        { label: '状态', prop: 'dataStatus', width: 100 },
        { label: '创建时间', prop: 'createTime', width: 200}
      ],
      button: [
        {
          label: '编辑',
          type: 'text',
          callback(index) {
            _this.isVisible = !_this.isVisible
            _this.dictionaryForm =  JSON.parse(JSON.stringify(_this.dicTableData[index]))
          }
        }, {
          label: '删除',
          type: 'text',
          callback(index, row) {
            console.log(row)
            _this.deleteDictionList(row.id)
          }
        }
      ],
      listBtnData:[
        {
          type: "text",
          name: "删除",
          pType: "delete"
        },
        {
          type: "text",
          name: "编辑",
          pType: "edit"
        },
      ],
      // 按钮数据
      btnData: [
        {
          type: 'primary',
          name: '新增',
          pType: 'add'
        }, {
          type: 'primary',
          name: '修改',
          pType: 'edit'
        }, {
          type: 'primary',
          name: '删除',
          pType: 'delete'
        }, 
        // {
        //   type: 'primary',
        //   name: '导出',
        //   pType: 'export'
        // }
      ],
      
    }
  },
  mounted() {
    this.dicId = this.$route.params.id;
    this.dicType = this.$route.params.dictType;
    this.dicName = this.$route.params.name;
    this.searchForm.name = this.dicName;

    this.searchData[3].optionValue = this.optionValue;
    console.log('//////////////',this.$route)
    this.getList()
    this.modeForm = JSON.parse(JSON.stringify(this.dictionaryForm))

    //权限按钮
    // var allBtn = this.$Common.filterAsycRouter(this.$store.getters.asycRouters,'/system/dictionaryData');
    // if(allBtn&&allBtn.length>0){
    //   //清空默认的的
    //   this.listBtnData = []
    //   this.btnData = []
      
    //   allBtn.map( item => {
    //     if(item.pType == 'edit'|| item.pType == 'delete'){
    //       item.type = 'text';
    //       this.listBtnData.push(item)
    //     }
    //     if(item.pType == 'add' || item.pType == 'edit'|| item.pType == 'delete'|| item.pType == 'export'){
    //       item.type = 'primary';
    //       this.btnData.push(item)
    //     }
    //   })
    // }
  },
  methods: {
    listBtnClick(pType, index, datas, btnType) {
      // console.log(pType, "列表 按钮点击事件");
      // console.log(index, "列表 按钮点击事件");
      switch(pType){
        case 'edit':
          this.isVisible = !this.isVisible
          this.dictionaryForm =  JSON.parse(JSON.stringify(this.dicTableData[index]))
        break;
        case 'delete':
          this.deleteDictionList(this.dicTableData[index].id)
        break;
      }
    },
    //重置 数据 
    clearAllData(type){
      console.log('=-------------清楚查询数据')
      this.pageIndex = 1;
      
      var arr = ['character','time','status']
      var arrAsyc = ['dictType','dataStatus','beginDate','endDate']

      switch(type){
        case 'search':
          //增加 修改 删除 tree 得重置查询条件
          let obj = {};
          let obj2 = {};
          arrAsyc.map( item => {
            obj[item] = ''
          })
          arr.map( item => {
            obj2[item] = ''
          })
          // this.$set(this,'searchFormAsyc',obj)
          this.$set(this,'searchForm',obj2)
        break;
        case 'select':
          this.selectAll = [];
          this.selectSum = 0;
        break;
      }
    },
    // 调用接口
    // 获取字典数据列表
    getList() {
      this.dicTableData = []
      this.loading = true ;

      let params = {};
      //查询条件 
      params = Object.assign(params,this.searchFormAsyc);
      //分页条件
      params.pageSize = this.pageSize;
      params.pageIndex = this.pageIndex;
      params.dictType = this.dicType;

      getDictDataList(params).then(res => {
        console.log(res.data.items)
        this.dicTableData = res.data.items
        this.total = res.data.page.totalCount
        this.loading = false
        this.$nextTick(() => {
          if(this.selectAll[this.pageIndex]) {
            for(var tableIndex in this.dicTableData) {
              for(var selectIndex in this.selectAll[this.pageIndex]) {
                if(this.dicTableData[tableIndex].id == this.selectAll[this.pageIndex][selectIndex].id) {
                  this.$refs.multipleTable.toggleRowSelection(this.dicTableData[tableIndex])
                }
              }
            }
          }
        })
      })
    },
    // 分页
    handleSizeChange(val) {
      this.pageSize = val
      this.selectAll = []
      this.getList()
    },
    handleCurrentChange(val) {
      this.slectValChange()
      this.pageIndex = val
      this.getList()
    },
    // 查询;获取到用户输入
    handleSearch(params) {
      //清除 查询 order 条件
      this.clearAllData('select');

      console.log(params,'------------------------------搜索条件 数据 处理');
       ['dataValue','dictType','dictStatus','beginDate','endDate']
      let obj = {};
      // obj.dataValue = params.name;
      obj.dictType = this.dicType; 
      obj.dataValue = params.number; 
      obj.dataStatus = params.option;
      obj.beginDate = params.time ? params.time[0] : undefined;
      obj.endDate = params.time ? params.time[1] : undefined;
      this.$set(this,'searchFormAsyc',obj)

      this.getList()
    },
    handleReset(){
      this.searchForm.name = this.dicName;
    },
    slectValChange() {
      !this.selectAll[this.pageIndex]
      ? 
      this.selectSum = this.selectSum + this.multipleSelection.length
      : 
      this.selectSum = this.selectSum - this.selectAll[this.pageIndex].length + this.multipleSelection.length
      this.selectAll[this.pageIndex] = this.multipleSelection
    },
    // 按钮 事件
    btnClick(pType, index, btnType) {
      this.slectValChange()
      switch(pType) {
        case 'add':  // 新增
          this.createDic()
          break;
        case 'edit':  // 修改
          this.modifyDic()
          break;
        case 'delete':  // 删除
          this.deleteDictionList()
          break;
        case 'export':   // 导出
          this.handleExport()
          break;
      }
    },
    // 新增
    createDic() {
      // if (this.dicId) {
        this.isVisible = !this.isVisible
        this.dictionaryForm = JSON.parse(JSON.stringify(this.modeForm))
        this.dictionaryForm.dictType = this.dicType
      // } else {
      //   this.$message({
      //     type: 'warning',
      //     message: '参数错误!'
      //   })
      // }
    },
    // 点击取消关闭弹窗
    hideDialog() {
      this.$refs[rulesForm].clearValidate()
      this.isVisible = !this.isVisible
      this.dictionaryForm = JSON.parse(JSON.stringify(this.modeForm))
      this.$set(this,'dictionaryForm',{
        dictType: '',
        dataValue: '',
        id: '',
        dataKey: '',
        sort: '',
        dataStatus: 1,
        remark: ''
      })
    },
    // 点击确定关闭弹窗
    handleDictionary() {
      //清除 查询 order 条件
      this.clearAllData('select');
      this.$refs[rulesForm].validate((valid) => {
        if (valid) {
          this.isDisable = true;
          this.isVisible = !this.isVisible
          if (this.dictionaryForm.id) {
            let obj = {};
            obj.id = this.dictionaryForm.id;
            obj.dataValue = this.dictionaryForm.dataValue; 
            obj.dataKey = this.dictionaryForm.dataKey;
            obj.sort = this.dictionaryForm.sort;
            obj.dictType = this.dictionaryForm.dictType;
            obj.dataStatus = this.dictionaryForm.dataStatus;
            obj.remark = this.dictionaryForm.remark;
            updateDictData(obj).then(res => {
              this.isDisable = false;
              // console.log(res.data)
              if(res.code == 200){
                this.getList()
                this.$refs[rulesForm].clearValidate()
                this.$message({
                  type: 'success',
                  message: '编辑成功!'
                })
              }else{
                that.$message({
                  type: "error",
                  message: response.message
                });
              }
            })
          } else {
            let obj = {};
            obj.dictType = this.dictionaryForm.dictType;
            obj.dataValue = this.dictionaryForm.dataValue;
            obj.dataKey = this.dictionaryForm.dataKey;
            obj.sort = this.dictionaryForm.sort;
            obj.dataStatus = this.dictionaryForm.dataStatus;
            obj.remark = this.dictionaryForm.remark;
            createDictData(obj).then(res => {
              this.isDisable = false;
              // console.log(res.data)
              if(res.code == 200){
                this.getList()
                this.$refs[rulesForm].clearValidate()
                this.$message({type: 'success', message: '新增成功!'})
              }else{
                that.$message({
                  type: "error",
                  message: response.message
                });
              }
            })
          }
        } else {
          return false
        }
      })
    },
    // 选中某一行
    handleSelect(row, event, column) {
      this.$Common.rowClick(this, row, column)
    },
    // 选中复选框
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 删除
    deleteDict(ids,btnType) {
      this.$confirm("是否删除"+(btnType != 'all'?"该":"当前"+this.selectSum)+"条记录?", '提示', {
        confirmButtonText: '继续',
        showCancelButton: false,
        type: 'error'
      }).then(() =>{
        //清除 查询 order 条件
        this.clearAllData('select');

        if (ids.length > 1) {
          this.$confirm('数据量巨大,请稍等', '提示', {
            confirmButtonText: '继续',
            showCancelButton: false,
            type: 'warning'
          })
        }
        
        deleteDictData({'ids':ids}).then( res => {
          console.log(res)
          this.getList();
          if(res.code == 200){
            this.$message({
              message: '删除成功',
              type: 'success'
            })
          }else{
            that.$message({
              type: "error",
              message: response.message
            });
          }
        })
      })
    },
    deleteDictionList(params) {
      if (params) {
        this.deleteDict(params)
      } else if (this.selectSum) {
        const ids = []
        this.selectAll.forEach(element => {
          element.forEach(item => {
            ids.push(item.id)
          })
        })
        this.deleteDict(ids,'all')
        console.log(ids)
      } else {
        this.$message({
          type: 'warning',
          message: '请选择删除项!'
        })
      }
    },
    // 修改
    modifyDic() {
      switch(this.selectSum) {
        case 0: 
          this.$message({
            type: 'warning',
            message: '请选择要修改的数据'
          })
          break
        case 1: 
          for(var item in this.selectAll) {
            if (this.selectAll[item].length) {
              this.dictionaryForm = JSON.parse(JSON.stringify(this.selectAll[item][0]))
            }
          }
          this.isVisible = !this.isVisible
          break
        default:
          this.$message({
            type: 'warning',
            message: "当前已选中"+this.selectSum+"条,每次仅能修改一条数据!"
          })
      }
    },
    // 导出
    handleExport() {
      console.log('这是导出方法')
      if (this.selectSum == 0) {
        console.log('导出全部')
      } else {
        const ids = []
        this.selectAll.forEach(element => {
          element.forEach(item => {
            ids.push(item.id)
          })
        })
        console.log(ids)
        exportDictionary(ids).then(res => {
          console.log(res)
          console.log('部分导出')
        })
      }
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.dictContnet {
  .btnBox {
    margin-bottom: 20px;
  }
}

</style>