index.vue 18.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
<template>
  <div>
    <h2>基础信息</h2>
    <el-divider></el-divider>
    <el-form ref="shopForm" :model="form" :rules="rules" label-width="135px" :inline="true">
      <el-form-item class="special-shop-input" label="特约店名称:" prop="specialShopName">
        <!-- <el-cascader
          :key="refresh"
          ref="specialShopCascader"
          v-model="form.specialShopName"
          :value="form.specialShopName"
          :placeholder="form.specialShopName || '请选择特约店名称'"
          :options="specialShopOptions"
          :props="specialShopDefaultProps"
          filterable
          @change="handleSpecialShopChange"
          size="small"
          clearable
          :disabled="readonlyStatus"
        ></el-cascader> -->
        <el-popover v-model="filterVisible" placement="bottom-start" width="400" @show="selectStore" trigger="click">
          <div v-loading="loading">
            <el-input placeholder="输入关键字进行过滤" v-model="filterText" size="small">
            </el-input>

            <el-tree style="max-height:50vh;overflow:auto" class="filter-tree" :data="filterTextData" :filter-node-method="filterNode" highlight-current ref="tree" node-key="id" check-on-click-node>
            </el-tree>
            <p style="text-align: right; margin-top: 10px">
              <el-button @click="filterConfirm" type="primary">确定</el-button>
              <el-button @click="closeFilter">取消</el-button>
            </p>
          </div>
          <el-input v-model="form.specialShopName" :placeholder="form.specialShopName || '请选择特约店名称'" slot="reference">
          </el-input>
        </el-popover>
      </el-form-item>
      <el-form-item label="特约店编码:">
        <el-input v-model="form.specialShopCode" :size="btn_size" readonly disabled Placeholder="特约店编码"></el-input>
      </el-form-item>
      <el-form-item label="特约店级别:">
        <el-radio-group v-model="form.shopLevel">
          <el-radio label="一级店" :disabled="readonlyStatus">一级店</el-radio>
          <el-radio label="二级店" :disabled="readonlyStatus">二级店</el-radio>
        </el-radio-group>
      </el-form-item>
      <h2>特约店围栏配置</h2>
      <el-divider></el-divider>
      <el-form-item label="特约店线路名称:" prop="fenceName">
        <el-input v-model.trim="form.fenceName" :size="btn_size" maxlength="200" placeholder="请输入特约店线路名称" :disabled="readonlyStatus"></el-input>
      </el-form-item>
      <el-form-item label="监控范围:" prop="radius">
        <el-input v-model="form.radius" :size="btn_size" Placeholder="请输入半径" style="width:140px" maxlength="10" :disabled="true">
          <template slot="append">M</template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :size="btn_size" @click="updateFence" :disabled="readonlyStatus">更新线路</el-button>
      </el-form-item>
      <div>
        <el-form-item label="经度:" prop="lng">
          <el-input v-model="form.lng" size="small" :disabled="readonlyStatus"></el-input>
        </el-form-item>
        <el-form-item label="纬度:" prop="lat">
          <el-input v-model="form.lat" size="small" :disabled="readonlyStatus"></el-input>
        </el-form-item>
        <el-form-item label="特约店详细地址:" prop="analysisAddress">
          <el-input v-model="form.analysisAddress" size="medium" placeholder="自动生成" readonly disabled style="width:350px"></el-input>
        </el-form-item>
      </div>
    </el-form>
    <div class="ibox-content" style="height: 500px; padding:0 20px;z-index: 100">
      <fence-map :mapData="mapData" @onUpdateMapData="updateMapData" ref="fenceMap">
      </fence-map>
    </div>
    <div class="fenceModalfooter">
      <el-button v-if="!readonlyStatus" type="primary" @click="saveFence" size="small" :loading="submitting">{{ btnText }}</el-button>
      <el-button @click="cancel" size="small">取消</el-button>
    </div>
  </div>
</template>

<script>
import { getSpecialShopList } from "services/vehicle";
import { getOrgTree } from "services/common";
import {
  getFenceDetailById,
  saveFence,
  updateFence,
} from "services/specialshop";
import fenceMap from "components/map/fenceMap.vue";
import mapService from "utils/mapService.js";
export default {
  components: { fenceMap },
  data() {
    let checkSpecialShop = (rule, value, cb) => {
      if (!this.form.specialShopName) {
        cb(new Error("请选择特约店"));
      } else {
        cb();
      }
    };

    let checkLng = (rule, vaule, cb) => {
      if (
        this.form.shapeType == 1 &&
        !/^(\+|-)?(?:180(?:(?:\.0{1,6})?)|(?:[0-9]|[1-9][0-9]|1[0-7][0-9])(?:(?:\.[0-9]{1,6})?))$/.test(
          this.form.lng
        )
      ) {
        cb(new Error("请输入正确的格式"));
      } else {
        cb();
      }
    };

    let checkLat = (rule, vaule, cb) => {
      if (
        this.form.shapeType == 1 &&
        !/^(\+|-)?(?:90(?:(?:\.0{1,6})?)|(?:[0-9]|[1-8][0-9])(?:(?:\.[0-9]{1,6})?))$/.test(
          this.form.lat
        )
      ) {
        cb(new Error("请输入正确的格式"));
      } else {
        cb();
      }
    };

    let checkRadius = (rule, vaule, cb) => {
      if (this.form.shapeType == 1 && !/^\d+$/.test(this.form.radius)) {
        console.log(this.form.radius);
        cb(new Error("请输入正确的格式,不能输入小数点"));
      } else {
        cb();
      }
    };

    let checkTransferAddr = (rule, vaule, cb) => {
      if (this.form.shapeType == 1) {
        if (this.transferError) {
          cb(new Error("所填经纬度不能正确解析地址,请重新输入"));
        } else {
          cb();
        }
      } else {
        cb();
      }
    };
    return {
      refresh: new Date().getTime(),
      btn_size: "small",
      configForm: {},
      mapData: [],
      submitting: false,
      btnText: "保存",
      restainFenceName: "",
      specialShopOptions: [],
      specialShopDefaultProps: {
        children: "children",
        label: "label",
        value: "code",
      },
      readonlyStatus: false,
      form: {
        shopName: "",
        shopCode: "",
        shopLevel: "一级店",
        fenceName: "",
        lng: "",
        lat: "",
        radius: 150,
        bizFenceType: "12",
        shapeType: 1,
        specialShopId: "",
        specialShopName: "",
        // fenceNo: '',
        electricFenceType: 1,
        startTime: "",
        endTime: "",
        des: "",
        areaCode: "",
        analysisAddress: "",
        status: 1,
        transferError: false,
        timeType: 2,
        electricTimeFenceType: "",
        electricTimeFenceTypes: [
          {
            value: 1,
            label: "1个月",
          },
          {
            value: 2,
            label: "2个月",
          },
          {
            value: 3,
            label: "3个月",
          },
          {
            value: 4,
            label: "4个月",
          },
          {
            value: 5,
            label: "5个月",
          },
          {
            value: 6,
            label: "6个月",
          },
        ],
        dateRange: [],
        msgtel: "",
      },
      rules: {
        specialShopName: [
          { required: true, validator: checkSpecialShop, trigger: "change" },
        ],
        fenceName: [
          { required: true, message: "请输入特约店线路名称", trigger: "blur" },
          { pattern: /^\S{2,200}$/, message: "至少2字", trigger: "blur" },
        ],
        radius: [{ validator: checkRadius, trigger: "blur" }],
        lng: [{ required: true, validator: checkLng, trigger: "blur" }],
        lat: [{ required: true, validator: checkLat, trigger: "blur" }],
        analysisAddress: [{ validator: checkTransferAddr, trigger: "change" }],
      },

      loading: true,
      filterVisible: false,
      filterText: "",
      filterTextData: [],
      isOpen: true,
      shopNameId: "",
    };
  },
  created() {
    //this.getSpecialShopList();
  },
  mounted() {
    this.init();
  },
  watch: {
    filterText(val) {
      this._timeout && clearTimeout(this._timeout);
      this._timeout = setTimeout(() => {
        this.$refs.tree.filter(val);
      }, 500);
    },
  },
  methods: {
    selectStore() {
      if (this.isReadonly) return;
      this.filterText = "";
      this.filterDialogTitle = "选择所属组织";
      if (!this.filterTextData.length) {
        this.getStoreList();
      }

      //this.closeFilter();
    },
    closeFilter() {
      this.filterVisible = !this.filterVisible;
    },

    getStoreList() {
      let _this = this;
      _this.loading = true;
      getOrgTree({ hasUserLeaf: true }).then((res) => {
        const { code, data } = res;
        _this.loading = false;
        if (code == 200) {
          this.filterTextData = data;
        }
      });
    },
    filterConfirm() {
      const { label, id, origin } = this.$refs.tree.getCurrentNode();
      if (origin.type != "u") {
        this.$message({
          message: "请选择下级所属组织",
          type: "error",
        });
        return;
      }
      this.form.specialShopName = label.split("/")[1];
      this.form.specialShopCode = label.split("/")[0];
      //this.$set(this, "form.specialShopName", label);
      this.form.specialShopId = id;
      this.closeFilter();
    },
    filterNode(value, data) {
      console.log(value, data);
      this.loading = false;
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    init() {
      const { id, readonlyStatus } = this.$route.query;
      this.readonlyStatus = readonlyStatus || false;
      if (typeof id != "undefined") {
        this.getFenceDetail(id);
      } else {
        this.$refs.fenceMap.initFence({
          fenceType: "circle",
        });
        this.restainFenceName = undefined;
      }
    },
    getSpecialShopList() {
      getSpecialShopList().then((res) => {
        const { code, data } = res;
        if (code == 200) {
          this.specialShopOptions = data;
        }
      });
    },
    getFenceDetail(id) {
      getFenceDetailById({ shopId: id }).then((res) => {
        if (res.code == 200) {
          const form = this.form;
          const data = res.data;
          if (!data) {
            return;
          }
          for (let key in data) {
            form[key] = data[key];
          }
          this.form.shapeType = 1;
          this.shopId = data.id;
          this.form.dateRange = [form.startTime, form.endTime];
          this.areaCode = form.areaCode;
          this.restainFenceName = form.fenceName;
          this.form.fenceName = form.fenceName;
          this.form.msgtel = data.telNumbers;
          this.form.specialShopId = data.specialShopId;
          this.form.specialShopName = data.shopName;
          this.form.specialShopCode = data.shopCode;
          this.form.shopLevel = data.shopLevel;
          this.form.analysisAddress = data.address;

          if (form.shapeType === 1) {
            this.mapData = {
              fenceType: "circle",
              lng: form.lng,
              lat: form.lat,
              radius: form.radius,
            };
          } else if (form.shapeType === 4) {
            this.mapData = {
              fenceType: "area",
              areaCode: this.form.areaCode,
            };
          } else if (form.shapeType === 3) {
            if (data.lnglat) {
              this.polygonPath = data.lnglat.split("|").map((item) => {
                return [item.split(",")[0], item.split(",")[1]];
              });
            }

            console.log(this.polygonPath);
            this.mapData = {
              fenceType: "polygon",
              polygonPath: this.polygonPath,
            };
          }

          this.$refs.fenceMap.initFence(this.mapData);
        }
      });
    },
    updateMapData(data) {
      if (data.radius) {
        this.form.radius = data.radius;
      }
      if (data.lng && data.lat) {
        this.form.lng = data.lng;
        this.form.lat = data.lat;
        this.transfer();
      }

      if (data.polygonPath && data.polygonPath.length) {
        this.polygonPath = data.polygonPath;
      }
    },
    handleSpecialShopChange(val) {
      // 特约店级联选项
      this.form.specialShopName = val[1];
      if (val && val.length > 1) {
        this.$nextTick(() => {
          this.refresh = new Date().getTime();
          const thisNodes =
            this.$refs["specialShopCascader"].getCheckedNodes()[0];
          if (thisNodes && thisNodes.data) {
            const { data } = thisNodes;
            this.form.specialShopId = data.id;
            this.form.specialShopCode = data.code;
            this.form.specialShopName = data.label;
          }
        });
      }
    },
    updateFence() {
      this.$refs.fenceMap.updateFence({
        fenceType: "circle",
        lng: this.form.lng,
        lat: this.form.lat,
        radius: this.form.radius,
      });
      this.transfer();
    },
    validateLngLat() {
      let pass = true;
      this.$refs.shopForm.validateField("lng", (error) => {
        if (error) {
          pass = false;
        }
      });
      this.$refs.shopForm.validateField("lat", (error) => {
        if (error) {
          pass = false;
        }
      });
      this.$refs.shopForm.validateField("radius", (error) => {
        if (error) {
          pass = false;
        }
      });
      return pass;
    },
    transfer() {
      if (this.form.lng && this.form.lat && this.validateLngLat()) {
        this.transferTimeoutId && window.clearTimeout(this.transferTimeoutId);
        this.transferTimeoutId = window.setTimeout(() => {
          const flag = new Date();
          this.transferFlag = flag;
          if (!this.geocoder) {
            mapService.getGeocoder().then((gc) => {
              this.geocoder = gc;
              this.getAddress(flag);
            });
          } else {
            this.getAddress(flag);
          }
        }, 500);
      }
    },
    getAddress(flag) {
      const lnglatXY = [this.form.lng, this.form.lat];
      this.form.analysisAddress = "";
      this.geocoder.getAddress(lnglatXY, (status, result) => {
        if (flag == this.transferFlag) {
          if (status === "complete" && result.info === "OK") {
            //获取地址成功
            //console.log(result.regeocode.formattedAddress)
            this.form.analysisAddress = result.regeocode.formattedAddress;
            this.transferError = false;
            this.$refs.shopForm.validateField("analysisAddress");
            //this.$refs.fenceMap.updateMap(this.form)
          } else {
            this.transferError = true;
            this.$refs.shopForm.validateField("analysisAddress");
          }
        }
      });
    },
    getParams() {
      const form = this.form;
      const params = {};

      // params.fenceNo = form.fenceNo;
      params.fenceName = form.fenceName;
      // params.electricFenceType = form.electricFenceType;
      //params.electricTimeFenceType = form.electricTimeFenceType
      //params.startTime = form.startTime;
      //params.endTime = form.endTime;
      // params.shopName = form.specialShopName;
      params.specialShopId = form.specialShopId;
      params.shopLevel = form.shopLevel;
      // params.shapeType = form.shapeType;
      // params.des = form.des;
      // params.timeType = form.timeType;
      if (form.shapeType == 4) {
        params.areaCode = form.areaCode;
        params.areaString = form.areaString;
      } else if (form.shapeType == 1) {
        params.lnglat = `${form.lng},${form.lat}`;
        params.radius = form.radius;
        params.address = form.analysisAddress;
      } else if (form.shapeType == 3) {
        params.lnglat = this.polygonPath.join("|");
      }
      // params.bizFenceType = form.bizFenceType
      // params.status = form.status;
      // if(this.tableData){
      // 	params.snList = this.tableData
      // }else{
      // 	params.snList = this.userArr;
      // }
      // if(form.timeType==1){
      // 	params.startTime = moment().format('YYYY-MM-DD');
      // 	params.endTime = moment().add(10, 'year').format('YYYY-MM-DD');
      // }else{
      // 	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
      // }

      return params;
    },
    saveFence() {
      if (this.form.shapeType == 3) {
        if (this.polygonPath.length === 0) {
          this.$message({
            message: "请绘制线路",
            type: "error",
          });
          return;
        }
      }
      this.$refs.shopForm.validate((valid) => {
        if (valid) {
          this.submitting = true;
          if (typeof this.shopId != "undefined") {
            const params = this.getParams();
            params.id = this.shopId;

            updateFence(params).then((res) => {
              if (res.code == 200) {
                //this.closeDialog();
                this.$message({
                  message: "提交成功",
                  type: "success",
                });
                history.go(-1);
              } else {
                this.$message({
                  message: res.message,
                  type: "error",
                });
              }
              this.submitting = false;
            });
          } else {
            saveFence(this.getParams()).then((res) => {
              if (res.code == 200) {
                //this.closeDialog();
                this.$message({
                  message: "提交成功",
                  type: "success",
                });
                history.go(-1);
              } else {
                this.$message({
                  message: res.message,
                  type: "error",
                });
              }
              this.submitting = false;
            });
          }
        } else {
          return false;
        }
      });
    },
    cancel() {
      history.go(-1);
    },
  },
};
</script>

<style scoped>
.fenceModalfooter {
  margin: 0;
  padding: 20px;
  padding-top: 10px;
  text-align: right;
  box-sizing: border-box;
  z-index: 500;
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  border-top: 1px solid #e7eaec;
  background-color: #fdfdfd;
  margin-left: 220px;
  padding-right: 260px;
}
</style>