index.vue 9.73 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"
        border
        lazy
        :load="load"
        :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="170"
          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 { getTableList, getChildren, addOrganiza, editOrganiza, deleteOrganiza } from 'services/orginizaManage'
export default {
  data() {
    return {
      ORGINIZASTATUS: this.$Constants.ORGINIZASTATUS,
      searchForm: {},
      orginizaFormList: [
        {
          type: 'input',
          inputType: 'text',
          prop: 'organizationName',
          placeholder: '请输入组织名称'
        },
        {
          type: 'select',
          prop: 'enableStatus',
          placeholder: '请选择组织状态',
          options: [{
            value: '1',
            label: '正常'
          }, {
            value: '2',
            label: '停用'
          }]
        }
      ],
      orginTableData: [],
      listHeadData: [ // 列表 头部设置 数据
        { prop: 'departmentName', label: '组织名称' },
        { prop: 'orderNum', label: '排序' },
        { prop: 'enableStatus', label: '状态' },
        { prop: 'createDate', 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: {
        enableStatus: true
      },
      orginizaData: [
        { label: '上级组织', prop: 'departmentName', type: 'input', hidden: false, readonly: true },
        { label: '组织名称', prop: 'name', type: 'input' },
        { label: '显示排序', prop: 'orderNum', type: 'input' },
        { label: '组织状态', prop: 'enableStatus', type: 'switch' }
      ],
      orginizaRules: {
        name: [{ required: true, message: '请输入组织名称', trigger: 'blur' }],
        orderNum: [{ required: true, message: '请输入显示排序', trigger: 'blur' }],
        enableStatus: [{ required: true }]
      },
      // 部门弹窗
      num: 1

    }
  },
  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 = {}
    },
    // 确定,新增组织、编辑组织
    handleConfirm() {
      console.log(this.orginizaForm)
      this.$refs.orginizaForm.$children[0].validate((valid) => {
        if (valid) {
          let params = JSON.parse(JSON.stringify(this.orginizaForm))
          params.enableStatus = this.orginizaForm.enableStatus ? 1 : 2
          if (params.departmentName) delete params.departmentName
          let api = addOrganiza
          if (params.id) api = editOrganiza
          api(params).then(res => {
            if (res.code = 200) {
              // this.$set(this.$refs.table.store.states.lazyTreeNodeMap, params.id, res.data)
              this.$message({
                type: 'success',
                message: '操作成功'
              })
              if (this.orginizaDialogTitle == '新增根组织') {
                this.getList()
              }
              this.handleClose()
            }
          })
        } else {
          console.log('error')
        }
      })
    },
    // 取消
    handleClose() {
      if (!this.orginizaVisible && this.$refs.orginizaForm) {
        this.$refs.orginizaForm.$children[0].resetFields()
      }
      this.orginizaVisible = !this.orginizaVisible
    },
    // 按钮事件
    btnClick(pType, scope) {
      console.log(pType, scope)
      switch (pType) {
        case 'root':
          this.orginizaData[0].hidden = true
          this.orginizaDialogTitle = '新增根组织'
          this.handleClose()
          break
        case 'create':
          this.orginizaData[0].hidden = false
          this.orginizaDialogTitle = '新增组织'
          this.orginizaForm.departmentName = scope.departmentName
          this.orginizaForm.superiorId = scope.id
          this.handleClose()
          break
        case 'edit':
          this.orginizaDialogTitle = '编辑组织'
          this.editEvent(scope)
          break
        case 'delete':
          this.deleteEvent(scope.id)
          break
      }
    },
    // 编辑触发事件
    editEvent(row) {
      console.log(row)
      this.orginizaForm = JSON.parse(JSON.stringify(row))
      this.orginizaForm.enableStatus = row.enableStatus == 1 ? true : false
      this.handleClose()
    },
    // 删除触发事件
    deleteEvent(id) {
      const that = this
      const tipData = {
        that: that,
        tipText: '是否删除当前记录',
        tipTitle: '提示',
        type: 'error',
        callBack: () => {
          deleteOrganiza({ id: id }).then(res => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              if (this.orginizaDialogTitle == '新增根组织') {
                this.getList()
              }
            } else {
              this.$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
        } else {
          this.orginTableData = []
        }
      })
    },
    // 子节点列表
    getChildren(id) {
      getChildren({ rootId: id }).then(res => {
        let nodes = res.data
        // nodes.map(item => {
        // const aa = this.num++
        // item.id = aa + '' + item.id
        // })
        return nodes
      })
    },
    // 页码
    handleSizeChange(value) {
      this.page.pageSize = value
      this.search()
    },
    handleCurrentChange(value) {
      this.page.pageNumber = value
      this.search()
    },
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>