org.vue 8.61 KB
<template>
  <div class="template-box content_div_main org-box">
    <div class="videoleft-box">
      <el-tree
        v-loading="loading"
        :data="treedata"
        :props="defaultProps"
        default-expand-all
        highlight-current
        @node-click="handleNodeClick"
      ></el-tree>
    </div>
    <div class="org-right-box">
      <el-button class="search-btn ml10 mt10" @click="addorg()" icon="el-icon-plus">添加</el-button>
      <el-table
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        ref="multipleTable"
        :data="tableData"
        :height="680"
        tooltip-effect="dark"
        style="width: 100%;margin-top:20px"
      >
        <div slot="empty">
          <div class="no-data-box">
            <img src="../../assets/img/nodata.png" alt="暂无数据" />
            <div>暂无数据</div>
          </div>
        </div>
        <el-table-column type="type" width="55" label="#"></el-table-column>
        <el-table-column label="名称" prop="org_name"></el-table-column>
        <el-table-column label="类型" :formatter="formatHpzl" prop="org_type"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="info" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="dialog-org">
      <el-dialog title="添加组织机构" :visible.sync="dialogVisible" width="30%" @close="dialogclose()">
        <el-row>
          <el-form ref="orgform" :model="orgform" label-width="170px" class="demo-ruleForm">
            <el-form-item label="父节点名称" prop="name">
              <el-input class v-model="curOrgData.org_name" disabled style="width:100%"></el-input>
            </el-form-item>
            <el-form-item
              label="节点类型"
              prop="oreType"
              :rules="[
                          { required: true, message: '请选择节点类型!', trigger: 'blur' } ]"
            >
              <el-select v-model="orgform.oreType" placeholder="请选择添加类型" class="selbox">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              label="节点名称"
              prop="orgname"
              :rules="[
                          { required: true, message: '请输入节点名称!', trigger: 'blur' } ]"
            >
              <el-input class v-model="orgform.orgname" style="width:100%"></el-input>
            </el-form-item>
            <el-col :span="15" :offset="5"></el-col>
          </el-form>
        </el-row>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="saveORg">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading:false,
      tableData: [],
      parentname: "",
      options: [
        {
          value: "org",
          label: "组织"
        },
        {
          value: "address",
          label: "地点"
        },
        {
          value: "root",
          label: "根节点"
        }
      ],
      treedata: [],
      defaultProps: {
        children: "childs",
        label: "org_name"
      },
      dialogVisible: false,
      orgform: {
        oreType:'',
        orgname:''
      },
      curOrgData: {}
    };
  },
  methods: {
    handleNodeClick(data) {
      this.tableData = data.childs;
      this.curOrgData = data;
    },
    formatHpzl(data) {
      if (data.org_type == "org") return "组织";
      if (data.org_type == "address") return "地点";
    },
    buildTree(data) {
      if (!data) {
        return;
      }
      var store = [];
      store.push(data);
      let substore;
      while (store.length > 0) {
        substore = store.pop();
        if (substore.childs && substore.childs.length > 0) {
          var i = substore.childs.length - 1;
          for (i = substore.childs.length - 1; i >= 0; i--) {
            if (substore.childs[i].org_type == "vchan") {
              substore.childs.splice(i,1)
            }
            store.push(substore.childs[i]);
          }
        }
      }
    },
    addorg(){
      this.dialogVisible = true
    },
    handleDelete(index, row) {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            this.axios.delete(this.API.org.deleteorg(row.unid)).then((response)=> {
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
                this.deleteorg(row)
            });

        }).catch(() => {
            this.$message({
                type: 'info',
                message: '已取消删除'
            });
        });
    },
    saveORg() {
      // store.append({ id: 11, label: 'testtest', children: [] }, data);
      var data = {
        org_name: this.orgform.orgname,
        org_pid: this.curOrgData.unid,
        is_leaf: true,
        org_type: this.orgform.oreType
      };
      if (data.org_type == "root") {
        data.org_pid = 0;
        data.org_type = "org";
        this.eiditroot = true;
      }

      if (this.curOrgData.org_type != "address") {
        if (data.org_type == "address" && this.curOrgData.pid == 0) {
          this.$message({
            type: "warning",
            message: "根节点下不能添加地址!"
          });
          return;
        }
        this.$refs["orgform"].validate(valid => {
          if (valid) {
            this.axios.post(this.API.org.getorg, data).then(response => {
              if (this.orgform.oreType == "root") {
                //添加根节点
                this.getorgtree();
              }
              if (this.orgform.oreType == "address") {
                this.faceapi.getAddress();
              }
              if (this.orgform.oreType == "org") {
                this.faceapi.getOrg();
              }

              this.dialogVisible = false;
              var obj = {
                unid: response.data.unid,
                org_name: this.orgform.orgname,
                org_type: response.data.org_type,
                pid: response.data.org_pid,
                childs: []
              };
              this.searchCurDate(this.treedata, obj);
              this.$message({
                type: "success",
                message: "添加成功"
              });
            });
          }
        });
      } else {
        if (this.curOrgData.org_type == "address") {
          this.$message({
            type: "warning",
            message: "地点下不能添加机构"
          });
        }
      }
    },
    deleteorg(row) {
      this.deleteorgData(this.treedata, row);
      this.getorgtree();
    },
    deleteorgData(alldata, deleteData) {
      for (var i = 0; i < alldata.length; i++) {
        if (alldata[i] == deleteData) {
          alldata.splice(i, 1);
        } else {
          if (alldata[i].childs)
            this.deleteorgData(alldata[i].childs, deleteData);
        }
      }
      console.log(alldata)
    },
    getorgtree() {
      this.loading = true;
      this.axios.get(this.API.org.getorgtree).then(res => {
        this.treedata = res.data;
        this.loading = false;
        if (res.data.length > 0) {
          res.data.forEach(e => {
            this.buildTree(e);
          });
        }
      });
    },
  searchCurDate(alldata, childrenData) {
      for (var i = 0; i < alldata.length; i++) {
        if (alldata[i] == this.curOrgData) {
          if (alldata[i].childs) {
            alldata[i].childs.push(childrenData);
          }
        } else {
          if (alldata[i].childs)
            this.searchCurDate(alldata[i].childs, childrenData);
        }
      }
      return alldata;
    },
  },
  created() {
    this.getorgtree();
  }
};
</script>
<style lang="stylus">
.org-box {
  display: flex;
}

.videoleft-box {
  margin: 5px;
  width: 230px;
  height: calc(100vh - 130px);
  box-shadow: 0px 0px 2px 1px rgba(0, 0, 0, 0.1);
  background: #ffffff;
  border-radius: 5px;
  overflow: hidden;
}

.org-right-box {
  width: calc(100vw - 220px);
  margin: 5px;
  box-shadow: 0px 0px 2px 1px rgba(0, 0, 0, 0.1);
}

.selbox {
  width: 100%;
}
</style>