index.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. <template>
  2. <div class="create-farm">
  3. <custom-header :name="isClient ? '新增用户' : '创建农场'"></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. {{ isClient ? "新增用户" : "创建农场" }}
  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. label-width="auto"
  46. class="demo-ruleForm"
  47. >
  48. <el-form-item label="农场位置" prop="address">
  49. <div class="position-wrap">
  50. <el-input
  51. placeholder="农场位置"
  52. readonly
  53. v-model="ruleForm.address"
  54. autocomplete="off"
  55. />
  56. <div class="draw-btn" @click="toSubPage">点击勾选地块</div>
  57. </div>
  58. </el-form-item>
  59. <el-form-item label="农场面积" prop="mianji">
  60. <div class="area-box">
  61. <el-input
  62. placeholder="勾选地块获得农场面积"
  63. v-model="ruleForm.mianji"
  64. autocomplete="off"
  65. style="width: fit-content"
  66. />
  67. <div class="unit">亩</div>
  68. </div>
  69. </el-form-item>
  70. <el-form-item label="种植作物" prop="speciesItem">
  71. <div class="select-wrap">
  72. <el-select
  73. @change="changeSpecie"
  74. class="select-item"
  75. v-model="ruleForm.speciesItem"
  76. placeholder="作物类型"
  77. >
  78. <el-option
  79. v-for="(item, index) in specieList"
  80. :key="index"
  81. :label="item.name"
  82. :value="{ value: item.id, ...item }"
  83. />
  84. </el-select>
  85. <el-select
  86. v-model="ruleForm.phenologyId"
  87. placeholder="物候期"
  88. class="period-select select-item"
  89. >
  90. <el-option
  91. v-for="(item, index) in phenologyList"
  92. :key="index"
  93. :label="item.name"
  94. :value="item.id"
  95. />
  96. </el-select>
  97. </div>
  98. </el-form-item>
  99. <el-form-item label="农场名称" prop="name">
  100. <el-input
  101. placeholder="请输入您的农场名称"
  102. v-model="ruleForm.name"
  103. autocomplete="off"
  104. />
  105. </el-form-item>
  106. </el-form>
  107. </div>
  108. <div class="create-btn">
  109. <div class="btn-item sencond-btn" @click="resetForm(ruleFormRef)">取消</div>
  110. <div class="btn-item primary-btn" @click="submitForm(ruleFormRef)">
  111. {{ isClient ? "添加" : "立即创建" }}
  112. </div>
  113. </div>
  114. </div>
  115. </div>
  116. </div>
  117. </div>
  118. </div>
  119. </div>
  120. </template>
  121. <script setup>
  122. import customHeader from "@/components/customHeader.vue";
  123. import IndexMap from "./map/index.js";
  124. import { useRoute, useRouter } from "vue-router";
  125. import { mapLocation } from "./map/index.js";
  126. import { onMounted, ref, reactive, watch, onActivated } from "vue";
  127. import { useStore } from "vuex";
  128. import { convertPointToArray } from "@/utils/index";
  129. import { ElMessage } from "element-plus";
  130. import { transformFromGCJToWGS, transformFromWGSToGCJ } from "@/utils/WSCoordinate.js";
  131. const route = useRoute();
  132. const router = useRouter();
  133. const store = useStore();
  134. const indexMap = new IndexMap();
  135. const mapContainer = ref(null);
  136. onMounted(() => {
  137. centerPoint.value = store.state.home.miniUserLocationPoint;
  138. const arr = convertPointToArray(centerPoint.value);
  139. getLocationName(`${arr[1]},${arr[0]}`);
  140. indexMap.initMap(centerPoint.value, mapContainer.value);
  141. getSpecieList();
  142. });
  143. const polygonArr = ref(null);
  144. const isClient = ref(false);
  145. onActivated(() => {
  146. if (route.query.type) {
  147. isClient.value = true;
  148. }
  149. if (route.query.isFromHome) {
  150. centerPoint.value = store.state.home.miniUserLocationPoint;
  151. const arr = convertPointToArray(centerPoint.value);
  152. getLocationName(`${arr[1]},${arr[0]}`);
  153. indexMap.setMapPosition(arr);
  154. }
  155. indexMap.clearLayer();
  156. // 绘制勾画范围
  157. const polygonData = store.state.home.polygonData;
  158. if (polygonData) {
  159. indexMap.setAreaGeometry(polygonData?.geometryArr);
  160. polygonArr.value = polygonData.geometryArr;
  161. ruleForm.mianji = polygonData.mianji;
  162. }
  163. });
  164. // 搜索
  165. const MAP_KEY = "CZLBZ-LJICQ-R4A5J-BN62X-YXCRJ-GNBUT";
  166. const locationVal = ref(null);
  167. const locationOptions = reactive({
  168. list: [],
  169. });
  170. const loading = ref(false);
  171. const remoteMethod = async (keyword) => {
  172. if (keyword) {
  173. locationOptions.list = [];
  174. loading.value = true;
  175. const params = {
  176. key: MAP_KEY,
  177. keyword,
  178. location: route.query.userLocation || "113.61702297075017,23.584863449735067",
  179. };
  180. await VE_API.old_mini_map.getCtiyList({ word: keyword }).then(({ data }) => {
  181. if (data && data.length) {
  182. data.forEach((item) => {
  183. item.point = item.location.lat + "," + item.location.lng;
  184. locationOptions.list.push(item);
  185. });
  186. }
  187. });
  188. VE_API.old_mini_map.search(params).then(({ data }) => {
  189. loading.value = false;
  190. data.forEach((item) => {
  191. item.point = item.location.lat + "," + item.location.lng;
  192. locationOptions.list.push(item);
  193. });
  194. });
  195. } else {
  196. locationOptions.list = [];
  197. }
  198. };
  199. const handleSearchRes = (v) => {
  200. const parts = v.value.split(",");
  201. let { latitude, longitude } = transformFromGCJToWGS(parseFloat(parts[0]), parseFloat(parts[1]));
  202. const coordinateArray = [longitude, latitude];
  203. indexMap.setMapPosition(coordinateArray);
  204. centerPoint.value = `POINT (${coordinateArray[0]} ${coordinateArray[1]})`;
  205. ruleForm.address = v.item?.title || v.item?.address;
  206. pointAddress.value = v.item?.province + v.item?.city + v.item?.district;
  207. };
  208. // 表单
  209. const ruleFormRef = ref(null);
  210. const ruleForm = reactive({
  211. address: "",
  212. mianji: "",
  213. speciesItem: null,
  214. phenologyId: "",
  215. name: "",
  216. });
  217. const rules = reactive({
  218. address: [{ required: true, message: "请选择农场位置", trigger: "blur" }],
  219. mianji: [{ required: true, message: "请勾选地块获得农场面积", trigger: "blur" }],
  220. speciesItem: [{ required: true, message: "请选择品类", trigger: "blur" }],
  221. phenologyId: [{ required: true, message: "请选择物候期", trigger: "blur" }],
  222. name: [{ required: true, message: "请输入您的农场名称", trigger: "blur" }],
  223. });
  224. const submitForm = (formEl) => {
  225. if (!formEl) return;
  226. formEl.validate((valid) => {
  227. if (valid) {
  228. const params = {
  229. ...ruleForm,
  230. wkt: centerPoint.value,
  231. speciesId: ruleForm.speciesItem?.id,
  232. containerId: ruleForm.speciesItem?.defaultContainerId,
  233. geom: polygonArr.value,
  234. };
  235. VE_API.farm.saveFarm(params).then((res) => {
  236. ElMessage.success("创建成功");
  237. // 重置表单
  238. ruleFormRef.value.resetFields();
  239. store.commit("home/SET_FARM_POLYGON", null);
  240. router.replace("/home?reload=true");
  241. });
  242. console.log("submit!", params);
  243. } else {
  244. console.log("error submit!");
  245. }
  246. });
  247. };
  248. const resetForm = (formEl) => {
  249. if (!formEl) return;
  250. formEl.resetFields();
  251. store.commit("home/SET_FARM_POLYGON", null);
  252. router.go(-1);
  253. };
  254. const centerPoint = ref(null);
  255. function toSubPage() {
  256. router.push(
  257. `/edit_map?mapCenter=${centerPoint.value}&pointName=${ruleForm.address}&pointAddress=${pointAddress.value}`
  258. );
  259. }
  260. const pointAddress = ref(null);
  261. function getLocationName(location) {
  262. const params = {
  263. key: MAP_KEY,
  264. location,
  265. };
  266. VE_API.old_mini_map.location(params).then(({ result }) => {
  267. // locationVal.value = result.formatted_addresses.recommend;
  268. const add = result.formatted_addresses?.recommend ? result.formatted_addresses.recommend : result.address + "";
  269. ruleForm.address = add;
  270. pointAddress.value = result.address;
  271. });
  272. }
  273. watch(
  274. () => mapLocation.data,
  275. (newValue, oldValue) => {
  276. if (newValue) {
  277. let { latitude, longitude } = transformFromWGSToGCJ(newValue[1], newValue[0]);
  278. centerPoint.value = `POINT (${newValue[0]} ${newValue[1]})`;
  279. getLocationName(`${latitude},${longitude}`);
  280. }
  281. }
  282. );
  283. const specieList = ref([]);
  284. function getSpecieList() {
  285. VE_API.farm.fetchSpecieList().then(({ data }) => {
  286. specieList.value = data;
  287. });
  288. }
  289. function changeSpecie(v) {
  290. getPhenology(v.defaultContainerId);
  291. }
  292. const phenologyList = ref([]);
  293. function getPhenology(containerId) {
  294. VE_API.farm.fetchPhenologyList({ containerId }).then(({ data }) => {
  295. phenologyList.value = data;
  296. });
  297. }
  298. </script>
  299. <style lang="scss" scoped>
  300. .create-farm {
  301. position: relative;
  302. width: 100%;
  303. height: 100vh;
  304. overflow: hidden;
  305. .map-container {
  306. width: 100%;
  307. height: calc(100% - 240px);
  308. }
  309. .farm-content {
  310. position: absolute;
  311. top: 40px;
  312. left: 0;
  313. width: 100%;
  314. height: calc(100% - 40px);
  315. pointer-events: none;
  316. z-index: 2;
  317. .top-mask {
  318. height: 100px;
  319. position: absolute;
  320. z-index: 2;
  321. top: 0;
  322. left: 0;
  323. width: 100%;
  324. background: linear-gradient(0deg, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.6) 100%);
  325. }
  326. }
  327. .farm-filter {
  328. pointer-events: all;
  329. margin: 12px;
  330. position: relative;
  331. z-index: 10;
  332. background: rgba(0, 0, 0, 0.3);
  333. border-radius: 20px;
  334. border: 1px solid rgba(255, 255, 255, 0.4);
  335. &::before {
  336. content: "";
  337. position: absolute;
  338. left: 12px;
  339. top: 9px;
  340. width: 14px;
  341. height: 14px;
  342. background: url("@/assets/img/home/search.png") no-repeat center center / 100% 100%;
  343. }
  344. ::v-deep {
  345. .el-select__wrapper {
  346. background: none;
  347. box-shadow: none;
  348. padding-left: 34px;
  349. font-size: 12px;
  350. .el-select__selected-item,
  351. .el-select__placeholder,
  352. .el-select__input {
  353. color: rgba(255, 255, 255, 0.6);
  354. &.is-transparent {
  355. color: #ccc;
  356. font-size: 12px;
  357. }
  358. }
  359. }
  360. .el-select {
  361. transition: all 0.3s;
  362. .el-input.is-focus .el-input__wrapper {
  363. box-shadow: none !important;
  364. }
  365. }
  366. .el-input {
  367. .el-input__wrapper {
  368. background: none;
  369. box-shadow: none;
  370. padding-left: 18px;
  371. font-size: 11px;
  372. .el-input__inner {
  373. color: rgba(255, 255, 255, 0.6);
  374. }
  375. &.is-focus {
  376. .el-input__inner {
  377. color: #ccc;
  378. font-size: 11px;
  379. }
  380. }
  381. }
  382. }
  383. }
  384. }
  385. .create-wrap {
  386. position: absolute;
  387. bottom: 0px;
  388. left: 0;
  389. width: 100%;
  390. background: linear-gradient(180deg, transparent 0%, #f5f7fb 20%);
  391. }
  392. .create-box {
  393. pointer-events: all;
  394. margin: 0 12px 10px 12px;
  395. width: calc(100% - 24px);
  396. background: #e0f1fe;
  397. border-radius: 14px;
  398. .box-content {
  399. position: relative;
  400. &::after {
  401. position: absolute;
  402. right: 10px;
  403. top: 2px;
  404. content: "";
  405. width: 79px;
  406. height: 72px;
  407. background: url("@/assets/img/home/creat-bg.png") no-repeat center / 100% 100%;
  408. }
  409. }
  410. .create-title {
  411. display: flex;
  412. align-items: center;
  413. padding: 12px 6px 12px 12px;
  414. color: #0089f5;
  415. font-size: 18px;
  416. font-weight: bold;
  417. .title-icon {
  418. width: 18px;
  419. padding-right: 10px;
  420. }
  421. }
  422. .create-content {
  423. background: #fff;
  424. border-radius: 14px;
  425. padding: 12px;
  426. position: relative;
  427. z-index: 2;
  428. .create-from {
  429. .select-wrap {
  430. display: flex;
  431. // width: 86%;
  432. ::v-deep {
  433. .el-input__wrapper {
  434. background: none;
  435. box-shadow: none;
  436. }
  437. .el-input__inner {
  438. font-size: 14px;
  439. color: rgba(0, 0, 0, 0.5);
  440. }
  441. .el-select__wrapper {
  442. background: none;
  443. box-shadow: none;
  444. gap: 2px;
  445. padding: 4px 2px;
  446. justify-content: center;
  447. }
  448. .el-select__selection {
  449. flex: none;
  450. width: fit-content;
  451. }
  452. .el-select__placeholder {
  453. color: #000;
  454. position: static;
  455. transform: none;
  456. width: fit-content;
  457. }
  458. }
  459. // .select-item {
  460. // width: fit-content;
  461. // }
  462. .period-select {
  463. margin-left: 6px;
  464. }
  465. .select-item {
  466. min-width: 76px;
  467. }
  468. }
  469. ::v-deep {
  470. .el-form-item__label {
  471. color: #000;
  472. }
  473. .el-form-item__error {
  474. top: 117%;
  475. }
  476. .el-form-item {
  477. position: relative;
  478. &::after {
  479. content: "";
  480. position: absolute;
  481. left: 60px;
  482. bottom: -5px;
  483. width: calc(100% - 60px);
  484. height: 1px;
  485. background: rgba(0, 0, 0, 0.08);
  486. }
  487. }
  488. .el-input__wrapper {
  489. box-shadow: none;
  490. padding: 1px 6px;
  491. // border-bottom: 1px solid rgba(0, 0, 0, 0.08);
  492. }
  493. }
  494. .area-box {
  495. display: flex;
  496. align-items: center;
  497. width: 100%;
  498. justify-content: space-between;
  499. .unit {
  500. padding-right: 10px;
  501. }
  502. }
  503. .position-wrap {
  504. display: flex;
  505. justify-content: space-between;
  506. align-items: center;
  507. .draw-btn {
  508. flex: none;
  509. padding: 0 12px;
  510. height: 30px;
  511. line-height: 30px;
  512. box-sizing: border-box;
  513. color: #2199f8;
  514. border: 1px solid #2199f8;
  515. background: rgba(33, 153, 248, 0.1);
  516. border-radius: 20px;
  517. font-size: 12px;
  518. }
  519. }
  520. }
  521. .create-btn {
  522. display: flex;
  523. align-items: center;
  524. width: 100%;
  525. padding-top: 10px;
  526. .btn-item {
  527. flex: 1;
  528. text-align: center;
  529. padding: 0 11px;
  530. height: 40px;
  531. line-height: 40px;
  532. border-radius: 34px;
  533. font-size: 16px;
  534. box-sizing: border-box;
  535. &.sencond-btn {
  536. border: 1px solid rgba(153, 153, 153, 0.5);
  537. color: #666666;
  538. }
  539. &.primary-btn {
  540. background: linear-gradient(180deg, #76c3ff, #2199f8);
  541. color: #fff;
  542. }
  543. }
  544. .btn-item + .btn-item {
  545. margin-left: 5px;
  546. }
  547. }
  548. }
  549. }
  550. }
  551. </style>
  552. <style lang="scss">
  553. .location-search-popper {
  554. .el-select-dropdown__list {
  555. max-width: 96vw;
  556. overflow-x: auto;
  557. }
  558. .sub-title {
  559. padding-left: 6px;
  560. font-size: 12px;
  561. color: #ccc;
  562. }
  563. }
  564. </style>