index.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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. 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. {{ paramsType === 'client' ? "添加" : paramsType === 'edit' ?"确认修改":"立即创建" }}
  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 paramsType = ref(null);
  145. onActivated(() => {
  146. paramsType.value = route.query.type;
  147. if (route.query.isFromHome) {
  148. centerPoint.value = store.state.home.miniUserLocationPoint;
  149. const arr = convertPointToArray(centerPoint.value);
  150. getLocationName(`${arr[1]},${arr[0]}`);
  151. indexMap.setMapPosition(arr);
  152. }
  153. indexMap.clearLayer();
  154. // 绘制勾画范围
  155. const polygonData = store.state.home.polygonData;
  156. if (polygonData) {
  157. indexMap.setAreaGeometry(polygonData?.geometryArr);
  158. polygonArr.value = polygonData.geometryArr;
  159. ruleForm.mianji = polygonData.mianji;
  160. }
  161. });
  162. // 搜索
  163. const MAP_KEY = "CZLBZ-LJICQ-R4A5J-BN62X-YXCRJ-GNBUT";
  164. const locationVal = ref(null);
  165. const locationOptions = reactive({
  166. list: [],
  167. });
  168. const loading = ref(false);
  169. const remoteMethod = async (keyword) => {
  170. if (keyword) {
  171. locationOptions.list = [];
  172. loading.value = true;
  173. const params = {
  174. key: MAP_KEY,
  175. keyword,
  176. location: route.query.userLocation || "113.61702297075017,23.584863449735067",
  177. };
  178. await VE_API.old_mini_map.getCtiyList({ word: keyword }).then(({ data }) => {
  179. if (data && data.length) {
  180. data.forEach((item) => {
  181. item.point = item.location.lat + "," + item.location.lng;
  182. locationOptions.list.push(item);
  183. });
  184. }
  185. });
  186. VE_API.old_mini_map.search(params).then(({ data }) => {
  187. loading.value = false;
  188. data.forEach((item) => {
  189. item.point = item.location.lat + "," + item.location.lng;
  190. locationOptions.list.push(item);
  191. });
  192. });
  193. } else {
  194. locationOptions.list = [];
  195. }
  196. };
  197. const handleSearchRes = (v) => {
  198. const parts = v.value.split(",");
  199. let { latitude, longitude } = transformFromGCJToWGS(parseFloat(parts[0]), parseFloat(parts[1]));
  200. const coordinateArray = [longitude, latitude];
  201. indexMap.setMapPosition(coordinateArray);
  202. centerPoint.value = `POINT (${coordinateArray[0]} ${coordinateArray[1]})`;
  203. ruleForm.address = v.item?.title || v.item?.address;
  204. pointAddress.value = v.item?.province + v.item?.city + v.item?.district;
  205. };
  206. // 表单
  207. const ruleFormRef = ref(null);
  208. const ruleForm = reactive({
  209. address: "",
  210. mianji: "",
  211. speciesItem: null,
  212. phenologyId: "",
  213. name: "",
  214. });
  215. const rules = reactive({
  216. address: [{ required: true, message: "请选择农场位置", trigger: "blur" }],
  217. mianji: [{ required: true, message: "请勾选地块获得农场面积", trigger: "blur" }],
  218. speciesItem: [{ required: true, message: "请选择品类", trigger: "blur" }],
  219. phenologyId: [{ required: true, message: "请选择物候期", trigger: "blur" }],
  220. name: [{ required: true, message: "请输入您的农场名称", trigger: "blur" }],
  221. });
  222. const submitForm = (formEl) => {
  223. if (!formEl) return;
  224. formEl.validate((valid) => {
  225. if (valid) {
  226. const params = {
  227. ...ruleForm,
  228. wkt: centerPoint.value,
  229. speciesId: ruleForm.speciesItem?.id,
  230. containerId: ruleForm.speciesItem?.defaultContainerId,
  231. geom: polygonArr.value,
  232. };
  233. VE_API.farm.saveFarm(params).then((res) => {
  234. ElMessage.success("创建成功");
  235. // 重置表单
  236. ruleFormRef.value.resetFields();
  237. store.commit("home/SET_FARM_POLYGON", null);
  238. router.replace("/home?reload=true");
  239. });
  240. console.log("submit!", params);
  241. } else {
  242. console.log("error submit!");
  243. }
  244. });
  245. };
  246. const resetForm = (formEl) => {
  247. if (!formEl) return;
  248. formEl.resetFields();
  249. store.commit("home/SET_FARM_POLYGON", null);
  250. router.replace("/home?reload=true");
  251. };
  252. const centerPoint = ref(null);
  253. function toSubPage() {
  254. router.push(
  255. `/edit_map?mapCenter=${centerPoint.value}&pointName=${ruleForm.address}&pointAddress=${pointAddress.value}`
  256. );
  257. }
  258. const pointAddress = ref(null);
  259. function getLocationName(location) {
  260. const params = {
  261. key: MAP_KEY,
  262. location,
  263. };
  264. VE_API.old_mini_map.location(params).then(({ result }) => {
  265. // locationVal.value = result.formatted_addresses.recommend;
  266. const add = result.formatted_addresses?.recommend ? result.formatted_addresses.recommend : result.address + "";
  267. ruleForm.address = add;
  268. pointAddress.value = result.address;
  269. });
  270. }
  271. watch(
  272. () => mapLocation.data,
  273. (newValue, oldValue) => {
  274. if (newValue) {
  275. let { latitude, longitude } = transformFromWGSToGCJ(newValue[1], newValue[0]);
  276. centerPoint.value = `POINT (${newValue[0]} ${newValue[1]})`;
  277. getLocationName(`${latitude},${longitude}`);
  278. }
  279. }
  280. );
  281. const specieList = ref([]);
  282. function getSpecieList() {
  283. VE_API.farm.fetchSpecieList().then(({ data }) => {
  284. specieList.value = data;
  285. });
  286. }
  287. function changeSpecie(v) {
  288. getPhenology(v.defaultContainerId);
  289. }
  290. const phenologyList = ref([]);
  291. function getPhenology(containerId) {
  292. VE_API.farm.fetchPhenologyList({ containerId }).then(({ data }) => {
  293. phenologyList.value = data;
  294. });
  295. }
  296. function backgToHome() {
  297. router.replace("/home")
  298. }
  299. </script>
  300. <style lang="scss" scoped>
  301. .create-farm {
  302. position: relative;
  303. width: 100%;
  304. height: 100vh;
  305. overflow: hidden;
  306. .map-container {
  307. width: 100%;
  308. height: calc(100% - 240px);
  309. }
  310. .farm-content {
  311. position: absolute;
  312. top: 40px;
  313. left: 0;
  314. width: 100%;
  315. height: calc(100% - 40px);
  316. pointer-events: none;
  317. z-index: 2;
  318. .top-mask {
  319. height: 100px;
  320. position: absolute;
  321. z-index: 2;
  322. top: 0;
  323. left: 0;
  324. width: 100%;
  325. background: linear-gradient(0deg, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.6) 100%);
  326. }
  327. }
  328. .farm-filter {
  329. pointer-events: all;
  330. margin: 12px;
  331. position: relative;
  332. z-index: 10;
  333. background: rgba(0, 0, 0, 0.3);
  334. border-radius: 20px;
  335. border: 1px solid rgba(255, 255, 255, 0.4);
  336. &::before {
  337. content: "";
  338. position: absolute;
  339. left: 12px;
  340. top: 9px;
  341. width: 14px;
  342. height: 14px;
  343. background: url("@/assets/img/home/search.png") no-repeat center center / 100% 100%;
  344. }
  345. ::v-deep {
  346. .el-select__wrapper {
  347. background: none;
  348. box-shadow: none;
  349. padding-left: 34px;
  350. font-size: 12px;
  351. .el-select__selected-item,
  352. .el-select__placeholder,
  353. .el-select__input {
  354. color: rgba(255, 255, 255, 0.6);
  355. &.is-transparent {
  356. color: #ccc;
  357. font-size: 12px;
  358. }
  359. }
  360. }
  361. .el-select {
  362. transition: all 0.3s;
  363. .el-input.is-focus .el-input__wrapper {
  364. box-shadow: none !important;
  365. }
  366. }
  367. .el-input {
  368. .el-input__wrapper {
  369. background: none;
  370. box-shadow: none;
  371. padding-left: 18px;
  372. font-size: 11px;
  373. .el-input__inner {
  374. color: rgba(255, 255, 255, 0.6);
  375. }
  376. &.is-focus {
  377. .el-input__inner {
  378. color: #ccc;
  379. font-size: 11px;
  380. }
  381. }
  382. }
  383. }
  384. }
  385. }
  386. .create-wrap {
  387. position: absolute;
  388. bottom: 0px;
  389. left: 0;
  390. width: 100%;
  391. background: linear-gradient(180deg, transparent 0%, #f5f7fb 20%);
  392. }
  393. .create-box {
  394. pointer-events: all;
  395. margin: 0 12px 10px 12px;
  396. width: calc(100% - 24px);
  397. background: #e0f1fe;
  398. border-radius: 14px;
  399. .box-content {
  400. position: relative;
  401. &::after {
  402. position: absolute;
  403. right: 10px;
  404. top: 2px;
  405. content: "";
  406. width: 79px;
  407. height: 72px;
  408. background: url("@/assets/img/home/creat-bg.png") no-repeat center / 100% 100%;
  409. }
  410. }
  411. .create-title {
  412. display: flex;
  413. align-items: center;
  414. padding: 12px 6px 12px 12px;
  415. color: #0089f5;
  416. font-size: 18px;
  417. font-weight: bold;
  418. .title-icon {
  419. width: 18px;
  420. padding-right: 10px;
  421. }
  422. }
  423. .create-content {
  424. background: #fff;
  425. border-radius: 14px;
  426. padding: 12px;
  427. position: relative;
  428. z-index: 2;
  429. .create-from {
  430. .select-wrap {
  431. display: flex;
  432. // width: 86%;
  433. ::v-deep {
  434. .el-input__wrapper {
  435. background: none;
  436. box-shadow: none;
  437. }
  438. .el-input__inner {
  439. font-size: 14px;
  440. color: rgba(0, 0, 0, 0.5);
  441. }
  442. .el-select__wrapper {
  443. background: none;
  444. box-shadow: none;
  445. gap: 2px;
  446. padding: 4px 2px;
  447. justify-content: center;
  448. }
  449. .el-select__selection {
  450. flex: none;
  451. width: fit-content;
  452. }
  453. .el-select__placeholder {
  454. color: #000;
  455. position: static;
  456. transform: none;
  457. width: fit-content;
  458. }
  459. }
  460. // .select-item {
  461. // width: fit-content;
  462. // }
  463. .period-select {
  464. margin-left: 6px;
  465. }
  466. .select-item {
  467. min-width: 76px;
  468. }
  469. }
  470. ::v-deep {
  471. .el-form-item__label {
  472. color: #000;
  473. }
  474. .el-form-item__error {
  475. top: 117%;
  476. }
  477. .el-form-item {
  478. position: relative;
  479. &::after {
  480. content: "";
  481. position: absolute;
  482. left: 60px;
  483. bottom: -5px;
  484. width: calc(100% - 60px);
  485. height: 1px;
  486. background: rgba(0, 0, 0, 0.08);
  487. }
  488. }
  489. .el-input__wrapper {
  490. box-shadow: none;
  491. padding: 1px 6px;
  492. // border-bottom: 1px solid rgba(0, 0, 0, 0.08);
  493. }
  494. }
  495. .area-box {
  496. display: flex;
  497. align-items: center;
  498. width: 100%;
  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>