123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591 |
- <template>
- <div class="create-farm">
- <custom-header :name="paramsType === 'client' ? '新增用户' : paramsType === 'edit' ? '编辑农场': '创建农场'" :isGoBack="true" @goback="backgToHome"></custom-header>
- <!-- 地图 -->
- <div class="map-container" ref="mapContainer"></div>
- <div class="farm-content">
- <div class="top-mask"></div>
- <div class="farm-filter">
- <el-select
- v-model="locationVal"
- filterable
- remote
- reserve-keyword
- placeholder="搜索位置"
- :remote-method="remoteMethod"
- :loading="loading"
- @change="handleSearchRes"
- popper-class="location-search-popper"
- >
- <el-option
- v-for="(item, index) in locationOptions.list"
- :key="index"
- :label="item.title"
- :value="{ value: item.point, item }"
- >
- <span>{{ item.title }}</span>
- <span class="sub-title">{{ item.province }}{{ item.city }}{{ item.district }}</span>
- </el-option>
- </el-select>
- </div>
- <!-- 创建 -->
- <div class="create-wrap">
- <div class="create-box">
- <div class="box-content">
- <div class="create-title">
- <img class="title-icon" src="@/assets/img/home/create-icon.png" alt="" />
- {{ paramsType === 'client' ? "新增用户" : paramsType === 'edit' ?"编辑农场":"创建农场" }}
- </div>
- <div class="create-content">
- <div class="create-from">
- <el-form
- ref="ruleFormRef"
- :model="ruleForm"
- :rules="rules"
- label-width="auto"
- class="demo-ruleForm"
- >
- <el-form-item label="农场位置" prop="address">
- <div class="position-wrap">
- <el-input
- placeholder="农场位置"
- readonly
- v-model="ruleForm.address"
- autocomplete="off"
- />
- <div class="draw-btn" @click="toSubPage">点击勾选地块</div>
- </div>
- </el-form-item>
- <el-form-item label="农场面积" prop="mianji">
- <div class="area-box">
- <el-input
- placeholder="勾选地块获得农场面积"
- v-model="ruleForm.mianji"
- autocomplete="off"
- style="width: fit-content"
- />
- <div class="unit">亩</div>
- </div>
- </el-form-item>
- <el-form-item label="种植作物" prop="speciesItem">
- <div class="select-wrap">
- <el-select
- @change="changeSpecie"
- class="select-item"
- v-model="ruleForm.speciesItem"
- placeholder="作物类型"
- >
- <el-option
- v-for="(item, index) in specieList"
- :key="index"
- :label="item.name"
- :value="{ value: item.id, ...item }"
- />
- </el-select>
- <el-select
- v-model="ruleForm.phenologyId"
- placeholder="物候期"
- class="period-select select-item"
- >
- <el-option
- v-for="(item, index) in phenologyList"
- :key="index"
- :label="item.name"
- :value="item.id"
- />
- </el-select>
- </div>
- </el-form-item>
- <el-form-item label="农场名称" prop="name">
- <el-input
- placeholder="请输入您的农场名称"
- v-model="ruleForm.name"
- autocomplete="off"
- />
- </el-form-item>
- </el-form>
- </div>
- <div class="create-btn">
- <div class="btn-item sencond-btn" @click="resetForm(ruleFormRef)">取消</div>
- <div class="btn-item primary-btn" @click="submitForm(ruleFormRef)">
- {{ paramsType === 'client' ? "添加" : paramsType === 'edit' ?"确认修改":"立即创建" }}
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
- </template>
- <script setup>
- import customHeader from "@/components/customHeader.vue";
- import IndexMap from "./map/index.js";
- import { useRoute, useRouter } from "vue-router";
- import { mapLocation } from "./map/index.js";
- import { onMounted, ref, reactive, watch, onActivated } from "vue";
- import { useStore } from "vuex";
- import { convertPointToArray } from "@/utils/index";
- import { ElMessage } from "element-plus";
- import { transformFromGCJToWGS, transformFromWGSToGCJ } from "@/utils/WSCoordinate.js";
- const route = useRoute();
- const router = useRouter();
- const store = useStore();
- const indexMap = new IndexMap();
- const mapContainer = ref(null);
- onMounted(() => {
- centerPoint.value = store.state.home.miniUserLocationPoint;
- const arr = convertPointToArray(centerPoint.value);
- getLocationName(`${arr[1]},${arr[0]}`);
- indexMap.initMap(centerPoint.value, mapContainer.value);
- getSpecieList();
- });
- const polygonArr = ref(null);
- const paramsType = ref(null);
- onActivated(() => {
- paramsType.value = route.query.type;
- if (route.query.isFromHome) {
- centerPoint.value = store.state.home.miniUserLocationPoint;
- const arr = convertPointToArray(centerPoint.value);
- getLocationName(`${arr[1]},${arr[0]}`);
- indexMap.setMapPosition(arr);
- }
- indexMap.clearLayer();
- // 绘制勾画范围
- const polygonData = store.state.home.polygonData;
- if (polygonData) {
- indexMap.setAreaGeometry(polygonData?.geometryArr);
- polygonArr.value = polygonData.geometryArr;
- ruleForm.mianji = polygonData.mianji;
- }
- });
- // 搜索
- const MAP_KEY = "CZLBZ-LJICQ-R4A5J-BN62X-YXCRJ-GNBUT";
- const locationVal = ref(null);
- const locationOptions = reactive({
- list: [],
- });
- const loading = ref(false);
- const remoteMethod = async (keyword) => {
- if (keyword) {
- locationOptions.list = [];
- loading.value = true;
- const params = {
- key: MAP_KEY,
- keyword,
- location: route.query.userLocation || "113.61702297075017,23.584863449735067",
- };
- await VE_API.old_mini_map.getCtiyList({ word: keyword }).then(({ data }) => {
- if (data && data.length) {
- data.forEach((item) => {
- item.point = item.location.lat + "," + item.location.lng;
- locationOptions.list.push(item);
- });
- }
- });
- VE_API.old_mini_map.search(params).then(({ data }) => {
- loading.value = false;
- data.forEach((item) => {
- item.point = item.location.lat + "," + item.location.lng;
- locationOptions.list.push(item);
- });
- });
- } else {
- locationOptions.list = [];
- }
- };
- const handleSearchRes = (v) => {
- const parts = v.value.split(",");
- let { latitude, longitude } = transformFromGCJToWGS(parseFloat(parts[0]), parseFloat(parts[1]));
- const coordinateArray = [longitude, latitude];
- indexMap.setMapPosition(coordinateArray);
- centerPoint.value = `POINT (${coordinateArray[0]} ${coordinateArray[1]})`;
- ruleForm.address = v.item?.title || v.item?.address;
- pointAddress.value = v.item?.province + v.item?.city + v.item?.district;
- };
- // 表单
- const ruleFormRef = ref(null);
- const ruleForm = reactive({
- address: "",
- mianji: "",
- speciesItem: null,
- phenologyId: "",
- name: "",
- });
- const rules = reactive({
- address: [{ required: true, message: "请选择农场位置", trigger: "blur" }],
- mianji: [{ required: true, message: "请勾选地块获得农场面积", trigger: "blur" }],
- speciesItem: [{ required: true, message: "请选择品类", trigger: "blur" }],
- phenologyId: [{ required: true, message: "请选择物候期", trigger: "blur" }],
- name: [{ required: true, message: "请输入您的农场名称", trigger: "blur" }],
- });
- const submitForm = (formEl) => {
- if (!formEl) return;
- formEl.validate((valid) => {
- if (valid) {
- const params = {
- ...ruleForm,
- wkt: centerPoint.value,
- speciesId: ruleForm.speciesItem?.id,
- containerId: ruleForm.speciesItem?.defaultContainerId,
- geom: polygonArr.value,
- };
- VE_API.farm.saveFarm(params).then((res) => {
- ElMessage.success("创建成功");
- // 重置表单
- ruleFormRef.value.resetFields();
- store.commit("home/SET_FARM_POLYGON", null);
- router.replace("/home?reload=true");
- });
- console.log("submit!", params);
- } else {
- console.log("error submit!");
- }
- });
- };
- const resetForm = (formEl) => {
- if (!formEl) return;
- formEl.resetFields();
- store.commit("home/SET_FARM_POLYGON", null);
- router.replace("/home?reload=true");
- };
- const centerPoint = ref(null);
- function toSubPage() {
- router.push(
- `/edit_map?mapCenter=${centerPoint.value}&pointName=${ruleForm.address}&pointAddress=${pointAddress.value}`
- );
- }
- const pointAddress = ref(null);
- function getLocationName(location) {
- const params = {
- key: MAP_KEY,
- location,
- };
- VE_API.old_mini_map.location(params).then(({ result }) => {
- // locationVal.value = result.formatted_addresses.recommend;
- const add = result.formatted_addresses?.recommend ? result.formatted_addresses.recommend : result.address + "";
- ruleForm.address = add;
- pointAddress.value = result.address;
- });
- }
- watch(
- () => mapLocation.data,
- (newValue, oldValue) => {
- if (newValue) {
- let { latitude, longitude } = transformFromWGSToGCJ(newValue[1], newValue[0]);
- centerPoint.value = `POINT (${newValue[0]} ${newValue[1]})`;
- getLocationName(`${latitude},${longitude}`);
- }
- }
- );
- const specieList = ref([]);
- function getSpecieList() {
- VE_API.farm.fetchSpecieList().then(({ data }) => {
- specieList.value = data;
- });
- }
- function changeSpecie(v) {
- getPhenology(v.defaultContainerId);
- }
- const phenologyList = ref([]);
- function getPhenology(containerId) {
- VE_API.farm.fetchPhenologyList({ containerId }).then(({ data }) => {
- phenologyList.value = data;
- });
- }
- function backgToHome() {
- router.replace("/home")
- }
- </script>
- <style lang="scss" scoped>
- .create-farm {
- position: relative;
- width: 100%;
- height: 100vh;
- overflow: hidden;
- .map-container {
- width: 100%;
- height: calc(100% - 240px);
- }
- .farm-content {
- position: absolute;
- top: 40px;
- left: 0;
- width: 100%;
- height: calc(100% - 40px);
- pointer-events: none;
- z-index: 2;
- .top-mask {
- height: 100px;
- position: absolute;
- z-index: 2;
- top: 0;
- left: 0;
- width: 100%;
- background: linear-gradient(0deg, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.6) 100%);
- }
- }
- .farm-filter {
- pointer-events: all;
- margin: 12px;
- position: relative;
- z-index: 10;
- background: rgba(0, 0, 0, 0.3);
- border-radius: 20px;
- border: 1px solid rgba(255, 255, 255, 0.4);
- &::before {
- content: "";
- position: absolute;
- left: 12px;
- top: 9px;
- width: 14px;
- height: 14px;
- background: url("@/assets/img/home/search.png") no-repeat center center / 100% 100%;
- }
- ::v-deep {
- .el-select__wrapper {
- background: none;
- box-shadow: none;
- padding-left: 34px;
- font-size: 12px;
- .el-select__selected-item,
- .el-select__placeholder,
- .el-select__input {
- color: rgba(255, 255, 255, 0.6);
- &.is-transparent {
- color: #ccc;
- font-size: 12px;
- }
- }
- }
- .el-select {
- transition: all 0.3s;
- .el-input.is-focus .el-input__wrapper {
- box-shadow: none !important;
- }
- }
- .el-input {
- .el-input__wrapper {
- background: none;
- box-shadow: none;
- padding-left: 18px;
- font-size: 11px;
- .el-input__inner {
- color: rgba(255, 255, 255, 0.6);
- }
- &.is-focus {
- .el-input__inner {
- color: #ccc;
- font-size: 11px;
- }
- }
- }
- }
- }
- }
- .create-wrap {
- position: absolute;
- bottom: 0px;
- left: 0;
- width: 100%;
- background: linear-gradient(180deg, transparent 0%, #f5f7fb 20%);
- }
- .create-box {
- pointer-events: all;
- margin: 0 12px 10px 12px;
- width: calc(100% - 24px);
- background: #e0f1fe;
- border-radius: 14px;
- .box-content {
- position: relative;
- &::after {
- position: absolute;
- right: 10px;
- top: 2px;
- content: "";
- width: 79px;
- height: 72px;
- background: url("@/assets/img/home/creat-bg.png") no-repeat center / 100% 100%;
- }
- }
- .create-title {
- display: flex;
- align-items: center;
- padding: 12px 6px 12px 12px;
- color: #0089f5;
- font-size: 18px;
- font-weight: bold;
- .title-icon {
- width: 18px;
- padding-right: 10px;
- }
- }
- .create-content {
- background: #fff;
- border-radius: 14px;
- padding: 12px;
- position: relative;
- z-index: 2;
- .create-from {
- .select-wrap {
- display: flex;
- // width: 86%;
- ::v-deep {
- .el-input__wrapper {
- background: none;
- box-shadow: none;
- }
- .el-input__inner {
- font-size: 14px;
- color: rgba(0, 0, 0, 0.5);
- }
- .el-select__wrapper {
- background: none;
- box-shadow: none;
- gap: 2px;
- padding: 4px 2px;
- justify-content: center;
- }
- .el-select__selection {
- flex: none;
- width: fit-content;
- }
- .el-select__placeholder {
- color: #000;
- position: static;
- transform: none;
- width: fit-content;
- }
- }
- // .select-item {
- // width: fit-content;
- // }
- .period-select {
- margin-left: 6px;
- }
- .select-item {
- min-width: 76px;
- }
- }
- ::v-deep {
- .el-form-item__label {
- color: #000;
- }
- .el-form-item__error {
- top: 117%;
- }
- .el-form-item {
- position: relative;
- &::after {
- content: "";
- position: absolute;
- left: 60px;
- bottom: -5px;
- width: calc(100% - 60px);
- height: 1px;
- background: rgba(0, 0, 0, 0.08);
- }
- }
- .el-input__wrapper {
- box-shadow: none;
- padding: 1px 6px;
- // border-bottom: 1px solid rgba(0, 0, 0, 0.08);
- }
- }
- .area-box {
- display: flex;
- align-items: center;
- width: 100%;
- .unit {
- padding-right: 10px;
- }
- }
- .position-wrap {
- display: flex;
- justify-content: space-between;
- align-items: center;
- .draw-btn {
- flex: none;
- padding: 0 12px;
- height: 30px;
- line-height: 30px;
- box-sizing: border-box;
- color: #2199f8;
- border: 1px solid #2199f8;
- background: rgba(33, 153, 248, 0.1);
- border-radius: 20px;
- font-size: 12px;
- }
- }
- }
- .create-btn {
- display: flex;
- align-items: center;
- width: 100%;
- padding-top: 10px;
- .btn-item {
- flex: 1;
- text-align: center;
- padding: 0 11px;
- height: 40px;
- line-height: 40px;
- border-radius: 34px;
- font-size: 16px;
- box-sizing: border-box;
- &.sencond-btn {
- border: 1px solid rgba(153, 153, 153, 0.5);
- color: #666666;
- }
- &.primary-btn {
- background: linear-gradient(180deg, #76c3ff, #2199f8);
- color: #fff;
- }
- }
- .btn-item + .btn-item {
- margin-left: 5px;
- }
- }
- }
- }
- }
- </style>
- <style lang="scss">
- .location-search-popper {
- .el-select-dropdown__list {
- max-width: 96vw;
- overflow-x: auto;
- }
- .sub-title {
- padding-left: 6px;
- font-size: 12px;
- color: #ccc;
- }
- }
- </style>
|