monitor.vue 8.05 KB
<template>
  <div class="monitor-tabContent">
    <mapContent @refreshCb="refreshCb" @getData="getData" ref="mapContentRefs">
      <template #mapBox>
        <div id="monitorContainer" class="map-container"></div>
      </template>
    </mapContent>
    <div
      class="toggle-bottom-monitor"
      :class="isShowAllList && locationList.length > 2 ? 'show' : 'hidden'"
    >
      <div class="toggle-btn" @click="showHideList">
        <span
          v-if="!isShowAllList || locationList.length <= 2"
          class="ident-icon"
        ></span>
        <a v-else class="icon icon-put-away"></a>
      </div>
      <div class="list-content-monitor">
        <div
          class="list-item"
          v-for="(item, index) in locationList"
          :key="index"
        >
          <div class="index-text">
            <span>{{ index + 1 }}</span>
          </div>
          <div class="right-text">
            <p class="title">{{ item.transferAddress }}</p>
            <p class="time">{{ item.time }}</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { onMounted, ref, nextTick, watch } from "vue";
import mapContent from "@/components/mapContent.vue";
import AMapLoader from "@amap/amap-jsapi-loader";
import { CONSTANT } from "../constant";
import { GET_CAR_LOCATION_INFO } from "@/api/index";
import { useRoute } from "vue-router";
import MAPGEO from "@/utils/mapgeo";
import { Dialog } from "vant";
import "vant/es/dialog/style";
export default {
  name: "MonitorPage",
  components: {
    mapContent,
  },

  setup() {
    const route = useRoute();
    const mapContentRefs = ref(null);
    const isShowAllList = ref(false);
    const locationList = ref([]);
    let currentMap = null;
    let routeQuery = route.query;
    let polyLineArr = ref([]);
    let carMarker = null;
    let firstPosLng = "";
    let firstPosLat = "";
    let beginMarker = null;
    let polyLine = null;
    const timeoutId = ref(null);
    const listScroll = ref(0);
    const changey = ref(null);
    const refreshCb = (map) => {
      map.clearMap();
      getData();
      polyLineArr.value = [];
      carMarker = null;
      firstPosLng = "";
      firstPosLat = "";
      beginMarker = null;
      polyLine = null;
      locationList.value = [];
      timeoutId.value && clearTimeout(timeoutId.value);
    };
    onMounted(() => {
      AMapLoader.load({
        key: CONSTANT.MAP_KEY,
        version: "1.4.8",
        plugins: CONSTANT.MAP_PLUGINS,
      }).then((AMap) => {
        window.AMap = AMap;
        mapContentRefs.value.initMap("monitorContainer");
      });
    });
    watch(changey, (newVal) => {
      let listNode = document.querySelector(".list-content-monitor");
      let flag = false;
      if (listScroll.value === 0) {
        flag = true;
      }
      if (flag && newVal >= 50) {
        isShowAllList.value = false;
      }
      if (
        newVal <= -50 &&
        listNode.scrollTop === 0 &&
        locationList.value.length > 2
      ) {
        // 上滑
        isShowAllList.value = true;
      }
    });
    let starty, endy;
    nextTick(() => {
      let touchNode = document.querySelector(".toggle-bottom-monitor");
      let listNode = document.querySelector(".list-content-monitor");
      const scroll = () => {
        let scroolTop = listNode.scrollTop;
        listScroll.value = scroolTop;
      };
      touchNode.addEventListener("touchstart", function (e) {
        listNode.addEventListener("scroll", scroll(e), false);
        listNode.removeEventListener("scroll", scroll(e), false);
        starty = e.touches[0].pageY;
      });
      //结束点击时获得触点
      touchNode.addEventListener("touchend", function (e) {
        endy = e.changedTouches[0].pageY;
        //判断触点差值
        changey.value = endy - starty;
      });
    });
    // 获取数据
    const getData = (map) => {
      currentMap = map ? map : currentMap;
      GET_CAR_LOCATION_INFO({ sns: routeQuery.sn }).then((res) => {
        if (res.code === 200) {
          if (res.data.length > 0) {
            init(currentMap, res.data);
          } else {
            Dialog({ message: "暂无数据" });
          }
        }
      });
    };
    // marker渲染
    const init = (currentMap, data) => {
      data.forEach((val) => {
        (val.elat =
          val[routeQuery.locationType] && val[routeQuery.locationType].elat),
          (val.elng =
            val[routeQuery.locationType] && val[routeQuery.locationType].elng);
        val.time =
          val[routeQuery.locationType] && val[routeQuery.locationType].dateTime;
      });
      MAPGEO.transferAddressByGroup(
        {
          data: data,
          timeoutIds: [],
        },
        () => {
          data.forEach((val) => {
            var lnglat = new window.AMap.LngLat(val.elng, val.elat);
            polyLineArr.value.push(lnglat);
            locationList.value.push(val);
            if (!carMarker) {
              firstPosLng = val.elng;
              firstPosLat = val.elat;
              carMarker = new window.AMap.Marker({
                position: lnglat,
                content:
                  '<img src=" ' +
                  require("@/assets/images/car.png") +
                  '" style="transform: scale(0.5);" />',
                map: currentMap,
                offset: new window.AMap.Pixel(-8.5, -13),
              });
              beginMarker = new window.AMap.Marker({
                position: lnglat,
                content:
                  '<span class="icon icon-begin" style="transform: scale(0.5);"></span>',
                map: currentMap,
                offset: new window.AMap.Pixel(-8.5, -13),
                visible: false,
              });
            } else {
              carMarker.setPosition(lnglat);
              if (firstPosLng !== val.elng || firstPosLat !== val.elat) {
                beginMarker.show();
              }
              if (!polyLine) {
                polyLine = new window.AMap.Polyline({
                  map: currentMap,
                  strokeWeight: 2,
                  strokeColor: "#0b94db",
                  strokeStyle: "solid",
                  strokeDasharray: [10, 10],
                });
              }
              polyLine.setPath(polyLineArr);
            }
          });
          currentMap.setFitView();
          timeoutId.value = setTimeout(function () {
            getData();
          }, 10 * 1000);
        }
      );
    };
    // 显示隐藏实时地址列表数据
    const showHideList = () => {
      isShowAllList.value = !isShowAllList.value;
    };
    return {
      mapContentRefs,
      getData,
      refreshCb,
      init,
      showHideList,
      isShowAllList,
      locationList,
      listScroll,
      changey,
      timeoutId,
    };
  },
};
</script>

<style lang="scss" scoped>
.toggle-bottom-monitor {
  position: fixed;
  width: 100%;
  left: 0px;
  bottom: 0px;
  z-index: 300;
  display: flex;
  border-radius: 20px 20px 0 0;
  align-items: center;
  flex-flow: column;
  background-color: #fff;
  .toggle-btn {
    position: absolute;
    top: -20px;
    .ident-icon {
      display: inline-block;
      width: 56px;
      height: 4px;
      border-radius: 2px;
      background: #a2a2a4;
    }
  }
  .list-content-monitor {
    width: calc(100% - 30px);
    padding: 5px 15px 0 15px;
    font-size: 14px;
    .list-item {
      padding: 14px 0 0 0;
      display: flex;
      .index-text {
        margin-top: 3px;
        min-width: 24px;
      }
      .right-text {
        display: flex;
        flex-flow: column;
        overflow: hidden;
        padding-bottom: 14px;
        border-bottom: 1px solid #f0f2f5;
      }
      .title {
        color: #000;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }
      .time {
        color: #666;
        font-size: 12px;
        margin-top: 5px;
      }
    }
    :last-child {
      border: none;
    }
  }
}
.toggle-bottom-monitor.hidden {
  max-height: 135px;
  transition: max-height 0.3s;
}
.toggle-bottom-monitor.show {
  max-height: 80%;
  transition: max-height 0.3s;
  .list-content-monitor {
    overflow-y: scroll;
  }
}
</style>