| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043 |
- <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="t('搜索位置')"
- :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" class="demo-ruleForm">
- <el-form-item :label="t('农场位置')" prop="farm_address">
- <div class="position-wrap">
- <el-input :placeholder="t('农场位置')" readonly v-model="ruleForm.farm_address"
- autocomplete="off" />
- </div>
- </el-form-item>
- <el-form-item :label="t('种植作物')" required>
- <div class="select-wrap">
- <div class="species-entry" @click="handleGoSelectCrop">
- <span class="species-entry__text">{{ selectedCropSummaryText || "点击选择作物" }}</span>
- </div>
- </div>
- </el-form-item>
- <el-form-item :label="t('农场面积')" prop="farm_area">
- <div class="area-box">
- <el-input :placeholder="isFromEditMap ? '勾选地块获得农场面积' : '请输入农场的真实面积'
- " v-model="ruleForm.farm_area" :readonly="isFromEditMap" type="text"
- autocomplete="off" style="width: fit-content" @input="handleMianjiInput"
- @keypress="handleMianjiKeypress" />
- <div class="unit">{{ t('亩') }}</div>
- </div>
- </el-form-item>
- <el-form-item :label="t('农场名称')" prop="farm_name">
- <el-input :placeholder="t('请输入您的农场名称')" v-model="ruleForm.farm_name"
- autocomplete="off" @input="handleFarmNameInput" />
- </el-form-item>
- </el-form>
- </div>
- <div class="create-btn">
- <div class="btn-item sencond-btn" @click="resetForm(ruleFormRef)">{{ t('取消') }}</div>
- <div
- class="btn-item primary-btn"
- :class="{ 'is-disabled': isSubmitting }"
- @click="submitForm(ruleFormRef)"
- >
- {{
- isSubmitting
- ? t('提交中...')
- : paramsType === "client"
- ? "添加"
- : paramsType === "edit"
- ? "确认修改"
- : "立即创建"
- }}
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
- <!-- 四大种植报告生成成功 -->
- <Popup v-model:show="showPlantingReportPopup" round class="planting-report-popup"
- :close-on-click-overlay="false">
- <div class="planting-report-popup__inner">
- <img class="icon" src="@/assets/img/home/right.png" alt="" />
- <div class="title">{{ t('您的四大种植报告') }}<br />{{ t('已全部生成') }}</div>
- <div class="links">
- <span class="link">{{ t('(作物长势报告)') }}</span>
- <span class="link">{{ t('(土壤改良报告)') }}</span>
- <span class="link">{{ t('(种植建议报告)') }}</span>
- </div>
- <div class="btn" @click="handlePlantingReportView">{{ t('点击查看') }}</div>
- </div>
- </Popup>
- </div>
- </template>
- <script setup>
- import { useI18n } from "@/i18n";
- const { t } = useI18n();
- 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, nextTick, onDeactivated } from "vue";
- import { useStore } from "vuex";
- import { convertPointToArray } from "@/utils/index";
- import wx from "weixin-js-sdk";
- import { Popup } from "vant";
- import { ElMessage } from "element-plus";
- import { transformFromGCJToWGS, transformFromWGSToGCJ } from "@/utils/WSCoordinate.js";
- import { selectNewlyCreatedFarm } from "@/utils/selectedFarm";
- /** 与 selectCrop.vue 写入的 sessionStorage 键一致 */
- const SESSION_SELECT_CROP_KEY = "selectedCrop";
- /** @returns {Array<object>|null} 已选作物品类列表,未选择或数据无效时为 null */
- function parseSelectedCropItems() {
- try {
- const raw = sessionStorage.getItem(SESSION_SELECT_CROP_KEY);
- if (!raw || raw === "null") return null;
- const data = JSON.parse(raw);
- if (!Array.isArray(data) || data.length === 0) return null;
- return data;
- } catch {
- return null;
- }
- }
- /** 入口展示:无选中为「点击选择作物」,有则 variety_name 去重后用顿号连接 */
- const selectedCropSummaryText = ref("");
- function refreshSelectedCropSummary() {
- const items = parseSelectedCropItems();
- if (!items?.length) {
- selectedCropSummaryText.value = "";
- return;
- }
- const seen = new Set();
- const ordered = [];
- for (const item of items) {
- const name = String(item.variety_name ?? "").trim();
- if (!name || seen.has(name)) continue;
- seen.add(name);
- ordered.push(name);
- }
- selectedCropSummaryText.value = ordered.join("、");
- }
- const route = useRoute();
- const router = useRouter();
- const store = useStore();
- const indexMap = new IndexMap();
- const mapContainer = ref(null);
- /** 创建成功后「四大种植报告」提示弹窗 */
- const showPlantingReportPopup = ref(false);
- function handlePlantingReportView() {
- showPlantingReportPopup.value = false;
- backgToHome();
- }
- // 标记是否从编辑地图页面确认返回
- const isFromEditMap = ref(false);
- // 标记用户是否手动修改过农场名称
- const isFarmNameManuallyModified = ref(false);
- /** 创建/提交接口请求中,防止重复点击 */
- const isSubmitting = ref(false);
- /**
- * 根据中心点和亩数生成正方形地块WKT
- * @param {Array} center - 中心点坐标 [lng, lat]
- * @param {Number} mu - 面积(亩)
- * @returns {Object} 包含WKT字符串和面积(亩)的对象
- */
- function generateSquarePolygonByMu(center, mu) {
- // 确保输入是数字类型
- const lng = parseFloat(center[0]);
- const lat = parseFloat(center[1]);
- // 1亩 ≈ 666.67平方米
- const areaInSquareMeters = mu * 666.67;
- // 正方形边长(米)
- const sideLength = Math.sqrt(areaInSquareMeters);
- // 半边长(米)
- const halfSide = sideLength / 2;
- // 纬度方向:1度约等于111000米
- const latDelta = halfSide / 111000;
- // 经度方向:需要根据纬度调整,1度约等于111000 * cos(lat)米
- const lngDelta = halfSide / (111000 * Math.cos((lat * Math.PI) / 180));
- // 计算四个顶点(逆时针顺序)
- const topLeft = [lng - lngDelta, lat + latDelta];
- const topRight = [lng + lngDelta, lat + latDelta];
- const bottomRight = [lng + lngDelta, lat - latDelta];
- const bottomLeft = [lng - lngDelta, lat - latDelta];
- // 生成MULTIPOLYGON格式的WKT(需要闭合,所以第一个点要重复)
- const formatPoint = (point) => `${point[0]} ${point[1]}`;
- const coordinates = [topLeft, topRight, bottomRight, bottomLeft, topLeft].map(formatPoint).join(", ");
- // 注意:MULTIPOLYGON 和括号之间需要有一个空格
- const wkt = `MULTIPOLYGON (((${coordinates})))`;
- return { wkt, area: parseFloat(mu).toFixed(2) };
- }
- onMounted(() => {
- // 清除上一次的地块数据,确保每次进入都是全新状态
- store.commit("home/SET_FARM_POLYGON", null);
- isFromEditMap.value = false; // 初始化时可以手动输入
- centerPoint.value = store.state.home.miniUserLocationPoint;
- indexMap.initMap(centerPoint.value, mapContainer.value);
- // 不再初始化时绘制默认地块,等待用户点击"新增地块"按钮
- // 清空地块和面积数据
- polygonArr.value = null;
- ruleForm.farm_area = "";
- if (route.query.type === "edit" && store.state.home.editFarmData) {
- populateEditData();
- }
- refreshSelectedCropSummary();
- });
- const polygonArr = ref(null);
- const paramsType = ref(null);
- /** 全新创建:带 isReload,且非编辑/小程序回流 */
- function isCreateFarmFullReload() {
- return (
- route.query.isReload &&
- route.query.type !== "edit" &&
- !route.query.miniJson
- );
- }
- onActivated(() => {
- paramsType.value = route.query.type;
- if (isCreateFarmFullReload()) {
- // 先清缓存与位置状态,避免沿用上次搜索的 centerPoint / 地址
- sessionStorage.removeItem("centerPoint");
- centerPoint.value = store.state.home.miniUserLocationPoint;
- locationVal.value = null;
- locationOptions.list = [];
- pointAddress.value = null;
- farmCity.value = null;
- polygonArr.value = null;
- isFromEditMap.value = false;
- isFarmNameManuallyModified.value = false;
- ruleForm.farm_address = "";
- ruleForm.farm_area = "";
- ruleForm.farm_name = "";
- ruleFormRef.value?.resetFields();
- store.commit("home/SET_FARM_POLYGON", null);
- isSubmitting.value = false;
- } else {
- const centerPointVal = sessionStorage.getItem("centerPoint")
- ? JSON.parse(sessionStorage.getItem("centerPoint"))
- : null;
- centerPoint.value = centerPointVal || store.state.home.miniUserLocationPoint;
- const arr = convertPointToArray(centerPoint.value);
- getLocationName(`${arr[1]},${arr[0]}`);
- }
- // 确保地图已初始化,使用 nextTick 等待 DOM 更新
- nextTick(() => {
- // 检查地图实例是否已初始化
- if (!indexMap.kmap) {
- // 如果地图未初始化,重新初始化
- if (mapContainer.value) {
- centerPoint.value = store.state.home.miniUserLocationPoint;
- const arr = convertPointToArray(centerPoint.value);
- indexMap.initMap(centerPoint.value, mapContainer.value);
- }
- // 等待地图初始化完成后再继续
- setTimeout(() => {
- handleMapUpdate();
- }, 100);
- return;
- }
- handleMapUpdate();
- });
- refreshSelectedCropSummary();
- });
- onDeactivated(() => {
- sessionStorage.setItem('centerPoint', JSON.stringify(centerPoint.value));
- });
- // 处理地图更新的逻辑
- function handleMapUpdate() {
- if (isCreateFarmFullReload()) {
- // 清除旧的地块数据
- store.commit("home/SET_FARM_POLYGON", null);
- isFromEditMap.value = false; // 从home进入,可以手动输入
- locationVal.value = null;
- centerPoint.value = store.state.home.miniUserLocationPoint;
- const arr = convertPointToArray(centerPoint.value);
- getLocationName(`${arr[1]},${arr[0]}`);
- indexMap.setMapPosition(arr);
- indexMap.clearLayer();
- // 不再自动生成默认地块,等待用户点击"新增地块"
- polygonArr.value = null;
- ruleForm.farm_area = "";
- return; // 直接返回,不执行下面的逻辑
- }
- indexMap.clearLayer();
- // 绘制勾画范围(从edit_map返回的情况)
- const polygonData = store.state.home.polygonData;
- // 优先处理从编辑地图页面返回的数据
- if (polygonData) {
- // 检查地块数据是否有效(数组存在且不为空)
- const hasValidGeometry =
- polygonData.geometryArr && Array.isArray(polygonData.geometryArr) && polygonData.geometryArr.length > 0;
- if (hasValidGeometry) {
- // 用户从edit_map返回,且有有效的地块数据
- // 根据 isConfirmed 判断是否锁定输入框
- if (polygonData.isConfirmed) {
- // 用户点击了"确认"按钮,锁定输入框并显示精确面积
- isFromEditMap.value = true;
- ruleForm.farm_area = polygonData.mianji;
- } else {
- // 用户点击了"取消",不锁定输入框,面积保持原样
- isFromEditMap.value = false;
- // 面积输入框保持之前的值,不更新
- }
- // 使用 nextTick 确保地图渲染完成后再设置地块
- nextTick(() => {
- indexMap.setAreaGeometry(polygonData.geometryArr);
- });
- polygonArr.value = polygonData.geometryArr;
- } else {
- // 用户在编辑页面删除了所有地块
- // 重置所有状态
- polygonArr.value = null;
- ruleForm.farm_area = "";
- isFromEditMap.value = false;
- }
- } else if (route.query.type === "edit" && store.state.home.editFarmData) {
- // 如果是编辑模式且没有从编辑地图返回的数据,回填原始编辑数据
- populateEditData();
- } else if (centerPoint.value && polygonArr.value) {
- // 没有新的编辑数据,保持当前地块
- // 同样需要检查数据有效性
- if (Array.isArray(polygonArr.value) && polygonArr.value.length > 0) {
- nextTick(() => {
- indexMap.setAreaGeometry(polygonArr.value);
- });
- }
- }
- }
- // 搜索
- 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.farm_address = v.item?.title || v.item?.address;
- pointAddress.value = v.item?.province + v.item?.city + (v.item?.district || '');
- // 更新farmCity以便后续更新农场名称
- farmCity.value = v.item?.city + (v.item?.district || "");
- // 地址修改后,如果满足条件则自动更新农场名称
- updateFarmNameIfNeeded();
- };
- // 表单
- const ruleFormRef = ref(null);
- const ruleForm = reactive({
- farm_address: "",
- farm_area: "",
- farm_name: "",
- });
- // 自定义验证规则:验证面积必须是大于0的数字
- const validateMianji = (rule, value, callback) => {
- if (!value) {
- callback(new Error("请输入农场面积"));
- } else {
- const num = parseFloat(value);
- if (isNaN(num)) {
- callback(new Error("面积必须是数字"));
- } else if (num <= 0) {
- callback(new Error("面积必须大于0"));
- } else {
- callback();
- }
- }
- };
- const rules = reactive({
- farm_address: [{ required: true, message: "请选择农场位置", trigger: "blur" }],
- farm_area: [
- { required: true, message: "请输入农场面积", trigger: "blur" },
- { validator: validateMianji, trigger: ["blur", "change"] },
- ],
- farm_name: [{ required: true, message: "请输入您的农场名称", trigger: ["blur", "change"] }],
- });
- const submitForm = (formEl) => {
- if (!formEl || isSubmitting.value) return;
- formEl.validate((valid) => {
- if (!valid) return;
- const cropItems = parseSelectedCropItems();
- if (!cropItems) {
- ElMessage.warning({ message: t('请先点击「种植作物」选择作物品类'), type: 'warning' });
- return;
- }
- const params = {
- ...ruleForm,
- farm_location: centerPoint.value,
- varieties: cropItems.map(
- (item) => `${item.variety_code}-${item.categorycode}`,
- ),
- farm_polygon:
- polygonArr.value && polygonArr.value.length > 0
- ? polygonArr.value[0]
- : null,
- };
- if (route.query.type === "edit" && route.query.farmId) {
- params.id = route.query.farmId;
- }
- if (route.query.type !== "edit") {
- const queryParams = { ...params };
- if (Array.isArray(queryParams.geom)) {
- queryParams.geom = JSON.stringify(queryParams.geom);
- }
- isSubmitting.value = true;
- VE_API.farm.createFarm(queryParams)
- .then(async (res) => {
- if (res?.code === 200) {
- // 创建成功后拉列表,选中最后一项(新建农场)
- await selectNewlyCreatedFarm();
- showPlantingReportPopup.value = true;
- sessionStorage.removeItem(SESSION_SELECT_CROP_KEY);
- }
- })
- .finally(() => {
- isSubmitting.value = false;
- });
- }
- });
- };
- const resetForm = (formEl) => {
- if (!formEl) return;
- formEl.resetFields();
- // 清除地块数据
- store.commit("home/SET_FARM_POLYGON", null);
- polygonArr.value = null;
- isFromEditMap.value = false;
- // 根据来源页面决定返回目标
- if (route.query.from && route.query.from !== "details") {
- router.replace(`/${route.query.from}`);
- return;
- }
- router.go(-1);
- };
- const centerPoint = ref(null);
- function handleGoSelectCrop() {
- router.push(`/select_crop`);
- }
- const pointAddress = ref(null);
- const farmCity = ref(null);
- function getLocationName(location) {
- const params = {
- key: MAP_KEY,
- location,
- };
- VE_API.old_mini_map.location(params).then(({ result }) => {
- const add = result.formatted_addresses?.recommend ? result.formatted_addresses.recommend : result.address + "";
- ruleForm.farm_address = add;
- pointAddress.value = result.address;
- farmCity.value = result.address_component?.city + (result.address_component?.district || "");
- // 地址修改后,如果满足条件则自动更新农场名称
- updateFarmNameIfNeeded();
- });
- }
- watch(() => mapLocation.data, (newValue) => {
- if (newValue) {
- let { latitude, longitude } = transformFromWGSToGCJ(newValue[1], newValue[0]);
- centerPoint.value = `POINT (${newValue[0]} ${newValue[1]})`;
- getLocationName(`${latitude},${longitude}`);
- }
- });
- function backgToHome() {
- ruleFormRef.value?.resetFields();
- sessionStorage.removeItem(SESSION_SELECT_CROP_KEY);
- // 根据来源页面决定返回目标
- const fromPage = route.query?.from;
- if (route.query.miniJson) {
- const json = JSON.parse(route.query.miniJson);
- if (json.isMini) {
- const dropdownGardenItem = ref({
- organId: json.farmId,
- periodId: json.periodId,
- name: json.name,
- page: "create_farm",
- showFarmSelect: true,
- images: json.images,
- });
- wx.miniProgram.reLaunch({
- url: `/pages/subPages/new_recognize/index?gardenData=${JSON.stringify(dropdownGardenItem.value)}`,
- });
- }
- json?.from && router.replace(`/${json.from}`);
- } else {
- if (fromPage && fromPage !== "details") {
- if (route.query?.type === "farmer") {
- router.go(-1);
- } else {
- router.replace(`/${fromPage}`);
- }
- return;
- } else {
- router.go(-1);
- }
- }
- }
- // 处理面积按键输入 - 只允许数字和小数点
- function handleMianjiKeypress(event) {
- // 如果是从编辑地图返回的,不允许手动修改
- if (isFromEditMap.value) {
- event.preventDefault();
- return;
- }
- const charCode = event.which ? event.which : event.keyCode;
- const char = String.fromCharCode(charCode);
- // 允许数字 (0-9) 和小数点 (.)
- if (!/^[0-9.]$/.test(char)) {
- event.preventDefault();
- return;
- }
- // 如果已经有小数点,不允许再输入小数点
- const currentValue = ruleForm.farm_area || "";
- if (char === "." && currentValue.includes(".")) {
- event.preventDefault();
- return;
- }
- // 不允许以小数点开头
- if (char === "." && !currentValue) {
- event.preventDefault();
- return;
- }
- }
- // 处理农场名称输入
- function handleFarmNameInput() {
- // 标记用户已手动修改过农场名称
- isFarmNameManuallyModified.value = true;
- }
- // 根据地址更新农场名称(如果满足条件)
- function updateFarmNameIfNeeded() {
- // 创建模式且未手动改过名称时,地址变化后同步更新(含已有自动名称的场景)
- if (route.query.type !== "edit" && !isFarmNameManuallyModified.value && farmCity.value) {
- ruleForm.farm_name = farmCity.value + "农场";
- }
- }
- // 回填编辑数据
- function populateEditData() {
- const editData = store.state.home.editFarmData;
- if (!editData) {
- return;
- }
- // 回填基本信息
- ruleForm.farm_name = editData.name || "";
- ruleForm.farm_area = editData.mianji || "";
- ruleForm.farm_address = editData.address || "";
- // 设置地图中心点
- if (editData.pointWkt) {
- centerPoint.value = editData.pointWkt;
- const arr = convertPointToArray(editData.pointWkt);
- indexMap.setMapPosition(arr);
- }
- // 处理地块数据
- if (editData.geomWkt) {
- polygonArr.value = [editData.geomWkt];
- indexMap.setAreaGeometry([editData.geomWkt]);
- isFromEditMap.value = true; // 编辑模式下锁定面积输入
- }
- // 设置地址信息
- if (editData.district) {
- try {
- const districtInfo = JSON.parse(editData.district);
- pointAddress.value = districtInfo.province + districtInfo.city + (districtInfo?.district || '');
- } catch (e) {
- console.warn("解析地址信息失败:", e);
- }
- }
- }
- // 处理面积输入
- let mianjiInputTimer = null;
- function handleMianjiInput(value) {
- // 如果是从编辑地图返回的,不允许手动修改
- if (isFromEditMap.value) {
- return;
- }
- // 过滤非法字符,只保留数字和小数点
- let filteredValue = value.replace(/[^\d.]/g, "");
- // 确保只有一个小数点
- const parts = filteredValue.split(".");
- if (parts.length > 2) {
- filteredValue = parts[0] + "." + parts.slice(1).join("");
- }
- // 限制小数点后最多2位
- if (parts.length === 2 && parts[1].length > 2) {
- filteredValue = parts[0] + "." + parts[1].substring(0, 2);
- }
- // 更新输入框的值(如果被过滤了)
- if (filteredValue !== value) {
- ruleForm.farm_area = filteredValue;
- return;
- }
- // 清除之前的定时器
- if (mianjiInputTimer) {
- clearTimeout(mianjiInputTimer);
- }
- // 防抖处理,用户停止输入500ms后再更新地块
- mianjiInputTimer = setTimeout(() => {
- const mu = parseFloat(filteredValue);
- // 验证输入的有效性
- if (!mu || isNaN(mu) || mu <= 0) {
- return;
- }
- // 根据亩数重新生成地块
- if (centerPoint.value) {
- const arr = convertPointToArray(centerPoint.value);
- const squareData = generateSquarePolygonByMu(arr, mu);
- const geometryData = {
- geometryArr: [squareData.wkt],
- mianji: squareData.area,
- };
- // 清除旧地块
- indexMap.clearLayer();
- // 绘制新地块
- indexMap.setAreaGeometry(geometryData.geometryArr);
- // 更新状态
- polygonArr.value = geometryData.geometryArr;
- }
- }, 500);
- }
- </script>
- <style lang="scss" scoped>
- ::v-deep {
- .el-form-item--default {
- margin-bottom: 20px;
- }
- }
- .create-farm {
- position: relative;
- width: 100%;
- height: 100vh;
- overflow: hidden;
- .map-container {
- width: 100%;
- height: calc(100% - 320px);
- }
- .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: 100%;
- .species-entry {
- display: inline-flex;
- align-items: center;
- color: #000;
- cursor: pointer;
- }
- }
- ::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;
- }
- }
- .area-box {
- display: flex;
- align-items: center;
- width: 100%;
- .unit {
- padding-right: 10px;
- }
- }
- .position-wrap {
- width: 100%;
- display: flex;
- justify-content: space-between;
- align-items: center;
- }
- }
- .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;
- &.is-disabled {
- opacity: 0.6;
- pointer-events: none;
- }
- }
- }
- .btn-item+.btn-item {
- margin-left: 5px;
- }
- }
- }
- }
- }
- .planting-report-popup {
- width: 82%;
- padding: 28px 20px 20px;
- .planting-report-popup__inner {
- text-align: center;
- .icon {
- width: 68px;
- height: 68px;
- margin-bottom: 16px;
- }
- .title {
- font-size: 24px;
- }
- .links {
- display: flex;
- flex-wrap: wrap;
- justify-content: center;
- gap: 10px 16px;
- margin: 18px 0 22px;
- .link {
- font-size: 18px;
- color: #2199f8;
- }
- }
- .btn {
- padding: 8px;
- border-radius: 25px;
- font-size: 16px;
- color: #fff;
- background: #2199F8;
- }
- }
- }
- </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>
|