restorePlantingPopup.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. <template>
  2. <popup
  3. v-model:show="visible"
  4. round
  5. teleport="body"
  6. :z-index="20000"
  7. class="restore-planting-popup"
  8. :close-on-click-overlay="true"
  9. >
  10. <div class="popup-content">
  11. <img
  12. class="restore-planting-popup__decor"
  13. src="@/assets/img/agricultural/question.png"
  14. alt=""
  15. />
  16. <div class="restore-planting-popup__header">
  17. <div class="restore-planting-popup__title">
  18. <div>{{ t("agriFile.restoreFillQuestions") }}</div>
  19. <div>{{ t("agriFile.restorePlantingService") }}</div>
  20. </div>
  21. </div>
  22. <!-- 问题一:选择时间 -->
  23. <div v-if="questionType === 'time'" class="restore-planting-popup__card">
  24. <div class="restore-planting-popup__question">
  25. <template v-if="highlightWord && questionText.includes(highlightWord)">
  26. <span>{{ questionText.split(highlightWord)[0] }}</span>
  27. <span class="restore-planting-popup__keyword">{{ highlightWord }}</span>
  28. <span>{{ questionText.split(highlightWord).slice(1).join(highlightWord) }}</span>
  29. </template>
  30. <template v-else>{{ questionText }}</template>
  31. </div>
  32. <div v-if="hintText" class="restore-planting-popup__hint">{{ hintText }}</div>
  33. <div class="restore-planting-popup__photo">
  34. <img :src="sampleImage" alt="" />
  35. </div>
  36. <el-date-picker
  37. v-model="selectedDate"
  38. class="restore-planting-popup__date-picker"
  39. type="date"
  40. format="YYYY.MM.DD"
  41. value-format="YYYY.MM.DD"
  42. :placeholder="t('agriFile.selectTime')"
  43. :editable="false"
  44. :clearable="false"
  45. teleported
  46. popper-class="restore-planting-date-popper"
  47. style="width: 100%"
  48. />
  49. </div>
  50. <!-- 问题二:物候期选择 -->
  51. <div v-else class="restore-planting-popup__card">
  52. <div class="restore-planting-popup__question">
  53. <template v-if="phenologyHighlight && phenologyQuestion.includes(phenologyHighlight)">
  54. <span>{{ phenologyQuestion.split(phenologyHighlight)[0] }}</span>
  55. <span class="restore-planting-popup__keyword">{{ phenologyHighlight }}</span>
  56. <span>{{ phenologyQuestion.split(phenologyHighlight).slice(1).join(phenologyHighlight) }}</span>
  57. </template>
  58. <template v-else>{{ phenologyQuestion }}</template>
  59. </div>
  60. <div class="restore-planting-popup__photo restore-planting-popup__photo--guide">
  61. <img :src="phenologyImage" alt="" />
  62. <photo-provider :photo-closable="true">
  63. <photo-consumer :src="patrolGuideImage" :intro="patrolGuideImage">
  64. <div class="restore-planting-popup__guide-btn">
  65. {{ t("agriFile.viewPatrolGuide") }}
  66. </div>
  67. </photo-consumer>
  68. </photo-provider>
  69. </div>
  70. <div class="restore-planting-popup__options">
  71. <div
  72. v-for="item in phenologyOptions"
  73. :key="item.value"
  74. class="restore-planting-popup__option"
  75. :class="{ active: phenologyAnswer === item.value }"
  76. @click="handlePhenologySelect(item.value)"
  77. >
  78. <span>{{ item.label }}</span>
  79. <span v-if="phenologyAnswer === item.value" class="restore-planting-popup__option-check">
  80. <el-icon><Check /></el-icon>
  81. </span>
  82. </div>
  83. </div>
  84. </div>
  85. <div class="restore-planting-popup__actions">
  86. <div
  87. class="restore-planting-popup__btn restore-planting-popup__btn--primary"
  88. :class="{ disabled: submitting }"
  89. @click="handleConfirm"
  90. >
  91. {{ questionType === "time" ? t("agriFile.confirmNow") : t("agriFile.submitInfo") }}
  92. </div>
  93. </div>
  94. </div>
  95. </popup>
  96. </template>
  97. <script setup>
  98. import { computed, onMounted, ref } from "vue";
  99. import { Popup } from "vant";
  100. import { Check } from "@element-plus/icons-vue";
  101. import { ElMessage } from "element-plus";
  102. import { useI18n } from "@/i18n";
  103. import { base_img_url2 } from "@/api/config";
  104. const { t } = useI18n();
  105. // ---------------------------------------------------------------------------
  106. // 常量
  107. // ---------------------------------------------------------------------------
  108. const ENTRY_FARM_DATA_KEY = "ENTRY_FARM_DATA";
  109. const DEFAULT_SAMPLE_IMAGE = require("@/assets/img/agricultural/baidian.png");
  110. const DEFAULT_PHENOLOGY_IMAGE = require("@/assets/img/common/sd-1.jpg");
  111. /** 巡园向导预览图 */
  112. const PATROL_GUIDE_IMAGE_PATH =
  113. "e84a8f1b-9842-434c-951f-068ff6963f31/6c8b7135-95b7-4c04-82ef-a2daeb674d0c/DJI_202608190758_001_6c8b7135-95b7-4c04-82ef-a2daeb674d0c/DJI_20260819080818_0085_V_code-ws0fsmg8zg8u.jpeg";
  114. const patrolGuideImage = base_img_url2 + PATROL_GUIDE_IMAGE_PATH;
  115. /** adjustPhenology:已到达 / 暂未到达 / 已过 */
  116. const ADJUST_ACTION = {
  117. reached: 0,
  118. not_reached: 1,
  119. passed: 2,
  120. };
  121. // ---------------------------------------------------------------------------
  122. // 页面状态
  123. // ---------------------------------------------------------------------------
  124. /** 弹窗显隐由接口数据控制,默认不展示 */
  125. const visible = ref(false);
  126. const questionType = ref("time"); // time | phenology
  127. const selectedDate = ref("");
  128. const phenologyAnswer = ref("");
  129. const submitting = ref(false);
  130. /** 当前物候期编码,调整后用接口返回值替换 */
  131. const currentPhenophaseCode = ref("");
  132. const cropType = ref("");
  133. const userTel = ref("");
  134. const questionText = ref("");
  135. const highlightWord = ref("");
  136. const hintText = ref("");
  137. const sampleImage = ref(DEFAULT_SAMPLE_IMAGE);
  138. const phenologyQuestion = ref("");
  139. const phenologyHighlight = ref("");
  140. const phenologyImage = ref(DEFAULT_PHENOLOGY_IMAGE);
  141. // ---------------------------------------------------------------------------
  142. // 计算属性
  143. // ---------------------------------------------------------------------------
  144. const phenologyOptions = computed(() => [
  145. { value: "not_reached", label: t("agriFile.notReached") },
  146. { value: "reached", label: t("agriFile.reached") },
  147. { value: "passed", label: t("agriFile.passed") },
  148. ]);
  149. // ---------------------------------------------------------------------------
  150. // 工具函数
  151. // ---------------------------------------------------------------------------
  152. const readEntryFarmData = () => {
  153. try {
  154. return JSON.parse(localStorage.getItem(ENTRY_FARM_DATA_KEY) || "null");
  155. } catch {
  156. return null;
  157. }
  158. };
  159. /** 拼接 CDN 图片地址 */
  160. const resolveImageUrl = (path) => {
  161. if (!path) return "";
  162. if (/^https?:\/\//i.test(path)) return path;
  163. return base_img_url2 + String(path).replace(/^\//, "");
  164. };
  165. /** 从引号中提取高亮词,如「坐粒」 */
  166. const extractHighlightWord = (text) => {
  167. if (!text) return "";
  168. const match = String(text).match(/[“"「]([^”"」]+)[”"」]/);
  169. return match ? match[1] : "";
  170. };
  171. /** 从物候问题中提取高亮词,如「灌浆期」 */
  172. const extractPhenologyHighlight = (text) => {
  173. if (!text) return "";
  174. const match = String(text).match(/进入到\s*(.+?)\s*[??]?$/);
  175. return match ? match[1].trim() : "";
  176. };
  177. /** Message 需高于弹窗 z-index: 20000 */
  178. const showMessage = (message, type = "warning") => {
  179. ElMessage({
  180. message,
  181. type,
  182. customClass: "restore-planting-message",
  183. zIndex: 40000,
  184. });
  185. };
  186. const showWarning = (message) => showMessage(message, "warning");
  187. const resetForm = () => {
  188. questionType.value = "time";
  189. selectedDate.value = "";
  190. phenologyAnswer.value = "";
  191. };
  192. // ---------------------------------------------------------------------------
  193. // 业务逻辑:加载 / 调整物候问题
  194. // ---------------------------------------------------------------------------
  195. /** 用 phenologyProblem 返回填充问题一、问题二 */
  196. const applyQuestionData = (data) => {
  197. if (!data) return;
  198. const timeQuestion = data.phenophase_question_time || "";
  199. const phenoQuestion = data.phenophase_question || "";
  200. const pic = resolveImageUrl(data.phenophase_pic_url);
  201. questionText.value = timeQuestion;
  202. highlightWord.value = extractHighlightWord(timeQuestion);
  203. hintText.value = "";
  204. sampleImage.value = pic || DEFAULT_SAMPLE_IMAGE;
  205. phenologyQuestion.value = phenoQuestion;
  206. phenologyHighlight.value = extractPhenologyHighlight(phenoQuestion);
  207. phenologyImage.value = pic || DEFAULT_PHENOLOGY_IMAGE;
  208. // 有时间题先展示问题一,否则直接问题二
  209. questionType.value = timeQuestion ? "time" : "phenology";
  210. phenologyAnswer.value = "";
  211. };
  212. /** 拉取物候期问题 */
  213. const fetchPhenologyProblem = async (phenophaseCode) => {
  214. const res = await VE_API.questionnaire.phenologyProblem({
  215. crop_type: cropType.value,
  216. phenophase_code: phenophaseCode,
  217. });
  218. if (res?.code !== 200 || !res.data) {
  219. throw new Error(res?.msg || "获取问题失败");
  220. }
  221. return res.data;
  222. };
  223. /** 先查报告状态,exists=2 不展示;否则拉取物候问题 */
  224. const checkShouldShow = async () => {
  225. const farm = readEntryFarmData();
  226. const crop = farm?.crop || "";
  227. const code = farm?.cropCode || "";
  228. const tel = farm?.phone || "";
  229. if (!tel) {
  230. visible.value = false;
  231. return;
  232. }
  233. cropType.value = crop;
  234. currentPhenophaseCode.value = String(code || "");
  235. userTel.value = tel;
  236. try {
  237. const checkRes = await VE_API.questionnaire.checkReportGenerated({ tel });
  238. if (checkRes?.code !== 200 || Number(checkRes?.data?.exists) === 2) {
  239. visible.value = false;
  240. return;
  241. }
  242. if (!crop || !code) {
  243. visible.value = false;
  244. return;
  245. }
  246. const data = await fetchPhenologyProblem(currentPhenophaseCode.value);
  247. applyQuestionData(data);
  248. visible.value = !!(data.phenophase_question_time || data.phenophase_question);
  249. } catch {
  250. visible.value = false;
  251. }
  252. };
  253. /**
  254. * 暂未到达 / 已过:调 adjustPhenology,再用新 code 刷新问题二
  255. * @param {0|1|2} action
  256. */
  257. const adjustAndRefreshQuestion = async (action) => {
  258. if (submitting.value) return;
  259. submitting.value = true;
  260. try {
  261. const adjustRes = await VE_API.questionnaire.adjustPhenology({
  262. phenophase_code: currentPhenophaseCode.value,
  263. action,
  264. tel: userTel.value,
  265. });
  266. if (adjustRes?.code !== 200) {
  267. showMessage(adjustRes?.msg || "调整物候期失败", "error");
  268. return;
  269. }
  270. const nextCode = adjustRes?.data?.adjusted_phenophase_code;
  271. if (nextCode == null || nextCode === "") {
  272. showMessage("暂无更多物候期可调整", "warning");
  273. return;
  274. }
  275. currentPhenophaseCode.value = String(nextCode);
  276. const data = await fetchPhenologyProblem(currentPhenophaseCode.value);
  277. // 调整后只替换问题二(及配图),停留在问题二
  278. const phenoQuestion = data.phenophase_question || "";
  279. const pic = resolveImageUrl(data.phenophase_pic_url);
  280. phenologyQuestion.value = phenoQuestion;
  281. phenologyHighlight.value = extractPhenologyHighlight(phenoQuestion);
  282. phenologyImage.value = pic || DEFAULT_PHENOLOGY_IMAGE;
  283. if (pic) sampleImage.value = pic;
  284. phenologyAnswer.value = "";
  285. } catch (err) {
  286. showMessage(err?.response?.data?.msg || err?.message || "调整物候期失败", "error");
  287. } finally {
  288. submitting.value = false;
  289. }
  290. };
  291. /** 已到达:确认当前物候期 */
  292. const confirmReachedPhenology = async () => {
  293. if (submitting.value) return false;
  294. submitting.value = true;
  295. try {
  296. const res = await VE_API.questionnaire.adjustPhenology({
  297. phenophase_code: currentPhenophaseCode.value,
  298. action: ADJUST_ACTION.reached,
  299. tel: userTel.value,
  300. });
  301. if (res?.code !== 200) {
  302. showMessage(res?.msg || "提交失败,请稍后再试", "error");
  303. return false;
  304. }
  305. // 供诊断报告弹窗判断:已确认物候后才可弹出
  306. if (userTel.value) {
  307. localStorage.setItem(`PHENOLOGY_REACHED_${userTel.value}`, "1");
  308. }
  309. showMessage(res.msg || "提交成功", "success");
  310. visible.value = false;
  311. resetForm();
  312. return true;
  313. } catch (err) {
  314. showMessage(err?.response?.data?.msg || err?.message || "提交失败,请稍后再试", "error");
  315. return false;
  316. } finally {
  317. submitting.value = false;
  318. }
  319. };
  320. // ---------------------------------------------------------------------------
  321. // 事件处理 / 生命周期
  322. // ---------------------------------------------------------------------------
  323. onMounted(() => {
  324. checkShouldShow();
  325. });
  326. /**
  327. * 暂未到达 / 已过:调接口刷新问题;
  328. * 已到达:仅选中,提交时再调接口
  329. */
  330. const handlePhenologySelect = async (value) => {
  331. if (value === "not_reached") {
  332. phenologyAnswer.value = value;
  333. await adjustAndRefreshQuestion(ADJUST_ACTION.not_reached);
  334. return;
  335. }
  336. if (value === "passed") {
  337. phenologyAnswer.value = value;
  338. await adjustAndRefreshQuestion(ADJUST_ACTION.passed);
  339. return;
  340. }
  341. phenologyAnswer.value = "reached";
  342. };
  343. const handleConfirm = async () => {
  344. if (submitting.value) return;
  345. // 问题一:确认后直接进入问题二,不调接口
  346. if (questionType.value === "time") {
  347. if (!selectedDate.value) {
  348. showWarning(t("agriFile.selectTime"));
  349. return;
  350. }
  351. if (!phenologyQuestion.value) {
  352. showMessage("暂无物候期问题", "warning");
  353. visible.value = false;
  354. resetForm();
  355. return;
  356. }
  357. questionType.value = "phenology";
  358. return;
  359. }
  360. // 问题二:需选择「已到达」后提交
  361. if (phenologyAnswer.value !== "reached") {
  362. showWarning("请确认当前物候期是否已到达");
  363. return;
  364. }
  365. await confirmReachedPhenology();
  366. };
  367. defineExpose({
  368. checkShouldShow,
  369. });
  370. </script>
  371. <style scoped lang="scss">
  372. .restore-planting-popup {
  373. width: 86%;
  374. padding: 18px 14px 16px;
  375. box-sizing: border-box;
  376. background: linear-gradient(180deg, #d7ecff 0%, #ffffff 28%);
  377. overflow: visible;
  378. &__header {
  379. display: flex;
  380. align-items: flex-start;
  381. justify-content: space-between;
  382. }
  383. &__title {
  384. font-size: 22px;
  385. line-height: 25px;
  386. color: #111;
  387. div {
  388. font-family: "PangMenZhengDao";
  389. }
  390. }
  391. .popup-content {
  392. position: relative;
  393. }
  394. &__decor {
  395. position: absolute;
  396. top: -8px;
  397. right: -4px;
  398. width: 78px;
  399. height: 78px;
  400. object-fit: contain;
  401. pointer-events: none;
  402. }
  403. &__card {
  404. margin-top: 12px;
  405. padding: 10px 12px 12px;
  406. border-radius: 12px;
  407. background: #eaf5ff;
  408. box-sizing: border-box;
  409. position: relative;
  410. z-index: 2;
  411. }
  412. &__question {
  413. font-size: 15px;
  414. line-height: 24px;
  415. color: #252525;
  416. font-weight: 500;
  417. }
  418. &__keyword {
  419. color: #2199f8;
  420. }
  421. &__hint {
  422. margin-top: 4px;
  423. font-size: 12px;
  424. line-height: 16px;
  425. color: #9e9e9e;
  426. }
  427. &__photo {
  428. position: relative;
  429. margin: 12px 0;
  430. border-radius: 8px;
  431. overflow: hidden;
  432. background: #dfeaf5;
  433. img {
  434. display: block;
  435. width: 100%;
  436. height: 148px;
  437. object-fit: cover;
  438. }
  439. &--guide img {
  440. height: 132px;
  441. }
  442. }
  443. &__guide-btn {
  444. position: absolute;
  445. top: 8px;
  446. right: 8px;
  447. height: 28px;
  448. padding: 0 10px;
  449. border-radius: 14px;
  450. background: rgba(0, 0, 0, 0.55);
  451. color: #fff;
  452. font-size: 12px;
  453. display: flex;
  454. align-items: center;
  455. justify-content: center;
  456. }
  457. &__options {
  458. margin-top: 12px;
  459. display: flex;
  460. gap: 8px;
  461. }
  462. &__option {
  463. position: relative;
  464. flex: 1;
  465. height: 36px;
  466. border-radius: 8px;
  467. background: #fff;
  468. border: 1px solid transparent;
  469. display: flex;
  470. align-items: center;
  471. justify-content: center;
  472. font-size: 13px;
  473. color: #333;
  474. box-sizing: border-box;
  475. &.active {
  476. border-color: #2199f8;
  477. color: #2199f8;
  478. }
  479. }
  480. &__option-check {
  481. position: absolute;
  482. top: 0;
  483. right: 0;
  484. width: 16px;
  485. height: 16px;
  486. border-radius: 0 6px 0 6px;
  487. background: #2199f8;
  488. color: #fff;
  489. display: flex;
  490. align-items: center;
  491. justify-content: center;
  492. font-size: 10px;
  493. }
  494. &__date-picker {
  495. width: 100%;
  496. :deep(.el-input__wrapper) {
  497. height: 40px;
  498. border-radius: 8px;
  499. box-shadow: none !important;
  500. background: #fff;
  501. padding: 0 12px;
  502. }
  503. :deep(.el-input__inner) {
  504. font-size: 14px;
  505. color: #333;
  506. }
  507. :deep(.el-input__prefix),
  508. :deep(.el-input__suffix) {
  509. color: rgba(0, 0, 0, 0.35);
  510. }
  511. }
  512. &__actions {
  513. margin-top: 12px;
  514. display: flex;
  515. align-items: center;
  516. justify-content: center;
  517. }
  518. &__btn {
  519. height: 40px;
  520. line-height: 40px;
  521. padding: 0 30px;
  522. border-radius: 20px;
  523. display: flex;
  524. align-items: center;
  525. justify-content: center;
  526. font-size: 16px;
  527. font-weight: 500;
  528. box-sizing: border-box;
  529. &--primary {
  530. background: linear-gradient(180deg, #76c3ff 0%, #2199f8 100%);
  531. color: #fff;
  532. &.disabled {
  533. opacity: 0.6;
  534. pointer-events: none;
  535. }
  536. }
  537. }
  538. }
  539. </style>
  540. <!-- 日期面板 / 图片预览 / Message teleport 到 body,需高于弹窗 z-index: 20000 -->
  541. <style lang="scss">
  542. .restore-planting-date-popper {
  543. z-index: 30000 !important;
  544. }
  545. .PhotoSlider__Wrapper {
  546. z-index: 30001 !important;
  547. }
  548. .el-message.restore-planting-message {
  549. z-index: 40000 !important;
  550. }
  551. </style>