
<template>
  <div id="container" class="box">
    <div id="cesiumContainer"></div>
    <div class="menu">
      <el-dropdown @command="handleCommand">
        <span class="el-dropdown-link">
          下拉菜单<i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item command="a">天气开关</el-dropdown-item>
          <el-dropdown-item command="b">地图中文标记</el-dropdown-item>
          <el-dropdown-item command="c">回到初始位置</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </div>
</template>
<script>
var Cesium = require("cesium/Cesium");
var widgets = require("cesium/Widgets/widgets.css");
export default {
  name: "cesiumPage",
  data() {
    return {
      weather:null,
      publicPath: process.env.BASE_URL,
      initPosition: [106.0231304, 37.73323706, 0],
      viewer: null,
    };
  },
  mounted() {
    var viewer = new Cesium.Viewer("cesiumContainer", {
      animation: false, //是否显示动画控件
      shouldAnimate: true,
      homeButton: false, //是否显示Home按钮
      fullscreenButton: false, //是否显示全屏按钮
      baseLayerPicker: false, //是否显示图层选择控件
      geocoder: false, //是否显示地名查找控件http://localhost:8080/#/cesiumScene
      timeline: false, //是否显示时间线控件
      sceneModePicker: false, //是否显示投影方式控件
      navigationHelpButton: false, //是否显示帮助信息控件
      infoBox: false, //是否显示点击要素之后显示的信息
      requestRenderMode: true, //启用请求渲染模式
      scene3DOnly: false, //每个几何实例将只能以3D渲染以节省GPU内存
      sceneMode: 3, //初始场景模式 1 2D模式 2 2D循环模式 3 3D模式  Cesium.SceneMode
      fullscreenElement: document.body, //全屏时渲染的HTML元素 暂时没发现用处
      imageryProvider: new Cesium.ArcGisMapServerImageryProvider({
        url:
          "https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer",
        baseLayerPicker: false,
      }),
    });
    viewer.scene.camera.setView({
      // 初始化相机经纬度
      destination: new Cesium.Cartesian3.fromDegrees(
        106.0231304,
        37.73323706,
        2000
      ),
      orientation: {
        heading: Cesium.Math.toRadians(0.0),
        pitch: Cesium.Math.toRadians(-90.0), //从上往下看为-90
        roll: 0,
      },
    });
    this.viewer = viewer;
  },
  methods: {
    addRain() {
      // 雨雪天气添加
      // scene.postProcessStages.add(Cesium.PostProcessStageLibrary.createRainStage())
      var scene = this.viewer.scene;
      let weather = this.weather;
      if (weather == null) {
        weather =scene.postProcessStages.add(
           Cesium.PostProcessStageLibrary.createSnowStage()
        );
        console.log(weather)
      }else{
        console.log(11111)
        scene.postProcessStages.remove(weather);
        weather = null;
      }
      // scene.skyAtmosphere.hueShift = -0.8;
      // scene.skyAtmosphere.saturationShift = -0.7;
      // scene.skyAtmosphere.brightnessShift = -0.33;
      // scene.fog.density = 0.001;
      // scene.fog.minimumBrightness = 0.8;
    },
    geoJSON() {
      //jeojson 加载宁夏地图
      Cesium.Math.setRandomNumberSeed(0);
      var promise = this.viewer.dataSources.add(
        Cesium.GeoJsonDataSource.load(
          `${this.publicPath}static/mapgeoJsonnx.json`,
          {
            stroke: Cesium.Color.RAD,
            fill: Cesium.Color.TRANSPARENT,
            strokeWidth: 3,
            markerSymbol: "?",
          }
        )
      );
    },
    flyto() {
      this.viewer.camera.flyTo({
        destination: Cesium.Cartesian3.fromDegrees(
          106.0231304,
          37.73323706,
          2000
        ),
        //duration:5, // 设置飞行持续时间，默认会根据距离来计算
        complete: function () {
          // 到达位置后执行的回调函数
          console.log("到达目的地");
        },
        cancle: function () {
          // 如果取消飞行则会调用此函数
          console.log("飞行取消");
        },
        pitchAdjustHeight: -90, // 如果摄像机飞越高于该值，则调整俯仰俯仰的俯仰角度，并将地球保持在视口中。
        maximumHeight: 5000, // 相机最大飞行高度
      });
    },
    handleCommand(command) {
      switch (command) {
        case "a":
          this.addRain();
          break;
        case "b":
          this.geoJSON();
          break;
        default:
          this.flyto();
          break;
      }
    },
    //初始化weosocket
    initWebSocket() {
      if (typeof WebSocket === "undefined") {
        alert("您的浏览器不支持WebSocket");
        return false;
      }
      const wsuri = "ws://10.155.32.4:8010/shbracelet";

      this.websock = new WebSocket(wsuri);
      this.websock.onopen = this.websocketonopen;
      this.websock.onmessage = this.websocketonmessage;
      this.websock.onerror = this.websocketonerror;
      this.websock.onclose = this.websocketclose;
    },
    //连接成功
    websocketonopen() {
      console.log("WebSocket连接成功");
      // 添加心跳检测，每30秒发一次数据，防止连接断开（这跟服务器的设置有关，如果服务器没有设置每隔多长时间不发消息断开，可以不进行心跳设置）
      let self = this;
      this.timer = setInterval(() => {
        try {
          self.websock.send("test");
          console.log("发送消息");
        } catch (err) {
          console.log("断开了：" + err);
          self.connection();
        }
      }, 30000);
    },
    //接收后端返回的数据，可以根据需要进行处理
    websocketonmessage(e) {
      var vm = this;
      let res = JSON.parse(e.data);
      console.log(res);
    },
    //连接建立失败重连
    websocketonerror(e) {
      console.log(`连接失败的信息：`, e);
      this.initWebSocket(); // 连接失败后尝试重新连接
    },
    //关闭连接
    websocketclose(e) {
      console.log("断开连接", e);
    },
  },
};
</script>
 
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style  lang="scss" scoped>
html,
body,
#cesiumContainer {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
}
.box {
  height: 100%;
  .menu {
    position: absolute;
    left: 20px;
    top: 20px;
    background-color: rgba(0, 0, 0, 0.5);
  }
}
</style>

