fence.vue 20.2 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
<template>
  <v-full-layout>
    <template slot="breadcrumb">
      <el-breadcrumb-item>电子围栏</el-breadcrumb-item>
    </template>
    <template slot="main">
      <div class="map-wrap">
        <div id="map" class="map-container" v-loading="mapLoading"></div>
        <map-controll></map-controll>
        <div class="controll-wrap">
          <el-scrollbar class="el_scrollbar">
            <div class="search-box">
              <h3>电子围栏</h3>
              <p class="info">{{ name }}/{{ standno }}/{{ sn }}</p>
              <el-form
                ref="form"
                :model="form"
                :rules="rules"
                size="mini"
                class="form"
                label-position="top"
              >
                <el-form-item label="定位方式">
                  <el-select
                    v-model="form.locationType"
                    class="a_select"
                    placeholder="请选择"
                    size="small"
                    :disabled="true"
                  >
                    <el-option label="GPS" :value="1"></el-option>
                    <el-option label="基站" :value="2"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="围栏名称" prop="fenceName">
                  <el-input
                    v-model.trim="form.fenceName"
                    maxlength="200"
                  ></el-input>
                </el-form-item>
                <el-form-item label="触发类型" prop="triggerType">
                  <el-select
                    v-model="form.triggerType"
                    class="a_select"
                    placeholder="请选择"
                    size="small"
                  >
                    <el-option label="驶出" :value="2"></el-option>
                    <el-option label="驶入" :value="1"></el-option>
                    <!-- v1.25.0版本需求,无线设备新增围栏只有驶入驶出报警 -->
                    <el-option
                      label="驶入驶出"
                      :value="3"
                      v-if="form.source == 1"
                    ></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="指定区间" prop="dateRange">
                  <el-date-picker
                    style="width: 100%"
                    v-model="form.dateRange"
                    type="daterange"
                    size="small"
                    :clearable="false"
                    :editable="false"
                    :unlink-panels="true"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    value-format="yyyy-MM-dd"
                  >
                  </el-date-picker>
                </el-form-item>
                <el-form-item
                  label="监控区域"
                  class="row-form-item is-required"
                >
                  <div class="el_radio_group">
                    <div class="radio_cell">
                      <el-radio :label="1" v-model="form.shapeType"
                        >圆形</el-radio
                      >
                      <el-form-item label="" prop="radius">
                        <el-input
                          v-model.trim="form.radius"
                          style="width: 120px"
                          maxlength="200"
                        >
                          <template slot="append">米</template>
                        </el-input>
                        <el-button
                          type="primary"
                          class="btn"
                          :disabled="!form.radius"
                          @click="changeRadius"
                          >更新</el-button
                        >
                      </el-form-item>
                    </div>
                    <div class="radio_cell">
                      <el-radio :label="3" v-model="form.shapeType"
                        >多边形</el-radio
                      >
                      <div class="shape_tip">
                        *注:地图任意位置,单击新增双击结束绘制。
                      </div>
                    </div>
                    <div class="radio_cell">
                      <el-radio :label="4" v-model="form.shapeType"
                        >行政区域</el-radio
                      >
                      <el-form-item label="" prop="areaName">
                        <city-select
                          v-model="form.areaName"
                          :checkStrictly="true"
                          :disabled="form.shapeType != 4"
                          @change="changeArea"
                        ></city-select>
                      </el-form-item>
                    </div>
                  </div>
                </el-form-item>
                <el-form-item label="短信通知" prop="telNumbers">
                  <el-input
                    v-model.trim="form.telNumbers"
                    maxlength="100"
                    placeholder="输入格式:手机号码,手机号码(支持双号)"
                  ></el-input>
                </el-form-item>
                <el-form-item label="围栏状态" class="is-required">
                  <el-radio v-model="form.enableStatus" :label="1"
                    >开启</el-radio
                  >
                  <el-radio v-model="form.enableStatus" :label="0"
                    >关闭</el-radio
                  >
                </el-form-item>
                <el-form-item>
                  <el-button
                    type="primary"
                    @click="submit"
                    size="small"
                    :loading="isSubmitting"
                    >保存</el-button
                  >
                </el-form-item>
              </el-form>
            </div>
          </el-scrollbar>
        </div>
        <!-- <el-alert v-show="showSuccessTips" :title="`保存成功,页面将在${timer}秒后自动关闭`" type="success" :closable="false" show-icon class="show_success_tips"></el-alert> -->
        <!-- <el-dialog title="提示" custom-class="monitor_dialog" :visible.sync="showSuccessTips" width="340px" append-to-body :show-close="false" :close-on-click-modal="false" :close-on-press-escape="false">
          <div class="monitor_dialog_content">
            <i class="el-icon-circle-check"></i>
            <span class="">{{`保存成功,页面将在${timer}秒后自动关闭`}}</span>
          </div>
          <span slot="footer" class="dialog-footer">
            <el-button type="primary" size="mini" @click="closePage">立即关闭</el-button>
          </span>
        </el-dialog> -->

        <!-- 提示弹框 开始 -->
        <div class="monitor_dialog" v-show="showSuccessTips">
          <div class="monitor_dialog_content">
            <i class="el-icon-circle-check i_icon"></i>
            <span class="tips">{{
              `保存成功,页面将在${timer}秒后自动关闭`
            }}</span>
            <el-button
              type="primary"
              class="btn"
              size="small"
              @click="closePage"
              >立即关闭</el-button
            >
          </div>
          <div class="blk"></div>
        </div>
        <!-- 提示弹框 end -->
      </div>
    </template>
  </v-full-layout>
</template>
<script>
import "@/assets/spritesmith-generated/sprite.css";
import mapService from "@/utils/mapService.js";
import "@/utils/mapEvent.js";
import EventBus from "@/event-bus";
import { services } from "@/api";
const MONITOR_SERVICE = services.monitor;

import { CONSTANT } from "@/constant";
import { deepClone, showToast } from "@/utils/utils";
import AreaSelect from "./AreaSelect.vue";
import MapControll from "./MapControll.vue";
import { GET_DEVICE_LOCATION } from "@/api/services/monitor";
import CitySelect from "@/components/area";
import { SAVE_ELECFENCE_INFO } from "@/api/services/fence";
export default {
  components: {
    AreaSelect,
    MapControll,
    CitySelect,
  },
  data() {
    let checkFenceName = (rule, vaule, cb) => {
      if (
        typeof this.restainFenceName != "undefined" &&
        this.form.electricFenceName === this.restainFenceName
      ) {
        cb();
      }
      MONITOR_SERVICE.checkFenceName({
        name: this.form.electricFenceName,
      }).then((res) => {
        if (res.returnCode == 0) {
          if (res.data.isExist == 1) {
            cb(new Error("该围栏名已存在,请重新输入!"));
          } else {
            cb();
          }
        }
      });
    };
    let checkDaterange = (rule, vaule, cb) => {
      const dateRange = this.form.dateRange;
      if (!dateRange || !dateRange.length) {
        cb(new Error("请选择日期"));
      } else {
        let min = dateRange[0];
        let max = dateRange[1];

        if (!moment.isMoment(min)) {
          min = moment(min);
        }
        if (!moment.isMoment(max)) {
          max = moment(max);
        }

        if (min.isBefore(moment(max).subtract(30 * 6, "days"), "minute")) {
          cb(new Error("日期范围不能超过180天"));
        }
      }
      cb();
    };

    let checkRadius = (rule, vaule, cb) => {
      const fenceRadius = this.form.radius;
      if (
        this.form.shapeType == 1 &&
        (!/^\d+$/.test(fenceRadius) || fenceRadius < 1000)
      ) {
        cb(new Error("请输入整数且不小于1000"));
      } else {
        cb();
      }
    };

    let checkMsgtel = (rule, vaule, cb) => {
      let reg = /^1\d{10}((\,1\d{10})|(\,1\d{10}))?$/;
      if (this.form.telNumbers && !reg.test(this.form.telNumbers)) {
        cb(new Error("请输入正确的格式"));
      } else {
        cb();
      }
    };

    let checkArea = (rule, vaule, cb) => {
      if (this.form.shapeType == 4 && !this.form.areaCode) {
        cb(new Error("请选择行政区域"));
      } else {
        cb();
      }
    };
    return {
      sn: this.$route.query.sn,
      name: this.$route.query.name,
      standno: this.$route.query.standno,
      AMap: null,
      mapObj: null,
      mapLoading: false,
      isInitMap: false,
      searchText: "",
      searchType: "sn",
      isSearching: false,
      areaId: "",
      areaStr: "",
      isSubmitting: false,
      form: {
        locationType: 1,
        triggerType: 1,
        shapeType: 1,
        radius: 1000,
        enableStatus: 1,
        source: 1,
      },
      rules: {
        fenceName: [
          { required: true, message: "请输入围栏名称", trigger: "blur" },
          { pattern: /^\S{2,200}$/, message: "至少2字", trigger: "blur" },
          // { validator: checkFenceName, trigger: "blur" },
        ],
        triggerType: [
          { required: true, message: "请选择触发类型", trigger: "change" },
        ],
        dateRange: [
          { required: true, message: "请选择指定区间", trigger: "change" },
          { validator: checkDaterange, trigger: "change" },
        ],
        raidus: [{ validator: checkRadius, trigger: "blur" }],
        telNumbers: [{ validator: checkMsgtel, trigger: "blur" }],
        areaName: [{ validator: checkArea, trigger: "blur" }],
      },
      fenceId: "",
      restainFenceName: "",
      polygonPath: [],
      deviceInfo: {}, // 设备信息
      deviceList: [], // 设备列表
      showSuccessTips: false,
      timer: 5,
    };
  },
  created() {
    EventBus.$on("uiMap.fence.onFenceRadiusChange", this.onFenceRadiusChange);
    EventBus.$on("uiMap.fence.polygonChange", this.onFencePolyChange);

    let _query = this.$route.query;
    this.$set(this.form, "source", _query.source || 0);
    this.initMap();
  },
  mounted() {
    //store.dispatch("setFullsize", true);
  },
  watch: {
    "form.shapeType": {
      handler: function (n, o) {
        this.changeShapeType(n);
      },
      immediate: true,
    },
  },
  methods: {
    // 初始化地图
    initMap() {
      let _this = this;
      _this.$mapInit().then((AMap) => {
        window.AMap = AMap;
        window.AMap = AMap;
        _this.map = mapService.init("map");
        _this.getDevinceInfo();
      });
    },

    // 获取设备信息
    getDevinceInfo() {
      let oParams = {};
      oParams.sns = this.$route.query.sn;
      oParams.deviceCode = "gps";
      GET_DEVICE_LOCATION(oParams).then((res) => {
        if (!res.data || res.data.length <= 0) {
          return false;
        }
        let obj = res.data[0] || {};
        this.deviceInfo = obj;
        this.deviceList = res.data || [];
        this.drawMarker(res);
        this.drawFenceCircle(obj.gpsLocation);
      });
    },
    pickDate(beforeDays) {},
    // 保存围栏信息
    submit() {
      let _this = this;
      _this.$refs.form.validate((valid) => {
        if (!valid) {
          return false;
        }
        let oParams = _this.getParams();
        // 多边形围栏校验
        if (
          oParams.shapeType == 3 &&
          (!_this.polygonPath || _this.polygonPath.length <= 0)
        ) {
          showToast({
            message: "请绘制线路",
          });
          return false;
        }

        console.log(oParams);
        _this.isSubmitting = true;
        SAVE_ELECFENCE_INFO(oParams)
          .then((res) => {
            _this.isSubmitting = false;
            if (res.code != 200) {
              showToast({
                message: res.message,
              });
              return false;
            }
            _this.showSuccessTips = true;
            let tipInterval = setInterval(function () {
              if (_this.timer == 0) {
                clearInterval(tipInterval);
                _this.showSuccessTips = false;
                _this.timer = 5;
                window.close();
                return false;
              }
              _this.timer -= 1;
            }, 1000);
          })
          .catch((error) => {
            _this.isSubmitting = false;
          });
      });
    },
    updateFenceRadius(radius) {},

    // 在地图上画圆形围栏
    drawFenceCircle(info) {
      let _this = this;
      _this.form.lng = info.elng;
      _this.form.lat = info.elat;
      EventBus.$emit("uiMap.fence.createFenceCircle", {
        lng: info.elng,
        lat: info.elat,
        radius: _this.form.radius,
      });
    },

    // 在地图上画行政区域
    drawDistrict(areaCode) {
      console.log(areaCode);
      EventBus.$emit("uiMap.fence.drawDistrict", areaCode);
    },

    // 在地图上画多边形
    drawPolygon() {
      EventBus.$emit("uiMap.fence.destroy");
      EventBus.$emit("uiMap.fence.drawPolygon", this.polygonPath);
    },

    // 根据设备地址,在地图上画车辆
    drawMarker(res) {
      let query = this.$route.query;
      let obj = res.data[0] || {};
      let g = obj.gpsLocation || {};
      let b = obj.bsLocation || {};
      let option = {};
      let sourceType = {
        1: "有线",
        2: "无线",
      };

      // GPS信息
      option.lngGPS = g.elng;
      option.latGPS = g.elat;
      option.timeGPS = g.dateTime;
      option.transferAddressGPS = g.address;

      // 基站信息
      option.lngBase = b.elng;
      option.latBase = b.elat;
      option.timeBase = b.dateTime;
      option.transferAddressBase = b.address;

      option.name = query.name;
      option.standno = query.standno;
      option.sn = query.sn;
      option.sourceString = sourceType[obj.source] || "--";
      option.status = obj.status;
      option.battery = obj.battery;

      // 标记车辆信息
      EventBus.$emit("uiMap.fence.createFenceMarker", option);
    },

    // 监听圆形围栏直径变化
    onFenceRadiusChange(e) {
      console.log("圆形围栏:", e);
      this.$set(this.form, "radius", e);
    },

    // 监听围栏类型变化
    changeShapeType(e) {
      let _this = this;
      let _info = this.deviceInfo;
      let areaCode = this.form.areaCode || [];
      // 1:圆形围栏  4:行政区域   3:多边形
      console.log(_info);
      switch (e) {
        case 4:
          _this.drawDistrict(areaCode);
          break;
        case 3:
          _this.drawPolygon();
          break;
        default:
          _this.drawFenceCircle(_info.gpsLocation || {});
          break;
      }
    },

    // 监听多边形围栏变化
    onFencePolyChange(e) {
      this.polygonPath = e.polygonPath;
    },

    // 监听行政区域
    changeArea(e) {
      console.log(e);
      this.form.areaCode = e.code;
      this.form.areaText = e.name;
      this.drawDistrict(e.code);
      this.$refs.form.clearValidate();
    },

    // 获取参数
    getParams() {
      let _this = this;
      let o = deepClone(_this.form);
      let query = this.$route.query;
      console.log(o);
      let oParams = {};
      let deviceList = this.deviceList;

      oParams.fenceName = o.fenceName;
      oParams.bizFenceType = 14;
      oParams.triggerType = o.triggerType;
      oParams.enableStatus = o.enableStatus;
      oParams.timeType = 2;
      oParams.shapeType = o.shapeType;
      oParams.telNumbers = o.telNumbers;

      // 指定区间
      if (o.dateRange && o.dateRange.length > 0) {
        oParams.startTime = o.dateRange[0];
        oParams.endTime = o.dateRange[1];
      }

      // 圆形围栏
      if (o.shapeType == 1) {
        oParams.radius = o.radius;
        oParams.lnglat = `${o.lng},${o.lat}`;
      }

      // 多边形围栏
      if (o.shapeType == 3) {
        oParams.lnglat = this.polygonPath.join("|");
      }

      // 行政区域围栏
      if (o.shapeType == 4) {
        oParams.areaCode = o.areaCode[o.areaCode.length - 1];
        oParams.areaText = o.areaText[o.areaText.length - 1];
      }

      // 设备列表
      let arr = [];
      deviceList.map((k) => {
        let cur = {};
        let _gps = k.gpsLocation || {};
        cur.address = _gps.address;
        cur.lat = _gps.elat;
        cur.lng = _gps.elng;
        cur.name = k.ownerName;
        cur.sn = k.sn;
        cur.time = _gps.dateTime;
        cur.userTel = "";
        cur.vin = k.vin;
        arr.push(cur);
      });
      oParams.snList = arr;
      oParams.source = query.source;
      return oParams;
    },

    // 监听圆形半径更改
    changeRadius() {
      let reg = /^\d+$/;
      let e = this.form.radius;
      if (!reg.test(e) || e > 10000) {
        showToast({
          message: "请输入1-10000间的整数",
        });
        return false;
      }
      EventBus.$emit("uiMap.fence.updateFenceRadius", {
        fenceType: "circle",
        radius: e,
      });
    },

    // 关闭标签页
    closePage() {
      window.close();
    },
  },
};
</script>
<style lang="scss" scoped>
/*样式写在  scss/monitor.scss*/
.side_auto_h {
  max-height: calc(100vh - 100px);
  overflow: auto;
}
.shape_tip {
  color: #999;
  line-height: 1.5;
  margin-bottom: 10px;
}
.search-box {
  height: 100%;
  display: block;
  .info {
    word-break: break-all;
  }
  .a_select {
    width: 100%;
  }
}
.map-wrap {
  .controll-wrap {
    top: 10px;
    left: 10px;
    bottom: 10px;
  }
  .el_scrollbar {
    height: 100%;
    z-index: 999;
    /deep/ .el-scrollbar__wrap {
      overflow-x: hidden;
    }
  }
}
.el_radio_group {
  display: flex;
  flex-direction: column;
  .radio_cell {
    display: flex;
    align-items: center;
    margin: 0 0 10px 0;
    .el-radio {
      margin-bottom: 15px;
    }
    .shape_tip {
      font-size: 12px;
    }
  }
}
.show_success_tips {
  position: absolute;
  width: auto;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  z-index: 9999;
}
.monitor_dialog_content {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  i {
    font-size: 24px;
    color: #67c23a;
    margin: 0 10px;
  }
}
</style>
<style lang="scss">
.monitor_dialog {
  .blk {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: #000;
    opacity: 0.3;
    z-index: 9998;
  }
  .monitor_dialog_content {
    position: fixed;
    z-index: 9999;
    width: 260px;
    // height: 200px;
    border-radius: 10px;
    background: #fff;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    flex-direction: column;
    padding: 30px 20px;
    .i_icon {
      font-size: 40px;
      color: #67c23a;
    }
    .tips {
      margin: 15px 0;
      font-size: 14px;
      color: #999;
    }
  }
}
</style>