fence.vue 13.7 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
<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="触发类型">
                  <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>
                    <el-option label="驶入驶出" :value="3"></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="结束日期">
                  </el-date-picker>
                </el-form-item>
                <el-form-item label="监控区域" class="row-form-item">
                  <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: 80px;" maxlength="200"></el-input>米
                      </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="radius">
                        <city-select v-model="form.areaName" :checkStrictly="true" :disabled="form.shapeType != 3" @change="changeArea"></city-select>
                      </el-form-item>
                    </div>
                  </div>
                </el-form-item>
                <el-form-item label="短信通知" prop="msgtel">
                  <el-input v-model.trim="form.telNumbers" maxlength="100" placeholder="输入格式:手机号码,手机号码(支持双号)"></el-input>
                </el-form-item>
                <el-form-item label="围栏状态">
                  <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>
      </div>
    </template>
  </v-full-layout>
</template>
<script>
import "@/assets/spritesmith-generated/sprite.css";
import store from "@/vuex/store.js";
import remoteLoad from "@/utils/remoteLoad.js";
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 } from "@/utils/utils";
import AreaSelect from "./AreaSelect.vue";
import MAPGEO from "@/utils/mapgeo";
import MapControll from "./MapControll.vue";
import AMapLoader from "@amap/amap-jsapi-loader";
import { GET_DEVICE_LOCATION } from "@/api/services/monitor";
import CitySelect from "@/components/area";
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.fenceRadius;
      if (
        this.form.shapeType == 2 &&
        (!/^\d+$/.test(fenceRadius) || fenceRadius < 1000)
      ) {
        cb(new Error("请输入整数且不小于1000"));
      } else {
        cb();
      }
    };

    let checkMsgtel = (rule, vaule, cb) => {
      if (this.form.msgtel && !/^1\d{10}(\,1\d{10})?$/.test(this.form.msgtel)) {
        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: 3,
        shapeType: 2,
        radius: 1000,
        status: 1,
      },
      rules: {
        fenceName: [
          { required: true, message: "请输入围栏名称", trigger: "blur" },
          { pattern: /^\S{2,200}$/, message: "至少2字", trigger: "blur" },
          { validator: checkFenceName, trigger: "blur" },
        ],
        daterange: [{ validator: checkDaterange }],
        fenceRadius: [{ validator: checkRadius, trigger: "blur" }],
        msgtel: [{ validator: checkMsgtel, trigger: "blur" }],
      },
      fenceId: undefined,
      restainFenceName: "",
      polygonPath: [],
      deviceInfo: {}, // 设备信息
      deviceList: [], // 设备列表
    };
  },
  created() {
    EventBus.$on("uiMap.fence.onFenceRadiusChange", this.onFenceRadiusChange);
    EventBus.$on("uiMap.fence.polygonChange", this.onFencePolyChange);
    this.initMap();
  },
  mounted() {
    //store.dispatch("setFullsize", true);
  },
  watch: {
    "form.shapeType": {
      handler: function (n, o) {
        this.changeShapeType(n);
      },
      immediate: true,
    },
  },
  methods: {
    // 初始化地图
    initMap() {
      let _this = this;
      AMapLoader.load({
        key: CONSTANT.MAP_KEY, // 申请好的Web端开发者Key,首次调用 load 时必填
        version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
        plugins: CONSTANT.MAP_PLUGINS, // 需要使用的的插件列表,如比例尺'AMap.Scale'等
      }).then((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() {},
    updateFenceRadius(radius) {},

    // 在地图上画圆形围栏
    drawFenceCircle(info) {
      let _this = this;
      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);
    },

    // 获取参数
    getParams() {
      let _this = this;
      let o = deepClone(_this.form);
      console.log(o);
      let oParams = {};
      let device = this.deviceInfo;
      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 = `${device.elng},${device.elat}`;
      }

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

      // 行政区域围栏
      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;
    },
  },
};
</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;
    }
  }
}
</style>