CropAlertPanel.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. <template>
  2. <div class="crop-alert-floating-panel">
  3. <floating-panel
  4. class="floating-panel"
  5. :class="{ 'background-panel': isBackground }"
  6. :style="{ bottom: `${tabBarHeight}px` }"
  7. v-model:height="height"
  8. :anchors="anchors"
  9. :content-draggable="true"
  10. @height-change="handleHeightChange"
  11. >
  12. <div class="floating-panel-content">
  13. <div class="panel-shell" ref="panelShellRef">
  14. <div class="crop-header">
  15. <div class="crop-info">
  16. <el-avatar class="crop-avatar" :size="24" :src="cropAvatar" />
  17. <span class="crop-name">{{ currentCropName }}</span>
  18. <span class="crop-badge">当前作物</span>
  19. </div>
  20. <div class="switch-wrap">
  21. <el-tooltip
  22. v-model:visible="cropMenuVisible"
  23. placement="bottom"
  24. trigger="click"
  25. effect="light"
  26. :show-arrow="true"
  27. popper-class="crop-switch-tooltip"
  28. :teleported="true"
  29. >
  30. <template #content>
  31. <div class="crop-option-list">
  32. <div
  33. v-for="item in cropOptions"
  34. :key="item"
  35. class="crop-option-item"
  36. :class="{ active: item === currentCropName }"
  37. @click="handleSelectCrop(item)"
  38. >
  39. {{ item }}
  40. </div>
  41. </div>
  42. </template>
  43. <div
  44. class="switch-btn"
  45. :class="{ 'is-open': cropMenuVisible }"
  46. @click="dismissGuide"
  47. >
  48. <span>{{ t("growthReport.switchCategory") }}</span>
  49. <el-icon
  50. class="switch-btn__icon"
  51. :class="{ 'is-open-icon': cropMenuVisible }"
  52. >
  53. <CaretBottom />
  54. </el-icon>
  55. </div>
  56. </el-tooltip>
  57. <div v-if="guideVisible" class="crop-guide-bubble">
  58. <span class="crop-guide-bubble__arrow"></span>
  59. <div class="crop-guide-bubble__content">
  60. 当下作物为<span class="crop-guide-bubble__crop">{{ currentCropName }}</span>,请录入拟种植的作物,之后变为对应作物的气象预警
  61. </div>
  62. </div>
  63. </div>
  64. </div>
  65. <div class="alert-list" v-show="!isBackground">
  66. <div
  67. v-for="(item, index) in alertCards"
  68. :key="index"
  69. class="alert-card"
  70. >
  71. <div class="alert-card__header">
  72. <div class="alert-card__title-wrap">
  73. <img class="alert-card__icon" :src="item.icon" alt="" />
  74. <span class="alert-card__title">{{ item.title }}</span>
  75. </div>
  76. <span class="alert-card__link" @click="handleViewDetail(item)">
  77. 查看详情
  78. </span>
  79. </div>
  80. <div class="alert-card__content-wrap">
  81. <span class="alert-card__content">{{ item.content }}</span>
  82. <span
  83. v-if="item.patrolTip"
  84. class="alert-card__patrol"
  85. @click="handlePatrolTip(item)"
  86. >
  87. <el-icon class="alert-card__patrol-icon"><Link /></el-icon>
  88. <span>{{ item.patrolTip }}</span>
  89. </span>
  90. </div>
  91. </div>
  92. </div>
  93. </div>
  94. </div>
  95. </floating-panel>
  96. </div>
  97. </template>
  98. <script setup>
  99. import { FloatingPanel } from "vant";
  100. import { CaretBottom, Link } from "@element-plus/icons-vue";
  101. import { nextTick, onBeforeUnmount, onMounted, ref, watch } from "vue";
  102. import { useStore } from "vuex";
  103. import { useI18n } from "@/i18n";
  104. import { useRouter } from "vue-router";
  105. const router = useRouter();
  106. const store = useStore();
  107. const { t } = useI18n();
  108. const GUIDE_STORAGE_KEY = "GROWTH_REPORT_CROP_SWITCH_GUIDE";
  109. const QUERY_PARAMS = {
  110. farm_id: "321",
  111. zone_id: "46",
  112. date: "2026-08-11",
  113. };
  114. const CARD_ID = {
  115. WARNING: "warning",
  116. STRESS: 2,
  117. };
  118. const props = defineProps({
  119. cropName: {
  120. type: String,
  121. default: "水稻",
  122. },
  123. cropAvatar: {
  124. type: String,
  125. default: require("@/assets/img/home/banner.png"),
  126. },
  127. cropOptions: {
  128. type: Array,
  129. default: () => ["水稻", "荔枝", "香蕉"],
  130. },
  131. });
  132. const emit = defineEmits(["switchCategory", "viewDetail", "heightChange"]);
  133. const weatherIcon = require("@/assets/img/report/weather.png");
  134. /** 底部 tabBar 高度(含小程序安全区),优先读 DOM / store */
  135. const resolveTabBarHeight = () => {
  136. const fromDom = document.querySelector(".van-tabbar")?.offsetHeight;
  137. const fromStore = Number(store.state.home.tabBarHeight) || 0;
  138. const fromStorage = Number(localStorage.getItem("tabBarHeight")) || 0;
  139. return fromDom || fromStore || fromStorage || 50;
  140. };
  141. const tabBarHeight = ref(resolveTabBarHeight());
  142. const currentCropName = ref(props.cropName);
  143. const cropMenuVisible = ref(false);
  144. const guideVisible = ref(false);
  145. watch(
  146. () => props.cropName,
  147. (name) => {
  148. if (name) currentCropName.value = name;
  149. }
  150. );
  151. const dismissGuide = () => {
  152. if (!guideVisible.value) return;
  153. guideVisible.value = false;
  154. localStorage.setItem(GUIDE_STORAGE_KEY, "1");
  155. };
  156. const tryShowGuide = () => {
  157. if (localStorage.getItem(GUIDE_STORAGE_KEY)) return;
  158. guideVisible.value = true;
  159. };
  160. const handleOutsideClick = (event) => {
  161. if (!guideVisible.value) return;
  162. const target = event.target;
  163. if (target?.closest?.(".crop-guide-bubble") || target?.closest?.(".switch-btn")) return;
  164. dismissGuide();
  165. };
  166. watch(cropMenuVisible, (visible) => {
  167. if (visible) dismissGuide();
  168. });
  169. const handleSelectCrop = (name) => {
  170. dismissGuide();
  171. if (!name || name === currentCropName.value) {
  172. cropMenuVisible.value = false;
  173. return;
  174. }
  175. currentCropName.value = name;
  176. cropMenuVisible.value = false;
  177. emit("switchCategory", name);
  178. };
  179. const PANEL_HEADER_BAR = 14; // van-floating-panel 原生 header 高度
  180. const PANEL_BOTTOM_GAP = 12;
  181. /** 收起态:仅作物头,高度不含 tabBar(tabBar 用 bottom 顶开) */
  182. const COLLAPSED_CONTENT_HEIGHT = 72;
  183. /** 锚点只算面板自身高度,底部用 style.bottom 避开 tabBar,避免高度+padding 重复抬高 */
  184. const getCollapsedAnchor = () => COLLAPSED_CONTENT_HEIGHT;
  185. const anchors = ref([getCollapsedAnchor(), 280]);
  186. const height = ref(anchors.value[1]);
  187. const isBackground = ref(false);
  188. const panelShellRef = ref(null);
  189. let resizeObserver = null;
  190. let syncing = false;
  191. const emitPanelHeight = (panelH = height.value) => {
  192. // 对外抛出「距视口底」的占用高度,方便邀请按钮等避让
  193. emit("heightChange", panelH + tabBarHeight.value);
  194. };
  195. const syncMidAnchor = () => {
  196. const shell = panelShellRef.value;
  197. if (!shell || syncing) return;
  198. tabBarHeight.value = resolveTabBarHeight();
  199. const collapsed = getCollapsedAnchor();
  200. // scrollHeight 才是内容真实高度;offsetHeight 在被裁切时会偏小
  201. const contentH = shell.scrollHeight;
  202. const idealMid = Math.round(contentH + PANEL_HEADER_BAR + PANEL_BOTTOM_GAP);
  203. // 留一点顶部空间,并扣掉底部 tabBar
  204. const maxMid = Math.round(window.innerHeight - tabBarHeight.value) - 48;
  205. const nextMid = Math.max(collapsed + 80, Math.min(idealMid, maxMid));
  206. if (
  207. Math.abs(nextMid - anchors.value[1]) < 2 &&
  208. Math.abs(collapsed - anchors.value[0]) < 2
  209. ) {
  210. return;
  211. }
  212. syncing = true;
  213. const wasAtMid = Math.abs(height.value - anchors.value[1]) < 2;
  214. const wasCollapsed = Math.abs(height.value - anchors.value[0]) < 2;
  215. anchors.value = [collapsed, nextMid];
  216. if (wasAtMid) {
  217. height.value = nextMid;
  218. } else if (wasCollapsed) {
  219. height.value = collapsed;
  220. }
  221. nextTick(() => {
  222. syncing = false;
  223. emitPanelHeight();
  224. });
  225. };
  226. watch(
  227. () => store.state.home.tabBarHeight,
  228. (val) => {
  229. if (!val) return;
  230. tabBarHeight.value = val;
  231. syncMidAnchor();
  232. }
  233. );
  234. const alertCards = ref([
  235. {
  236. id: CARD_ID.WARNING,
  237. source: "warning",
  238. title: "",
  239. icon: weatherIcon,
  240. content: "",
  241. patrolTip: "",
  242. },
  243. {
  244. id: CARD_ID.STRESS,
  245. source: "stress",
  246. type: "abnormal",
  247. title: "",
  248. icon: weatherIcon,
  249. content: "",
  250. patrolTip: "异常态势巡园要点",
  251. },
  252. ]);
  253. /** 接口可能返回对象或数组,统一取第一条 */
  254. const pickFirstRecord = (res) => {
  255. const data = res?.data ?? res;
  256. if (Array.isArray(data)) return data[0] || null;
  257. if (data && typeof data === "object") return data;
  258. return null;
  259. };
  260. const patchAlertCard = (cardId, patch) => {
  261. const card = alertCards.value.find((item) => item.id === cardId);
  262. if (!card) return;
  263. Object.assign(card, patch);
  264. };
  265. const applyWeatherRisk = (record) => {
  266. if (!record) return;
  267. patchAlertCard(CARD_ID.WARNING, {
  268. title: record.risk_name || "",
  269. content: record.explain_simple || "",
  270. });
  271. };
  272. const applyWeatherStress = (record) => {
  273. if (!record) return;
  274. if (record.crop_name) currentCropName.value = record.crop_name;
  275. patchAlertCard(CARD_ID.STRESS, {
  276. title: record.stress_name || "",
  277. content: record.explain_simple || "",
  278. });
  279. };
  280. const fetchAlertCards = async () => {
  281. const [riskRes, stressRes] = await Promise.allSettled([
  282. VE_API.questionnaire.getWeatherRisk(QUERY_PARAMS),
  283. VE_API.questionnaire.getWeatherStress(QUERY_PARAMS),
  284. ]);
  285. if (riskRes.status === "fulfilled") {
  286. applyWeatherRisk(pickFirstRecord(riskRes.value));
  287. }
  288. if (stressRes.status === "fulfilled") {
  289. applyWeatherStress(pickFirstRecord(stressRes.value));
  290. }
  291. nextTick(() => syncMidAnchor());
  292. };
  293. const handleViewDetail = (item) => {
  294. emit("viewDetail", item);
  295. };
  296. const handlePatrolTip = (item) => {
  297. router.push({
  298. path: "/growth_track",
  299. query: {
  300. id: item.id,
  301. type: item.type || "growth",
  302. },
  303. });
  304. };
  305. const handleHeightChange = ({ height: nextHeight }) => {
  306. isBackground.value = nextHeight < anchors.value[1];
  307. emitPanelHeight(nextHeight);
  308. };
  309. onMounted(() => {
  310. fetchAlertCards();
  311. nextTick(() => {
  312. tabBarHeight.value = resolveTabBarHeight();
  313. // 等布局完成后再量一次,避免首屏高度偏小;小程序里 tabBar 可能稍晚才量到
  314. requestAnimationFrame(() => {
  315. syncMidAnchor();
  316. setTimeout(() => {
  317. tabBarHeight.value = resolveTabBarHeight();
  318. syncMidAnchor();
  319. emitPanelHeight();
  320. tryShowGuide();
  321. }, 80);
  322. setTimeout(() => {
  323. tabBarHeight.value = resolveTabBarHeight();
  324. syncMidAnchor();
  325. }, 600);
  326. });
  327. if (!panelShellRef.value || typeof ResizeObserver === "undefined") return;
  328. resizeObserver = new ResizeObserver(() => {
  329. syncMidAnchor();
  330. emitPanelHeight();
  331. });
  332. resizeObserver.observe(panelShellRef.value);
  333. });
  334. document.addEventListener("click", handleOutsideClick, true);
  335. });
  336. onBeforeUnmount(() => {
  337. document.removeEventListener("click", handleOutsideClick, true);
  338. resizeObserver?.disconnect?.();
  339. resizeObserver = null;
  340. });
  341. </script>
  342. <style lang="scss" scoped>
  343. .van-floating-panel {
  344. border-radius: 0;
  345. }
  346. .floating-panel {
  347. background: transparent;
  348. ::v-deep {
  349. .van-floating-panel__header {
  350. background: transparent;
  351. height: 16px;
  352. }
  353. .van-floating-panel__header-bar {
  354. width: 36px;
  355. height: 4px;
  356. background: rgba(255, 255, 255, 0.5);
  357. border-radius: 4px;
  358. }
  359. .van-floating-panel__content {
  360. background: transparent;
  361. overflow-y: auto;
  362. }
  363. }
  364. .floating-panel-content {
  365. width: calc(100% - 20px);
  366. margin: 0 auto;
  367. // margin: 14px auto 0; // 补偿隐藏 header 的 10px,原本30px,整体下移
  368. box-sizing: border-box;
  369. }
  370. .panel-shell {
  371. padding: 12px;
  372. border-radius: 16px;
  373. background: linear-gradient(180deg, #b2dbfb 0%, #cbe8ff 100%);
  374. box-sizing: border-box;
  375. }
  376. .crop-header {
  377. display: flex;
  378. align-items: center;
  379. justify-content: space-between;
  380. gap: 10px;
  381. margin-bottom: 10px;
  382. }
  383. .crop-info {
  384. display: flex;
  385. align-items: center;
  386. gap: 6px;
  387. min-width: 0;
  388. }
  389. .crop-avatar {
  390. flex-shrink: 0;
  391. }
  392. .crop-name {
  393. font-size: 18px;
  394. font-weight: 500;
  395. color: #000000;
  396. line-height: 26px;
  397. }
  398. .crop-badge {
  399. flex-shrink: 0;
  400. height: 20px;
  401. padding: 0 5px;
  402. border-radius: 2px;
  403. background: #2199f8;
  404. color: #fff;
  405. backdrop-filter: blur(4px);
  406. font-size: 12px;
  407. line-height: 20px;
  408. }
  409. .switch-wrap {
  410. position: relative;
  411. flex-shrink: 0;
  412. z-index: 5;
  413. }
  414. .crop-guide-bubble {
  415. position: absolute;
  416. top: calc(100% + 10px);
  417. right: 20px;
  418. width: 216px;
  419. padding: 6px 10px;
  420. border-radius: 6px;
  421. background: rgba(0, 0, 0, 0.8);
  422. box-sizing: border-box;
  423. z-index: 20;
  424. pointer-events: auto;
  425. &__arrow {
  426. position: absolute;
  427. top: -5px;
  428. right: 12px;
  429. width: 0;
  430. height: 0;
  431. border-left: 6px solid transparent;
  432. border-right: 6px solid transparent;
  433. border-bottom: 6px solid rgba(0, 0, 0, 0.8);
  434. }
  435. &__content {
  436. font-size: 14px;
  437. line-height: 22px;
  438. color: #fff;
  439. word-break: break-all;
  440. text-align: justify;
  441. }
  442. &__crop {
  443. padding: 0 2px;
  444. color: #2199f8;
  445. }
  446. }
  447. .switch-btn {
  448. display: flex;
  449. align-items: center;
  450. gap: 2px;
  451. flex-shrink: 0;
  452. height: 28px;
  453. padding: 0 10px;
  454. border-radius: 14px;
  455. border: 1px solid #2199f8;
  456. background: #fff;
  457. color: #2199f8;
  458. font-size: 12px;
  459. &.is-open {
  460. border-color: #2199f8;
  461. box-shadow: 0 0 0 1px rgba(33, 153, 248, 0.15);
  462. }
  463. &__icon {
  464. font-size: 14px;
  465. color: #2199f8;
  466. transition: transform 0.3s ease;
  467. }
  468. .is-open-icon {
  469. transform: rotate(180deg);
  470. }
  471. }
  472. .alert-list {
  473. display: flex;
  474. flex-direction: column;
  475. gap: 10px;
  476. }
  477. .alert-card {
  478. padding: 12px;
  479. border-radius: 12px;
  480. background: #fff;
  481. box-sizing: border-box;
  482. &__header {
  483. display: flex;
  484. align-items: center;
  485. justify-content: space-between;
  486. gap: 8px;
  487. }
  488. &__title-wrap {
  489. display: flex;
  490. align-items: flex-end;
  491. gap: 6px;
  492. min-width: 0;
  493. }
  494. &__icon {
  495. width: 17px;
  496. height: 17px;
  497. flex-shrink: 0;
  498. }
  499. &__title {
  500. font-size: 14px;
  501. font-weight: 500;
  502. color: #000000;
  503. }
  504. &__link {
  505. flex-shrink: 0;
  506. font-size: 12px;
  507. color: #2199f8;
  508. }
  509. &__content-wrap {
  510. margin-top: 8px;
  511. }
  512. &__content {
  513. font-size: 14px;
  514. line-height: 21px;
  515. color: rgba(6, 6, 6, 0.5);
  516. word-break: break-all;
  517. }
  518. &__patrol {
  519. padding-left: 6px;
  520. display: inline-flex;
  521. align-items: center;
  522. gap: 4px;
  523. font-size: 14px;
  524. color: #2199f8;
  525. text-decoration: underline;
  526. text-underline-offset: 4px;
  527. }
  528. &__patrol-icon {
  529. font-size: 16px;
  530. }
  531. }
  532. &.background-panel {
  533. .panel-shell {
  534. padding: 6px 10px 10px 10px;
  535. background: url("@/assets/img/report/type-bg.png");
  536. background-size: calc(100vw - 20px) 40px;
  537. background-repeat: no-repeat;
  538. background-position: center top;
  539. .crop-header {
  540. // padding-bottom: 60px;
  541. }
  542. }
  543. }
  544. }
  545. </style>
  546. <style lang="scss">
  547. .crop-switch-tooltip {
  548. z-index: 3000 !important;
  549. padding: 8px 0 !important;
  550. min-width: 120px;
  551. border: none !important;
  552. border-radius: 12px !important;
  553. box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12) !important;
  554. background: #fff !important;
  555. .el-popper__arrow::before {
  556. background: #fff !important;
  557. border: none !important;
  558. }
  559. .crop-option-list {
  560. display: flex;
  561. flex-direction: column;
  562. }
  563. .crop-option-item {
  564. padding: 10px 20px;
  565. font-size: 14px;
  566. line-height: 20px;
  567. color: #333;
  568. cursor: pointer;
  569. &:active,
  570. &.active {
  571. color: #2199f8;
  572. background: rgba(33, 153, 248, 0.06);
  573. }
  574. }
  575. }
  576. </style>