index.vue 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. <template>
  2. <div class="create-farm">
  3. <custom-header :name="paramsType === 'client' ? '新增用户' : paramsType === 'edit' ? '编辑农场': '创建农场'" :isGoBack="true" @goback="backgToHome"></custom-header>
  4. <!-- 地图 -->
  5. <div class="map-container" ref="mapContainer"></div>
  6. <div class="farm-content">
  7. <div class="top-mask"></div>
  8. <div class="farm-filter">
  9. <el-select
  10. v-model="locationVal"
  11. filterable
  12. remote
  13. reserve-keyword
  14. placeholder="搜索位置"
  15. :remote-method="remoteMethod"
  16. :loading="loading"
  17. @change="handleSearchRes"
  18. popper-class="location-search-popper"
  19. >
  20. <el-option
  21. v-for="(item, index) in locationOptions.list"
  22. :key="index"
  23. :label="item.title"
  24. :value="{ value: item.point, item }"
  25. >
  26. <span>{{ item.title }}</span>
  27. <span class="sub-title">{{ item.province }}{{ item.city }}{{ item.district }}</span>
  28. </el-option>
  29. </el-select>
  30. </div>
  31. <!-- 创建 -->
  32. <div class="create-wrap">
  33. <div class="create-box">
  34. <div class="box-content">
  35. <div class="create-title">
  36. <img class="title-icon" src="@/assets/img/home/create-icon.png" alt="" />
  37. {{ paramsType === 'client' ? "新增用户" : paramsType === 'edit' ?"编辑农场":"创建农场" }}
  38. </div>
  39. <div class="create-content">
  40. <div class="create-from">
  41. <el-form
  42. ref="ruleFormRef"
  43. :model="ruleForm"
  44. :rules="rules"
  45. class="demo-ruleForm"
  46. >
  47. <el-form-item label="农场位置" prop="address">
  48. <div class="position-wrap">
  49. <el-input
  50. placeholder="农场位置"
  51. readonly
  52. v-model="ruleForm.address"
  53. autocomplete="off"
  54. />
  55. <div class="draw-btn" @click="toSubPage">{{ hasDefaultPolygon ? '点击勾选地块' : '新增地块' }}</div>
  56. </div>
  57. </el-form-item>
  58. <el-form-item label="种植作物" prop="speciesItem">
  59. <div class="select-wrap">
  60. <el-select
  61. @change="changeSpecie"
  62. class="select-item"
  63. v-model="ruleForm.speciesItem"
  64. placeholder="品类"
  65. >
  66. <el-option
  67. v-for="(item, index) in specieList"
  68. :key="index"
  69. :label="item.name"
  70. :value="{ value: item.id, ...item }"
  71. />
  72. </el-select>
  73. <el-select
  74. v-model="ruleForm.typeId"
  75. placeholder="品种"
  76. @change="changeTypeId"
  77. class="period-select select-item"
  78. >
  79. <el-option
  80. v-for="(item, index) in fruitsList"
  81. :key="index"
  82. :label="item.name"
  83. :value="item.id"
  84. />
  85. </el-select>
  86. </div>
  87. </el-form-item>
  88. <el-form-item label="联系人" prop="fzr">
  89. <div class="area-box">
  90. <el-input
  91. placeholder="请输入联系人姓名"
  92. v-model="ruleForm.fzr"
  93. autocomplete="off"
  94. style="width: fit-content"
  95. />
  96. </div>
  97. </el-form-item>
  98. <el-form-item label="联系电话" prop="tel">
  99. <div class="area-box">
  100. <el-input
  101. placeholder="请输入联系人电话"
  102. v-model="ruleForm.tel"
  103. autocomplete="off"
  104. style="width: fit-content"
  105. />
  106. </div>
  107. </el-form-item>
  108. <el-form-item label="农场面积" prop="mu">
  109. <div class="area-box">
  110. <el-input
  111. :placeholder="isFromEditMap ? '勾选地块获得农场面积' : '请输入农场的真实面积'"
  112. v-model="ruleForm.mu"
  113. :readonly="isFromEditMap"
  114. type="text"
  115. autocomplete="off"
  116. style="width: fit-content"
  117. @input="handleMianjiInput"
  118. @keypress="handleMianjiKeypress"
  119. />
  120. <div class="unit">亩</div>
  121. </div>
  122. </el-form-item>
  123. <el-form-item label="农场名称" prop="name">
  124. <el-input
  125. placeholder="请输入您的农场名称"
  126. v-model="ruleForm.name"
  127. autocomplete="off"
  128. />
  129. </el-form-item>
  130. </el-form>
  131. </div>
  132. <div class="create-btn">
  133. <div class="btn-item sencond-btn" @click="resetForm(ruleFormRef)">取消</div>
  134. <div class="btn-item primary-btn" @click="submitForm(ruleFormRef)">
  135. {{ paramsType === 'client' ? "添加" : paramsType === 'edit' ?"确认修改":"立即创建" }}
  136. </div>
  137. </div>
  138. </div>
  139. </div>
  140. </div>
  141. </div>
  142. </div>
  143. </div>
  144. </template>
  145. <script setup>
  146. import customHeader from "@/components/customHeader.vue";
  147. import IndexMap from "./map/index.js";
  148. import { useRoute, useRouter } from "vue-router";
  149. import { mapLocation } from "./map/index.js";
  150. import { onMounted, ref, reactive, watch, onActivated } from "vue";
  151. import { useStore } from "vuex";
  152. import { convertPointToArray } from "@/utils/index";
  153. import { ElMessage } from "element-plus";
  154. import { transformFromGCJToWGS, transformFromWGSToGCJ } from "@/utils/WSCoordinate.js";
  155. const route = useRoute();
  156. const router = useRouter();
  157. const store = useStore();
  158. const indexMap = new IndexMap();
  159. const mapContainer = ref(null);
  160. // 标记是否从编辑地图页面确认返回
  161. const isFromEditMap = ref(false);
  162. // 标记是否已创建默认地块
  163. const hasDefaultPolygon = ref(false);
  164. /**
  165. * 根据中心点生成指定边长的正方形地块WKT
  166. * @param {Array} center - 中心点坐标 [lng, lat]
  167. * @param {Number} sideLength - 正方形边长(米)
  168. * @returns {Object} 包含WKT字符串和面积(亩)的对象
  169. */
  170. function generateSquarePolygonBySideLength(center, sideLength) {
  171. // 确保输入是数字类型
  172. const lng = parseFloat(center[0]);
  173. const lat = parseFloat(center[1]);
  174. // 半边长(米)
  175. const halfSide = sideLength / 2;
  176. // 纬度方向:1度约等于111000米
  177. const latDelta = halfSide / 111000;
  178. // 经度方向:需要根据纬度调整,1度约等于111000 * cos(lat)米
  179. const lngDelta = halfSide / (111000 * Math.cos(lat * Math.PI / 180));
  180. // 计算四个顶点(逆时针顺序)
  181. const topLeft = [lng - lngDelta, lat + latDelta];
  182. const topRight = [lng + lngDelta, lat + latDelta];
  183. const bottomRight = [lng + lngDelta, lat - latDelta];
  184. const bottomLeft = [lng - lngDelta, lat - latDelta];
  185. // 生成MULTIPOLYGON格式的WKT(需要闭合,所以第一个点要重复)
  186. // 明确使用join来格式化坐标点
  187. const formatPoint = (point) => `${point[0]} ${point[1]}`;
  188. const coordinates = [topLeft, topRight, bottomRight, bottomLeft, topLeft]
  189. .map(formatPoint)
  190. .join(', ');
  191. // 注意:MULTIPOLYGON 和括号之间需要有一个空格
  192. const wkt = `MULTIPOLYGON (((${coordinates})))`;
  193. // 计算面积(亩): 边长200米 = 40,000平方米 ≈ 60亩
  194. const areaInSquareMeters = sideLength * sideLength;
  195. const areaInMu = (areaInSquareMeters / 666.67).toFixed(2);
  196. return { wkt, area: areaInMu };
  197. }
  198. /**
  199. * 根据中心点和亩数生成正方形地块WKT
  200. * @param {Array} center - 中心点坐标 [lng, lat]
  201. * @param {Number} mu - 面积(亩)
  202. * @returns {Object} 包含WKT字符串和面积(亩)的对象
  203. */
  204. function generateSquarePolygonByMu(center, mu) {
  205. // 确保输入是数字类型
  206. const lng = parseFloat(center[0]);
  207. const lat = parseFloat(center[1]);
  208. // 1亩 ≈ 666.67平方米
  209. const areaInSquareMeters = mu * 666.67;
  210. // 正方形边长(米)
  211. const sideLength = Math.sqrt(areaInSquareMeters);
  212. // 半边长(米)
  213. const halfSide = sideLength / 2;
  214. // 纬度方向:1度约等于111000米
  215. const latDelta = halfSide / 111000;
  216. // 经度方向:需要根据纬度调整,1度约等于111000 * cos(lat)米
  217. const lngDelta = halfSide / (111000 * Math.cos(lat * Math.PI / 180));
  218. // 计算四个顶点(逆时针顺序)
  219. const topLeft = [lng - lngDelta, lat + latDelta];
  220. const topRight = [lng + lngDelta, lat + latDelta];
  221. const bottomRight = [lng + lngDelta, lat - latDelta];
  222. const bottomLeft = [lng - lngDelta, lat - latDelta];
  223. // 生成MULTIPOLYGON格式的WKT(需要闭合,所以第一个点要重复)
  224. const formatPoint = (point) => `${point[0]} ${point[1]}`;
  225. const coordinates = [topLeft, topRight, bottomRight, bottomLeft, topLeft]
  226. .map(formatPoint)
  227. .join(', ');
  228. // 注意:MULTIPOLYGON 和括号之间需要有一个空格
  229. const wkt = `MULTIPOLYGON (((${coordinates})))`;
  230. return { wkt, area: parseFloat(mu).toFixed(2) };
  231. }
  232. onMounted(() => {
  233. // 清除上一次的地块数据,确保每次进入都是全新状态
  234. store.commit("home/SET_FARM_POLYGON", null);
  235. isFromEditMap.value = false; // 初始化时可以手动输入
  236. hasDefaultPolygon.value = false; // 初始化时没有默认地块
  237. centerPoint.value = store.state.home.miniUserLocationPoint;
  238. const arr = convertPointToArray(centerPoint.value);
  239. getLocationName(`${arr[1]},${arr[0]}`);
  240. indexMap.initMap(centerPoint.value, mapContainer.value);
  241. // 不再初始化时绘制默认地块,等待用户点击"新增地块"按钮
  242. // 清空地块和面积数据
  243. polygonArr.value = null;
  244. ruleForm.mu = '';
  245. ruleForm.typeId = '';
  246. getSpecieList();
  247. });
  248. const polygonArr = ref(null);
  249. const paramsType = ref(null);
  250. onActivated(() => {
  251. paramsType.value = route.query.type;
  252. if (route.query.isReload) {
  253. // 清除旧的地块数据
  254. store.commit("home/SET_FARM_POLYGON", null);
  255. isFromEditMap.value = false; // 从home进入,可以手动输入
  256. hasDefaultPolygon.value = false; // 重置默认地块状态
  257. centerPoint.value = store.state.home.miniUserLocationPoint;
  258. const arr = convertPointToArray(centerPoint.value);
  259. getLocationName(`${arr[1]},${arr[0]}`);
  260. indexMap.setMapPosition(arr);
  261. indexMap.clearLayer();
  262. // 不再自动生成默认地块,等待用户点击"新增地块"
  263. polygonArr.value = null;
  264. ruleForm.mu = '';
  265. ruleForm.typeId = '';
  266. return; // 直接返回,不执行下面的逻辑
  267. }
  268. indexMap.clearLayer();
  269. // 绘制勾画范围(从edit_map返回的情况)
  270. const polygonData = store.state.home.polygonData;
  271. if (polygonData) {
  272. // 检查地块数据是否有效(数组存在且不为空)
  273. const hasValidGeometry = polygonData.geometryArr &&
  274. Array.isArray(polygonData.geometryArr) &&
  275. polygonData.geometryArr.length > 0;
  276. if (hasValidGeometry) {
  277. // 用户从edit_map返回,且有有效的地块数据
  278. // 根据 isConfirmed 判断是否锁定输入框
  279. if (polygonData.isConfirmed) {
  280. // 用户点击了"确认"按钮,锁定输入框并显示精确面积
  281. isFromEditMap.value = true;
  282. ruleForm.mu = polygonData.mianji;
  283. } else {
  284. // 用户点击了"取消",不锁定输入框,面积保持原样
  285. isFromEditMap.value = false;
  286. // 面积输入框保持之前的值,不更新
  287. }
  288. indexMap.setAreaGeometry(polygonData.geometryArr);
  289. polygonArr.value = polygonData.geometryArr;
  290. // 有地块数据时,标记已创建默认地块
  291. hasDefaultPolygon.value = true;
  292. } else {
  293. // 用户在编辑页面删除了所有地块
  294. // 重置所有状态
  295. polygonArr.value = null;
  296. ruleForm.mu = '';
  297. isFromEditMap.value = false;
  298. hasDefaultPolygon.value = false;
  299. }
  300. } else if (centerPoint.value && polygonArr.value) {
  301. // 没有新的编辑数据,保持当前地块
  302. // 同样需要检查数据有效性
  303. if (Array.isArray(polygonArr.value) && polygonArr.value.length > 0) {
  304. indexMap.setAreaGeometry(polygonArr.value);
  305. // 保持 hasDefaultPolygon 状态
  306. }
  307. }
  308. });
  309. // 搜索
  310. const MAP_KEY = "CZLBZ-LJICQ-R4A5J-BN62X-YXCRJ-GNBUT";
  311. const locationVal = ref(null);
  312. const locationOptions = reactive({
  313. list: [],
  314. });
  315. const loading = ref(false);
  316. const remoteMethod = async (keyword) => {
  317. if (keyword) {
  318. locationOptions.list = [];
  319. loading.value = true;
  320. const params = {
  321. key: MAP_KEY,
  322. keyword,
  323. location: route.query.userLocation || "113.61702297075017,23.584863449735067",
  324. };
  325. await VE_API.old_mini_map.getCtiyList({ word: keyword }).then(({ data }) => {
  326. if (data && data.length) {
  327. data.forEach((item) => {
  328. item.point = item.location.lat + "," + item.location.lng;
  329. locationOptions.list.push(item);
  330. });
  331. }
  332. });
  333. VE_API.old_mini_map.search(params).then(({ data }) => {
  334. loading.value = false;
  335. data.forEach((item) => {
  336. item.point = item.location.lat + "," + item.location.lng;
  337. locationOptions.list.push(item);
  338. });
  339. });
  340. } else {
  341. locationOptions.list = [];
  342. }
  343. };
  344. const handleSearchRes = (v) => {
  345. const parts = v.value.split(",");
  346. let { latitude, longitude } = transformFromGCJToWGS(parseFloat(parts[0]), parseFloat(parts[1]));
  347. const coordinateArray = [longitude, latitude];
  348. indexMap.setMapPosition(coordinateArray);
  349. centerPoint.value = `POINT (${coordinateArray[0]} ${coordinateArray[1]})`;
  350. ruleForm.address = v.item?.title || v.item?.address;
  351. pointAddress.value = v.item?.province + v.item?.city + v.item?.district;
  352. };
  353. // 表单
  354. const ruleFormRef = ref(null);
  355. const ruleForm = reactive({
  356. address: "",
  357. mu: "",
  358. speciesItem: null,
  359. typeId: "",
  360. name: "",
  361. fzr: "",
  362. tel: "",
  363. });
  364. // 自定义验证规则:验证面积必须是大于0的数字
  365. const validateMianji = (rule, value, callback) => {
  366. if (!value) {
  367. callback(new Error('请输入农场面积'));
  368. } else {
  369. const num = parseFloat(value);
  370. if (isNaN(num)) {
  371. callback(new Error('面积必须是数字'));
  372. } else if (num <= 0) {
  373. callback(new Error('面积必须大于0'));
  374. } else {
  375. callback();
  376. }
  377. }
  378. };
  379. const rules = reactive({
  380. address: [{ required: true, message: "请选择农场位置", trigger: "blur" }],
  381. mu: [
  382. { required: true, message: "请输入农场面积", trigger: "blur" },
  383. { validator: validateMianji, trigger: ["blur", "change"] }
  384. ],
  385. speciesItem: [{ required: true, message: "请选择品类", trigger: "blur" }],
  386. typeId: [{ required: true, message: "请选择品种", trigger: "blur" }],
  387. name: [{ required: true, message: "请输入您的农场名称", trigger: "blur" }],
  388. fzr: [{ required: true, message: "请输入联系人姓名", trigger: "blur" }],
  389. tel: [
  390. { required: true, message: "请输入联系人电话", trigger: "blur" },
  391. {
  392. pattern: /^1[3-9]\d{9}$/,
  393. message: "请输入正确的手机号码",
  394. trigger: "blur"
  395. }
  396. ],
  397. });
  398. const submitForm = (formEl) => {
  399. if (!formEl) return;
  400. formEl.validate((valid) => {
  401. if (valid) {
  402. const params = {
  403. ...ruleForm,
  404. wkt: centerPoint.value,
  405. speciesId: ruleForm.speciesItem?.id,
  406. containerId: ruleForm.speciesItem?.defaultContainerId,
  407. geom: polygonArr.value,
  408. };
  409. VE_API.farm.saveFarm(params).then((res) => {
  410. if(res.code === 0) {
  411. ElMessage.success("创建成功");
  412. // 重置表单和地块数据
  413. ruleFormRef.value.resetFields();
  414. store.commit("home/SET_FARM_POLYGON", null);
  415. polygonArr.value = null;
  416. isFromEditMap.value = false;
  417. // 根据来源页面决定跳转目标
  418. const fromPage = route.query.from;
  419. if (fromPage === 'monitor') {
  420. // 如果是从monitor页面来的,跳转回monitor页面并刷新农场列表
  421. router.replace(`/${fromPage}`);
  422. } else {
  423. // 默认跳转到首页
  424. router.replace("/home?reload=true&showSuccess=true");
  425. }
  426. }else{
  427. ElMessage.error(res.msg);
  428. }
  429. });
  430. }
  431. });
  432. };
  433. const resetForm = (formEl) => {
  434. if (!formEl) return;
  435. formEl.resetFields();
  436. // 清除地块数据
  437. store.commit("home/SET_FARM_POLYGON", null);
  438. polygonArr.value = null;
  439. isFromEditMap.value = false;
  440. hasDefaultPolygon.value = false; // 重置默认地块状态
  441. // 根据来源页面决定返回目标
  442. const fromPage = route.query.from;
  443. if (fromPage) {
  444. router.replace(`/${fromPage}?reload=true`);
  445. return;
  446. }
  447. router.go(-1)
  448. };
  449. const centerPoint = ref(null);
  450. function toSubPage() {
  451. // 如果还没有默认地块,先创建默认地块
  452. if (!hasDefaultPolygon.value) {
  453. if (centerPoint.value) {
  454. const arr = convertPointToArray(centerPoint.value);
  455. const squareData = generateSquarePolygonBySideLength(arr, 200); // 200米边长
  456. const geometryData = {
  457. geometryArr: [squareData.wkt],
  458. mianji: squareData.area,
  459. };
  460. // 绘制默认地块
  461. indexMap.setAreaGeometry(geometryData.geometryArr);
  462. // 保存地块数据
  463. polygonArr.value = geometryData.geometryArr;
  464. // 标记已创建默认地块
  465. hasDefaultPolygon.value = true;
  466. // 不跳转,停留在当前页面
  467. return;
  468. }
  469. }
  470. // 如果已有默认地块,则跳转到编辑页面
  471. // 保存到store中以便在编辑页面回显
  472. if (polygonArr.value) {
  473. const polygonData = {
  474. geometryArr: polygonArr.value,
  475. mianji: ruleForm.mu || '', // 保存用户输入的面积,如果没有输入则为空
  476. isConfirmed: false, // 标记:还未从编辑页面确认返回
  477. };
  478. store.commit("home/SET_FARM_POLYGON", polygonData);
  479. }
  480. router.push(
  481. `/edit_map?mapCenter=${centerPoint.value}&pointName=${ruleForm.address}&pointAddress=${pointAddress.value}`
  482. );
  483. }
  484. const pointAddress = ref(null);
  485. const farmCity = ref(null);
  486. function getLocationName(location) {
  487. const params = {
  488. key: MAP_KEY,
  489. location,
  490. };
  491. VE_API.old_mini_map.location(params).then(({ result }) => {
  492. // locationVal.value = result.formatted_addresses.recommend;
  493. const add = result.formatted_addresses?.recommend ? result.formatted_addresses.recommend : result.address + "";
  494. ruleForm.address = add;
  495. pointAddress.value = result.address;
  496. farmCity.value = result.address_component?.city || '';
  497. });
  498. }
  499. watch(
  500. () => mapLocation.data,
  501. (newValue, oldValue) => {
  502. if (newValue) {
  503. let { latitude, longitude } = transformFromWGSToGCJ(newValue[1], newValue[0]);
  504. centerPoint.value = `POINT (${newValue[0]} ${newValue[1]})`;
  505. getLocationName(`${latitude},${longitude}`);
  506. }
  507. }
  508. );
  509. const specieList = ref([]);
  510. function getSpecieList() {
  511. VE_API.farm.fetchSpecieList().then(({ data }) => {
  512. specieList.value = data;
  513. });
  514. }
  515. function changeSpecie(v) {
  516. getFruitsTypeItemList(v.id);
  517. }
  518. function changeTypeId(v) {
  519. const fruitsName = fruitsList.value.find(item => item.id === v).name;
  520. ruleForm.name = farmCity.value + fruitsName + "农场";
  521. }
  522. const fruitsList = ref([]);
  523. function getFruitsTypeItemList(parentId) {
  524. VE_API.farm.fruitsTypeItemList({ parentId }).then(({ data }) => {
  525. fruitsList.value = data;
  526. });
  527. }
  528. function backgToHome() {
  529. ruleFormRef.value?.resetFields();
  530. // 根据来源页面决定返回目标
  531. const fromPage = route.query.from;
  532. if (fromPage) {
  533. router.replace(`/${fromPage}?reload=true`);
  534. return;
  535. }
  536. router.go(-1)
  537. }
  538. // 处理面积按键输入 - 只允许数字和小数点
  539. function handleMianjiKeypress(event) {
  540. // 如果是从编辑地图返回的,不允许手动修改
  541. if (isFromEditMap.value) {
  542. event.preventDefault();
  543. return;
  544. }
  545. const charCode = event.which ? event.which : event.keyCode;
  546. const char = String.fromCharCode(charCode);
  547. // 允许数字 (0-9) 和小数点 (.)
  548. if (!/^[0-9.]$/.test(char)) {
  549. event.preventDefault();
  550. return;
  551. }
  552. // 如果已经有小数点,不允许再输入小数点
  553. const currentValue = ruleForm.mu || '';
  554. if (char === '.' && currentValue.includes('.')) {
  555. event.preventDefault();
  556. return;
  557. }
  558. // 不允许以小数点开头
  559. if (char === '.' && !currentValue) {
  560. event.preventDefault();
  561. return;
  562. }
  563. }
  564. // 处理面积输入
  565. let mianjiInputTimer = null;
  566. function handleMianjiInput(value) {
  567. // 如果是从编辑地图返回的,不允许手动修改
  568. if (isFromEditMap.value) {
  569. return;
  570. }
  571. // 过滤非法字符,只保留数字和小数点
  572. let filteredValue = value.replace(/[^\d.]/g, '');
  573. // 确保只有一个小数点
  574. const parts = filteredValue.split('.');
  575. if (parts.length > 2) {
  576. filteredValue = parts[0] + '.' + parts.slice(1).join('');
  577. }
  578. // 限制小数点后最多2位
  579. if (parts.length === 2 && parts[1].length > 2) {
  580. filteredValue = parts[0] + '.' + parts[1].substring(0, 2);
  581. }
  582. // 更新输入框的值(如果被过滤了)
  583. if (filteredValue !== value) {
  584. ruleForm.mu = filteredValue;
  585. return;
  586. }
  587. // 清除之前的定时器
  588. if (mianjiInputTimer) {
  589. clearTimeout(mianjiInputTimer);
  590. }
  591. // 防抖处理,用户停止输入500ms后再更新地块
  592. mianjiInputTimer = setTimeout(() => {
  593. const mu = parseFloat(filteredValue);
  594. // 验证输入的有效性
  595. if (!mu || isNaN(mu) || mu <= 0) {
  596. return;
  597. }
  598. // 根据亩数重新生成地块
  599. if (centerPoint.value) {
  600. const arr = convertPointToArray(centerPoint.value);
  601. const squareData = generateSquarePolygonByMu(arr, mu);
  602. const geometryData = {
  603. geometryArr: [squareData.wkt],
  604. mianji: squareData.area,
  605. };
  606. // 清除旧地块
  607. indexMap.clearLayer();
  608. // 绘制新地块
  609. indexMap.setAreaGeometry(geometryData.geometryArr);
  610. // 更新状态
  611. polygonArr.value = geometryData.geometryArr;
  612. // 标记已创建默认地块
  613. hasDefaultPolygon.value = true;
  614. }
  615. }, 500);
  616. }
  617. </script>
  618. <style lang="scss" scoped>
  619. ::v-deep{
  620. .el-form-item--default{
  621. margin-bottom: 20px;
  622. }
  623. }
  624. .create-farm {
  625. position: relative;
  626. width: 100%;
  627. height: 100vh;
  628. overflow: hidden;
  629. .map-container {
  630. width: 100%;
  631. height: calc(100% - 240px);
  632. }
  633. .farm-content {
  634. position: absolute;
  635. top: 40px;
  636. left: 0;
  637. width: 100%;
  638. height: calc(100% - 40px);
  639. pointer-events: none;
  640. z-index: 2;
  641. .top-mask {
  642. height: 100px;
  643. position: absolute;
  644. z-index: 2;
  645. top: 0;
  646. left: 0;
  647. width: 100%;
  648. background: linear-gradient(0deg, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.6) 100%);
  649. }
  650. }
  651. .farm-filter {
  652. pointer-events: all;
  653. margin: 12px;
  654. position: relative;
  655. z-index: 10;
  656. background: rgba(0, 0, 0, 0.3);
  657. border-radius: 20px;
  658. border: 1px solid rgba(255, 255, 255, 0.4);
  659. &::before {
  660. content: "";
  661. position: absolute;
  662. left: 12px;
  663. top: 9px;
  664. width: 14px;
  665. height: 14px;
  666. background: url("@/assets/img/home/search.png") no-repeat center center / 100% 100%;
  667. }
  668. ::v-deep {
  669. .el-select__wrapper {
  670. background: none;
  671. box-shadow: none;
  672. padding-left: 34px;
  673. font-size: 12px;
  674. .el-select__selected-item,
  675. .el-select__placeholder,
  676. .el-select__input {
  677. color: rgba(255, 255, 255, 0.6);
  678. &.is-transparent {
  679. color: #ccc;
  680. font-size: 12px;
  681. }
  682. }
  683. }
  684. .el-select {
  685. transition: all 0.3s;
  686. .el-input.is-focus .el-input__wrapper {
  687. box-shadow: none !important;
  688. }
  689. }
  690. .el-input {
  691. .el-input__wrapper {
  692. background: none;
  693. box-shadow: none;
  694. padding-left: 18px;
  695. font-size: 11px;
  696. .el-input__inner {
  697. color: rgba(255, 255, 255, 0.6);
  698. }
  699. &.is-focus {
  700. .el-input__inner {
  701. color: #ccc;
  702. font-size: 11px;
  703. }
  704. }
  705. }
  706. }
  707. }
  708. }
  709. .create-wrap {
  710. position: absolute;
  711. bottom: 0px;
  712. left: 0;
  713. width: 100%;
  714. background: linear-gradient(180deg, transparent 0%, #f5f7fb 20%);
  715. }
  716. .create-box {
  717. pointer-events: all;
  718. margin: 0 12px 10px 12px;
  719. width: calc(100% - 24px);
  720. background: #e0f1fe;
  721. border-radius: 14px;
  722. .box-content {
  723. position: relative;
  724. &::after {
  725. position: absolute;
  726. right: 10px;
  727. top: 2px;
  728. content: "";
  729. width: 79px;
  730. height: 72px;
  731. background: url("@/assets/img/home/creat-bg.png") no-repeat center / 100% 100%;
  732. }
  733. }
  734. .create-title {
  735. display: flex;
  736. align-items: center;
  737. padding: 12px 6px 12px 12px;
  738. color: #0089f5;
  739. font-size: 18px;
  740. font-weight: bold;
  741. .title-icon {
  742. width: 18px;
  743. padding-right: 10px;
  744. }
  745. }
  746. .create-content {
  747. background: #fff;
  748. border-radius: 14px;
  749. padding: 12px;
  750. position: relative;
  751. z-index: 2;
  752. .create-from {
  753. .select-wrap {
  754. display: flex;
  755. // width: 86%;
  756. ::v-deep {
  757. .el-input__wrapper {
  758. background: none;
  759. box-shadow: none;
  760. }
  761. .el-input__inner {
  762. font-size: 14px;
  763. color: rgba(0, 0, 0, 0.5);
  764. }
  765. .el-select__wrapper {
  766. background: none;
  767. box-shadow: none;
  768. gap: 2px;
  769. padding: 4px 2px;
  770. justify-content: center;
  771. }
  772. .el-select__selection {
  773. flex: none;
  774. width: fit-content;
  775. }
  776. .el-select__placeholder {
  777. color: #000;
  778. position: static;
  779. transform: none;
  780. width: fit-content;
  781. }
  782. }
  783. // .select-item {
  784. // width: fit-content;
  785. // }
  786. .period-select {
  787. margin-left: 6px;
  788. }
  789. .select-item {
  790. min-width: 76px;
  791. }
  792. }
  793. ::v-deep {
  794. .el-form-item__label {
  795. color: #000;
  796. }
  797. .el-form-item__error {
  798. top: 117%;
  799. }
  800. .el-form-item {
  801. position: relative;
  802. &::after {
  803. content: "";
  804. position: absolute;
  805. left: 60px;
  806. bottom: -5px;
  807. width: calc(100% - 60px);
  808. height: 1px;
  809. background: rgba(0, 0, 0, 0.08);
  810. }
  811. }
  812. .el-input__wrapper {
  813. box-shadow: none;
  814. padding: 1px 6px;
  815. // border-bottom: 1px solid rgba(0, 0, 0, 0.08);
  816. }
  817. }
  818. .area-box {
  819. display: flex;
  820. align-items: center;
  821. width: 100%;
  822. .unit {
  823. padding-right: 10px;
  824. }
  825. }
  826. .position-wrap {
  827. display: flex;
  828. justify-content: space-between;
  829. align-items: center;
  830. .draw-btn {
  831. flex: none;
  832. padding: 0 12px;
  833. height: 30px;
  834. line-height: 30px;
  835. box-sizing: border-box;
  836. color: #2199f8;
  837. border: 1px solid #2199f8;
  838. background: rgba(33, 153, 248, 0.1);
  839. border-radius: 20px;
  840. font-size: 12px;
  841. }
  842. }
  843. }
  844. .create-btn {
  845. display: flex;
  846. align-items: center;
  847. width: 100%;
  848. padding-top: 10px;
  849. .btn-item {
  850. flex: 1;
  851. text-align: center;
  852. padding: 0 11px;
  853. height: 40px;
  854. line-height: 40px;
  855. border-radius: 34px;
  856. font-size: 16px;
  857. box-sizing: border-box;
  858. &.sencond-btn {
  859. border: 1px solid rgba(153, 153, 153, 0.5);
  860. color: #666666;
  861. }
  862. &.primary-btn {
  863. background: linear-gradient(180deg, #76c3ff, #2199f8);
  864. color: #fff;
  865. }
  866. }
  867. .btn-item + .btn-item {
  868. margin-left: 5px;
  869. }
  870. }
  871. }
  872. }
  873. }
  874. </style>
  875. <style lang="scss">
  876. .location-search-popper {
  877. .el-select-dropdown__list {
  878. max-width: 96vw;
  879. overflow-x: auto;
  880. }
  881. .sub-title {
  882. padding-left: 6px;
  883. font-size: 12px;
  884. color: #ccc;
  885. }
  886. }
  887. </style>