baInformation.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. <template>
  2. <div class="ba-information">
  3. <div class="ba-information__content">
  4. <div class="page-header">
  5. <div class="page-title">请完善农场信息</div>
  6. </div>
  7. <div class="info-card">
  8. <div class="section-title">
  9. <img class="title-icon" src="@/assets/img/home/label-icon.png" alt="" />
  10. <span>基本信息</span>
  11. </div>
  12. <el-form
  13. ref="formRef"
  14. :model="form"
  15. :rules="rules"
  16. label-position="left"
  17. label-width="72px"
  18. class="info-form"
  19. require-asterisk-position="right"
  20. >
  21. <el-form-item label="姓名" prop="name">
  22. <el-input
  23. v-model="form.name"
  24. placeholder="请输入名字"
  25. maxlength="20"
  26. clearable
  27. />
  28. </el-form-item>
  29. <el-form-item label="手机号" prop="phone">
  30. <el-input
  31. v-model="form.phone"
  32. placeholder="请输入手机号"
  33. maxlength="11"
  34. clearable
  35. type="tel"
  36. />
  37. </el-form-item>
  38. <el-form-item label="种植品类" prop="crop">
  39. <el-select
  40. v-model="form.crop"
  41. class="crop-select"
  42. placeholder="选择品类"
  43. placement="bottom-end"
  44. popper-class="crop-select-popper"
  45. teleported
  46. :loading="cropLoading"
  47. >
  48. <el-option
  49. v-for="item in cropOptions"
  50. :key="item.name"
  51. :label="item.name"
  52. :value="item.name"
  53. />
  54. </el-select>
  55. </el-form-item>
  56. <el-form-item label="种植点位" prop="location" class="location-form-item">
  57. <div class="location-block" @click="goSelectLocation">
  58. <div class="map-preview">
  59. <div class="map-preview__map" ref="previewMapRef"></div>
  60. <div v-if="!form.location" class="map-preview__mask">点击地图选择种植点位</div>
  61. </div>
  62. </div>
  63. </el-form-item>
  64. </el-form>
  65. </div>
  66. </div>
  67. <div class="custom-bottom-fixed-btns">
  68. <div
  69. class="bottom-btn primary-btn"
  70. :class="{ disabled: submitting }"
  71. @click="handleNext"
  72. >
  73. {{ submitting ? "提交中..." : "确认信息" }}
  74. </div>
  75. </div>
  76. </div>
  77. </template>
  78. <script setup>
  79. import { nextTick, onActivated, onBeforeUnmount, onMounted, reactive, ref } from "vue";
  80. import { useRoute, useRouter } from "vue-router";
  81. import { useStore } from "vuex";
  82. import { ElMessage } from "element-plus";
  83. import SelectLocationMap from "../map/selectLocationMap.js";
  84. const LOCATION_KEY = "ENTRY_RESIDENT_LOCATION";
  85. const FORM_KEY = "ENTRY_BA_FORM";
  86. const STEP_KEY = "ENTRY_INFORMATION_STEP";
  87. const EQUIPMENT_KEY = "ENTRY_SELECTED_EQUIPMENT";
  88. /** 本地标记:用户已新增农场 */
  89. const HAS_FARM_KEY = "HAS_ENTRY_FARM";
  90. /** 新增农场信息(点位、品类等) */
  91. const ENTRY_FARM_DATA_KEY = "ENTRY_FARM_DATA";
  92. /** 默认定位:河北宁晋县 */
  93. const DEFAULT_POINT = "POINT(114.91863572509733 37.67702031436258)";
  94. /** 默认区县:河北宁晋县 */
  95. const DEFAULT_COUNTY_CODE = "130528";
  96. const MAP_KEY = "CZLBZ-LJICQ-R4A5J-BN62X-YXCRJ-GNBUT";
  97. const router = useRouter();
  98. const route = useRoute();
  99. const store = useStore();
  100. const formRef = ref(null);
  101. const previewMapRef = ref(null);
  102. let previewMap = null;
  103. const cropOptions = ref([]);
  104. const cropLoading = ref(false);
  105. const submitting = ref(false);
  106. /** 区县接口附带的气象预警 / 胁迫,确认时写入 ENTRY_FARM_DATA */
  107. const weatherMeta = reactive({
  108. weatherRisk: "",
  109. weatherStress: "",
  110. });
  111. const form = reactive({
  112. name: "",
  113. phone: "",
  114. crop: undefined,
  115. location: "",
  116. });
  117. const rules = {
  118. name: [
  119. { required: true, message: "请输入名字", trigger: "blur" },
  120. { min: 1, max: 20, message: "姓名长度不超过20个字", trigger: "blur" },
  121. ],
  122. phone: [
  123. { required: true, message: "请输入手机号", trigger: "blur" },
  124. {
  125. pattern: /^1[3-9]\d{9}$/,
  126. message: "请输入正确的手机号码",
  127. trigger: "blur",
  128. },
  129. ],
  130. crop: [{ required: true, message: "请选择种植品类", trigger: "change" }],
  131. location: [{ required: true, message: "请选择常驻点位", trigger: "change" }],
  132. };
  133. function getDefaultPoint() {
  134. return DEFAULT_POINT;
  135. }
  136. function readJson(key) {
  137. try {
  138. const raw = sessionStorage.getItem(key);
  139. return raw ? JSON.parse(raw) : null;
  140. } catch {
  141. return null;
  142. }
  143. }
  144. function saveFormDraft() {
  145. sessionStorage.setItem(
  146. FORM_KEY,
  147. JSON.stringify({ name: form.name, phone: form.phone, crop: form.crop })
  148. );
  149. }
  150. function restoreFormDraft() {
  151. const draft = readJson(FORM_KEY);
  152. if (!draft) return;
  153. if (draft.name != null) form.name = draft.name;
  154. if (draft.phone != null) form.phone = draft.phone;
  155. if (draft.crop) form.crop = draft.crop;
  156. }
  157. function utilWktToCoordinate(point) {
  158. const match = String(point).match(/POINT\s*\(([\d.\-]+)\s+([\d.\-]+)\)/i);
  159. if (!match) return null;
  160. return [Number(match[1]), Number(match[2])];
  161. }
  162. function initPreviewMap(point, showPoint = !!form.location) {
  163. if (!previewMapRef.value || !point) return;
  164. const previewPadding = [50, 0, 10, 0];
  165. const coordinate = utilWktToCoordinate(point);
  166. if (!previewMap) {
  167. previewMap = new SelectLocationMap();
  168. previewMap.initMap(point, previewMapRef.value, {
  169. enableClick: false,
  170. padding: previewPadding,
  171. showPoint,
  172. });
  173. return;
  174. }
  175. if (previewMap.kmap) {
  176. if (coordinate) {
  177. if (showPoint) {
  178. previewMap.showPoint(coordinate);
  179. } else {
  180. previewMap.hidePoint();
  181. previewMap.setMapPosition(coordinate, false);
  182. }
  183. }
  184. return;
  185. }
  186. previewMap.initMap(point, previewMapRef.value, {
  187. enableClick: false,
  188. padding: previewPadding,
  189. showPoint,
  190. });
  191. }
  192. function syncLocationFromSession() {
  193. const saved = readJson(LOCATION_KEY);
  194. if (saved?.point) {
  195. form.location = saved.point;
  196. formRef.value?.clearValidate("location");
  197. nextTick(() => initPreviewMap(saved.point, true));
  198. return;
  199. }
  200. nextTick(() => initPreviewMap(getDefaultPoint(), false));
  201. }
  202. /** 解析当前区县 code:优先选点 adcode,否则默认宁晋县 */
  203. function resolveCountyCode() {
  204. const saved = readJson(LOCATION_KEY);
  205. if (saved?.adcode) return String(saved.adcode);
  206. return DEFAULT_COUNTY_CODE;
  207. }
  208. async function fetchCropOptions() {
  209. const countyCode = resolveCountyCode();
  210. cropLoading.value = true;
  211. try {
  212. const res = await VE_API.questionnaire.countyPhenophase({
  213. county_code: countyCode,
  214. });
  215. if (res?.code != null && res.code !== 200) {
  216. cropOptions.value = [];
  217. weatherMeta.weatherRisk = "";
  218. weatherMeta.weatherStress = "";
  219. ElMessage.error(res.msg || "获取种植品类失败");
  220. return;
  221. }
  222. weatherMeta.weatherRisk = res?.data?.weather_risk || "";
  223. weatherMeta.weatherStress = res?.data?.weather_stress || "";
  224. const list = Array.isArray(res?.data?.phenophases) ? res.data.phenophases : [];
  225. const options = [];
  226. const seen = new Set();
  227. list.forEach((item) => {
  228. const name = item?.crop_category_name;
  229. if (typeof name !== "string" || !name.trim() || seen.has(name)) return;
  230. seen.add(name);
  231. options.push({
  232. name,
  233. code: item?.phenophase_code != null ? String(item.phenophase_code) : "",
  234. });
  235. });
  236. cropOptions.value = options;
  237. if (form.crop && !seen.has(form.crop)) {
  238. form.crop = undefined;
  239. }
  240. } catch {
  241. cropOptions.value = [];
  242. weatherMeta.weatherRisk = "";
  243. weatherMeta.weatherStress = "";
  244. ElMessage.error("获取种植品类失败,请稍后再试");
  245. } finally {
  246. cropLoading.value = false;
  247. }
  248. }
  249. const goSelectLocation = () => {
  250. saveFormDraft();
  251. const mapCenter = form.location || getDefaultPoint();
  252. router.push({
  253. path: "/entry_select_location",
  254. query: { mapCenter },
  255. });
  256. };
  257. function clearFormDraft() {
  258. form.name = "";
  259. form.phone = "";
  260. form.crop = undefined;
  261. form.location = "";
  262. sessionStorage.removeItem(FORM_KEY);
  263. sessionStorage.removeItem(LOCATION_KEY);
  264. sessionStorage.removeItem(STEP_KEY);
  265. }
  266. /** 根据点位反查地区名(区县优先) */
  267. async function resolveRegionName(point) {
  268. const coordinate = utilWktToCoordinate(point);
  269. if (!coordinate) return "";
  270. try {
  271. const { result } = await VE_API.old_mini_map.location({
  272. key: MAP_KEY,
  273. location: `${coordinate[1]},${coordinate[0]}`,
  274. });
  275. return (
  276. result?.ad_info?.district ||
  277. result?.ad_info?.city ||
  278. result?.address_component?.district ||
  279. result?.address_component?.city ||
  280. ""
  281. );
  282. } catch {
  283. return "";
  284. }
  285. }
  286. function toFiniteNumber(value, fallback) {
  287. const n = Number(value);
  288. return Number.isFinite(n) ? n : fallback;
  289. }
  290. /** 邀请管理员 id:优先路由参数 */
  291. function resolveAdminId() {
  292. return toFiniteNumber(route.query.adminId ?? route.query.admin_id, 1);
  293. }
  294. function resolveUserId() {
  295. return toFiniteNumber(store.state.home.miniUserId || localStorage.getItem("MINI_USER_ID"), 1);
  296. }
  297. /** 已选农机 id(当前简化流程可能为空) */
  298. function resolveMachineIds() {
  299. try {
  300. const cache = JSON.parse(sessionStorage.getItem(EQUIPMENT_KEY) || "null");
  301. if (!cache) return [];
  302. const list = Array.isArray(cache)
  303. ? cache
  304. : [...(cache.field || []), ...(cache.fruit || []), ...(cache.list || [])];
  305. return list
  306. .map((item) => Number(item?.id ?? item?.machine_id))
  307. .filter((id) => Number.isFinite(id));
  308. } catch {
  309. return [];
  310. }
  311. }
  312. const handleNext = async () => {
  313. if (!formRef.value || submitting.value) return;
  314. try {
  315. await formRef.value.validate();
  316. } catch {
  317. return;
  318. }
  319. const saved = readJson(LOCATION_KEY);
  320. let region = saved?.region || "";
  321. if (!region && form.location) {
  322. region = await resolveRegionName(form.location);
  323. }
  324. if (!region) {
  325. ElMessage.warning("无法识别种植点位所属地区,请重新选择");
  326. return;
  327. }
  328. const crop = form.crop;
  329. const selectedCrop = cropOptions.value.find((item) => item.name === crop);
  330. const cropCode = selectedCrop?.code || "";
  331. const point = form.location || saved?.point || "";
  332. const countyCode = toFiniteNumber(saved?.adcode || resolveCountyCode(), Number(DEFAULT_COUNTY_CODE));
  333. submitting.value = true;
  334. try {
  335. const res = await VE_API.questionnaire.addPlotInfo({
  336. user_name: form.name,
  337. tel: form.phone,
  338. admin_id: resolveAdminId(),
  339. user_id: resolveUserId(),
  340. machine_id: resolveMachineIds(),
  341. county_code: countyCode,
  342. });
  343. if (res?.code != null && res.code !== 200) {
  344. ElMessage.error(res.msg || "提交失败,请稍后再试");
  345. return;
  346. }
  347. const farmData = {
  348. name: form.name,
  349. phone: form.phone,
  350. crop,
  351. cropCode,
  352. point,
  353. coordinate: saved?.coordinate || null,
  354. region,
  355. adcode: saved?.adcode || String(countyCode),
  356. weatherRisk: weatherMeta.weatherRisk,
  357. weatherStress: weatherMeta.weatherStress,
  358. };
  359. localStorage.setItem(ENTRY_FARM_DATA_KEY, JSON.stringify(farmData));
  360. if (point) {
  361. localStorage.setItem("GROWTH_REPORT_MAP_POINT", point);
  362. localStorage.setItem("selectedFarmPoint", point);
  363. }
  364. VE_API.questionnaire.generateReport({ region, crop, tel: form.phone }).catch(() => {});
  365. localStorage.setItem(HAS_FARM_KEY, "1");
  366. clearFormDraft();
  367. router.replace("/growth_report");
  368. ElMessage.success("上传成功,报告正在生成中...");
  369. } catch {
  370. ElMessage.error("提交失败,请稍后再试");
  371. } finally {
  372. submitting.value = false;
  373. }
  374. };
  375. onMounted(() => {
  376. restoreFormDraft();
  377. syncLocationFromSession();
  378. });
  379. onActivated(() => {
  380. restoreFormDraft();
  381. syncLocationFromSession();
  382. fetchCropOptions();
  383. });
  384. onBeforeUnmount(() => {
  385. previewMap?.clearLayer();
  386. previewMap = null;
  387. });
  388. </script>
  389. <style lang="scss" scoped>
  390. .ba-information {
  391. flex: 1;
  392. display: flex;
  393. flex-direction: column;
  394. overflow: hidden;
  395. padding-bottom: 80px;
  396. &__content {
  397. flex: 1;
  398. overflow-y: auto;
  399. padding: 8px 16px 20px;
  400. }
  401. .page-header {
  402. padding: 8px 4px 16px;
  403. .page-title {
  404. font-size: 26px;
  405. color: #005599;
  406. font-family: "PangMenZhengDao";
  407. line-height: 36px;
  408. }
  409. .page-subtitle {
  410. margin-top: 4px;
  411. font-size: 14px;
  412. color: rgba(46, 46, 46, 0.4);
  413. line-height: 20px;
  414. }
  415. }
  416. .info-card {
  417. background: #fff;
  418. border-radius: 12px;
  419. padding: 16px 14px 8px;
  420. box-shadow: 0 2px 12px rgba(33, 153, 248, 0.08);
  421. }
  422. .section-title {
  423. display: flex;
  424. align-items: center;
  425. gap: 8px;
  426. margin-bottom: 8px;
  427. font-size: 16px;
  428. font-weight: 600;
  429. color: #1a1a1a;
  430. .title-icon {
  431. width: 14px;
  432. height: 14px;
  433. flex-shrink: 0;
  434. object-fit: contain;
  435. }
  436. }
  437. .info-form {
  438. :deep(.el-form-item) {
  439. margin-bottom: 0;
  440. padding: 10px 0;
  441. }
  442. :deep(.el-form-item__label) {
  443. color: #1a1a1a;
  444. font-size: 15px;
  445. font-weight: 400;
  446. padding: 0;
  447. line-height: 32px;
  448. height: 32px;
  449. }
  450. :deep(.el-form-item__content) {
  451. justify-content: flex-end;
  452. line-height: 32px;
  453. }
  454. :deep(.el-form-item__error) {
  455. padding-top: 4px;
  456. text-align: right;
  457. }
  458. :deep(.el-input__wrapper) {
  459. box-shadow: none;
  460. background: transparent;
  461. padding: 0;
  462. .el-input__inner {
  463. text-align: right;
  464. color: #1a1a1a;
  465. font-size: 15px;
  466. &::placeholder {
  467. color: rgba(0, 0, 0, 0.25);
  468. }
  469. }
  470. }
  471. .crop-select {
  472. width: 120px;
  473. flex-shrink: 0;
  474. margin-left: auto;
  475. height: 32px;
  476. :deep(.el-select__wrapper) {
  477. box-shadow: none;
  478. background: transparent;
  479. padding: 0;
  480. height: 32px;
  481. min-height: 32px !important;
  482. width: 100%;
  483. align-items: center;
  484. flex-wrap: nowrap;
  485. justify-content: flex-end;
  486. }
  487. :deep(.el-select__selection) {
  488. position: relative;
  489. flex: 1;
  490. min-width: 0;
  491. height: 32px;
  492. display: flex;
  493. align-items: center;
  494. justify-content: flex-end;
  495. }
  496. :deep(.el-select__selected-item) {
  497. font-size: 15px;
  498. color: #1a1a1a;
  499. line-height: 32px;
  500. height: 32px;
  501. display: flex;
  502. align-items: center;
  503. justify-content: flex-end;
  504. }
  505. :deep(.el-select__placeholder) {
  506. position: absolute;
  507. inset: 0;
  508. display: flex;
  509. align-items: center;
  510. justify-content: flex-end;
  511. margin: 0;
  512. font-size: 15px;
  513. color: #1a1a1a;
  514. line-height: 32px;
  515. transform: none;
  516. }
  517. /* EP:is-transparent 表示未选中的占位态,不是隐藏文字 */
  518. :deep(.el-select__placeholder.is-transparent) {
  519. color: #2199f8;
  520. }
  521. :deep(.el-select__suffix) {
  522. display: flex;
  523. align-items: center;
  524. height: 32px;
  525. flex-shrink: 0;
  526. }
  527. :deep(.el-select__caret) {
  528. color: #2199f8;
  529. font-size: 14px;
  530. }
  531. }
  532. .location-form-item {
  533. display: block;
  534. :deep(.el-form-item__label) {
  535. float: none;
  536. display: block;
  537. text-align: left;
  538. width: auto !important;
  539. margin-bottom: 10px;
  540. height: auto;
  541. line-height: 22px;
  542. }
  543. :deep(.el-form-item__content) {
  544. margin-left: 0 !important;
  545. line-height: normal;
  546. justify-content: flex-start;
  547. }
  548. :deep(.el-form-item__error) {
  549. text-align: left;
  550. padding-top: 6px;
  551. }
  552. }
  553. }
  554. .location-block {
  555. width: 100%;
  556. .map-preview {
  557. position: relative;
  558. width: 100%;
  559. height: 140px;
  560. border-radius: 8px;
  561. overflow: hidden;
  562. background: linear-gradient(135deg, #e8eef3 0%, #d4dde6 100%);
  563. &__map {
  564. width: 100%;
  565. height: 100%;
  566. pointer-events: none;
  567. }
  568. &__mask {
  569. position: absolute;
  570. left: 0;
  571. top: 0;
  572. z-index: 2;
  573. padding: 0 12px;
  574. height: 30px;
  575. line-height: 30px;
  576. background: rgba(0, 0, 0, 0.4);
  577. color: #fff;
  578. font-size: 14px;
  579. text-align: center;
  580. width: fit-content;
  581. border-radius: 8px 0 8px 0;
  582. }
  583. }
  584. }
  585. .custom-bottom-fixed-btns {
  586. background: #fff;
  587. box-shadow: 2px 2px 5px 0px rgba(0, 0, 0, 0.4);
  588. .bottom-btn {
  589. padding: 0 30px;
  590. height: 40px;
  591. line-height: 40px;
  592. font-size: 14px;
  593. border-radius: 25px;
  594. &.disabled {
  595. opacity: 0.6;
  596. pointer-events: none;
  597. }
  598. }
  599. .primary-btn {
  600. background: #2199f8;
  601. color: #fff;
  602. }
  603. }
  604. }
  605. </style>
  606. <style lang="scss">
  607. .crop-select-popper {
  608. min-width: 120px !important;
  609. }
  610. </style>