restorePlantingPopup.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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 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. // Props
  107. // ---------------------------------------------------------------------------
  108. const props = defineProps({
  109. /** 分区 id,初始互动接口必填 */
  110. zoneId: { type: [Number, String], default: 42 },
  111. });
  112. // ---------------------------------------------------------------------------
  113. // 常量
  114. // ---------------------------------------------------------------------------
  115. const PHENOLOGY_PREFIX = "您的农场是否进入到 ";
  116. const DEFAULT_SAMPLE_IMAGE = require("@/assets/img/agricultural/baidian.png");
  117. const DEFAULT_PHENOLOGY_IMAGE = require("@/assets/img/common/sd-1.jpg");
  118. /** 巡园向导预览图 */
  119. const PATROL_GUIDE_IMAGE_PATH =
  120. "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";
  121. const patrolGuideImage = base_img_url2 + PATROL_GUIDE_IMAGE_PATH;
  122. // ---------------------------------------------------------------------------
  123. // 页面状态
  124. // ---------------------------------------------------------------------------
  125. /** 弹窗显隐由接口数据控制,默认不展示 */
  126. const visible = ref(false);
  127. const questionType = ref("time"); // time | phenology
  128. const selectedDate = ref("");
  129. const phenologyAnswer = ref("");
  130. const submitting = ref(false);
  131. const hasDateInteract = ref(false);
  132. const hasOptionsInteract = ref(false);
  133. /** date_interact 原始数据,提交时可能用到 */
  134. const dateInteractData = ref(null);
  135. /** options_interact.interact 映射表,key 为 start_with 同类序号 */
  136. const interactMap = ref({});
  137. /** 当前展示的物候期序号(对应 interact 的 key) */
  138. const currentInteractKey = ref("");
  139. /** 用户确认「已到达」后写入,提交接口使用 */
  140. const selectedPeriodCode = ref("");
  141. const questionText = ref("");
  142. const highlightWord = ref("");
  143. const hintText = ref("");
  144. const sampleImage = ref(DEFAULT_SAMPLE_IMAGE);
  145. const phenologyQuestion = ref("");
  146. const phenologyHighlight = ref("");
  147. const phenologyImage = ref(DEFAULT_PHENOLOGY_IMAGE);
  148. // ---------------------------------------------------------------------------
  149. // 计算属性
  150. // ---------------------------------------------------------------------------
  151. const phenologyOptions = computed(() => [
  152. { value: "not_reached", label: t("agriFile.notReached") },
  153. { value: "reached", label: t("agriFile.reached") },
  154. { value: "passed", label: t("agriFile.passed") },
  155. ]);
  156. const interactKeys = computed(() =>
  157. Object.keys(interactMap.value).sort((a, b) => Number(a) - Number(b))
  158. );
  159. // ---------------------------------------------------------------------------
  160. // 工具函数
  161. // ---------------------------------------------------------------------------
  162. /** 当天日期 YYYY-MM-DD */
  163. const formatToday = () => {
  164. const d = new Date();
  165. const y = d.getFullYear();
  166. const m = String(d.getMonth() + 1).padStart(2, "0");
  167. const day = String(d.getDate()).padStart(2, "0");
  168. return `${y}-${m}-${day}`;
  169. };
  170. /** 日期选择器 YYYY.MM.DD → 接口 YYYY-MM-DD */
  171. const toApiDate = (value) => (value ? String(value).replace(/\./g, "-") : "");
  172. /** 拼接 CDN 图片地址 */
  173. const resolveImageUrl = (path) => {
  174. if (!path) return "";
  175. if (/^https?:\/\//i.test(path)) return path;
  176. return base_img_url2 + String(path).replace(/^\//, "");
  177. };
  178. /** 从 interact_theme 引号中提取高亮词,如「来梢」 */
  179. const extractHighlightWord = (text) => {
  180. if (!text) return "";
  181. const match = String(text).match(/[“"「]([^”"」]+)[”"」]/);
  182. return match ? match[1] : "";
  183. };
  184. /** Message 需高于弹窗 z-index: 20000 */
  185. const showMessage = (message, type = "warning") => {
  186. ElMessage({
  187. message,
  188. type,
  189. customClass: "restore-planting-message",
  190. zIndex: 40000,
  191. });
  192. };
  193. const showWarning = (message) => showMessage(message, "warning");
  194. const resetForm = () => {
  195. questionType.value = hasDateInteract.value ? "time" : "phenology";
  196. selectedDate.value = "";
  197. phenologyAnswer.value = "";
  198. selectedPeriodCode.value = "";
  199. };
  200. // ---------------------------------------------------------------------------
  201. // 业务逻辑:填充问题 / 物候期切换
  202. // ---------------------------------------------------------------------------
  203. const applyDateInteract = (data) => {
  204. dateInteractData.value = data;
  205. questionText.value = data.interact_theme || "";
  206. highlightWord.value = extractHighlightWord(data.interact_theme);
  207. hintText.value = data.interact_reason || "";
  208. sampleImage.value = resolveImageUrl(data.guide_url) || DEFAULT_SAMPLE_IMAGE;
  209. };
  210. /** 按 interact key 刷新问题二文案与配图 */
  211. const applyPhenologyByKey = (key) => {
  212. const item = interactMap.value[key];
  213. if (!item) return;
  214. currentInteractKey.value = String(key);
  215. const name = item.period_name || "";
  216. phenologyHighlight.value = name;
  217. phenologyQuestion.value = `${PHENOLOGY_PREFIX}${name} ?`;
  218. phenologyImage.value = resolveImageUrl(item.url) || DEFAULT_PHENOLOGY_IMAGE;
  219. selectedPeriodCode.value = "";
  220. };
  221. /** 暂未到达 → 前一个;已过 → 后一个 */
  222. const movePhenology = (direction) => {
  223. const keys = interactKeys.value;
  224. const idx = keys.indexOf(String(currentInteractKey.value));
  225. if (idx < 0) return false;
  226. const nextIdx = direction === "prev" ? idx - 1 : idx + 1;
  227. if (nextIdx < 0 || nextIdx >= keys.length) {
  228. showWarning(direction === "prev" ? "已是最早物候期" : "已是最晚物候期");
  229. return false;
  230. }
  231. applyPhenologyByKey(keys[nextIdx]);
  232. phenologyAnswer.value = "";
  233. return true;
  234. };
  235. const applyQuestionData = (data) => {
  236. hasDateInteract.value = !!data?.date_interact;
  237. hasOptionsInteract.value = !!data?.options_interact?.interact;
  238. if (hasDateInteract.value) {
  239. applyDateInteract(data.date_interact);
  240. }
  241. if (hasOptionsInteract.value) {
  242. interactMap.value = data.options_interact.interact || {};
  243. const startKey = String(data.options_interact.start_with ?? interactKeys.value[0] ?? "");
  244. applyPhenologyByKey(startKey);
  245. }
  246. // 有时间题优先展示;仅物候题则直接进入问题二
  247. questionType.value = hasDateInteract.value ? "time" : "phenology";
  248. visible.value = hasDateInteract.value || hasOptionsInteract.value;
  249. };
  250. /** 请求接口判断是否展示,并填充问题内容 */
  251. const checkShouldShow = async () => {
  252. try {
  253. const res = await VE_API.questionnaire.getInitialInteraction({
  254. zone_id: props.zoneId,
  255. date: formatToday(),
  256. // date: '2025-07-10',
  257. });
  258. if (res?.code === 200 && res.data) {
  259. applyQuestionData(res.data);
  260. } else {
  261. visible.value = false;
  262. }
  263. } catch (e) {
  264. visible.value = false;
  265. }
  266. };
  267. // ---------------------------------------------------------------------------
  268. // 事件处理 / 生命周期
  269. // ---------------------------------------------------------------------------
  270. onMounted(() => {
  271. checkShouldShow();
  272. });
  273. /**
  274. * 暂未到达 / 已过:立刻切到相邻物候期;
  275. * 已到达:选中并记下当前 period_code,供提交使用
  276. */
  277. const handlePhenologySelect = (value) => {
  278. if (value === "not_reached") {
  279. movePhenology("prev");
  280. return;
  281. }
  282. if (value === "passed") {
  283. movePhenology("next");
  284. return;
  285. }
  286. phenologyAnswer.value = "reached";
  287. const current = interactMap.value[currentInteractKey.value];
  288. selectedPeriodCode.value = current?.period_code || "";
  289. };
  290. /**
  291. * 组装保存感知结果载荷
  292. * @param {'time'|'phenology'} type 问题一不含 period_code;问题二不含 interact_code
  293. */
  294. const buildSubmitPayload = (type = "time") => {
  295. const payload = {
  296. zone_id: Number(props.zoneId) || props.zoneId,
  297. date: toApiDate(selectedDate.value) || formatToday(),
  298. };
  299. if (type === "time") {
  300. payload.interact_code = dateInteractData.value?.interact_code || "";
  301. } else {
  302. payload.period_code = selectedPeriodCode.value || "";
  303. }
  304. return payload;
  305. };
  306. /** 从 axios 错误中取出可读信息 */
  307. const getRequestErrorMessage = (err) => {
  308. const data = err?.response?.data;
  309. if (typeof data === "string" && data) return data;
  310. if (data?.msg) return data.msg;
  311. if (data?.message) return data.message;
  312. if (err?.message) return err.message;
  313. return "提交失败,请稍后再试";
  314. };
  315. /**
  316. * 调用保存感知结果接口
  317. * @param {'time'|'phenology'} type
  318. * @param {{ closeOnSuccess?: boolean }} options closeOnSuccess=false 时成功后不关弹窗(用于继续答问题二)
  319. */
  320. const submitInteractResult = async (type = "time", { closeOnSuccess = true } = {}) => {
  321. if (submitting.value) return false;
  322. submitting.value = true;
  323. try {
  324. const res = await VE_API.questionnaire.saveInteractResult(buildSubmitPayload(type));
  325. if (res?.code === 200) {
  326. if (closeOnSuccess) {
  327. showMessage(res.msg || "提交成功", "success");
  328. visible.value = false;
  329. resetForm();
  330. }
  331. return true;
  332. }
  333. showMessage(res?.msg || "提交失败,请稍后再试", "error");
  334. return false;
  335. } catch (err) {
  336. showMessage(getRequestErrorMessage(err), "error");
  337. return false;
  338. } finally {
  339. submitting.value = false;
  340. }
  341. };
  342. const handleConfirm = async () => {
  343. if (submitting.value) return;
  344. // 问题一:立即确认 → 提交(不含 period_code)
  345. if (questionType.value === "time") {
  346. if (!selectedDate.value) {
  347. showWarning(t("agriFile.selectTime"));
  348. return;
  349. }
  350. const ok = await submitInteractResult("time", {
  351. // 还有问题二则提交成功后继续展示,不关弹窗
  352. closeOnSuccess: !hasOptionsInteract.value,
  353. });
  354. if (ok && hasOptionsInteract.value) {
  355. questionType.value = "phenology";
  356. }
  357. return;
  358. }
  359. // 问题二:需已选择「已到达」并写入 period_code;提交不含 interact_code
  360. if (phenologyAnswer.value !== "reached" || !selectedPeriodCode.value) {
  361. const current = interactMap.value[currentInteractKey.value];
  362. if (phenologyAnswer.value === "reached" && current?.period_code) {
  363. selectedPeriodCode.value = current.period_code;
  364. } else {
  365. showWarning("请确认当前物候期是否已到达");
  366. return;
  367. }
  368. }
  369. await submitInteractResult("phenology");
  370. };
  371. defineExpose({
  372. checkShouldShow,
  373. buildSubmitPayload,
  374. selectedPeriodCode,
  375. });
  376. </script>
  377. <style scoped lang="scss">
  378. .restore-planting-popup {
  379. width: 86%;
  380. padding: 18px 14px 16px;
  381. box-sizing: border-box;
  382. background: linear-gradient(180deg, #d7ecff 0%, #ffffff 28%);
  383. overflow: visible;
  384. &__header {
  385. display: flex;
  386. align-items: flex-start;
  387. justify-content: space-between;
  388. }
  389. &__title {
  390. font-size: 22px;
  391. line-height: 25px;
  392. color: #111;
  393. div {
  394. font-family: "PangMenZhengDao";
  395. }
  396. }
  397. .popup-content {
  398. position: relative;
  399. }
  400. &__decor {
  401. position: absolute;
  402. top: -8px;
  403. right: -4px;
  404. width: 78px;
  405. height: 78px;
  406. object-fit: contain;
  407. pointer-events: none;
  408. }
  409. &__card {
  410. margin-top: 12px;
  411. padding: 10px 12px 12px;
  412. border-radius: 12px;
  413. background: #eaf5ff;
  414. box-sizing: border-box;
  415. position: relative;
  416. z-index: 2;
  417. }
  418. &__question {
  419. font-size: 15px;
  420. line-height: 24px;
  421. color: #252525;
  422. font-weight: 500;
  423. }
  424. &__keyword {
  425. color: #2199f8;
  426. }
  427. &__hint {
  428. margin-top: 4px;
  429. font-size: 12px;
  430. line-height: 16px;
  431. color: #9e9e9e;
  432. }
  433. &__photo {
  434. position: relative;
  435. margin: 12px 0;
  436. border-radius: 8px;
  437. overflow: hidden;
  438. background: #dfeaf5;
  439. img {
  440. display: block;
  441. width: 100%;
  442. height: 148px;
  443. object-fit: cover;
  444. }
  445. &--guide img {
  446. height: 132px;
  447. }
  448. }
  449. &__guide-btn {
  450. position: absolute;
  451. top: 8px;
  452. right: 8px;
  453. height: 28px;
  454. padding: 0 10px;
  455. border-radius: 14px;
  456. background: rgba(0, 0, 0, 0.55);
  457. color: #fff;
  458. font-size: 12px;
  459. display: flex;
  460. align-items: center;
  461. justify-content: center;
  462. }
  463. &__options {
  464. margin-top: 12px;
  465. display: flex;
  466. gap: 8px;
  467. }
  468. &__option {
  469. position: relative;
  470. flex: 1;
  471. height: 36px;
  472. border-radius: 8px;
  473. background: #fff;
  474. border: 1px solid transparent;
  475. display: flex;
  476. align-items: center;
  477. justify-content: center;
  478. font-size: 13px;
  479. color: #333;
  480. box-sizing: border-box;
  481. &.active {
  482. border-color: #2199f8;
  483. color: #2199f8;
  484. }
  485. }
  486. &__option-check {
  487. position: absolute;
  488. top: 0;
  489. right: 0;
  490. width: 16px;
  491. height: 16px;
  492. border-radius: 0 6px 0 6px;
  493. background: #2199f8;
  494. color: #fff;
  495. display: flex;
  496. align-items: center;
  497. justify-content: center;
  498. font-size: 10px;
  499. }
  500. &__date-picker {
  501. width: 100%;
  502. :deep(.el-input__wrapper) {
  503. height: 40px;
  504. border-radius: 8px;
  505. box-shadow: none !important;
  506. background: #fff;
  507. padding: 0 12px;
  508. }
  509. :deep(.el-input__inner) {
  510. font-size: 14px;
  511. color: #333;
  512. }
  513. :deep(.el-input__prefix),
  514. :deep(.el-input__suffix) {
  515. color: rgba(0, 0, 0, 0.35);
  516. }
  517. }
  518. &__actions {
  519. margin-top: 12px;
  520. display: flex;
  521. align-items: center;
  522. justify-content: center;
  523. }
  524. &__btn {
  525. height: 40px;
  526. line-height: 40px;
  527. padding: 0 30px;
  528. border-radius: 20px;
  529. display: flex;
  530. align-items: center;
  531. justify-content: center;
  532. font-size: 16px;
  533. font-weight: 500;
  534. box-sizing: border-box;
  535. &--primary {
  536. // flex: 1;
  537. background: linear-gradient(180deg, #76c3ff 0%, #2199f8 100%);
  538. color: #fff;
  539. &.disabled {
  540. opacity: 0.6;
  541. pointer-events: none;
  542. }
  543. }
  544. }
  545. }
  546. </style>
  547. <!-- 日期面板 / 图片预览 / Message teleport 到 body,需高于弹窗 z-index: 20000 -->
  548. <style lang="scss">
  549. .restore-planting-date-popper {
  550. z-index: 30000 !important;
  551. }
  552. .PhotoSlider__Wrapper {
  553. z-index: 30001 !important;
  554. }
  555. .el-message.restore-planting-message {
  556. z-index: 40000 !important;
  557. }
  558. </style>