fence copy.vue 17.8 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
<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">
          <div class="search-box active side_auto_h">
            <h3>电子围栏</h3>
            <p class="info">{{ name }}/{{ standno }}/{{ sn }}</p>
            <el-form
              ref="form"
              :model="form"
              :rules="rules"
              :inline="false"
              size="mini"
              class="form"
            >
              <el-form-item label="定位方式">
                <el-select
                  v-model="form.locationType"
                  placeholder="请选择"
                  size="small"
                  style="width: 280px"
                >
                  <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="electricFenceName">
                <el-input
                  v-model.trim="form.electricFenceName"
                  maxlength="200"
                ></el-input>
              </el-form-item>
              <el-form-item label="触发类型">
                <el-select
                  v-model="form.triggerType"
                  placeholder="请选择"
                  size="small"
                  style="width: 280px"
                >
                  <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">
                <el-row>
                  <el-col :span="8">
                    <el-radio v-model="form.shapeType" :label="2"
                      >圆形</el-radio
                    >
                  </el-col>
                  <el-col :span="16">
                    <el-form-item label="" prop="fenceRadius">
                      <el-input
                        v-model.trim="form.fenceRadius"
                        style="width: 80px"
                        maxlength="200"
                      ></el-input
                      >米
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="8">
                    <el-radio v-model="form.shapeType" :label="5"
                      >多边形</el-radio
                    >
                  </el-col>
                  <el-col :span="16" class="shape_tip"
                    >*注:地图任意位置,单击新增双击结束绘制。</el-col
                  >
                </el-row>
                <el-row>
                  <el-col :span="8">
                    <el-radio v-model="form.shapeType" :label="3"
                      >行政区域</el-radio
                    >
                  </el-col>
                  <el-col :span="16">
                    <el-form-item label="">
                      <!-- 监控省市区组件 -->
                      <!-- <area-select ref="clearArea" @onChangArea="areaChoose" :prependFirst="true" :id="form.areaCode"></area-select> -->
                      <!-- 监控省市区组件 /-->
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form-item>
              <el-form-item label="短信通知" prop="msgtel">
                <el-input
                  v-model.trim="form.msgtel"
                  maxlength="100"
                  placeholder="输入格式:手机号码,手机号码(支持双号)"
                ></el-input>
              </el-form-item>
              <el-form-item label="围栏状态">
                <el-radio v-model="form.status" :label="1">启用</el-radio>
                <el-radio v-model="form.status" :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>
        </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 AreaSelect from "./AreaSelect.vue";
import MAPGEO from "@/utils/mapgeo";
import MapControll from "./MapControll.vue";

export default {
  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: {
        fenceNo: "",
        electricFenceName: "",
        locationType: 1,
        triggerType: 3,
        dateRange: [],
        shapeType: 2,
        fenceRadius: "1000",
        status: 1,
        areaCode: "",
        lng: "",
        lat: "",
        analysisAddress: "",
        msgtel: "",
      },
      rules: {
        electricFenceName: [
          { 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: [],
    };
  },
  mounted() {
    store.dispatch("setFullsize", true);
  },
  watch: {
    "form.fenceRadius"(newVal) {
      if (this.form.shapeType === 2) {
        if (this._updateFenceRadiusTimeoutId) {
          clearTimeout(this._updateFenceRadiusTimeoutId);
        }
        this._updateFenceRadiusTimeoutId = setTimeout(() => {
          this.$refs.form.validateField("fenceRadius", (error) => {
            if (error) {
              return false;
            } else {
              this.updateFenceRadius(newVal);
            }
          });

          /*this.$refs.form.validate((valid, field)=>{
			    			if(valid){
			    				this.updateFenceRadius(newVal)
								
			    			}else{
			    				return false;
			    			}
			    		});*/
        }, 500);
      }
    },
    "form.shapeType"(newVal) {
      this._shapeTypeTimeoutId && clearTimeout(this._shapeTypeTimeoutId);
      this._shapeTypeTimeoutId = setTimeout(() => {
        console.log(newVal);
        if (newVal === 2) {
          this.$refs.form.validateField("fenceRadius", (error) => {
            if (error) {
              EventBus.$emit("uiMap.fence.destroy");
              return false;
            } else {
              this.drawFenceCircle();
            }
          });
        } else if (newVal === 3) {
          if (this.areaId) {
            this.drawDistrict(this.areaId);
          } else {
            EventBus.$emit("uiMap.fence.destroy");
          }
        } else if (newVal === 5) {
          this.drawPolygon();
        }
      }, 500);
    },
  },
  methods: {
    initMap() {
      let check = () => {
        this.checkMapLoaded && window.clearTimeout(this.checkMapLoaded);

        return setTimeout(() => {
          if (
            "AMap" in window &&
            typeof AMap.Map == "function" &&
            !this.mapObj
          ) {
            window.AMap = AMap;
            this.mapObj = mapService.init("map");

            //this.bindEvent();
            this.isInitMap = true;
            this.initFence();
          } else {
            this.checkMapLoaded = check();
          }
        }, 100);
      };
      this.checkMapLoaded = check();
    },
    initFence() {
      MONITOR_SERVICE.getSingleFence({
        sn: this.sn,
      }).then((res) => {
        if (res.returnCode == 0) {
          const data = res.data;
          if (data) {
            this.fenceId = data.fenceId;
            const form = this.form;
            form.locationType = data.locationType;
            form.electricFenceName = data.electricFenceName;
            this.restainFenceName = form.electricFenceName;

            form.triggerType = data.electricFenceType;
            form.dateRange = [moment(data.startTime), moment(data.endTime)];
            form.shapeType = data.shapeType;
            form.status = data.status;
            form.areaCode = data.areaCode;
            form.msgtel = data.telNumbers;

            if (form.shapeType === 2) {
              form.fenceRadius = data.radius;
              form.lng = data.lng;
              form.lat = data.lat;
              EventBus.$emit("uiMap.fence.createFenceCircle", {
                lng: data.lng,
                lat: data.lat,
                radius: data.radius,
              });
            } else if (form.shapeType === 3) {
              this.drawDistrict(data.areaCode);
            } else if (form.shapeType === 5) {
              if (data.lnglat) {
                this.polygonPath = data.lnglat.split("|").map((item) => {
                  return [item.split(",")[0], item.split(",")[1]];
                });
              }
              this.drawPolygon();
            }
          } else {
            this.restainFenceName = undefined;
          }

          this.initDetail();
        }
      });
    },
    initDetail() {
      MONITOR_SERVICE.getTrackBySn({
        sn: this.sn,
      }).then((res) => {
        if (res.returnCode == 0) {
          const data = res.data;
          const form = this.form;
          if (!this.restainFenceName) {
            form.electricFenceName = "单设备围栏-" + this.name;
          }

          if (!form.lng && !form.lat) {
            form.lng = data.lngGPS;
            form.lat = data.latGPS;
            this.drawFenceCircle();
          }
          EventBus.$emit("uiMap.fence.createFenceMarker", data);
        }
      });
    },
    pickDate(beforeDays) {
      this.form.dateRange = [
        moment().subtract(beforeDays, "days"),
        moment().subtract(1, "days"),
      ];
    },
    submit() {
      if (this.form.shapeType == 5) {
        if (this.polygonPath.length === 0) {
          this.$message({
            message: "请绘制围栏",
            type: "error",
          });
          return;
        }
      }
      this.$refs.form.validate((valid, field) => {
        if (valid) {
          this.isSubmitting = true;

          this.getParams().then((data) => {
            let params = data;

            if (typeof this.fenceId != "undefined") {
              params.id = this.fenceId;

              MONITOR_SERVICE.updateFence(JSON.stringify(params)).then(
                (res) => {
                  if (res.returnCode == 0) {
                    //this.closeDialog();
                    this.$message({
                      message: res.message,
                      type: "success",
                    });
                  }
                  this.isSubmitting = false;
                }
              );
            } else {
              MONITOR_SERVICE.getNewFenceId().then((res) => {
                if (res.returnCode == 0) {
                  params.fenceNo = res.data.fenceNo;
                  MONITOR_SERVICE.saveFence(JSON.stringify(params)).then(
                    (res) => {
                      if (res.returnCode == 0) {
                        //this.closeDialog();
                        this.$message({
                          message: res.message,
                          type: "success",
                        });
                        this.fenceId = res.data;
                        this.restainFenceName = this.form.electricFenceName;
                      }
                      this.isSubmitting = false;
                    }
                  );
                }
              });
            }
          });
        } else {
          return false;
        }
      });
    },
    getParams() {
      return new Promise((resolve, reject) => {
        const form = this.form;
        const params = {};
        const transferItem = {
          data: [
            {
              elng: form.lng,
              elat: form.lat,
            },
          ],
        };
        MAPGEO.transferAddressByGroup(transferItem, () => {
          params.electricFenceName = form.electricFenceName;
          params.electricFenceType = form.triggerType;
          params.shapeType = form.shapeType;
          params.locationType = form.locationType;

          if (params.shapeType == 3) {
            params.areaCode = this.areaId;
            params.areaString = this.areaStr;
          } else if (params.shapeType == 2) {
            params.lng = form.lng;
            params.lat = form.lat;
            params.radius = form.fenceRadius;
            params.analysisAddress = transferItem.data[0].transferAddress;
          } else if (params.shapeType == 5) {
            params.lnglat = this.polygonPath.join("|");
          }

          params.status = form.status;
          params.snList = [{ sn: this.sn }];

          params.startTime = moment(this.form.dateRange[0]).format(
            "YYYY-MM-DD"
          );
          params.endTime = moment(this.form.dateRange[1]).format("YYYY-MM-DD");
          if (form.msgtel) {
            params.telNumbers = form.msgtel;
          }
          params.timeType = 2;

          resolve(params);
        });
      });
    },
    areaChoose(areaId, areaStr) {
      this.areaId = areaId;
      this.areaStr = areaStr;
      if (this.form.shapeType === 3) {
        this.drawDistrict(areaId);
      }
    },
    updateFenceRadius(radius) {
      EventBus.$emit("uiMap.fence.updateFenceRadius", radius);
    },
    drawFenceCircle() {
      EventBus.$emit("uiMap.fence.createFenceCircle", {
        lng: this.form.lng,
        lat: this.form.lat,
        radius: this.form.fenceRadius,
      });
    },
    drawDistrict(areaCode) {
      this._drawDistrictTimeoutId && clearTimeout(this._drawDistrictTimeoutId);

      this._drawDistrictTimeoutId = setTimeout(() => {
        EventBus.$emit("uiMap.fence.drawDistrict", areaCode);
      }, 500);
    },
    drawPolygon() {
      EventBus.$emit("uiMap.fence.drawPolygon", this.polygonPath);
    },
  },
  created() {
    let _this = this;
    _this.$mapInit({ version: "1.4.2" }).then((AMap) => {
      _this.initMap();
    });
    EventBus.$on("uiMap.fence.loading", (isLoading) => {
      this.mapLoading = isLoading;
    });
    EventBus.$on("uiMap.fence.onFenceRadiusChange", (radius) => {
      this.form.fenceRadius = radius;
      //this.$refs.form.validateField('fenceRadius');
    });
    EventBus.$on("uiMap.fence.onFenceLngLatChange", (data) => {
      this.form.lng = data.lng;
      this.form.lat = data.lat;
    });

    EventBus.$on("uiMap.fence.polygonChange", (data) => {
      this.polygonPath = data.polygonPath;
    });
  },
  beforeDestroy() {},
  components: {
    AreaSelect,
    MapControll,
  },
};
</script>
<style 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;
}
</style>