index.vue 21.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
<template>
  <div>
    <mapContent
      @refreshCb="refreshCb"
      @isUploaded="isUploaded"
      ref="mapContentRefs"
    >
      <template #mapBox>
        <div id="container" class="map-container index-map"></div>
      </template>
    </mapContent>
    <!-- 搜索区域 -->
    <div class="search-box" v-if="mapIsUploaded">
      <div class="search-header">
        <div
          class="select-box"
          :class="carItem ? 'text' : ''"
          @click="isShowList = !isShowList"
        >
          <span>{{
            carItem
              ? (carItem.ownerName || "--") + " | " + (carItem.vin || "--")
              : "请选择"
          }}</span>
          <span class="arrow-icon"><van-icon name="arrow-down" /></span>
        </div>
      </div>
      <div class="search-content" :class="isShowList ? '' : 'hidden'">
        <van-search
          class="search-input"
          v-model="keyWord"
          placeholder="输入两位以上关键字搜索"
          @search="getCarList(keyWord, true)"
        />
        <van-list
          class="list-box"
          v-model:loading="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="getCarList"
          :immediate-check="false"
        >
          <van-cell v-for="item in list" @click="getCarInfo(item)" :key="item">
            <div
              class="list-item"
              :class="(carItem && carItem.id) == item.id ? 'active' : ''"
            >
              <!-- <i v-if="item.offLineDuration" class="icon icon-off-line"></i>
              <i v-else class="icon icon-on-line"></i> -->
              <div class="item-right">
                <span class="item-text">{{
                  (item.ownerName || "--") + " | " + (item.vin || "--")
                }}</span>
                <span
                  v-if="(carItem && carItem.vin) == item.vin"
                  class="slectedicon"
                  ><i class="icon icon-checked"></i
                ></span>
              </div>
            </div>
          </van-cell>
        </van-list>
        <div
          class="no-data-class"
          v-if="list.length <= 0 && !searched && !ISLOADING"
        >
          <img src="../assets/images/no-data.png" alt="" />
          <span>暂无数据</span>
        </div>
        <div class="no-data-class" v-if="list.length <= 0 && searched">
          <img src="../assets/images/search-noData.png" alt="" />
          <span>搜索无结果</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, defineComponent, onMounted, watch, computed } from "vue";
import { useStore } from "vuex";
import mapContent from "@/components/mapContent.vue";
import AMapLoader from "@amap/amap-jsapi-loader";
import { CONSTANT } from "../constant";
import MAPGEO from "@/utils/mapgeo";
import wx from "wx";
import { useRoute } from "vue-router";
import { Dialog, Toast } from "vant";
import "vant/es/toast/style";
import { GET_ALL_CAR_LIST, GET_CAR_LOCATION_INFO } from "@/api/index";
export default defineComponent({
  name: "IndexPage",
  components: {
    mapContent,
  },
  setup() {
    const mapContentRefs = ref(null);
    const isShowList = ref(false);
    const keyWord = ref("");
    const loading = ref(false);
    const finished = ref(false);
    const list = ref([]);
    const carItem = ref(null);
    const loginUserId = ref("");
    const isShareDead = ref(1);
    const carInfoList = ref([]);
    const route = useRoute();
    const routeQuery = ref(null);
    const searched = ref(false);
    const pageNumber = ref(0);
    const mapIsUploaded = ref(false);
    const store = useStore();
    const ISLOADING = computed(() => store.state.LOADING);
    let map = undefined;
    const refreshCb = () => {
      if (map && carItem.value) {
        map && map.clearMap();
        if (Object.keys(carItem.value).length > 0) {
          getCarInfo(carItem.value);
        }
      }
    };
    watch(mapIsUploaded, (newVal) => {
      // 被分享者打开直接获取车辆定位数据
      routeQuery.value = route.query;
      if (newVal && routeQuery.value.sn) {
        if (route.query.token) {
          localStorage.setItem("token", route.query.token);
        }
        carItem.value = {
          sns: routeQuery.value.sn,
          ownerName:
            routeQuery.value.ownerName == "null"
              ? "--"
              : routeQuery.value.ownerName,
          vin: routeQuery.value.vin == "null" ? "--" : routeQuery.value.vin,
        };
        getCarInfo(carItem.value);
      }
    });
    onMounted(() => {
      getCarList();
      if (route.query.token) {
        localStorage.setItem("token", route.query.token);
      }
      AMapLoader.load({
        key: CONSTANT.MAP_KEY,
        version: "1.4.8",
        plugins: CONSTANT.MAP_PLUGINS,
      }).then((AMap) => {
        window.AMap = AMap;
        mapContentRefs.value.initMap("container");
      });
    });
    const isUploaded = (data) => {
      mapIsUploaded.value = data;
    };
    const getCarInfo = (it) => {
      carItem.value = it;
      isShowList.value = false;
      wx.miniProgram.postMessage({
        data: {
          carInfo: it,
        },
      });
      localStorage.setItem("carInfo", JSON.stringify(it));
      map = map ? map : mapContentRefs.value.init("container");
      map.clearMap();
      GET_CAR_LOCATION_INFO({ sns: it.sns }).then((res) => {
        if (res.code === 200) {
          if (res.data.length > 0) {
            const markers = [];
            let locates = [];
            res.data.forEach((item) => {
              item.locationType = "gpsLocation";
              if (
                item.gpsLocation &&
                item.gpsLocation.elng &&
                item.gpsLocation.elat
              ) {
                locates.push(item.gpsLocation);
                item.gpsLocation.transferAddress = "";
              }
              if (
                item.bsLocation &&
                item.bsLocation.elng &&
                item.bsLocation.elat
              ) {
                locates.push(item.bsLocation);
                item.bsLocation.transferAddress = "";
              }
              if (
                item.wifiLocation &&
                item.wifiLocation.elng &&
                item.wifiLocation.elat
              ) {
                locates.push(item.wifiLocation);
                item.wifiLocation.transferAddress = "";
              }
              markers.push(drawMarker(item, map, locates));
            });
            if (locates.length <= 0) return;
            MAPGEO.transferAddressByGroup(
              { data: locates, timeoutIds: [] },
              () => {
                map.setFitView(markers);
              }
            );
          } else {
            Dialog({ message: "暂无数据" });
          }
        }
      });
    };
    const drawMarker = (item, map, locates) => {
      item.locationType = "gpsLocation";
      if (!item[item.locationType]) return;
      //修改数据格式方便获取
      locates.forEach((val) => {
        if (
          val.elat === item[item.locationType].elat &&
          val.elng === item[item.locationType].elng
        ) {
          item[item.locationType].transferAddress = val.transferAddress;
        }
      });
      var lnglat = new window.AMap.LngLat(
        item[item.locationType].elng,
        item[item.locationType].elat
      );
      var marker = new window.AMap.Marker({
        position: lnglat,
        content:
          item.source == "0"
            ? '<span class="icon icon-off-car"></span>'
            : '<span class="icon icon-on-car"></span>',
        map: map,
        offset: new window.AMap.Pixel(-13, -18),
      });
      var carInfoWindowTpl = `<div class="car-info-window">`;
      carInfoWindowTpl += `<div class="main-info">`;
      carInfoWindowTpl += `<h4 class="info-title"><div>${
        item.ownerName || "--"
      }</div><div>${item.vin || "--"}</div></h4>`;
      carInfoWindowTpl += `<div class="info-list">`;
      carInfoWindowTpl += `<p class="device-info"><i class="icon icon-device"></i><span class="label">${
        item.source == 1 ? "有线设备" : "无线设备"
      }</span><span>${item.sn}</span></p>`;
      if (item[item.locationType].offLineDuration) {
        carInfoWindowTpl += `<p class="offline-info"><i class="icon icon-offline-time"></i><span class="label">离线时长</span><span>${
          item[item.locationType].offLineDuration
        }</span></p>`;
      }
      carInfoWindowTpl += `<p class="time-info"><i class="icon icon-online-time"></i><span class="label">在线时间</span><span>${
        item[item.locationType].dateTime
      }</span></p>
					<p class="locate-info"><i class="icon icon-locate-sm"></i><span class="label">详细地址</span><span data-tranferAddress></span></p>`;
      if (item.hasWifi) {
        carInfoWindowTpl += `<p p class="list-info" > <i class="icon icon-typeList"></i><span class="label">定位类型</span>
						<a data-GPS><span class="radio active"></span>GPS</a>
						<a data-GPRS><span class="radio"></span></i>基站</a>
            <a data-WIFI><span class="radio"></span></i>WIFI</a>
					</p>`;
      } else {
        carInfoWindowTpl += `<p p class="list-info" > <i class="icon icon-typeList"></i><span class="label">定位类型</span>
						<a data-GPS><span class="radio active"></span><span>GPS</span></a>
						<a data-GPRS><span class="radio"></span><span>基站</span></a>
					</p>`;
      }
      carInfoWindowTpl += `</div>
			</div>
			<div class="cmd-list">
        <div class="cmd-list-top">
          <a data-car-location>车辆追踪</a>
          <a data-replay>轨迹回放</a>
          <a data-resident>常驻地</a>
          <a data-monitor>实时监控</a>
        </div>
        <div class="cmd-list-bottom">
          <a data-freq>设备调频</a>
          <a data-fence>电子围栏</a>
          <a data-share>分享找车</a>
        </div>
			</div>
		</div>`;
      let infoDiv = document.createElement("div");
      infoDiv.innerHTML = carInfoWindowTpl;
      let infoWindow = new window.AMap.InfoWindow({
        isCustom: true,
        autoMove: false,
        position: lnglat,
        content: infoDiv,
        offset: new window.AMap.Pixel(0, -20),
      });
      const move = (newLnglat) => {
        marker.setPosition(newLnglat);
        infoWindow.setPosition(newLnglat);
        map.setCenter(newLnglat);
      };
      //信息窗体内容更新
      let GPSBtn = infoDiv.querySelector("[data-GPS]");
      let GPRSBtn = infoDiv.querySelector("[data-GPRS]");
      let WIFIBtn = infoDiv.querySelector("[data-WIFI]");
      let carLocationBtn = infoDiv.querySelector("[data-car-location]");
      let carReplayBtn = infoDiv.querySelector("[data-replay]");
      let carResidentBtn = infoDiv.querySelector("[data-resident]");
      let carMonitorBtn = infoDiv.querySelector("[data-monitor]");
      let carFreqBtn = infoDiv.querySelector("[data-freq]");
      let carFenceBtn = infoDiv.querySelector("[data-fence]");
      let shareBtn = infoDiv.querySelector("[data-share]");
      let GPSIcon = GPSBtn.querySelector(".radio");
      let GPRSIcon = GPRSBtn.querySelector(".radio");
      let WIFIIcon = WIFIBtn && WIFIBtn.querySelector(".radio");
      // 地址赋值
      const setAddress = () => {
        let Address = infoDiv.querySelector("[data-tranferAddress]");
        Address.textContent = item[item.locationType].transferAddress;
      };
      if (item.source == 1) {
        // 有线设备
        carFreqBtn && carFreqBtn.classList.add("disabled");
      } else {
        carFenceBtn && carFenceBtn.classList.add("disabled");
      }

      // 点击GPS
      GPSBtn &&
        (GPSBtn.onclick = () => {
          check(() => {
            let gpsObj = item["gpsLocation"];
            if (gpsObj) {
              var newLnglat = new window.AMap.LngLat(gpsObj.elng, gpsObj.elat);
              GPSIcon && GPSIcon.classList.add("active");
              GPRSIcon && GPRSIcon.classList.remove("active");
              WIFIIcon && WIFIIcon.classList.remove("active");
              item.locationType = "gpsLocation";
              setAddress();
              move(newLnglat);
            } else {
              Toast("暂无GPS定位信息");
            }
          });
        });
      // 点击基站
      GPRSBtn &&
        (GPRSBtn.onclick = () => {
          check(() => {
            let gprsObj = item["bsLocation"];
            if (gprsObj) {
              GPRSIcon && GPRSIcon.classList.add("active");
              GPSIcon && GPSIcon.classList.remove("active");
              WIFIIcon && WIFIIcon.classList.remove("active");
              item.locationType = "bsLocation";
              var newLnglat = new window.AMap.LngLat(
                gprsObj.elng,
                gprsObj.elat
              );
              setAddress();
              move(newLnglat);
            } else {
              Toast("暂无基站定位信息");
            }
          });
        });
      // 点击WIFI
      WIFIBtn &&
        (WIFIBtn.onclick = () => {
          check(() => {
            let wifiObj = item["wifiLocation"];
            if (wifiObj) {
              var newLnglat = new window.AMap.LngLat(
                wifiObj.elng,
                wifiObj.elat
              );
              WIFIIcon && WIFIIcon.classList.add("active");
              GPSIcon && GPSIcon.classList.remove("active");
              GPRSIcon && GPRSIcon.classList.remove("active");
              item.locationType = "wifiLocation";
              setAddress();
              move(newLnglat);
            } else {
              Toast("暂无wifi定位信息");
            }
          });
        });

      // 车辆追踪
      carLocationBtn &&
        (carLocationBtn.onclick = () => {
          let lng = item[item.locationType].elng;
          let lat = item[item.locationType].elat;
          let address = item[item.locationType].transferAddress;
          check(() => {
            wx.miniProgram.navigateTo({
              url: `../../mapPackage/pages/tracking?lat=${lat}&lng=${lng}&address=${address}`,
            });
          });
        });

      // 轨迹回放
      carReplayBtn &&
        (carReplayBtn.onclick = () => {
          check(() => {
            wx.miniProgram.navigateTo({
              url: `../../mapPackage/pages/findCar?sn=${item.sn}&tabItem=replay&source=${item.source}&locationType=${item.locationType}`,
            });
          });
        });

      // 常驻地
      carResidentBtn &&
        (carResidentBtn.onclick = () => {
          check(() => {
            wx.miniProgram.navigateTo({
              url: `../../mapPackage/pages/findCar?sn=${item.sn}&tabItem=resident&source=${item.source}&locationType=${item.locationType}`,
            });
          });
        });
      // 实时监控
      carMonitorBtn &&
        (carMonitorBtn.onclick = () => {
          check(() => {
            wx.miniProgram.navigateTo({
              url: `../../mapPackage/pages/findCar?sn=${item.sn}&tabItem=monitor&source=${item.source}&locationType=${item.locationType}`,
            });
          });
        });

      // 设备调频
      (carFreqBtn && carFreqBtn).onclick = () => {
        if (item.source == 1) {
          // 仅无线设备可调频
          return;
        } else {
          wx.miniProgram.navigateTo({
            url: `../../mapPackage/pages/findCar?sn=${item.sn}&tabItem=freq&source=${item.source}&locationType=${item.locationType}`,
          });
        }
      };

      // 电子围栏
      (carFenceBtn && carFenceBtn).onclick = () => {
        if (loginUserId.value || item.source == 0) {
          return;
        } else {
          wx.miniProgram.navigateTo({
            url: `../../mapPackage/pages/findCar?sn=${item.sn}&tabItem=fence&source=${item.source}&locationType=${item.locationType}`,
          });
        }
      };
      // 分享找车
      shareBtn.onclick = () => {
        wx.miniProgram.navigateTo({
          url: `../../mapPackage/pages/findCar?sn=${item.sn}&tabItem=share&source=${item.source}&locationType=${item.locationType}`,
        });
      };
      //点击车辆打开信息窗体
      marker.on("click", () => {
        map.setCenter(infoWindow.getPosition());
        if (infoWindow.getIsOpen()) {
          infoWindow.close();
        } else {
          infoWindow.open(map);
        }
        setAddress();
      });
      return marker;
    };
    const check = (cb) => {
      if (loginUserId.value && isShareDead.value) {
        Toast("该分享已失效");
        return;
      }
      cb();
    };
    // 加载更多
    const getCarList = (data, flag) => {
      if (flag) {
        pageNumber.value = 0;
        list.value = [];
      }
      pageNumber.value++;
      if (keyWord.value && keyWord.value.length < 2) {
        // 关键字小于两个字符 忽略 全量搜索
        keyWord.value = "";
      }
      searched.value = false;
      GET_ALL_CAR_LIST({
        pageSize: 10,
        pageNumber: pageNumber.value,
        term: keyWord.value,
      }).then((res) => {
        if (res.code === 200) {
          list.value = list.value.concat(res.data.list);
          loading.value = false;
          searched.value = flag;
          if (res.data.totalPages === pageNumber.value) {
            finished.value = true;
          }
        }
      });
    };
    return {
      refreshCb,
      isShowList,
      mapContentRefs,
      keyWord,
      loading,
      finished,
      list,
      getCarList,
      getCarInfo,
      loginUserId,
      check,
      carInfoList,
      carItem,
      searched,
      pageNumber,
      isUploaded,
      mapIsUploaded,
      routeQuery,
      ISLOADING,
    };
  },
});
</script>

<style lang="scss" scoped>
.index-map {
  position: relative;
}
.search-box {
  width: 100%;
  z-index: 102;
  position: fixed;
  top: 0;
  .search-header {
    box-shadow: 0px 3px 6px 0px rgba(0, 0, 0, 0.06);
    background-color: rgba(255, 255, 255, 1);
    border-radius: 6px;
    height: 44px;
    margin: 12px 12px 0 12px;
    .select-box {
      color: #b8bdc2;
      font-size: 14px;
      padding: 0 16px;
      line-height: 44px;
      display: flex;
      justify-content: space-between;
      .arrow-icon {
        color: #9ca4ad;
      }
    }
    .select-box.text {
      color: #111111;
    }
  }
  .search-content {
    max-height: 516px;
    margin-top: 8px;
    border-radius: 12px;
    background: #fff;
    overflow: hidden;
    transition: max-height 0.3s;
    .search-input {
      border-radius: 12px;
    }
    .list-box {
      max-height: 464px;
      overflow-y: auto;
      .list-item {
        height: 52px;
        padding: 0 14px;
        display: flex;
        align-items: center;
        .item-right {
          margin-left: 10px;
          display: flex;
          justify-content: space-between;
          width: 100%;
          .slectedicon {
            display: inline-block;
            width: 20px;
            height: 20px;
          }
        }
      }
      .list-item.active {
        background: #ecf5fe;
      }
    }
    .no-data-class {
      display: flex;
      flex-flow: column;
      justify-content: center;
      align-items: center;
      padding: 40px;
      span {
        font-size: 14px;
        color: #9fa6ad;
        margin-top: 16px;
      }
    }
  }
  .search-content.hidden {
    max-height: 0;
    transition: max-height 0.3s;
  }
}
</style>
<style lang="scss">
.car-info-window {
  width: 280px;
  background-color: #fff;
  border-radius: 10px;
  border: 1px solid #ececec;
  color: #111111;
  word-break: break-all;
  user-select: text;
  .main-info {
    padding: 10px;
  }
  .info-title {
    font-weight: normal;
    border-bottom: 1px solid #f5f6f7;
    padding-bottom: 10px;
    font-size: 14px;
    color: #333;
    div {
      color: #111111;
      font-weight: 600;
      margin-bottom: 5px;
    }
    :last-child {
      font-weight: 500;
    }
  }
  .info-list {
    position: relative;
    font-size: 12px;
    padding-top: 10px;
    p {
      display: flex;
      align-items: flex-start;
      line-height: 100%;
      margin: 8px 0;
      i {
        flex: 0 0 14px;
      }
      span {
        color: #111111;
        line-height: 16px;
      }
      .label {
        color: #6c737a;
        flex: 0 0 60px;
        margin-left: 5px;
      }
      a {
        display: flex;
        justify-items: center;
        align-items: center;
      }
    }
    .list-info {
      align-items: center;
      a {
        margin-right: 10px;
      }
      .radio {
        width: 12px;
        height: 12px;
        border-radius: 50%;
        margin-right: 5px;
        border: 1px solid #c4c8cc;
      }
      .radio.active {
        width: 4px;
        height: 4px;
        border: 5px solid var(--base-color);
      }
    }
  }
  .cmd-list {
    font-size: 0px;
    border-top: 1px solid #f5f6f7;
    .cmd-list-top {
      border-bottom: 1px solid #f5f6f7;
      padding: 14px 12px;
      a {
        font-size: 12px;
        display: inline-block;
        width: 25%;
        text-align: center;
        box-sizing: border-box;
        border-right: 1px solid #f5f6f7;
      }
      :last-child {
        border-right: none;
      }
      a.disabled {
        color: #6c737a;
      }
    }
    .cmd-list-bottom {
      padding: 14px 12px;
      a {
        font-size: 12px;
        display: inline-block;
        width: 25%;
        text-align: center;
        box-sizing: border-box;
        border-right: 1px solid #ececec;
      }
      :last-child {
        border-right: none;
      }
      a.disabled {
        color: #6c737a;
      }
    }
  }
}
.van-cell {
  padding: 0;
}
.van-search__content {
  height: var(--van-search-input-height);
  display: flex;
  align-items: center;
}
</style>