growthTrack.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. <template>
  2. <div class="growth-track-page">
  3. <custom-header
  4. :name="pageTitle"
  5. :isGoBack="!isFromShare"
  6. :isClose="isFromShare"
  7. :showClose="false"
  8. @goback="handleBack"
  9. />
  10. <div class="growth-track-body">
  11. <div class="growth-hero">
  12. <div class="growth-hero__top">
  13. <div class="growth-hero__main">
  14. <div class="growth-hero__title-row">
  15. <span class="growth-hero__title">{{ theme }}</span>
  16. <span class="growth-hero__tag growth-hero__tag--level">
  17. <el-icon>
  18. <BellFilled />
  19. </el-icon>
  20. {{ level }}
  21. </span>
  22. <span class="growth-hero__tag growth-hero__tag--zone">{{ zoneName }}</span>
  23. </div>
  24. <div class="growth-hero__reason">{{ reason }}</div>
  25. </div>
  26. <div class="growth-hero__share" @click="handleShare">
  27. <Icon name="share" />
  28. <span>{{ t("agriFile.forward") }}</span>
  29. </div>
  30. </div>
  31. </div>
  32. <div class="growth-content">
  33. <div class="growth-content__title">{{ t("agriFile.agriAssessment") }}</div>
  34. <div class="growth-content__desc">{{ assessmentDesc }}</div>
  35. </div>
  36. <div class="interact-card">
  37. <div class="interact-card__head">
  38. <div class="interact-card__tab">
  39. <img class="interact-card__star" src="@/assets/img/agricultural/start.png" alt="" />
  40. <span>{{ t("agriFile.interactQuestion") }}</span>
  41. </div>
  42. </div>
  43. <div class="interact-card__qa">
  44. <div v-if="currentTask?.interact_feedback" class="interact-card__phenology">
  45. {{ currentTask.interact_feedback }}
  46. </div>
  47. <template v-else>
  48. <div class="interact-card__question">{{ interactProblem }}</div>
  49. <div class="interact-card__options">
  50. <div
  51. v-for="item in interactOptions"
  52. :key="item.id"
  53. class="interact-option"
  54. :class="{ selected: selectedOptionId === item.id }"
  55. @click="selectedOptionId = item.id"
  56. >
  57. <span class="interact-option__text">{{ item.name }}</span>
  58. </div>
  59. </div>
  60. <div class="interact-card__submit" @click="handleSubmit">{{ t("agriFile.submit") }}</div>
  61. </template>
  62. </div>
  63. </div>
  64. <div class="guide-card">
  65. <div class="guide-title">{{ t("agriFile.patrolGuide") }}</div>
  66. <photo-provider :photo-closable="true">
  67. <photo-consumer :src="guideImage">
  68. <img class="guide-card__img" :src="guideImage" alt="" />
  69. </photo-consumer>
  70. </photo-provider>
  71. </div>
  72. </div>
  73. <div class="guide-card__upload" @click="handleUploadClick">
  74. <el-icon>
  75. <Plus />
  76. </el-icon>
  77. <span>{{ t("agriFile.uploadPhoto") }}</span>
  78. </div>
  79. <album-upload-popup v-model:show="showUploadPopup" @confirm="handleUploadConfirm" />
  80. <tip-popup
  81. v-model:show="showSuccessPopup"
  82. type="executeSuccess"
  83. text="您已上传成功"
  84. text2="请等待诊断报告生成"
  85. hideBtn
  86. >
  87. <template #footer>
  88. <div class="bottom-btn-container">
  89. <div class="bottom-btn primary-btn" @click="handleComplete">完成</div>
  90. </div>
  91. </template>
  92. </tip-popup>
  93. <leave-confirm-popup v-model:show="showLeavePopup" @confirm="confirmLeave" />
  94. </div>
  95. </template>
  96. <script setup>
  97. import { computed, nextTick, onActivated, ref } from "vue";
  98. import { useRoute, useRouter } from "vue-router";
  99. import { Icon } from "vant";
  100. import { ElMessage } from "element-plus";
  101. import customHeader from "@/components/customHeader.vue";
  102. import albumUploadPopup from "../components/albumUploadPopup.vue";
  103. import leaveConfirmPopup from "@/components/popup/leaveConfirmPopup.vue";
  104. import tipPopup from "@/components/popup/tipPopup.vue";
  105. import { useI18n } from "@/i18n";
  106. import { base_img_url2 } from "@/api/config";
  107. import wx from "weixin-js-sdk";
  108. const { t } = useI18n();
  109. const route = useRoute();
  110. const router = useRouter();
  111. // ---------- 常量 ----------
  112. const DEFAULT_GUIDE_IMAGE = require("@/assets/img/agricultural/patrol-guide.png");
  113. // ---------- 页面状态 ----------
  114. const interactTasks = ref([]);
  115. const currentTaskIndex = ref(0);
  116. const selectedOptionId = ref(null);
  117. const showUploadPopup = ref(false);
  118. const showSuccessPopup = ref(false);
  119. const showLeavePopup = ref(false);
  120. const needDraw = ref(true); // status≠1 时需先勾画种植范围
  121. // ---------- 计算属性 ----------
  122. const pageTitle = computed(() => route.query.pageName || t("agriFile.patrolGrowthTitle"));
  123. const id = computed(() => route.query.id);
  124. /** 通过分享进入:无返回箭头 */
  125. const isFromShare = computed(() => {
  126. if (isTruthyFlag(route.query.fromShare)) return true;
  127. const paramsPage = parseMaybeJson(route.query.paramsPage);
  128. if (isTruthyFlag(paramsPage?.fromShare)) return true;
  129. const miniJson = parseMaybeJson(route.query.miniJson);
  130. if (isTruthyFlag(miniJson?.fromShare)) return true;
  131. if (isTruthyFlag(parseMaybeJson(miniJson?.paramsPage)?.fromShare)) return true;
  132. return false;
  133. });
  134. const currentTask = computed(() => interactTasks.value[currentTaskIndex.value] || null);
  135. const currentContent = computed(() => currentTask.value?.interact_content || {});
  136. const theme = computed(
  137. () => currentContent.value.interact_title || t("agriFile.interactTheme")
  138. );
  139. const reason = computed(
  140. () => currentContent.value.interact_theme || t("agriFile.interactReason")
  141. );
  142. const level = computed(() => t("agriFile.riskLevel2"));
  143. const zoneName = computed(() => t("agriFile.zoneOne"));
  144. const assessmentDesc = computed(
  145. () => currentContent.value.interact_explain || t("agriFile.agriAssessmentDesc")
  146. );
  147. const interactProblem = computed(
  148. () => currentContent.value.interact_problem || t("agriFile.interactQuestionText")
  149. );
  150. const guideImage = computed(
  151. () => resolveImageUrl(currentContent.value.guide_url) || DEFAULT_GUIDE_IMAGE
  152. );
  153. const interactOptions = computed(() => {
  154. const options = currentContent.value.interact_options;
  155. if (!Array.isArray(options)) return [];
  156. return options.map((name, index) => ({
  157. id: index + 1,
  158. name: String(name),
  159. }));
  160. });
  161. // ---------- 工具函数 ----------
  162. function isTruthyFlag(value) {
  163. return value === 1 || value === "1" || value === true || value === "true";
  164. }
  165. function parseMaybeJson(value) {
  166. if (value == null || value === "") return null;
  167. if (typeof value === "object") return value;
  168. try {
  169. return JSON.parse(value);
  170. } catch {
  171. return null;
  172. }
  173. }
  174. function resolveImageUrl(path) {
  175. if (!path) return "";
  176. const text = String(path).trim();
  177. if (!text) return "";
  178. if (/^https?:\/\//i.test(text)) return text;
  179. return base_img_url2 + text.replace(/^\//, "");
  180. }
  181. function getSelectedFarm() {
  182. try {
  183. return JSON.parse(localStorage.getItem("selectedFarmData") || "{}");
  184. } catch {
  185. return {};
  186. }
  187. }
  188. const hasAnswered = () => selectedOptionId.value != null;
  189. // ---------- 业务逻辑 ----------
  190. const applyTaskIndex = (index) => {
  191. currentTaskIndex.value = index;
  192. selectedOptionId.value = null;
  193. };
  194. const fetchInternalInteractTasks = async () => {
  195. try {
  196. const res = await VE_API.questionnaire.getInternalInteractTasks({ zone_id: id.value });
  197. const tasks = res?.data?.interact_tasks;
  198. interactTasks.value = Array.isArray(tasks) ? tasks : [];
  199. applyTaskIndex(0);
  200. } catch (e) {
  201. console.warn("[growthTrack] getInternalInteractTasks failed", e);
  202. interactTasks.value = [];
  203. }
  204. };
  205. /** status=1 有范围无需勾画;status=0 无范围需要勾画 */
  206. const fetchZoneRangeStatus = async () => {
  207. const farm = getSelectedFarm();
  208. const farmId = farm.farm_id ?? farm.id;
  209. const zoneId = id.value || farm.zone_id || farm.zoneId;
  210. if (farmId == null || farmId === "" || zoneId == null || zoneId === "") {
  211. needDraw.value = true;
  212. return;
  213. }
  214. try {
  215. const res = await VE_API.questionnaire.checkZoneRange({
  216. farm_id: farmId,
  217. zone_id: zoneId,
  218. });
  219. needDraw.value = Number(res?.data?.status) !== 1;
  220. } catch (e) {
  221. console.warn("[growthTrack] checkZoneRange failed", e);
  222. needDraw.value = true;
  223. }
  224. };
  225. // ---------- 事件处理 / 生命周期 ----------
  226. const handleShare = () => {
  227. const query = {
  228. askInfo: { title: "转发巡园要点", content: "是否分享给好友" },
  229. shareText: reason.value || "速长窗口不追肥?后期发育慢人一步,直接掉队!",
  230. targetUrl: "growth_track",
  231. paramsPage: JSON.stringify({
  232. id: id.value,
  233. fromShare: 1,
  234. ...(route.query.pageName ? { pageName: route.query.pageName } : {}),
  235. }),
  236. imageUrl: "https://birdseye-img.sysuimars.com/temp/field.png",
  237. };
  238. wx.miniProgram.navigateTo({
  239. url: `/pages/subPages/share_page/index?pageParams=${JSON.stringify(query)}&type=sharePage`,
  240. });
  241. };
  242. const handleSubmit = () => {
  243. if (selectedOptionId.value == null) {
  244. ElMessage.warning(t("agriFile.pleaseSelectOption"));
  245. return;
  246. }
  247. // 选第二项:切换到下一条内部感知任务
  248. const isSecondOption = selectedOptionId.value === 2;
  249. if (isSecondOption && currentTaskIndex.value < interactTasks.value.length - 1) {
  250. applyTaskIndex(currentTaskIndex.value + 1);
  251. return;
  252. }
  253. ElMessage.success(t("agriFile.submitSuccess"));
  254. };
  255. /** 需勾画则先画范围;否则直接上传 */
  256. const handleUploadClick = () => {
  257. if (needDraw.value) {
  258. router.push({
  259. path: "/add_zone",
  260. query: {
  261. type: "farm",
  262. targetUrl: "/growth_track",
  263. ...(route.query.id ? { trackId: route.query.id } : {}),
  264. },
  265. });
  266. return;
  267. }
  268. showUploadPopup.value = true;
  269. };
  270. const handleUploadConfirm = async ({ images, date } = {}) => {
  271. const cloud_url = Array.isArray(images) ? images : [];
  272. if (!cloud_url.length) {
  273. ElMessage.warning("请先上传照片");
  274. return;
  275. }
  276. const farm = getSelectedFarm();
  277. try {
  278. const res = await VE_API.questionnaire.uploadImages({
  279. cloud_url,
  280. farm_id: farm.farm_id ?? farm.id ?? "",
  281. zone_id: id.value || "",
  282. category_code: farm.category_code || "",
  283. zone_name: farm.variety_name || "",
  284. create_time: date,
  285. });
  286. if (res?.code === 200) {
  287. showSuccessPopup.value = true;
  288. return;
  289. }
  290. ElMessage.error(res?.msg || "上传失败,请稍后再试");
  291. } catch {
  292. ElMessage.error("上传失败,请稍后再试");
  293. }
  294. };
  295. const handleComplete = () => {
  296. showSuccessPopup.value = false;
  297. router.replace("/agri_file");
  298. };
  299. const handleBack = () => {
  300. if (!hasAnswered() && !currentTask.value?.interact_feedback) {
  301. showLeavePopup.value = true;
  302. return;
  303. }
  304. router.back();
  305. };
  306. const confirmLeave = () => {
  307. router.back();
  308. };
  309. onActivated(() => {
  310. fetchZoneRangeStatus();
  311. fetchInternalInteractTasks();
  312. if (route.query.showUpload !== "1") return;
  313. nextTick(() => {
  314. showUploadPopup.value = true;
  315. const { showUpload, ...restQuery } = route.query;
  316. router.replace({ path: route.path, query: restQuery });
  317. });
  318. });
  319. </script>
  320. <style lang="scss" scoped>
  321. .growth-track-page {
  322. display: flex;
  323. flex-direction: column;
  324. width: 100%;
  325. height: 100vh;
  326. overflow: hidden;
  327. background: #f5f5f5;
  328. .growth-track-body {
  329. flex: 1;
  330. overflow-y: auto;
  331. -webkit-overflow-scrolling: touch;
  332. .growth-hero {
  333. padding: 18px 10px 38px;
  334. background: #2199f8;
  335. color: #fff;
  336. &__top {
  337. display: flex;
  338. align-items: center;
  339. justify-content: space-between;
  340. }
  341. &__title-row {
  342. display: flex;
  343. align-items: center;
  344. gap: 10px;
  345. }
  346. &__title {
  347. font-size: 28px;
  348. font-family: "PangMenZhengDao";
  349. margin-top: -5px;
  350. }
  351. &__reason {
  352. margin-top: 10px;
  353. padding: 4px 10px;
  354. border-radius: 4px;
  355. background: linear-gradient(90deg, rgba(255, 255, 255, 0.2) 0%, rgba(255, 255, 255, 0) 100%);
  356. }
  357. &__tag {
  358. display: inline-flex;
  359. align-items: center;
  360. gap: 4px;
  361. padding: 1px 4px;
  362. border-radius: 2px;
  363. background: #fff;
  364. font-size: 12px;
  365. }
  366. &__tag--level {
  367. color: #ff6a6a;
  368. }
  369. &__tag--zone {
  370. color: #2199f8;
  371. }
  372. &__share {
  373. flex: none;
  374. display: flex;
  375. align-items: center;
  376. gap: 4px;
  377. padding: 4px 14px;
  378. border-radius: 25px;
  379. background: rgba(19, 125, 209, 0.5);
  380. color: #fff;
  381. }
  382. }
  383. }
  384. .growth-content {
  385. position: relative;
  386. z-index: 1;
  387. margin: -24px 10px 12px;
  388. padding: 10px;
  389. border-radius: 8px;
  390. background: #fff;
  391. &__title {
  392. color: #4f4f4f;
  393. font-size: 16px;
  394. font-family: "PangMenZhengDao";
  395. }
  396. &__desc {
  397. margin-top: 4px;
  398. color: #9c9c9c;
  399. font-size: 14px;
  400. }
  401. }
  402. .interact-card {
  403. margin: 0 10px 10px;
  404. overflow: hidden;
  405. border: 1px solid rgba(33, 153, 248, 0.5);
  406. border-radius: 12px;
  407. background: #fff;
  408. &__head {
  409. position: relative;
  410. background: linear-gradient(180deg, #e5f4ff 0%, #ffffff 72%);
  411. }
  412. &__tab {
  413. display: inline-flex;
  414. align-items: center;
  415. gap: 5px;
  416. height: 32px;
  417. padding: 0 32px 10px 8px;
  418. color: #fff;
  419. font-size: 16px;
  420. font-family: "PangMenZhengDao";
  421. background: url("@/assets/img/agricultural/title-bg.png") no-repeat center / 100% 100%;
  422. span {
  423. margin-top: -3px;
  424. }
  425. }
  426. &__star {
  427. width: 16px;
  428. height: 14px;
  429. }
  430. &__qa {
  431. margin: 10px;
  432. padding: 10px;
  433. border-radius: 8px;
  434. background: rgba(33, 153, 248, 0.1);
  435. }
  436. &__question {
  437. font-size: 14px;
  438. font-weight: 500;
  439. }
  440. &__phenology {
  441. color: #2199f8;
  442. font-size: 14px;
  443. line-height: 20px;
  444. }
  445. &__options {
  446. display: flex;
  447. gap: 8px;
  448. margin-top: 10px;
  449. }
  450. &__submit {
  451. width: 120px;
  452. margin: 12px auto 0;
  453. padding: 8px 0;
  454. border-radius: 25px;
  455. background: #2199f8;
  456. color: #fff;
  457. font-size: 14px;
  458. text-align: center;
  459. }
  460. .interact-option {
  461. position: relative;
  462. flex: 1;
  463. min-width: 0;
  464. height: 36px;
  465. line-height: 36px;
  466. border-radius: 6px;
  467. background: #fff;
  468. border: 1px solid transparent;
  469. text-align: center;
  470. box-sizing: border-box;
  471. cursor: pointer;
  472. &__text {
  473. display: block;
  474. padding: 0 6px;
  475. overflow: hidden;
  476. text-overflow: ellipsis;
  477. white-space: nowrap;
  478. font-size: 14px;
  479. color: #666;
  480. }
  481. &.selected {
  482. border-color: #2199f8;
  483. .interact-option__text {
  484. color: #2199f8;
  485. }
  486. &::after {
  487. content: "";
  488. position: absolute;
  489. top: -1px;
  490. right: -1px;
  491. width: 18px;
  492. height: 14px;
  493. background: url("@/assets/img/home/checked-bg-top.png") no-repeat bottom right / 18px 13px;
  494. }
  495. }
  496. }
  497. }
  498. .guide-card__upload {
  499. position: fixed;
  500. right: 7px;
  501. bottom: 94px;
  502. z-index: 10;
  503. display: flex;
  504. flex-direction: column;
  505. align-items: center;
  506. justify-content: center;
  507. width: 58px;
  508. height: 58px;
  509. border-radius: 50%;
  510. background: #2199f8;
  511. color: #fff;
  512. box-shadow: 0px 4px 4px 0px rgba(0, 0, 0, 0.1);
  513. font-size: 15px;
  514. }
  515. .guide-card__upload span {
  516. margin-top: 3px;
  517. font-size: 10px;
  518. }
  519. .guide-title {
  520. position: relative;
  521. z-index: 1;
  522. display: inline-block;
  523. font-size: 16px;
  524. font-weight: 500;
  525. &::after {
  526. content: "";
  527. position: absolute;
  528. left: 0;
  529. bottom: 2px;
  530. z-index: -1;
  531. width: 90px;
  532. height: 6px;
  533. border-radius: 25px;
  534. background: linear-gradient(90deg, #2199f8 0%, rgba(255, 255, 255, 0.37) 100%);
  535. opacity: 0.4;
  536. }
  537. }
  538. .guide-card {
  539. position: relative;
  540. margin: 10px;
  541. padding: 10px;
  542. border-radius: 8px;
  543. background: #fff;
  544. &__img {
  545. display: block;
  546. width: 100%;
  547. margin-top: 8px;
  548. }
  549. }
  550. }
  551. .bottom-btn-container {
  552. display: flex;
  553. justify-content: space-between;
  554. gap: 12px;
  555. width: 100%;
  556. .bottom-btn {
  557. padding: 0 25px;
  558. border-radius: 4px;
  559. font-size: 16px;
  560. height: 40px;
  561. line-height: 40px;
  562. box-sizing: border-box;
  563. font-weight: 500;
  564. text-align: center;
  565. &.primary-btn {
  566. flex: 1;
  567. background: #2199f8;
  568. color: #fff;
  569. }
  570. }
  571. }
  572. </style>