index.vue 11.3 KB
<template>
  <div class="system-container">
    <div class="orginiza-container">
      <search-form
        class="searchTop border-shadow"
        :formModel="searchForm"
        :formList="orginizaFormList"
      >
        <slot name="form-slot">
          <el-button
            type="primary"
            :size="btn_size"
            icon="el-icon-search"
            @click="search"
          >搜索</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('root')"
        >新增根组织</el-button>
      </div>
      <el-table
        ref="table"
        :data="orginTableData"
        style="width: 100%"
        row-key="id"
        lazy
        stripe
        :load="load"
        :row-style="tableRowClassName"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        <el-table-column
          v-for="(item,index) in listHeadData"
          :key="index"
          :prop="item.prop"
          :sortable="item.sortable"
          :ref="item.ref"
          :width="item.width"
          :label="item.label"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <template v-if="item.prop === 'enableStatus'">
              <span
                class="dot"
                :class="scope.row.enableStatus ? ORGINIZASTATUS[scope.row.enableStatus].split(',')[1] : ''"
              ></span>
              <span class="statusText">{{ scope.row.enableStatus ? ORGINIZASTATUS[scope.row.enableStatus].split(',')[0] : '--'}}</span>
            </template>
            <template v-else>
              {{scope.row[item.prop]?scope.row[item.prop]:'--'}}
            </template>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          width="250"
          fixed="right"
          prop="btn"
          label="操作"
        >
          <template 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.row)"
            >{{j.name}}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNum"
        :page-sizes="[10,20,30,40]"
        :page-size="page.pageSize"
        :total="total"
        layout="sizes,total,->,prev,pager,next,jumper"
      >
      </el-pagination>
      <basic-dialog
        :title="orginizaDialogTitle"
        :isVisible="orginizaVisible"
        @confirmDialog="handleConfirm"
        @closeDialog="handleClose"
      >
        <slot>
          <rule-form
            :ruleForm="orginizaForm"
            :rules="orginizaRules"
            :ruleData="orginizaData"
            ref='orginizaForm'
          ></rule-form>
        </slot>
      </basic-dialog>
    </div>
  </div>
</template>
<script>
import _ from 'lodash'
import { getTableList, getChildren, addOrganiza, editOrganiza, deleteOrganiza, organizaInfo } from 'services/orginizaManage'
export default {
  data() {
    return {
      ORGINIZASTATUS: this.$Constants.ORGINIZA_STATUS,
      searchForm: {},
      orginizaFormList: [
        {
          type: 'input',
          inputType: 'text',
          prop: 'orgName',
          placeholder: '请输入组织名称'
        },
        {
          type: 'select',
          prop: 'enableStatus',
          placeholder: '请选择组织状态',
          options: [{
            value: '1',
            label: '正常'
          }, {
            value: '2',
            label: '停用'
          }]
        }
      ],
      orginTableData: [],
      listHeadData: [ // 列表 头部设置 数据
        { prop: 'departmentName', label: '组织名称', width: 300 },
        { prop: 'orderNum', label: '排序' },
        { prop: 'enableStatus', label: '状态' },
        { prop: 'createdDate', label: '创建时间' }
      ],
      page: {
        pageSize: 10,
        pageNumber: 1
      },
      total: 10,
      tableParams: {
        tableOption: {
          btnList: [
            {
              type: 'primary',
              name: '新增',
              pType: 'create'
            },
            {
              type: 'primary',
              name: '编辑',
              pType: 'edit'
            },
            {
              type: 'danger',
              name: '删除',
              pType: 'delete'
            }
          ]
        }
      },
      // 弹窗数据
      orginizaDialogTitle: '新增组织',
      orginizaVisible: false,
      orginizaForm: {
        superiorName: '',
        enableStatus: true
      },
      form: {
        enableStatus: true
      },
      orginizaData: [
        { label: '上级组织:', prop: 'superiorName', type: 'input', hidden: false, readonly: true },
        { label: '组织名称:', prop: 'departmentName', type: 'input', maxlength: 20 },
        { label: '显示排序:', prop: 'orderNum', type: 'input', maxlength: 5 },
        { label: '组织状态:', prop: 'enableStatus', type: 'switch' }
      ],
      orginizaRules: {
        departmentName: [{ required: true, message: '请输入组织名称', trigger: 'blur' }],
        orderNum: [{ required: true, message: '请输入显示排序', trigger: 'blur' },
        { validator: this.$Common.checkRegExpNumber, trigger: 'blur' }],
        enableStatus: [{ required: true }]
      },
      // 部门弹窗
      isRoot: false
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    }
  },
  mounted() {
    this.search()
  },
  methods: {
    search() {
      let params = Object.assign({}, this.searchForm, this.page)
      this.getList(params)
    },
    // 重置搜索条件
    refresh() {
      this.searchForm = {}
    },
    emptyData(params) {
      let arr = params
      for (let key in arr) {
        if (params[key] == null || params[key] == '' || params[key] == []) {
          delete params[key]
        }
      }
      return arr
    },
    // 确定,新增组织、编辑组织
    handleConfirm() {
      this.$refs.orginizaForm.$children[0].validate((valid) => {
        if (valid) {
          let params = {
            departmentName: this.orginizaForm.departmentName,
            enableStatus: this.orginizaForm.enableStatus ? 1 : 2,
            orderNum: this.orginizaForm.orderNum
          }
          if (this.orginizaForm.superiorId) params.superiorId = this.orginizaForm.superiorId
          let api = addOrganiza
          if (this.orginizaForm.id) {
            api = editOrganiza
            params.id = this.orginizaForm.id
          }
          api(params).then(res => {
            if (res.code = 200) {
              this.$message({
                type: 'success',
                message: '操作成功'
              })
              if (this.isRoot) {
                this.search()
              } else {
                getChildren({ rootId: params.superiorId }).then(res => {
                  const nodes = res.data
                  this.$set(this.$refs.table.store.states.lazyTreeNodeMap, params.superiorId, nodes)
                })
              }
              this.handleClose()
            } else {
              this.$message({
                type: 'error',
                message: res.message
              })
            }
          })
        }
      })
    },
    // 取消
    handleClose() {
      if (!this.orginizaVisible && this.$refs.orginizaForm) {
        this.$refs.orginizaForm.$children[0].resetFields()
      }
      this.orginizaVisible = !this.orginizaVisible
    },
    // 按钮事件
    btnClick(pType, scope) {
      switch (pType) {
        case 'root':
          this.isRoot = true
          this.orginizaData[0].hidden = true
          this.orginizaForm = {
            enableStatus: true
          }
          this.orginizaDialogTitle = '新增根组织'
          this.handleClose()
          break
        case 'create':
          this.isRoot = false
          this.orginizaData[0].hidden = false
          const obj = {
            superiorId: scope.id,
            superiorName: scope.departmentName,
            enableStatus: true
          }
          this.orginizaForm = obj
          this.orginizaDialogTitle = '新增组织'
          this.handleClose()
          break
        case 'edit':
          this.orginizaDialogTitle = '编辑组织'
          this.editEvent(scope)
          break
        case 'delete':
          organizaInfo({ id: scope.id }).then(res => {
            this.deleteEvent(res.data)
          })
          break
      }
    },
    // 编辑触发事件
    editEvent(row) {
      organizaInfo({ id: row.id }).then(res => {
        this.orginizaForm = _.cloneDeep(res.data)
        this.orginizaForm.enableStatus = row.enableStatus == 1 ? true : false
        if (!this.orginizaForm.superiorId) {
          this.orginizaData[0].hidden = true
          this.isRoot = true
        } else {
          this.isRoot = false
        }
        this.handleClose()
      })
    },
    // 删除触发事件
    deleteEvent(row) {
      const that = this
      const tipData = {
        that: that,
        tipText: '是否删除当前记录',
        tipTitle: '提示',
        type: 'error',
        callBack: () => {
          deleteOrganiza({ id: row.id }).then(res => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              if (!row.superiorId) {
                this.search()
              } else {
                getChildren({ rootId: row.superiorId }).then(res => {
                  const nodes = res.data
                  this.$set(this.$refs.table.store.states.lazyTreeNodeMap, row.superiorId, nodes)
                })
              }
            } else {
              this.$message({
                type: 'error',
                message: res.message
              })
            }

          })
        }
      }
      this.$Common.confirmDialog(tipData)
    },
    // 树形table懒加载
    load(tree, treeNode, resolve) {
      console.log(tree, treeNode, resolve)
      getChildren({ rootId: tree.id }).then(res => {
        let nodes = res.data
        resolve(nodes)
      })
    },
    // 获取根节点列表
    getList(params) {
      getTableList(params).then((res) => {
        if (res.code == 200) {
          this.total = res.data.totalElements
          this.orginTableData = res.data.list
        } else {
          this.orginTableData = []
        }
      })
    },
    // 子节点列表
    getChildren(id) {
      getChildren({ rootId: id }).then(res => {
        return res.data
      })
    },
    // 页码
    handleSizeChange(value) {
      this.page.pageSize = value
      this.search()
    },
    handleCurrentChange(value) {
      this.page.pageNumber = value
      this.search()
    },
    tableRowClassName() {
      return 'padding-left: 16px;'
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>