管道基础大数据平台系统开发-【前端】-新系統界面
13693261870
2023-06-13 be4c814230641d490b218c8cb5bd29c51cab7978
src/views/Tools/LayerTree.vue
@@ -76,18 +76,21 @@
           v-show="shwoHistogram">
        <i class="el-icon-tickets"></i>&nbsp;&nbsp;钻孔柱状图
      </div>
      <div class="edit"
           v-show="shwoTileDown"
           @click="tileDownload()">
        <i class="el-icon-download"></i>&nbsp;&nbsp;瓦片下载
      </div>
    </el-card>
  </div>
</template>
<script>
import ImageWMS from "ol/source/ImageWMS";
import Image from "ol/layer/Image";
import GeoJSON from "ol/format/GeoJSON.js";
import queryinfo from "./queryinfo.vue";
import { Vector as VectorSource } from "ol/source";
import { Vector as VectorLayer } from "ol/layer";
import { perms_selectLayers, comprehensive_selectPubById, project_selectDirAll, project_selectByDirid, } from "../../api/api.js";
import { perms_selectLayers, comprehensive_selectPubById, project_selectDirAll } from "../../api/api.js";
export default {
  name: "tree",
@@ -95,10 +98,12 @@
  data () {
    return {
      isBusy: false, // 正忙
      lastPrjCode: "", // 最后选择项目编码
      showProp: false, // 属性
      shwoHistogram: false, // 钻孔柱状图
      showLocal: false, // 定位
      showOpacity: false, // 不透明度
      shwoTileDown: false, // 瓦片下载
      menuVisible: false, // 菜单
      levelArray: [ // 高程数组
        0, 40000000, 20000000, 10000000, 5000000, 2000000, 1000000, 500000,
@@ -117,6 +122,7 @@
        "m_sitepoint",
      ],
      treeData: [], // 树数据
      sourceData: [], // 源数据
      defaultProps: { // 默认属性值
        children: "children",
        label: "cnName",
@@ -131,7 +137,7 @@
    },
  },
  mounted () {
    this.getMenus()
    this.getMenus();
    this.$bus.$on("treeDataCopy", (res) => {
      this.$store.state.treeData = this.treeData;
      this.$store.state.checkedKeys = this.$refs.tree.getCheckedKeys();
@@ -167,19 +173,50 @@
      });
      // 存储选中图层
      this.treeData = this.setTreeData(layers);
      this.sourceData = JSON.parse(JSON.stringify(this.treeData));
      this.$refs.tree.setCheckedKeys(checkKey);
      sessionStorage.setItem("checkedLayers", JSON.stringify(checkedLayers));
    },
    // 设置树数据
    setTreeData (source) {
      let cloneData = JSON.parse(JSON.stringify(source)); // 对源数据深度克隆
      /*let cloneData = JSON.parse(JSON.stringify(source)); // 对源数据深度克隆
      return cloneData.filter((father) => {
        // 循环所有项
        let branchArr = cloneData.filter((child) => father.id == child.pid); // 对比ID,分别上下级菜单,并返回数据
        branchArr.length > 0 ? (father.children = branchArr) : ""; // 给父级添加一个children属性,并赋值
        // 属于同一对象问题,例如:令 a=b、c=1 ,然后再令 b.c=c ,那么 a.c=b.c=c=1 ;同理,后续令 c.d=2 ,那么 a.c.d 也是=2;
        return father.pid == 0; // 返回一级菜单
      });*/
      let arr = [];
      let data = JSON.parse(JSON.stringify(source));
      this.setTreeChildData(data, arr, 0);
      return arr;
    },
    // 设置树子节点数据
    setTreeChildData (data, arr, pid) {
      let i = 0;
      while (i < data.length) {
        let d = data[i];
        if (d.pid == pid) {
          arr.push(d);
          data.splice(i, 1);
          continue;
        }
        i++;
      }
      arr.sort(function (a, b) {
        return a.orderNum - b.orderNum;
      });
      for (let i in arr) {
        let children = [];
        this.setTreeChildData(data, children, arr[i].id);
        if (children.length) arr[i].children = children;
      }
    },
    // 节点获得焦点事件
    nodeBlur (node, data) {
@@ -216,7 +253,8 @@
      this.shwoHistogram = this.showProp && object.enName == "s_explorationpoint"; // 钻孔柱状图
      this.showLocal = ["Tileset", "3DML", "Mpt", "TMS", "DOM", "DEM"].indexOf(object.serveType) > -1; // 定位
      this.showOpacity = ["Tileset", "3DML"].indexOf(object.serveType) > -1; // 不透明度
      this.menuVisible = this.showProp || this.shwoHistogram || this.showLocal || this.showOpacity; // 菜单
      this.shwoTileDown = ["TMS", "DOM"].indexOf(object.serveType) > -1 && object.pubid; // 瓦片下载
      this.menuVisible = this.showProp || this.shwoHistogram || this.showLocal || this.showOpacity || this.shwoTileDown; // 菜单
      this.$refs.card.$el.style.left = event.pageX + 20 + "px";
      this.$refs.card.$el.style.top = event.pageY + "px";
@@ -284,6 +322,33 @@
      return this.levelArray[this.levelArray.length - 1];
    },
    // 瓦片下载
    tileDownload () {
      this.rmListener();
      var that = this;
      sgworld.Creator.createSimpleGraphic(
        "rectangle", {},
        function (entity) {
          that.getTileRectangle(entity);
          sgworld.Creator.SimpleGraphic.clear();
        }
      );
    },
    // 获取瓦片四至范围
    getTileRectangle (res) {
      var value = res.rectangle.coordinates.getValue();
      var val = {
        pubid: this.currentData.pubid,
        pwd: "",
        title: "",
        xmax: Cesium.Math.toDegrees(value.east),
        xmin: Cesium.Math.toDegrees(value.west),
        ymax: Cesium.Math.toDegrees(value.north),
        ymin: Cesium.Math.toDegrees(value.south)
      }
      this.$bus.$emit("titleDown", val);
    },
    // 图层选中事件
    handleCheckChange (data, checked) {
      if (this.isBusy) return;
@@ -314,11 +379,11 @@
        }
      }
      if (obj.WMS.length) this.setChangeWMS(obj.WMS, checked);
      if (obj.Tileset.length) this.setChangeTileset(obj.Tileset, checked);
      if (obj.TMS.length) this.setChangeDEM(obj.DEM, checked);
      if (obj.TMS.length) this.setChangeTMS(obj.TMS, checked);
      if (obj.Mpt.length) this.setChangeMpt(obj.Mpt, checked);
      if (obj.DEM.length) this.setChangeDEM(obj.DEM, checked);
      if (obj.Tileset.length) this.setChangeTileset(obj.Tileset, checked);
      if (obj.WMS.length) this.setChangeWMS(obj.WMS, checked);
    },
    // 获取子节点
    getNodes (data, arr) {
@@ -455,17 +520,18 @@
    addTMSLayers (layers) {
      for (let i in layers) {
        let res = layers[i];
        let url = res.url.indexOf("{host}") > -1 ? res.url.replace("{host}", iisHost) : res.url;
        res.url = res.url.indexOf("{host}") > -1 ? res.url.replace("{host}", iisHost) : res.url;
        if (res.pubid) {
          this.setQueryPubid(res);
          this.setTMSLayerByPubid(res);
          continue;
        }
        let layer = Viewer.imageryLayers.addImageryProvider(new Cesium.UrlTemplateImageryProvider({
          url: url,
          url: res.url,
          maximumLevel: 22,
        }));
          enablePickFeatures: false
        }), 1);
        layer.id = res.id;
        layer.name = res.cnName;
      }
@@ -506,36 +572,37 @@
    },
    // 切换DEM
    setChangeDEM (layers, checked) {
      let ids = this.getCheckNodesIds(), arr = [], isDel = false, useSG = false;
      let ids = this.getCheckNodesIds(), arr = [];
      for (let i in layers) {
        let layer = layers[i];
        if (ids.indexOf(layer.id) != -1) {
          arr.push(layer.url);
          continue;
        }
        if (window.Viewer.terrainProvider._layers[0].resource._url == layer.url) isDel = true; // 删除DEM
      }
      if (!arr.length && isDel) useSG = true; // demLayer
      if (arr.length) this.addDEMLayers(arr[0], useSG);
      let url = arr.length ? arr[arr.length - 1] : null;
      this.addDEMLayers(url, null == url);
    },
    // 添加DEM图层
    addDEMLayer (url, useSG) {
    addDEMLayers (url, useSG) {
      if (useSG) {
        if (Viewer.terrainProvider._isMPT) return;
        let option = {
          url: window.sceneConfig.SGUrl,
          layerName: window.sceneConfig.mptName,
          requestVertexNormals: true
        };
        window.terrainLayer = sgworld.Creator.sfsterrainprovider("", option, "", true, "");
      } else {
        window.terrainLayer = new Cesium.CesiumTerrainProvider({
          url: url,
          requestVertexNormals: true
        });
        sgworld.Creator.sfsterrainprovider("", option, "", true, "");
        return;
      }
      Viewer.terrainProvider = terrainLayer;
      if (url.indexOf("{host}") > -1) url = url.replace("{host}", iisHost);
      Viewer.terrainProvider = new Cesium.CesiumTerrainProvider({
        url: url,
        requestVertexNormals: true
      });
    },
    // 设置Tileset参数
    async setTilesetArgs (tileset, res) {
@@ -545,7 +612,7 @@
      }
      const data = await comprehensive_selectPubById({ id: res.pubid })
      if (data.code == 200) this.setTilesetCoord(tileset, data.result.json);
      if (data.code == 200 && data.result.json) this.setTilesetCoord(tileset, data.result.json);
    },
    // 设置Tileset高度
    setTilesetHeigth (tileset, height) {
@@ -572,208 +639,173 @@
      tileset._root.transform = matrix;
      // Viewer.flyTo(tileset);
    },
    async setQueryPubid (res) {
      const data = await comprehensive_selectPubById({ id: res.pubid })
      if (data.code != 200) {
        //
      } else {
        let url = data.result.url;
        if (res.url.indexOf("{host}") != -1) {
          url = res.url.replace("{host}", iisHost);
        }
        let min = data.result.min;
        let max = data.result.max;
        let layer;
        if (max > 0) {
          layer = Viewer.imageryLayers.addImageryProvider(
            new Cesium.UrlTemplateImageryProvider({
              url: url,
              minimumLevel: min,
              maximumLevel: max,
            })
          );
        } else {
          layer = Viewer.imageryLayers.addImageryProvider(
            new Cesium.UrlTemplateImageryProvider({
              url: url,
              minimumLevel: min,
            })
          );
        }
    // 根据Pubid设置TMS图层
    async setTMSLayerByPubid (res) {
      const data = await comprehensive_selectPubById({ id: res.pubid });
      if (!data || data.code != 200) return;
        layer.name = res.cnName;
        if (data.result.geom) {
          // let wkt = this.$wkt.parse(data.result.geom);
          // Viewer.camera.flyTo({
          //   destination: Cesium.Cartesian3.fromDegrees(wkt.coordinates[1], wkt.coordinates[0], 2000),
          // });
        }
        this.setChangeWMS();
      }
      let provider = data.result.max > 0 ?
        new Cesium.UrlTemplateImageryProvider({
          url: res.url,
          minimumLevel: data.result.min,
          maximumLevel: data.result.max,
          enablePickFeatures: false
        }) :
        new Cesium.UrlTemplateImageryProvider({
          url: res.url,
          minimumLevel: data.result.min,
          enablePickFeatures: false
        });
      let layer = Viewer.imageryLayers.addImageryProvider(provider, 1);
      layer.name = res.cnName;
      // if (data.result.geom) {
      //   let wkt = this.$wkt.parse(data.result.geom);
      //   Viewer.camera.flyTo({
      //     destination: Cesium.Cartesian3.fromDegrees(wkt.coordinates[1], wkt.coordinates[0], 2000)
      //   });
      // }
    },
    // 切换项目
    async prjChanged (res) {
    prjChanged (code) {
      this.$store.state.pigCode = code;
      //if (code) {
      this.isBusy = true;
      let checkedLayers = this.getCheckedLayersByCode(code);
      let checkedKeys = [];
      for (let i in checkedLayers) {
        checkedKeys.push(checkedLayers[i].id);
      }
      this.treeData = this.getNewTreeData(code);
      this.$store.state.treeData = this.treeData;
      this.$refs.tree.setCheckedKeys(checkedKeys);
      sessionStorage.setItem("checkedLayers", JSON.stringify(checkedLayers));
      this.isBusy = false;
      //}
      for (let i = 0, c = this.treeData.length; i < c; i++) {
        this.handleCheckChange(this.treeData[i], true);
      }
    },
    // 根据项目编码获取选中图层
    getCheckedLayersByCode (code) {
      let layers = [];
      for (let i = 0, c = this.treeData.length; i < c; i++) {
        this.getCheckedChildLayersByCode(this.treeData[i], code, layers, false);
      }
    async setproChange (res) {
      if (res) {
        this.setCheeckedLayer = this.$refs.tree.getCheckedKeys();
        this.$store.state.pigCode = res;
        let st_code = this.menus.filter(rs => {
          if (rs.code == res) {
            return rs
      return layers;
    },
    // 根据项目编码获取选中子图层
    getCheckedChildLayersByCode (data, code, layers, isPrj) {
      if (data.children && data.children.length) {
        for (let i = 0, c = data.children.length; i < c; i++) {
          let layer = data.children[i];
          if (layer.children && layer.children.length) {
            let flag = layer.type == 1 && layer.isProject && layer.enName == code;
            this.getCheckedChildLayersByCode(layer, code, layers, flag);
            continue;
          }
        })
        let val = this.$store.state.oldTree.filter(rs => {
          if (rs.enName == st_code[0].code) {
            return rs
          }
        })
        this.removeAllLayer();
        let obj = {
          dirid: res,
          pageIndex: 1,
          pageSize: 10,
        }
        const data = await project_selectByDirid(obj);
        if (data.code != 200) {
          this.$message.error("列表获取失败");
          return;
        }
        if (data.result[0]) {
          let geom = data.result[0].geom
          if (geom) {
            let wkt = this.$wkt.parse(geom)
            Viewer.camera.flyTo({
              destination: Cesium.Cartesian3.fromDegrees(wkt.coordinates[0].toFixed(6), wkt.coordinates[1].toFixed(6), 5000),
            });
          }
        }
        //this.cannelTerrainLayer();
        //this.setTerrainMptLayer();
        //清空图层树选中状态
        this.$refs.tree.setCheckedKeys([]);
        if (val.length > 0) {
          if (val[0].type == 1 && val[0].isProject == 1) {//项目分类
            let std_check = [];
            for (let i in val) {
              let std_val = this.getNewTree([this.$refs.tree.getNode(val[i].id).data], [])
              for (let j in std_val) {
                std_check.push(std_val[j])
              }
            }
            let value = this.$store.state.isProjectLayer;
            for (let i in value) {
              std_check.push(value[i])
            }
            this.setProjectLayer(std_check);
          //if (layer.type == 2 && (layer.isProject || isPrj)) {
          //  layers.push(layer);
          //}
          if (code) {
            if (layer.type == 2 && (layer.isProject || isPrj)) layers.push(layer);
          } else {
            this.setChangeProLayer();
          }
        } else {
          this.setChangeProLayer();
        }
      } else {
        this.removeAllLayer();
        this.$store.state.pigCode = null;
        this.$refs.tree.setCheckedKeys(this.setCheeckedLayer);
        let std = [];
        for (let i in this.setCheeckedLayer) {
          std.push(this.$refs.tree.getNode(this.setCheeckedLayer[i]).data)
        }
        this.setProjectLayer(std);
      }
    },
    removeAllLayer () {
      for (let j in Viewer.scene.primitives._primitives) {
        Viewer.scene.primitives.remove(Viewer.scene.primitives._primitives[j])
      }
      for (let i in window.Viewer.dataSources._dataSources) {
        window.Viewer.dataSources.remove(
          window.Viewer.dataSources._dataSources[i]
        );
      }
      for (let i = 0; i < window.Viewer.imageryLayers._layers.length; i++) {
        let val_id = window.Viewer.imageryLayers._layers[i].id;
        if (val_id == 'TMS') {
          window.Viewer.imageryLayers.remove(
            window.Viewer.imageryLayers._layers[i]
          );
        }
      }
      for (let i in this.mptLayer) {
        this.mptLayer[i].deleteObject();
        this.mptLayer.splice(i, 1)
      }
      this.mptLayer = [];
    },
    setProjectLayer (res) {
      let layerid = [];
      for (let j in res) {
        layerid.push(res[j].id)
      }
      this.$refs.tree.setCheckedKeys(layerid);
      // let value = this.$refs.tree.getCheckedNodes();
      // this.$bus.$emit("showMenuLayer", value);
      let listWMS = [];
      let listWFS = [];
      let listTileset = [];
      let listTMS = [];
      let listMpt = [];
      for (let i in res) {
        if (res[i].type == 2) {
          switch (res[i].serveType) {
            case 'WMS':
              listWMS.push(res[i])
              break;
            case 'WFS':
              listWFS.push(res[i])
              break;
            case '3DML':
            case 'Tileset':
              listTileset.push(res[i])
              break;
            case 'Mpt':
              listMpt.push(res[i])
              break;
            case 'TMS':
              listTMS.push(res[i])
              break;
            if (layer.isShow) layers.push(layer);
          }
        }
        return;
      }
      // this.setChangeProLayer();
      this.setChangeWMS(listWMS, true);
      this.setChangeWFS(listWFS, true);
      this.setChangeTileset(listTileset, true);
      this.setChangeTMS(listTMS, true);
      this.setChangeMpt(listMpt, true);
    },
    getNewTree (obj, result) {
      for (const i in obj) {
        console.log(obj[i].cnName)
        result.push(obj[i])
        if (obj[i].children && obj[i].children.length > 0) {
          this.getNewTree(obj[i].children, result)
        }
      }
      return result
    },
    setChangeProLayer () {
      let value = this.$store.state.isProjectLayer;
      if (value.length != 0) {
        let std = [];
        for (let i in value) {
          std.push(value[i].id)
      if (data.type == 2 && (data.isProject || isPrj)) {
        layers.push(data);
      }
    },
    // 获取新树数据
    getNewTreeData (code) {
      let data = JSON.parse(JSON.stringify(this.sourceData));
      if (!code) return data;
      this.setSubTreeData(data, code);
      this.rmPrjTreeData(data, code);
      return data;
    },
    // 设置树数据子节点
    setSubTreeData (data, code) {
      let j = 0;
      while (j < data.length) {
        let d = data[j];
        if (d.type == 1 && d.isProject && d.enName != code) {
          data.splice(j, 1);
          continue;
        }
        this.$refs.tree.setCheckedKeys(std);
        this.$bus.$emit("showMenuLayer", value);
        j++;
      }
      for (let i = 0, c = data.length; i < c; i++) {
        if (data[i].children && data[i].children.length) {
          this.setSubTreeData(data[i].children, code);
        }
      }
    },
    // 移除项目树数据
    rmPrjTreeData (data, code) {
      /*let j = 0;
      while (j < data.length) {
        let d = data[j];
        if (d.type == 1 && d.isProject && d.enName == code) {
          data.splice(j, 1);
          data.concat(d.children);
          continue;
        }
        j++;
      }
      for (let i = 0, c = data.length; i < c; i++) {
        if (data[i].children && data[i].children.length) {
          this.rmPrjTreeData(data[i].children, code);
        }
      }*/
      let i = 0;
      while (i < data.length) {
        let d = data[i];
        if (!d.children) {
          i++;
          continue;
        }
        if (!d.children.length) {
          data.splice(i, 1);
          continue;
        }
        let j = 0;
        while (j < d.children.length) {
          let e = d.children[j];
          if (e.type == 1 && e.isProject && e.enName == code) {
            d.children.splice(j, 1);
            if (e.children && e.children.length) d.children = d.children.concat(e.children);
            continue;
          }
          j++;
        }
        i++;
      }
      for (let i = 0, c = data.length; i < c; i++) {
        if (data[i].children && data[i].children.length) {
          this.rmPrjTreeData(data[i].children, code);
        }
      }
    },
    // 添加WFS图层 *