CropAlertPanel.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <template>
  2. <div class="crop-alert-floating-panel">
  3. <floating-panel
  4. class="floating-panel"
  5. :class="{ 'background-panel': isBackground }"
  6. v-model:height="height"
  7. :anchors="anchors"
  8. :content-draggable="true"
  9. @height-change="handleHeightChange"
  10. >
  11. <div
  12. class="floating-panel-content"
  13. :style="{ paddingBottom: `${tabBarHeight}px` }"
  14. >
  15. <div class="panel-shell" ref="panelShellRef">
  16. <div class="crop-header">
  17. <div class="crop-info">
  18. <el-avatar class="crop-avatar" :size="24" :src="cropAvatar" />
  19. <span class="crop-name">{{ currentCropName }}</span>
  20. <span class="crop-badge">当前作物</span>
  21. </div>
  22. <el-tooltip
  23. v-model:visible="cropMenuVisible"
  24. placement="bottom"
  25. trigger="click"
  26. effect="light"
  27. :show-arrow="true"
  28. popper-class="crop-switch-tooltip"
  29. :teleported="true"
  30. >
  31. <template #content>
  32. <div class="crop-option-list">
  33. <div
  34. v-for="item in cropOptions"
  35. :key="item"
  36. class="crop-option-item"
  37. :class="{ active: item === currentCropName }"
  38. @click="handleSelectCrop(item)"
  39. >
  40. {{ item }}
  41. </div>
  42. </div>
  43. </template>
  44. <div class="switch-btn" :class="{ 'is-open': cropMenuVisible }">
  45. <span>{{ t("growthReport.switchCategory") }}</span>
  46. <icon
  47. :name="cropMenuVisible ? 'arrow-up' : 'arrow-down'"
  48. class="switch-btn__icon"
  49. />
  50. </div>
  51. </el-tooltip>
  52. </div>
  53. <div class="alert-list">
  54. <div
  55. v-for="item in alertCards"
  56. :key="item.id"
  57. class="alert-card"
  58. >
  59. <div class="alert-card__header">
  60. <div class="alert-card__title-wrap">
  61. <img class="alert-card__icon" :src="item.icon" alt="" />
  62. <span class="alert-card__title">{{ item.title }}</span>
  63. </div>
  64. <span class="alert-card__link" @click="handleViewDetail(item)">
  65. 查看详情
  66. </span>
  67. </div>
  68. <div class="alert-card__content-wrap">
  69. <span class="alert-card__content">{{ item.content }}</span>
  70. <span
  71. v-if="item.patrolTip"
  72. class="alert-card__patrol"
  73. @click="handlePatrolTip(item)"
  74. >
  75. <el-icon class="alert-card__patrol-icon"><Link /></el-icon>
  76. <span>{{ item.patrolTip }}</span>
  77. </span>
  78. </div>
  79. </div>
  80. </div>
  81. </div>
  82. </div>
  83. </floating-panel>
  84. <div class="expand-btn-wrap" v-show="height === defaultHeight">
  85. <span>{{ t("growthReport.cropTitle") }}</span>
  86. <div class="expand-btn" @click="handleExpandBtnClick">
  87. <span>{{ t("growthReport.expand") }}</span>
  88. <el-icon><ArrowUpBold /></el-icon>
  89. </div>
  90. </div>
  91. </div>
  92. </template>
  93. <script setup>
  94. import { FloatingPanel, Icon } from "vant";
  95. import { ArrowUpBold, Link } from "@element-plus/icons-vue";
  96. import { nextTick, onBeforeUnmount, onMounted, ref, watch } from "vue";
  97. import { useI18n } from "@/i18n";
  98. const { t } = useI18n();
  99. const props = defineProps({
  100. cropName: {
  101. type: String,
  102. default: "水稻",
  103. },
  104. cropAvatar: {
  105. type: String,
  106. default: require("@/assets/img/home/banner.png"),
  107. },
  108. cropOptions: {
  109. type: Array,
  110. default: () => ["水稻", "荔枝", "香蕉"],
  111. },
  112. });
  113. const emit = defineEmits(["switchCategory", "viewDetail", "patrolTip", "heightChange"]);
  114. const weatherIcon = require("@/assets/img/report/weather.png");
  115. const tabBarHeight = ref(Number(localStorage.getItem("tabBarHeight")) || 50);
  116. const currentCropName = ref(props.cropName);
  117. const cropMenuVisible = ref(false);
  118. watch(
  119. () => props.cropName,
  120. (name) => {
  121. if (name) currentCropName.value = name;
  122. }
  123. );
  124. const handleSelectCrop = (name) => {
  125. if (!name || name === currentCropName.value) {
  126. cropMenuVisible.value = false;
  127. return;
  128. }
  129. currentCropName.value = name;
  130. cropMenuVisible.value = false;
  131. emit("switchCategory", name);
  132. };
  133. const PANEL_HEADER_BAR = 30; // 隐藏原生 header 后,内容顶部留白
  134. const PANEL_BOTTOM_GAP = 16;
  135. const defaultHeight = ref(0);
  136. const fullHeight = Math.round(window.innerHeight);
  137. const anchors = ref([defaultHeight.value, 280 + tabBarHeight.value, fullHeight]);
  138. const height = ref(anchors.value[1]);
  139. const isBackground = ref(false);
  140. const panelShellRef = ref(null);
  141. let resizeObserver = null;
  142. let syncing = false;
  143. const syncMidAnchor = () => {
  144. const shell = panelShellRef.value;
  145. if (!shell || syncing) return;
  146. // scrollHeight 才是内容真实高度;offsetHeight 在被裁切时会偏小
  147. const contentH = shell.scrollHeight;
  148. const idealMid = Math.round(
  149. contentH + PANEL_HEADER_BAR + PANEL_BOTTOM_GAP + tabBarHeight.value
  150. );
  151. // 留一点顶部空间,避免直接顶满屏
  152. const maxMid = fullHeight - 48;
  153. const nextMid = Math.max(240, Math.min(idealMid, maxMid));
  154. if (Math.abs(nextMid - anchors.value[1]) < 2) return;
  155. syncing = true;
  156. const wasAtMid = Math.abs(height.value - anchors.value[1]) < 2;
  157. anchors.value = [defaultHeight.value, nextMid, fullHeight];
  158. if (wasAtMid) {
  159. height.value = nextMid;
  160. }
  161. nextTick(() => {
  162. syncing = false;
  163. });
  164. };
  165. const alertCards = [
  166. {
  167. id: "warning",
  168. title: "具体预警",
  169. icon: weatherIcon,
  170. content: "详情体预警体预警体预警体预警体预警体预详情详情详情详情详情详情详情详情详情详情",
  171. patrolTip: "",
  172. },
  173. {
  174. id: "stress",
  175. title: "某某胁迫",
  176. icon: weatherIcon,
  177. content: "详情详情详情具体预警具体预警情详情详详情详情详情详情详情详情",
  178. patrolTip: "异常态势巡园要点",
  179. },
  180. ];
  181. const handleViewDetail = (item) => {
  182. emit("viewDetail", item);
  183. };
  184. const handlePatrolTip = (item) => {
  185. emit("patrolTip", item);
  186. };
  187. const handleHeightChange = ({ height: nextHeight }) => {
  188. isBackground.value = nextHeight > anchors.value[1];
  189. emit("heightChange", nextHeight);
  190. };
  191. const handleExpandBtnClick = () => {
  192. height.value = anchors.value[1];
  193. emit("heightChange", anchors.value[1]);
  194. };
  195. onMounted(() => {
  196. nextTick(() => {
  197. // 等布局完成后再量一次,避免首屏高度偏小
  198. requestAnimationFrame(() => {
  199. syncMidAnchor();
  200. setTimeout(() => {
  201. syncMidAnchor();
  202. emit("heightChange", height.value);
  203. }, 80);
  204. });
  205. if (!panelShellRef.value || typeof ResizeObserver === "undefined") return;
  206. resizeObserver = new ResizeObserver(() => {
  207. syncMidAnchor();
  208. emit("heightChange", height.value);
  209. });
  210. resizeObserver.observe(panelShellRef.value);
  211. });
  212. });
  213. onBeforeUnmount(() => {
  214. resizeObserver?.disconnect?.();
  215. resizeObserver = null;
  216. });
  217. </script>
  218. <style lang="scss" scoped>
  219. .van-floating-panel {
  220. border-radius: 0;
  221. }
  222. .floating-panel {
  223. background: transparent;
  224. ::v-deep {
  225. .van-floating-panel__content {
  226. background: transparent;
  227. overflow-y: auto;
  228. }
  229. // 隐藏拖拽条,并取消其占位
  230. .van-floating-panel__header {
  231. height: 0 !important;
  232. min-height: 0 !important;
  233. padding: 0 !important;
  234. margin: 0 !important;
  235. overflow: hidden;
  236. opacity: 0;
  237. pointer-events: none;
  238. }
  239. }
  240. .floating-panel-content {
  241. width: calc(100% - 20px);
  242. margin: 30px auto 0; // 补偿隐藏 header 的 30px,整体下移
  243. box-sizing: border-box;
  244. }
  245. .panel-shell {
  246. padding: 12px;
  247. border-radius: 16px;
  248. background: linear-gradient(180deg, #b2dbfb 0%, #cbe8ff 100%);
  249. box-sizing: border-box;
  250. }
  251. .crop-header {
  252. display: flex;
  253. align-items: center;
  254. justify-content: space-between;
  255. gap: 10px;
  256. margin-bottom: 10px;
  257. }
  258. .crop-info {
  259. display: flex;
  260. align-items: center;
  261. gap: 6px;
  262. min-width: 0;
  263. }
  264. .crop-avatar {
  265. flex-shrink: 0;
  266. }
  267. .crop-name {
  268. font-size: 18px;
  269. font-weight: 500;
  270. color: #000000;
  271. line-height: 26px;
  272. }
  273. .crop-badge {
  274. flex-shrink: 0;
  275. height: 20px;
  276. padding: 0 5px;
  277. border-radius: 2px;
  278. background: #2199f8;
  279. color: #fff;
  280. backdrop-filter: blur(4px);
  281. font-size: 12px;
  282. line-height: 20px;
  283. }
  284. .switch-btn {
  285. display: flex;
  286. align-items: center;
  287. gap: 2px;
  288. flex-shrink: 0;
  289. height: 28px;
  290. padding: 0 10px;
  291. border-radius: 14px;
  292. border: 1px solid #2199f8;
  293. background: #fff;
  294. color: #2199f8;
  295. font-size: 12px;
  296. &.is-open {
  297. border-color: #2199f8;
  298. box-shadow: 0 0 0 1px rgba(33, 153, 248, 0.15);
  299. }
  300. &__icon {
  301. font-size: 12px;
  302. color: #2199f8;
  303. }
  304. }
  305. .alert-list {
  306. display: flex;
  307. flex-direction: column;
  308. gap: 10px;
  309. }
  310. .alert-card {
  311. padding: 12px;
  312. border-radius: 12px;
  313. background: #fff;
  314. box-sizing: border-box;
  315. &__header {
  316. display: flex;
  317. align-items: center;
  318. justify-content: space-between;
  319. gap: 8px;
  320. }
  321. &__title-wrap {
  322. display: flex;
  323. align-items: flex-end;
  324. gap: 6px;
  325. min-width: 0;
  326. }
  327. &__icon {
  328. width: 17px;
  329. height: 17px;
  330. flex-shrink: 0;
  331. }
  332. &__title {
  333. font-size: 14px;
  334. font-weight: 500;
  335. color: #000000;
  336. }
  337. &__link {
  338. flex-shrink: 0;
  339. font-size: 12px;
  340. color: #2199f8;
  341. }
  342. &__content-wrap {
  343. margin-top: 8px;
  344. }
  345. &__content {
  346. font-size: 14px;
  347. line-height: 21px;
  348. color: rgba(6, 6, 6, 0.5);
  349. word-break: break-all;
  350. }
  351. &__patrol {
  352. padding-left: 6px;
  353. display: inline-flex;
  354. align-items: center;
  355. gap: 4px;
  356. font-size: 14px;
  357. color: #2199f8;
  358. text-decoration: underline;
  359. text-underline-offset: 4px;
  360. }
  361. &__patrol-icon {
  362. font-size: 16px;
  363. }
  364. }
  365. &.background-panel {
  366. background: #f5f7fb;
  367. }
  368. }
  369. .expand-btn-wrap {
  370. position: absolute;
  371. bottom: 62px;
  372. left: 12px;
  373. width: calc(100% - 24px);
  374. background-image: linear-gradient(180deg, #d7eafc 0%, #ffffff 100%);
  375. border-radius: 14px;
  376. padding: 15px 12px;
  377. box-sizing: border-box;
  378. display: flex;
  379. align-items: center;
  380. justify-content: space-between;
  381. font-weight: 500;
  382. font-size: 15px;
  383. .expand-btn {
  384. display: flex;
  385. align-items: center;
  386. justify-content: center;
  387. gap: 4px;
  388. font-size: 13px;
  389. color: #2199f8;
  390. }
  391. }
  392. </style>
  393. <style lang="scss">
  394. .crop-switch-tooltip {
  395. z-index: 3000 !important;
  396. padding: 8px 0 !important;
  397. min-width: 120px;
  398. border: none !important;
  399. border-radius: 12px !important;
  400. box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12) !important;
  401. background: #fff !important;
  402. .el-popper__arrow::before {
  403. background: #fff !important;
  404. border: none !important;
  405. }
  406. .crop-option-list {
  407. display: flex;
  408. flex-direction: column;
  409. }
  410. .crop-option-item {
  411. padding: 10px 20px;
  412. font-size: 14px;
  413. line-height: 20px;
  414. color: #333;
  415. cursor: pointer;
  416. &:active,
  417. &.active {
  418. color: #2199f8;
  419. background: rgba(33, 153, 248, 0.06);
  420. }
  421. }
  422. }
  423. </style>