| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682 |
- <template>
- <popup
- v-model:show="visible"
- round
- teleport="body"
- :z-index="20000"
- class="restore-planting-popup"
- :close-on-click-overlay="true"
- >
- <div class="popup-content">
- <img
- class="restore-planting-popup__decor"
- src="@/assets/img/agricultural/question.png"
- alt=""
- />
- <div class="restore-planting-popup__header">
- <div class="restore-planting-popup__title">
- <div>{{ t("agriFile.restoreFillQuestions") }}</div>
- <div>{{ t("agriFile.restorePlantingService") }}</div>
- </div>
- </div>
- <!-- 问题一:选择时间 -->
- <div v-if="questionType === 'time'" class="restore-planting-popup__card">
- <div class="restore-planting-popup__question">
- <template v-if="highlightWord && questionText.includes(highlightWord)">
- <span>{{ questionText.split(highlightWord)[0] }}</span>
- <span class="restore-planting-popup__keyword">{{ highlightWord }}</span>
- <span>{{ questionText.split(highlightWord).slice(1).join(highlightWord) }}</span>
- </template>
- <template v-else>{{ questionText }}</template>
- </div>
- <div class="restore-planting-popup__hint">{{ hintText }}</div>
- <div class="restore-planting-popup__photo">
- <img :src="sampleImage" alt="" />
- </div>
- <el-date-picker
- v-model="selectedDate"
- class="restore-planting-popup__date-picker"
- type="date"
- format="YYYY.MM.DD"
- value-format="YYYY.MM.DD"
- :placeholder="t('agriFile.selectTime')"
- :editable="false"
- :clearable="false"
- teleported
- popper-class="restore-planting-date-popper"
- style="width: 100%"
- />
- </div>
- <!-- 问题二:物候期选择 -->
- <div v-else class="restore-planting-popup__card">
- <div class="restore-planting-popup__question">
- <template v-if="phenologyHighlight && phenologyQuestion.includes(phenologyHighlight)">
- <span>{{ phenologyQuestion.split(phenologyHighlight)[0] }}</span>
- <span class="restore-planting-popup__keyword">{{ phenologyHighlight }}</span>
- <span>{{ phenologyQuestion.split(phenologyHighlight).slice(1).join(phenologyHighlight) }}</span>
- </template>
- <template v-else>{{ phenologyQuestion }}</template>
- </div>
- <div class="restore-planting-popup__photo restore-planting-popup__photo--guide">
- <img :src="phenologyImage" alt="" />
- <photo-provider :photo-closable="true">
- <photo-consumer :src="patrolGuideImage" :intro="patrolGuideImage">
- <div class="restore-planting-popup__guide-btn">
- {{ t("agriFile.viewPatrolGuide") }}
- </div>
- </photo-consumer>
- </photo-provider>
- </div>
- <div class="restore-planting-popup__options">
- <div
- v-for="item in phenologyOptions"
- :key="item.value"
- class="restore-planting-popup__option"
- :class="{ active: phenologyAnswer === item.value }"
- @click="handlePhenologySelect(item.value)"
- >
- <span>{{ item.label }}</span>
- <span v-if="phenologyAnswer === item.value" class="restore-planting-popup__option-check">
- <el-icon><Check /></el-icon>
- </span>
- </div>
- </div>
- </div>
- <div class="restore-planting-popup__actions">
- <div
- class="restore-planting-popup__btn restore-planting-popup__btn--primary"
- :class="{ disabled: submitting }"
- @click="handleConfirm"
- >
- {{ questionType === "time" ? t("agriFile.confirmNow") : t("agriFile.submitInfo") }}
- </div>
- </div>
- </div>
- </popup>
- </template>
- <script setup>
- import { computed, onActivated, onDeactivated, ref } from "vue";
- import { Popup } from "vant";
- import { Check } from "@element-plus/icons-vue";
- import { ElMessage } from "element-plus";
- import { useI18n } from "@/i18n";
- import { base_img_url2 } from "@/api/config";
- const { t } = useI18n();
- // ---------------------------------------------------------------------------
- // Props
- // ---------------------------------------------------------------------------
- const props = defineProps({
- /** 分区 id;不传则读当前选中农场缓存 */
- zoneId: { type: [Number, String], default: null },
- });
- // ---------------------------------------------------------------------------
- // 常量
- // ---------------------------------------------------------------------------
- const PHENOLOGY_PREFIX = "您的农场是否进入到 ";
- const DEFAULT_SAMPLE_IMAGE = require("@/assets/img/agricultural/baidian.png");
- const DEFAULT_PHENOLOGY_IMAGE = require("@/assets/img/common/sd-1.jpg");
- /** 巡园向导预览图 */
- const PATROL_GUIDE_IMAGE_PATH =
- "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";
- const patrolGuideImage = base_img_url2 + PATROL_GUIDE_IMAGE_PATH;
- // ---------------------------------------------------------------------------
- // 页面状态
- // ---------------------------------------------------------------------------
- /** 弹窗显隐由接口数据控制,默认不展示 */
- const visible = ref(false);
- const questionType = ref("time"); // time | phenology
- const selectedDate = ref("");
- const phenologyAnswer = ref("");
- const submitting = ref(false);
- const hasDateInteract = ref(false);
- const hasOptionsInteract = ref(false);
- /** date_interact 原始数据,提交时可能用到 */
- const dateInteractData = ref(null);
- /** options_interact.interact 映射表,key 为 start_with 同类序号 */
- const interactMap = ref({});
- /** 当前展示的物候期序号(对应 interact 的 key) */
- const currentInteractKey = ref("");
- /** 用户确认「已到达」后写入,提交接口使用 */
- const selectedPeriodCode = ref("");
- const questionText = ref("");
- const highlightWord = ref("");
- const hintText = ref("");
- const sampleImage = ref(DEFAULT_SAMPLE_IMAGE);
- const phenologyQuestion = ref("");
- const phenologyHighlight = ref("");
- const phenologyImage = ref(DEFAULT_PHENOLOGY_IMAGE);
- // ---------------------------------------------------------------------------
- // 计算属性
- // ---------------------------------------------------------------------------
- const phenologyOptions = computed(() => [
- { value: "not_reached", label: t("agriFile.notReached") },
- { value: "reached", label: t("agriFile.reached") },
- { value: "passed", label: t("agriFile.passed") },
- ]);
- const interactKeys = computed(() =>
- Object.keys(interactMap.value).sort((a, b) => Number(a) - Number(b))
- );
- const getSelectedFarmId = () => {
- try {
- const farm = JSON.parse(localStorage.getItem("selectedFarmData") || "{}");
- return String(
- localStorage.getItem("selectedFarmId") || farm.farm_id || farm.id || ""
- );
- } catch {
- return String(localStorage.getItem("selectedFarmId") || "");
- }
- };
- const getSelectedZoneId = () => {
- try {
- const farm = JSON.parse(localStorage.getItem("selectedFarmData") || "{}");
- return farm.zone_id ?? farm.zoneId ?? null;
- } catch {
- return null;
- }
- };
- /** 每次实时读缓存,避免 computed 缓存旧 zone_id */
- const resolveZoneId = () => props.zoneId ?? getSelectedZoneId();
- /** 上次成功请求对应的农场/分区 */
- let lastFetchedFarmKey = "";
- /** 递增后旧请求的返回不再打开弹窗 */
- let requestSeq = 0;
- /** 当前页是否处于前台,离开后迟到的响应不展示 */
- let pageActive = false;
- const getFarmFetchKey = () => `${getSelectedFarmId()}|${resolveZoneId() ?? ""}`;
- // ---------------------------------------------------------------------------
- // 工具函数
- // ---------------------------------------------------------------------------
- /** 当天日期 YYYY-MM-DD */
- const formatToday = () => {
- const d = new Date();
- const y = d.getFullYear();
- const m = String(d.getMonth() + 1).padStart(2, "0");
- const day = String(d.getDate()).padStart(2, "0");
- return `${y}-${m}-${day}`;
- };
- /** 日期选择器 YYYY.MM.DD → 接口 YYYY-MM-DD */
- const toApiDate = (value) => (value ? String(value).replace(/\./g, "-") : "");
- /** 拼接 CDN 图片地址 */
- const resolveImageUrl = (path) => {
- if (!path) return "";
- if (/^https?:\/\//i.test(path)) return path;
- return base_img_url2 + String(path).replace(/^\//, "");
- };
- /** 从 interact_theme 引号中提取高亮词,如「来梢」 */
- const extractHighlightWord = (text) => {
- if (!text) return "";
- const match = String(text).match(/[“"「]([^”"」]+)[”"」]/);
- return match ? match[1] : "";
- };
- /** Message 需高于弹窗 z-index: 20000 */
- const showMessage = (message, type = "warning") => {
- ElMessage({
- message,
- type,
- customClass: "restore-planting-message",
- zIndex: 40000,
- });
- };
- const showWarning = (message) => showMessage(message, "warning");
- const resetForm = () => {
- questionType.value = hasDateInteract.value ? "time" : "phenology";
- selectedDate.value = "";
- phenologyAnswer.value = "";
- selectedPeriodCode.value = "";
- };
- // ---------------------------------------------------------------------------
- // 业务逻辑:填充问题 / 物候期切换
- // ---------------------------------------------------------------------------
- const applyDateInteract = (data) => {
- dateInteractData.value = data;
- questionText.value = data.interact_theme || "";
- highlightWord.value = extractHighlightWord(data.interact_theme);
- hintText.value = data.interact_reason || "";
- sampleImage.value = resolveImageUrl(data.guide_url) || DEFAULT_SAMPLE_IMAGE;
- };
- /** 按 interact key 刷新问题二文案与配图 */
- const applyPhenologyByKey = (key) => {
- const item = interactMap.value[key];
- if (!item) return;
- currentInteractKey.value = String(key);
- const name = item.period_name || "";
- phenologyHighlight.value = name;
- phenologyQuestion.value = `${PHENOLOGY_PREFIX}${name} ?`;
- phenologyImage.value = resolveImageUrl(item.url) || DEFAULT_PHENOLOGY_IMAGE;
- selectedPeriodCode.value = "";
- };
- /** 暂未到达 → 前一个;已过 → 后一个 */
- const movePhenology = (direction) => {
- const keys = interactKeys.value;
- const idx = keys.indexOf(String(currentInteractKey.value));
- if (idx < 0) return false;
- const nextIdx = direction === "prev" ? idx - 1 : idx + 1;
- if (nextIdx < 0 || nextIdx >= keys.length) {
- showWarning(direction === "prev" ? "已是最早物候期" : "已是最晚物候期");
- return false;
- }
- applyPhenologyByKey(keys[nextIdx]);
- phenologyAnswer.value = "";
- return true;
- };
- const applyQuestionData = (data) => {
- hasDateInteract.value = !!data?.date_interact;
- hasOptionsInteract.value = !!data?.options_interact?.interact;
- if (hasDateInteract.value) {
- applyDateInteract(data.date_interact);
- }
- if (hasOptionsInteract.value) {
- interactMap.value = data.options_interact.interact || {};
- const startKey = String(data.options_interact.start_with ?? interactKeys.value[0] ?? "");
- applyPhenologyByKey(startKey);
- }
- // 有时间题优先展示;仅物候题则直接进入问题二
- questionType.value = hasDateInteract.value ? "time" : "phenology";
- visible.value = hasDateInteract.value || hasOptionsInteract.value;
- };
- /** 请求接口判断是否展示,并填充问题内容 */
- const checkShouldShow = async () => {
- const seq = ++requestSeq;
- const zoneId = resolveZoneId();
- const fetchKey = getFarmFetchKey();
- if (!pageActive) return;
- if (zoneId == null || zoneId === "") {
- visible.value = false;
- lastFetchedFarmKey = fetchKey;
- return;
- }
- try {
- const res = await VE_API.questionnaire.getInitialInteraction({
- zone_id: zoneId,
- date: formatToday(),
- });
- // 已切页或已有更新的请求时,丢弃这次结果,避免弹窗叠加
- if (seq !== requestSeq || !pageActive) return;
- if (res?.code === 200 && res.data) {
- applyQuestionData(res.data);
- } else {
- visible.value = false;
- }
- lastFetchedFarmKey = fetchKey;
- } catch (e) {
- if (seq !== requestSeq || !pageActive) return;
- visible.value = false;
- }
- };
- // ---------------------------------------------------------------------------
- // 事件处理 / 生命周期
- // ---------------------------------------------------------------------------
- // 切换农场返回后重新请求初始互动
- onActivated(() => {
- pageActive = true;
- const fetchKey = getFarmFetchKey();
- if (fetchKey === lastFetchedFarmKey) return;
- visible.value = false;
- resetForm();
- checkShouldShow();
- });
- onDeactivated(() => {
- pageActive = false;
- requestSeq += 1;
- visible.value = false;
- });
- /**
- * 暂未到达 / 已过:立刻切到相邻物候期;
- * 已到达:选中并记下当前 period_code,供提交使用
- */
- const handlePhenologySelect = (value) => {
- if (value === "not_reached") {
- movePhenology("prev");
- return;
- }
- if (value === "passed") {
- movePhenology("next");
- return;
- }
- phenologyAnswer.value = "reached";
- const current = interactMap.value[currentInteractKey.value];
- selectedPeriodCode.value = current?.period_code || "";
- };
- /**
- * 组装保存感知结果载荷
- * @param {'time'|'phenology'} type 问题一不含 period_code;问题二不含 interact_code
- */
- const buildSubmitPayload = (type = "time") => {
- const payload = {
- zone_id: Number(resolveZoneId()) || resolveZoneId(),
- date: toApiDate(selectedDate.value) || formatToday(),
- };
- if (type === "time") {
- payload.interact_code = dateInteractData.value?.interact_code || "";
- } else {
- payload.period_code = selectedPeriodCode.value || "";
- }
- return payload;
- };
- /** 从 axios 错误中取出可读信息 */
- const getRequestErrorMessage = (err) => {
- const data = err?.response?.data;
- if (typeof data === "string" && data) return data;
- if (data?.msg) return data.msg;
- if (data?.message) return data.message;
- if (err?.message) return err.message;
- return "提交失败,请稍后再试";
- };
- /**
- * 调用保存感知结果接口
- * @param {'time'|'phenology'} type
- * @param {{ closeOnSuccess?: boolean }} options closeOnSuccess=false 时成功后不关弹窗(用于继续答问题二)
- */
- const submitInteractResult = async (type = "time", { closeOnSuccess = true } = {}) => {
- if (submitting.value) return false;
- submitting.value = true;
- try {
- const res = await VE_API.questionnaire.saveInteractResult(buildSubmitPayload(type));
- if (res?.code === 200) {
- if (closeOnSuccess) {
- showMessage(res.msg || "提交成功", "success");
- visible.value = false;
- resetForm();
- }
- return true;
- }
- showMessage(res?.msg || "提交失败,请稍后再试", "error");
- return false;
- } catch (err) {
- showMessage(getRequestErrorMessage(err), "error");
- return false;
- } finally {
- submitting.value = false;
- }
- };
- const handleConfirm = async () => {
- if (submitting.value) return;
- // 问题一:立即确认 → 提交(不含 period_code)
- if (questionType.value === "time") {
- if (!selectedDate.value) {
- showWarning(t("agriFile.selectTime"));
- return;
- }
- const ok = await submitInteractResult("time", {
- // 还有问题二则提交成功后继续展示,不关弹窗
- closeOnSuccess: !hasOptionsInteract.value,
- });
- if (ok && hasOptionsInteract.value) {
- questionType.value = "phenology";
- }
- return;
- }
- // 问题二:需已选择「已到达」并写入 period_code;提交不含 interact_code
- if (phenologyAnswer.value !== "reached" || !selectedPeriodCode.value) {
- const current = interactMap.value[currentInteractKey.value];
- if (phenologyAnswer.value === "reached" && current?.period_code) {
- selectedPeriodCode.value = current.period_code;
- } else {
- showWarning("请确认当前物候期是否已到达");
- return;
- }
- }
- await submitInteractResult("phenology");
- };
- defineExpose({
- checkShouldShow,
- buildSubmitPayload,
- selectedPeriodCode,
- });
- </script>
- <style scoped lang="scss">
- .restore-planting-popup {
- width: 86%;
- padding: 18px 14px 16px;
- box-sizing: border-box;
- background: linear-gradient(180deg, #d7ecff 0%, #ffffff 28%);
- overflow: visible;
- &__header {
- display: flex;
- align-items: flex-start;
- justify-content: space-between;
- }
- &__title {
- font-size: 22px;
- line-height: 25px;
- color: #111;
- div {
- font-family: "PangMenZhengDao";
- }
- }
- .popup-content {
- position: relative;
- }
- &__decor {
- position: absolute;
- top: -8px;
- right: -4px;
- width: 78px;
- height: 78px;
- object-fit: contain;
- pointer-events: none;
- }
- &__card {
- margin-top: 12px;
- padding: 10px 12px 12px;
- border-radius: 12px;
- background: #eaf5ff;
- box-sizing: border-box;
- position: relative;
- z-index: 2;
- }
- &__question {
- font-size: 15px;
- line-height: 24px;
- color: #252525;
- font-weight: 500;
- }
- &__keyword {
- color: #2199f8;
- }
- &__hint {
- margin-top: 4px;
- font-size: 12px;
- line-height: 16px;
- color: #9e9e9e;
- }
- &__photo {
- position: relative;
- margin: 12px 0;
- border-radius: 8px;
- overflow: hidden;
- background: #dfeaf5;
- img {
- display: block;
- width: 100%;
- height: 148px;
- object-fit: cover;
- }
- &--guide img {
- height: 132px;
- }
- }
- &__guide-btn {
- position: absolute;
- top: 8px;
- right: 8px;
- height: 28px;
- padding: 0 10px;
- border-radius: 14px;
- background: rgba(0, 0, 0, 0.55);
- color: #fff;
- font-size: 12px;
- display: flex;
- align-items: center;
- justify-content: center;
- }
- &__options {
- margin-top: 12px;
- display: flex;
- gap: 8px;
- }
- &__option {
- position: relative;
- flex: 1;
- height: 36px;
- border-radius: 8px;
- background: #fff;
- border: 1px solid transparent;
- display: flex;
- align-items: center;
- justify-content: center;
- font-size: 13px;
- color: #333;
- box-sizing: border-box;
- &.active {
- border-color: #2199f8;
- color: #2199f8;
- }
- }
- &__option-check {
- position: absolute;
- top: 0;
- right: 0;
- width: 16px;
- height: 16px;
- border-radius: 0 6px 0 6px;
- background: #2199f8;
- color: #fff;
- display: flex;
- align-items: center;
- justify-content: center;
- font-size: 10px;
- }
- &__date-picker {
- width: 100%;
- :deep(.el-input__wrapper) {
- height: 40px;
- border-radius: 8px;
- box-shadow: none !important;
- background: #fff;
- padding: 0 12px;
- }
- :deep(.el-input__inner) {
- font-size: 14px;
- color: #333;
- }
- :deep(.el-input__prefix),
- :deep(.el-input__suffix) {
- color: rgba(0, 0, 0, 0.35);
- }
- }
- &__actions {
- margin-top: 12px;
- display: flex;
- align-items: center;
- justify-content: center;
- }
- &__btn {
- height: 40px;
- line-height: 40px;
- padding: 0 30px;
- border-radius: 20px;
- display: flex;
- align-items: center;
- justify-content: center;
- font-size: 16px;
- font-weight: 500;
- box-sizing: border-box;
- &--primary {
- // flex: 1;
- background: linear-gradient(180deg, #76c3ff 0%, #2199f8 100%);
- color: #fff;
- &.disabled {
- opacity: 0.6;
- pointer-events: none;
- }
- }
- }
- }
- </style>
- <!-- 日期面板 / 图片预览 / Message teleport 到 body,需高于弹窗 z-index: 20000 -->
- <style lang="scss">
- .restore-planting-date-popper {
- z-index: 30000 !important;
- }
- .PhotoSlider__Wrapper {
- z-index: 30001 !important;
- }
- .el-message.restore-planting-message {
- z-index: 40000 !important;
- }
- </style>
|