baInformation.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. >
  47. <el-option
  48. v-for="item in CROP_OPTIONS"
  49. :key="item"
  50. :label="item"
  51. :value="item"
  52. />
  53. </el-select>
  54. </el-form-item>
  55. <el-form-item label="种植点位" prop="location" class="location-form-item">
  56. <div class="location-block" @click="goSelectLocation">
  57. <div class="map-preview">
  58. <div class="map-preview__map" ref="previewMapRef"></div>
  59. <div v-if="!form.location" class="map-preview__mask">点击地图选择常驻点位</div>
  60. </div>
  61. </div>
  62. </el-form-item>
  63. </el-form>
  64. </div>
  65. </div>
  66. <div class="custom-bottom-fixed-btns">
  67. <div class="bottom-btn primary-btn" @click="handleNext">确认信息</div>
  68. </div>
  69. </div>
  70. </template>
  71. <script setup>
  72. import { nextTick, onActivated, onBeforeUnmount, onMounted, reactive, ref } from "vue";
  73. import { useRouter } from "vue-router";
  74. import { ElMessage } from "element-plus";
  75. import SelectLocationMap from "../map/selectLocationMap.js";
  76. const LOCATION_KEY = "ENTRY_RESIDENT_LOCATION";
  77. const FORM_KEY = "ENTRY_BA_FORM";
  78. const STEP_KEY = "ENTRY_INFORMATION_STEP";
  79. /** 本地标记:用户已新增农场 */
  80. const HAS_FARM_KEY = "HAS_ENTRY_FARM";
  81. /** 新增农场信息(点位、品类等) */
  82. const ENTRY_FARM_DATA_KEY = "ENTRY_FARM_DATA";
  83. /** 默认定位:河北宁晋县 */
  84. const DEFAULT_POINT = "POINT(114.91863572509733 37.67702031436258)";
  85. const MAP_KEY = "CZLBZ-LJICQ-R4A5J-BN62X-YXCRJ-GNBUT";
  86. /** 河北主要作物(固定下拉选项) */
  87. const CROP_OPTIONS = ["玉米", "冬小麦", "水稻", "马铃薯", "大豆", "谷子", "高粱", "莜麦"];
  88. const router = useRouter();
  89. const formRef = ref(null);
  90. const previewMapRef = ref(null);
  91. let previewMap = null;
  92. const form = reactive({
  93. name: "",
  94. phone: "",
  95. crop: undefined,
  96. location: "",
  97. });
  98. const rules = {
  99. name: [
  100. { required: true, message: "请输入名字", trigger: "blur" },
  101. { min: 1, max: 20, message: "姓名长度不超过20个字", trigger: "blur" },
  102. ],
  103. phone: [
  104. { required: true, message: "请输入手机号", trigger: "blur" },
  105. {
  106. pattern: /^1[3-9]\d{9}$/,
  107. message: "请输入正确的手机号码",
  108. trigger: "blur",
  109. },
  110. ],
  111. crop: [{ required: true, message: "请选择种植品类", trigger: "change" }],
  112. location: [{ required: true, message: "请选择常驻点位", trigger: "change" }],
  113. };
  114. function getDefaultPoint() {
  115. return DEFAULT_POINT;
  116. }
  117. function readJson(key) {
  118. try {
  119. const raw = sessionStorage.getItem(key);
  120. return raw ? JSON.parse(raw) : null;
  121. } catch {
  122. return null;
  123. }
  124. }
  125. function saveFormDraft() {
  126. sessionStorage.setItem(
  127. FORM_KEY,
  128. JSON.stringify({ name: form.name, phone: form.phone, crop: form.crop })
  129. );
  130. }
  131. function restoreFormDraft() {
  132. const draft = readJson(FORM_KEY);
  133. if (!draft) return;
  134. if (draft.name != null) form.name = draft.name;
  135. if (draft.phone != null) form.phone = draft.phone;
  136. if (draft.crop) form.crop = draft.crop;
  137. }
  138. function utilWktToCoordinate(point) {
  139. const match = String(point).match(/POINT\s*\(([\d.\-]+)\s+([\d.\-]+)\)/i);
  140. if (!match) return null;
  141. return [Number(match[1]), Number(match[2])];
  142. }
  143. function initPreviewMap(point, showPoint = !!form.location) {
  144. if (!previewMapRef.value || !point) return;
  145. const previewPadding = [50, 0, 10, 0];
  146. const coordinate = utilWktToCoordinate(point);
  147. if (!previewMap) {
  148. previewMap = new SelectLocationMap();
  149. previewMap.initMap(point, previewMapRef.value, {
  150. enableClick: false,
  151. padding: previewPadding,
  152. showPoint,
  153. });
  154. return;
  155. }
  156. if (previewMap.kmap) {
  157. if (coordinate) {
  158. if (showPoint) {
  159. previewMap.showPoint(coordinate);
  160. } else {
  161. previewMap.hidePoint();
  162. previewMap.setMapPosition(coordinate, false);
  163. }
  164. }
  165. return;
  166. }
  167. previewMap.initMap(point, previewMapRef.value, {
  168. enableClick: false,
  169. padding: previewPadding,
  170. showPoint,
  171. });
  172. }
  173. function syncLocationFromSession() {
  174. const saved = readJson(LOCATION_KEY);
  175. if (saved?.point) {
  176. form.location = saved.point;
  177. formRef.value?.clearValidate("location");
  178. nextTick(() => initPreviewMap(saved.point, true));
  179. return;
  180. }
  181. nextTick(() => initPreviewMap(getDefaultPoint(), false));
  182. }
  183. const goSelectLocation = () => {
  184. saveFormDraft();
  185. const mapCenter = form.location || getDefaultPoint();
  186. router.push({
  187. path: "/entry_select_location",
  188. query: { mapCenter },
  189. });
  190. };
  191. function clearFormDraft() {
  192. form.name = "";
  193. form.phone = "";
  194. form.crop = undefined;
  195. form.location = "";
  196. sessionStorage.removeItem(FORM_KEY);
  197. sessionStorage.removeItem(LOCATION_KEY);
  198. sessionStorage.removeItem(STEP_KEY);
  199. }
  200. /** 根据点位反查地区名(区县优先) */
  201. async function resolveRegionName(point) {
  202. const coordinate = utilWktToCoordinate(point);
  203. if (!coordinate) return "";
  204. try {
  205. const { result } = await VE_API.old_mini_map.location({
  206. key: MAP_KEY,
  207. location: `${coordinate[1]},${coordinate[0]}`,
  208. });
  209. return (
  210. result?.ad_info?.district ||
  211. result?.ad_info?.city ||
  212. result?.address_component?.district ||
  213. result?.address_component?.city ||
  214. ""
  215. );
  216. } catch {
  217. return "";
  218. }
  219. }
  220. const handleNext = async () => {
  221. if (!formRef.value) return;
  222. try {
  223. await formRef.value.validate();
  224. } catch {
  225. return;
  226. }
  227. const saved = readJson(LOCATION_KEY);
  228. let region = saved?.region || "";
  229. if (!region && form.location) {
  230. region = await resolveRegionName(form.location);
  231. }
  232. if (!region) {
  233. ElMessage.warning("无法识别种植点位所属地区,请重新选择");
  234. return;
  235. }
  236. const crop = form.crop;
  237. const point = form.location || saved?.point || "";
  238. const farmData = {
  239. name: form.name,
  240. phone: form.phone,
  241. crop,
  242. point,
  243. coordinate: saved?.coordinate || null,
  244. region,
  245. adcode: saved?.adcode || "",
  246. };
  247. localStorage.setItem(ENTRY_FARM_DATA_KEY, JSON.stringify(farmData));
  248. if (point) {
  249. localStorage.setItem("GROWTH_REPORT_MAP_POINT", point);
  250. localStorage.setItem("selectedFarmPoint", point);
  251. }
  252. VE_API.questionnaire.generateReport({ region, crop }).catch(() => {});
  253. localStorage.setItem(HAS_FARM_KEY, "1");
  254. clearFormDraft();
  255. router.replace("/growth_report");
  256. ElMessage.success("上传成功,报告正在生成中...");
  257. };
  258. onMounted(() => {
  259. restoreFormDraft();
  260. syncLocationFromSession();
  261. });
  262. onActivated(() => {
  263. restoreFormDraft();
  264. syncLocationFromSession();
  265. });
  266. onBeforeUnmount(() => {
  267. previewMap?.clearLayer();
  268. previewMap = null;
  269. });
  270. </script>
  271. <style lang="scss" scoped>
  272. .ba-information {
  273. flex: 1;
  274. display: flex;
  275. flex-direction: column;
  276. overflow: hidden;
  277. padding-bottom: 80px;
  278. &__content {
  279. flex: 1;
  280. overflow-y: auto;
  281. padding: 8px 16px 20px;
  282. }
  283. .page-header {
  284. padding: 8px 4px 16px;
  285. .page-title {
  286. font-size: 26px;
  287. color: #005599;
  288. font-family: "PangMenZhengDao";
  289. line-height: 36px;
  290. }
  291. .page-subtitle {
  292. margin-top: 4px;
  293. font-size: 14px;
  294. color: rgba(46, 46, 46, 0.4);
  295. line-height: 20px;
  296. }
  297. }
  298. .info-card {
  299. background: #fff;
  300. border-radius: 12px;
  301. padding: 16px 14px 8px;
  302. box-shadow: 0 2px 12px rgba(33, 153, 248, 0.08);
  303. }
  304. .section-title {
  305. display: flex;
  306. align-items: center;
  307. gap: 8px;
  308. margin-bottom: 8px;
  309. font-size: 16px;
  310. font-weight: 600;
  311. color: #1a1a1a;
  312. .title-icon {
  313. width: 14px;
  314. height: 14px;
  315. flex-shrink: 0;
  316. object-fit: contain;
  317. }
  318. }
  319. .info-form {
  320. :deep(.el-form-item) {
  321. margin-bottom: 0;
  322. padding: 10px 0;
  323. }
  324. :deep(.el-form-item__label) {
  325. color: #1a1a1a;
  326. font-size: 15px;
  327. font-weight: 400;
  328. padding: 0;
  329. line-height: 32px;
  330. height: 32px;
  331. }
  332. :deep(.el-form-item__content) {
  333. justify-content: flex-end;
  334. line-height: 32px;
  335. }
  336. :deep(.el-form-item__error) {
  337. padding-top: 4px;
  338. text-align: right;
  339. }
  340. :deep(.el-input__wrapper) {
  341. box-shadow: none;
  342. background: transparent;
  343. padding: 0;
  344. .el-input__inner {
  345. text-align: right;
  346. color: #1a1a1a;
  347. font-size: 15px;
  348. &::placeholder {
  349. color: rgba(0, 0, 0, 0.25);
  350. }
  351. }
  352. }
  353. .crop-select {
  354. width: 120px;
  355. flex-shrink: 0;
  356. margin-left: auto;
  357. height: 32px;
  358. :deep(.el-select__wrapper) {
  359. box-shadow: none;
  360. background: transparent;
  361. padding: 0;
  362. height: 32px;
  363. min-height: 32px !important;
  364. width: 100%;
  365. align-items: center;
  366. flex-wrap: nowrap;
  367. justify-content: flex-end;
  368. }
  369. :deep(.el-select__selection) {
  370. position: relative;
  371. flex: 1;
  372. min-width: 0;
  373. height: 32px;
  374. display: flex;
  375. align-items: center;
  376. justify-content: flex-end;
  377. }
  378. :deep(.el-select__selected-item) {
  379. font-size: 15px;
  380. color: #1a1a1a;
  381. line-height: 32px;
  382. height: 32px;
  383. display: flex;
  384. align-items: center;
  385. justify-content: flex-end;
  386. }
  387. :deep(.el-select__placeholder) {
  388. position: absolute;
  389. inset: 0;
  390. display: flex;
  391. align-items: center;
  392. justify-content: flex-end;
  393. margin: 0;
  394. font-size: 15px;
  395. color: #1a1a1a;
  396. line-height: 32px;
  397. transform: none;
  398. }
  399. /* EP:is-transparent 表示未选中的占位态,不是隐藏文字 */
  400. :deep(.el-select__placeholder.is-transparent) {
  401. color: #2199f8;
  402. }
  403. :deep(.el-select__suffix) {
  404. display: flex;
  405. align-items: center;
  406. height: 32px;
  407. flex-shrink: 0;
  408. }
  409. :deep(.el-select__caret) {
  410. color: #2199f8;
  411. font-size: 14px;
  412. }
  413. }
  414. .location-form-item {
  415. display: block;
  416. :deep(.el-form-item__label) {
  417. float: none;
  418. display: block;
  419. text-align: left;
  420. width: auto !important;
  421. margin-bottom: 10px;
  422. height: auto;
  423. line-height: 22px;
  424. }
  425. :deep(.el-form-item__content) {
  426. margin-left: 0 !important;
  427. line-height: normal;
  428. justify-content: flex-start;
  429. }
  430. :deep(.el-form-item__error) {
  431. text-align: left;
  432. padding-top: 6px;
  433. }
  434. }
  435. }
  436. .location-block {
  437. width: 100%;
  438. .map-preview {
  439. position: relative;
  440. width: 100%;
  441. height: 140px;
  442. border-radius: 8px;
  443. overflow: hidden;
  444. background: linear-gradient(135deg, #e8eef3 0%, #d4dde6 100%);
  445. &__map {
  446. width: 100%;
  447. height: 100%;
  448. pointer-events: none;
  449. }
  450. &__mask {
  451. position: absolute;
  452. left: 0;
  453. top: 0;
  454. z-index: 2;
  455. padding: 0 12px;
  456. height: 30px;
  457. line-height: 30px;
  458. background: rgba(0, 0, 0, 0.4);
  459. color: #fff;
  460. font-size: 14px;
  461. text-align: center;
  462. width: fit-content;
  463. border-radius: 8px 0 8px 0;
  464. }
  465. }
  466. }
  467. .custom-bottom-fixed-btns {
  468. background: #fff;
  469. box-shadow: 2px 2px 5px 0px rgba(0, 0, 0, 0.4);
  470. .bottom-btn {
  471. padding: 0 30px;
  472. height: 40px;
  473. line-height: 40px;
  474. font-size: 14px;
  475. border-radius: 25px;
  476. }
  477. .primary-btn {
  478. background: #2199f8;
  479. color: #fff;
  480. }
  481. }
  482. }
  483. </style>
  484. <style lang="scss">
  485. .crop-select-popper {
  486. min-width: 120px !important;
  487. }
  488. </style>