weatherInfo.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. <template>
  2. <div class="weather-info is-garden"
  3. :class="{ expanded: isExpanded, 'no-farm': !hasFarm, 'no-weather': !hasWeather, 'farm-list': activeGarden === 'list' }">
  4. <div class="header flex-center">
  5. <div class="header-left">
  6. <div class="address-select flex-center" v-if="hasFarm">
  7. <div class="garden-tabs">
  8. <div class="garden-item left-item" @click="handleGardenClick('current')"
  9. :class="{ 'active': activeGarden === 'current' }">
  10. <img class="current-icon"
  11. :src="activeGarden === 'current' ? require('@/assets/img/common/farm-active.png') : require('@/assets/img/common/farm.png')"
  12. alt="">
  13. <span class="current-name van-ellipsis">{{ farmName }}</span>
  14. <el-icon
  15. @click="handleFarmInfo"
  16. v-if="activeGarden === 'current'"
  17. class="farm-edit-icon"
  18. >
  19. <Edit />
  20. </el-icon>
  21. </div>
  22. <img class="title-block" v-show="activeGarden === 'current'"
  23. src="@/assets/img/common/title-block.png" alt="">
  24. <img class="title-block" v-show="activeGarden !== 'current'"
  25. src="@/assets/img/common/title-block-active.png" alt="">
  26. <div class="garden-item right-item" @click="handleGardenClick('list')"
  27. :class="{ 'active': activeGarden === 'list' }">
  28. <img class="current-icon"
  29. :src="activeGarden === 'list' ? require('@/assets/img/common/menu-active.png') : require('@/assets/img/common/menu.png')"
  30. alt="">
  31. <span class="current-name">{{ t("weather.farmList") }}</span>
  32. </div>
  33. <div class="mask-wrap" v-if="props.showTabMask" @click="emit('closeTabMask')">
  34. <div class="mask-content">
  35. <div class="mask-text">
  36. {{ t("weather.farmListGuide") }}
  37. </div>
  38. </div>
  39. </div>
  40. </div>
  41. </div>
  42. <div class="address-select flex-center farm-name" v-else>
  43. {{ t("common.demoFarm") }}
  44. </div>
  45. <div class="farm-l" v-show="activeGarden === 'current' && hasWeather">
  46. <div class="temperature flex-center">
  47. <div class="weather-icon" v-if="currentWeather.iconDay">
  48. <i :class="'qi-' + currentWeather.iconDay + '-fill'"></i>
  49. </div>
  50. <div class="temperature-number">{{ currentWeather.temp || '--' }}</div>
  51. <div class="temperature-text">
  52. <span>{{ currentWeather.text }}</span>
  53. <div class="temperature-text-time">
  54. <span>{{ currentDateText }}</span>
  55. <span class="temperature-text-date">{{ currentWeekText }}</span>
  56. <span v-show="!isExpanded" class="temperature-text-more" @click="toggleExpand">
  57. {{ t("weather.expandMore") }}
  58. </span>
  59. </div>
  60. </div>
  61. </div>
  62. </div>
  63. <div v-if="!hasWeather" class="report-tabs">
  64. <div
  65. v-for="(item, index) in reportTabs"
  66. :key="item.key"
  67. class="report-tab-item"
  68. @click="emit('reportTabClick', item)"
  69. >
  70. <img class="report-tab-icon" :src="getReportTabIcon(index)" alt="" />
  71. <span class="report-tab-label">{{ getReportTabLabel(item) }}</span>
  72. </div>
  73. </div>
  74. </div>
  75. <!-- <div class="weather-icon" v-else>
  76. <img :src="`https://birdseye-img.sysuimars.com/weather/${currentWeather.iconDay}.svg`" alt="" />
  77. </div> -->
  78. </div>
  79. <div class="weather-chart-container" v-show="activeGarden === 'current' && hasWeather">
  80. <div class="weather-chart-title">
  81. <span>{{ t("weather.future7Days") }}</span>
  82. <div class="weather-chart-title-more" @click="toggleExpand">{{ t("weather.collapse") }}</div>
  83. </div>
  84. <weather-chart class="weather-chart" :weather-data="weatherData"></weather-chart>
  85. </div>
  86. <!-- 农场筛选 -->
  87. <div class="farm-filter" v-show="activeGarden === 'list'">
  88. <div class="filter-l">
  89. <el-input v-model="searchFarm" style="width: 96px" :placeholder="t('weather.searchFarm')" :prefix-icon="Search" />
  90. </div>
  91. <div class="filter-r">
  92. <el-select v-model="regionVal" :placeholder="t('weather.selectRegion')" style="width: 92px">
  93. <el-option v-for="item in regionOptions" :key="item.value" :label="item.label"
  94. :value="item.value" />
  95. </el-select>
  96. <el-select v-model="typeVal" :placeholder="t('weather.selectCategory')" style="width: 92px">
  97. <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value" />
  98. </el-select>
  99. </div>
  100. </div>
  101. </div>
  102. </template>
  103. <script setup>
  104. import { ref, onActivated, computed } from "vue";
  105. import weatherChart from "./weatherChart.vue";
  106. import { useRouter } from "vue-router";
  107. import { useStore } from "vuex";
  108. import { Edit, Search } from '@element-plus/icons-vue';
  109. import { convertPointToArray } from "@/utils/index";
  110. import { useI18n } from "@/i18n";
  111. const { t } = useI18n();
  112. const store = useStore();
  113. const props = defineProps({
  114. showTabMask: {
  115. type: Boolean,
  116. default: false
  117. },
  118. gardenId: {
  119. type: [Number, String],
  120. default: null
  121. },
  122. hasWeather: {
  123. type: Boolean,
  124. default: true
  125. },
  126. reportTabs: {
  127. type: Array,
  128. default: () => [
  129. { key: "historyRisk" },
  130. { key: "soilImprovement" },
  131. { key: "rotationAdvice" },
  132. ],
  133. },
  134. from: {
  135. type: String,
  136. default: null
  137. }
  138. });
  139. const regionVal = ref('');
  140. const typeVal = ref('');
  141. const searchFarm = ref('');
  142. const regionOptions = ref([{
  143. label: '地区1',
  144. value: '1'
  145. }, {
  146. label: '地区2',
  147. value: '2'
  148. }]);
  149. const typeOptions = ref([{
  150. label: '品类1',
  151. value: '1'
  152. }]);
  153. const REPORT_TAB_ICONS = [
  154. require("@/assets/img/common/header-icon-1.png"),
  155. require("@/assets/img/common/header-icon-2.png"),
  156. require("@/assets/img/common/header-icon-3.png"),
  157. ];
  158. const getReportTabIcon = (index) => REPORT_TAB_ICONS[index] || REPORT_TAB_ICONS[0];
  159. const getReportTabLabel = (item) => {
  160. if (!item?.key) return item?.label || "";
  161. const i18nKey = `weather.reportTab.${item.key}`;
  162. const translated = t(i18nKey);
  163. if (translated !== i18nKey) return translated;
  164. return item.label || item.key;
  165. };
  166. // 定义emit事件
  167. const emit = defineEmits(['weatherExpanded', 'changeGarden', 'changeGardenTab', 'closeTabMask', 'reportTabClick']);
  168. const router = useRouter();
  169. const isExpanded = ref(false);
  170. const toggleExpand = () => {
  171. isExpanded.value = !isExpanded.value;
  172. emit('weatherExpanded', isExpanded.value);
  173. };
  174. const farmId = ref(null);
  175. const farmName = ref("");
  176. const hasFarm = ref(false)
  177. const activeGarden = ref('current');
  178. onActivated(() => {
  179. handleGardenClick('current');
  180. });
  181. function setGardenLoaded(hasFarmData) {
  182. hasFarm.value = !!hasFarmData;
  183. if (!hasFarm.value) {
  184. farmId.value = null;
  185. farmName.value = "";
  186. }
  187. }
  188. function setSelectedGarden(payload) {
  189. if (!payload?.id) {
  190. setGardenLoaded(false);
  191. getLocationName();
  192. getWeatherData();
  193. return;
  194. }
  195. hasFarm.value = true;
  196. farmId.value = Number(payload.id);
  197. farmName.value = payload.name || "";
  198. getLocationName();
  199. getWeatherData();
  200. emit('changeGarden', payload);
  201. }
  202. const handleGardenClick = (type) => {
  203. activeGarden.value = type;
  204. emit("changeGardenTab", type);
  205. };
  206. const locationName = ref("");
  207. const weatherData = ref(null);
  208. const currentWeather = ref({ temp: "--", text: "--", iconDay: "" });
  209. const MAP_KEY = "CZLBZ-LJICQ-R4A5J-BN62X-YXCRJ-GNBUT";
  210. function getLocationName() {
  211. const locationPoint = localStorage.getItem('selectedFarmPoint') || store.state.home.miniUserLocationPoint;
  212. const farmLocation = convertPointToArray(locationPoint);
  213. let formattedLocation = `${farmLocation[1]},${farmLocation[0]}`;
  214. const params = {
  215. key: MAP_KEY,
  216. location: formattedLocation,
  217. };
  218. VE_API.old_mini_map.location(params).then(({ result }) => {
  219. // locationVal.value = result.formatted_addresses.recommend;
  220. locationName.value = result?.address_component
  221. ? result.address_component.city + result.address_component.district
  222. : result?.address + "";
  223. });
  224. }
  225. const handleFarmInfo = () => {
  226. router.push(`/farm_info?subjectId=${farmId.value}`);
  227. }
  228. // 获取天气数据
  229. function getWeatherData() {
  230. const point = localStorage.getItem('selectedFarmPoint') || store.state.home.miniUserLocationPoint;
  231. if (!point) {
  232. return;
  233. }
  234. VE_API.old_mini_map.get7d({ point }).then(({ data }) => {
  235. if (data && data.daily && data.daily.length > 0) {
  236. weatherData.value = data;
  237. // 设置当前天气(第一天的数据)
  238. const today = data.daily[0];
  239. currentWeather.value = {
  240. temp: today.tempMax || today.tempMin || 26,
  241. text: today.textDay || t("weather.sunny"),
  242. iconDay: today.iconDay,
  243. fxDate: today.fxDate
  244. };
  245. }
  246. }).catch(() => {
  247. // 获取天气数据失败,使用默认值
  248. });
  249. }
  250. // 获取当前日期和星期
  251. const currentDateText = computed(() => {
  252. const now = new Date();
  253. const month = String(now.getMonth() + 1).padStart(2, '0');
  254. const day = String(now.getDate()).padStart(2, '0');
  255. return `${month}/${day}`;
  256. });
  257. const currentWeekText = computed(() => {
  258. if (!currentWeather.value?.fxDate) {
  259. return "";
  260. }
  261. // 兼容 "YYYY-MM-DD" 格式,避免部分环境下直接 new Date() 解析异常
  262. const date = new Date(currentWeather.value.fxDate.replace(/-/g, "/"));
  263. if (Number.isNaN(date.getTime())) {
  264. return "";
  265. }
  266. return t(`week.${date.getDay()}`);
  267. });
  268. // 暴露方法供父页面(如长势报告)同步农场列表状态
  269. defineExpose({
  270. setGardenLoaded,
  271. setSelectedGarden,
  272. toggleExpand,
  273. handleGardenClick
  274. });
  275. </script>
  276. <style lang="scss" scoped>
  277. .mask-wrap {
  278. position: fixed;
  279. top: 0;
  280. left: 0;
  281. width: 100%;
  282. height: calc(100% - 300px);
  283. background-color: rgba(0, 0, 0, 0.52);
  284. z-index: 99999;
  285. color: rgba(0, 0, 0, 0.9);
  286. .mask-content {
  287. position: absolute;
  288. right: 51px;
  289. top: 58px;
  290. .mask-text {
  291. background: #fff;
  292. padding: 6px 16px;
  293. border-radius: 10px;
  294. font-size: 14px;
  295. line-height: 22px;
  296. text-align: center;
  297. width: fit-content;
  298. box-sizing: border-box;
  299. border: 1px solid #DCDCDC;
  300. position: relative;
  301. z-index: 10px;
  302. &::after {
  303. content: '';
  304. position: absolute;
  305. right: 24px;
  306. top: -10px;
  307. width: 0;
  308. height: 0;
  309. border-right: 5px solid transparent;
  310. border-top: 5px solid transparent;
  311. border-left: 5px solid transparent;
  312. border-bottom: 5px solid #fff;
  313. }
  314. }
  315. }
  316. }
  317. .weather-info {
  318. width: 100%;
  319. // height: 58px;
  320. height: 80px;
  321. border-radius: 14px;
  322. border: 0.5px solid #ffffff;
  323. background: linear-gradient(180deg, #89CBFF 0%, #2199F8 100%);
  324. box-sizing: border-box;
  325. transition: height 0.3s ease-in-out;
  326. overflow: hidden;
  327. &.is-garden {
  328. border-radius: 8px;
  329. box-shadow: 0px -2px 4px 0px #0000000D;
  330. // height: 130px;
  331. height: 96px;
  332. }
  333. &.no-weather {
  334. height: auto;
  335. position: relative;
  336. }
  337. &.no-farm {
  338. height: 80px;
  339. &.expanded {
  340. height: 312px;
  341. }
  342. }
  343. &.expanded {
  344. height: 362px;
  345. }
  346. &.bg-white {
  347. border-radius: 14px;
  348. background-image: linear-gradient(90deg, #e2f1fe 0%, #ffffff 80%);
  349. }
  350. &.farm-list {
  351. height: 94px;
  352. }
  353. .flex-center {
  354. display: flex;
  355. align-items: center;
  356. }
  357. .header {
  358. display: flex;
  359. align-items: flex-end;
  360. justify-content: space-between;
  361. .header-left {
  362. width: 100%;
  363. .address-select {
  364. .select-garden {
  365. width: fit-content;
  366. max-width: 170px;
  367. margin-right: 8px;
  368. margin-bottom: 10px;
  369. .el-dropdown-link {
  370. font-size: 15px;
  371. font-weight: 500;
  372. color: #000000;
  373. span {
  374. width: fit-content;
  375. max-width: 95%;
  376. margin-right: 4px;
  377. }
  378. .default-text {
  379. font-size: 12px;
  380. color: #fff;
  381. padding: 2px 4px;
  382. width: 44px;
  383. text-align: center;
  384. box-sizing: border-box;
  385. font-weight: 400;
  386. background-color: #2199f8;
  387. border-radius: 25px;
  388. }
  389. }
  390. }
  391. .btn-wrap {
  392. position: absolute;
  393. right: 10px;
  394. top: 8px;
  395. display: flex;
  396. gap: 8px;
  397. }
  398. .add-garden {
  399. font-size: 12px;
  400. color: #2199f8;
  401. padding: 3px 10px;
  402. border: 1px solid rgba(33, 153, 248, 0.5);
  403. border-radius: 25px;
  404. display: flex;
  405. align-items: center;
  406. }
  407. .gray-btn {
  408. color: #919191;
  409. border: 1px solid rgba(145, 145, 145, 0.5);
  410. }
  411. }
  412. .farm-name {
  413. font-size: 16px;
  414. color: #1D2129;
  415. background: #fff;
  416. padding: 2px 0 4px 12px;
  417. }
  418. .temperature {
  419. padding-left: 10px;
  420. width: 100%;
  421. box-sizing: border-box;
  422. position: relative;
  423. background: #fff;
  424. .temperature-number {
  425. font-size: 38px;
  426. position: relative;
  427. margin: 0 8px;
  428. &::after {
  429. content: "°";
  430. font-size: 10px;
  431. position: absolute;
  432. right: -6px;
  433. top: 2px;
  434. }
  435. }
  436. .temperature-text {
  437. .temperature-text-time {
  438. font-size: 12px;
  439. font-weight: 400;
  440. }
  441. .temperature-text-more {
  442. font-size: 12px;
  443. color: #2199f8;
  444. margin-left: 10px;
  445. font-weight: 500;
  446. cursor: pointer;
  447. }
  448. .temperature-text-date {
  449. color: #1D2129;
  450. margin-left: 4px;
  451. }
  452. }
  453. }
  454. .report-tabs {
  455. width: 100%;
  456. display: flex;
  457. align-items: center;
  458. gap: 7px;
  459. padding: 8px 10px 10px;
  460. background: #fff;
  461. box-sizing: border-box;
  462. .report-tab-item {
  463. flex: 1;
  464. display: flex;
  465. align-items: center;
  466. justify-content: center;
  467. gap: 5px;
  468. padding: 5px 8px;
  469. font-size: 12px;
  470. color: #5A5A5A;
  471. border: 0.5px solid rgba(180, 180, 180, 0.4);
  472. border-radius: 4px;
  473. .report-tab-icon {
  474. width: 14px;
  475. height: 14px;
  476. }
  477. .report-tab-label {
  478. white-space: nowrap;
  479. overflow: hidden;
  480. text-overflow: ellipsis;
  481. }
  482. }
  483. }
  484. }
  485. .header-right {
  486. width: 84px;
  487. height: 73px;
  488. }
  489. .weather-icon {
  490. i {
  491. font-size: 40px;
  492. color: #a7cffb;
  493. }
  494. }
  495. }
  496. .garden-tabs {
  497. width: 100%;
  498. display: flex;
  499. align-items: center;
  500. .garden-item {
  501. display: flex;
  502. align-items: center;
  503. justify-content: center;
  504. gap: 6px;
  505. height: 40px;
  506. width: calc(50% - 17px);
  507. // width:100px;
  508. // background: linear-gradient(180deg, #89CBFF 0%, #2199F8 100%);
  509. &.left-item {
  510. padding-left: 10px;
  511. justify-content: flex-start;
  512. .farm-edit-icon {
  513. width: 16px;
  514. height: 16px;
  515. color: #2199F8;
  516. flex-shrink: 0;
  517. margin-left: 2px;
  518. }
  519. }
  520. &.right-item {
  521. padding-right: 10px;
  522. z-index: 9999999;
  523. }
  524. &.active {
  525. background: #fff;
  526. // background: linear-gradient(360deg, #FFFFFF 30.18%, #FFFFFF 82.87%, #D3ECFF 106.69%);
  527. .current-name {
  528. color: #2199F8;
  529. }
  530. }
  531. .current-name {
  532. font-size: 18px;
  533. color: #fff;
  534. font-family: "PangMenZhengDao";
  535. }
  536. .current-icon {
  537. width: 16px;
  538. }
  539. }
  540. .title-block {
  541. width: 34px;
  542. height: 40px;
  543. }
  544. }
  545. .farm-filter {
  546. display: flex;
  547. justify-content: space-between;
  548. align-items: center;
  549. padding: 12px 7px 8px 7px;
  550. height: 53px;
  551. box-sizing: border-box;
  552. background: #ffffff;
  553. .filter-l {
  554. ::v-deep {
  555. .el-input__wrapper {
  556. border-radius: 20px;
  557. }
  558. .el-input .el-input__icon {
  559. margin-right: 3px;
  560. }
  561. }
  562. }
  563. .filter-r {
  564. display: flex;
  565. align-items: center;
  566. gap: 10px;
  567. ::v-deep {
  568. .el-select__wrapper {
  569. border-radius: 20px;
  570. padding: 4px 6px 4px 10px;
  571. }
  572. }
  573. }
  574. }
  575. .weather-chart-container {
  576. width: 100%;
  577. height: 100%;
  578. overflow: hidden;
  579. padding: 8px 10px 0 10px;
  580. box-sizing: border-box;
  581. background: #ffffff;
  582. .weather-chart-title {
  583. display: flex;
  584. justify-content: space-between;
  585. align-items: center;
  586. font-size: 12px;
  587. font-weight: 500;
  588. .weather-chart-title-more {
  589. color: #828282;
  590. cursor: pointer;
  591. padding: 3px 10px;
  592. border-radius: 25px;
  593. font-weight: 400;
  594. border: 1px solid rgba(130, 130, 130, 0.5);
  595. }
  596. }
  597. .weather-chart {
  598. margin-top: 5px;
  599. width: 100%;
  600. height: 180px;
  601. }
  602. }
  603. }
  604. </style>
  605. <style lang="scss">
  606. .select-garden-popper {
  607. max-height: calc(100vh - 200px);
  608. overflow-y: auto;
  609. &.el-dropdown__popper {
  610. .el-dropdown__list {
  611. max-width: 250px;
  612. }
  613. .el-dropdown-menu__item {
  614. background-color: transparent !important;
  615. color: #606266 !important;
  616. }
  617. .selected-active-garden {
  618. color: #2199f8 !important;
  619. background-color: rgba(33, 153, 248, 0.1) !important;
  620. font-weight: 500;
  621. }
  622. }
  623. .dropdown-default-text {
  624. font-size: 11px;
  625. color: #2199f8;
  626. margin-left: 8px;
  627. padding: 0 5px;
  628. background-color: rgba(33, 153, 248, 0.1);
  629. border-radius: 10px;
  630. font-weight: 400;
  631. }
  632. }
  633. </style>