weatherInfo.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. <template>
  2. <div class="weather-info is-garden" :class="{ expanded: isExpanded}">
  3. <div class="header flex-center">
  4. <div class="header-left">
  5. <div class="address-select flex-center" v-if="hasFarm">
  6. <el-dropdown class="select-garden" trigger="click" popper-class="select-garden-popper">
  7. <div class="el-dropdown-link flex-center">
  8. <span class="ellipsis-l1">{{ farmName }}</span>
  9. <div class="default-text" v-show="isDefaultFarm">默认</div>
  10. <el-icon class="el-icon--right"><arrow-down /></el-icon>
  11. </div>
  12. <template #dropdown>
  13. <el-dropdown-menu>
  14. <el-dropdown-item
  15. @click="handleCommand(item)"
  16. v-for="item in farmList"
  17. :key="item.id"
  18. :class="{ 'selected-active-garden': farmId === item.id }"
  19. >
  20. <span>{{ item.name }}</span>
  21. <span v-if="item.defaultOption" class="dropdown-default-text">默认</span>
  22. </el-dropdown-item>
  23. </el-dropdown-menu>
  24. </template>
  25. </el-dropdown>
  26. <div class="btn-wrap">
  27. <div class="add-garden" @click="handleFarmInfo">农场信息</div>
  28. <div class="add-garden gray-btn" @click="handleAddFarm">
  29. <el-icon><Plus /></el-icon>
  30. <span>新建农场</span>
  31. </div>
  32. </div>
  33. </div>
  34. <div class="address-select flex-center farm-name" v-else>
  35. 示范农场
  36. </div>
  37. <div class="temperature flex-center">
  38. <div class="temperature-number">{{ currentWeather.temp || '--' }}</div>
  39. <div class="temperature-text">
  40. <span>{{ locationName || '--' }}</span>
  41. <div class="temperature-text-time">
  42. <span>{{ currentWeather.text }}-</span>
  43. <span>{{ currentDateText }}</span>
  44. <span v-show="!isExpanded" class="temperature-text-more" @click="toggleExpand">
  45. 展开更多天气
  46. </span>
  47. </div>
  48. </div>
  49. </div>
  50. </div>
  51. <div class="weather-icon" v-if="currentWeather.iconDay">
  52. <i :class="'qi-'+currentWeather.iconDay + '-fill'"></i>
  53. </div>
  54. <!-- <div class="weather-icon" v-else>
  55. <img :src="`https://birdseye-img.sysuimars.com/weather/${currentWeather.iconDay}.svg`" alt="" />
  56. </div> -->
  57. </div>
  58. <div class="weather-chart-container">
  59. <div class="weather-chart-title">
  60. <span>未来七天天气</span>
  61. <div class="weather-chart-title-more" @click="toggleExpand">收起</div>
  62. </div>
  63. <weather-chart class="weather-chart" :weather-data="weatherData"></weather-chart>
  64. </div>
  65. <!-- 农场信息 -->
  66. <farm-info-popup
  67. ref="myFarmInfoRef"
  68. :showEditBtn="false"
  69. :showBtn="true"
  70. :farmId="farmId"
  71. @success="onFarmBasicInfoSaved"
  72. ></farm-info-popup>
  73. </div>
  74. </template>
  75. <script setup>
  76. import { ref, onActivated, computed, watch, onMounted } from "vue";
  77. import weatherChart from "./weatherChart.vue";
  78. import { useRouter } from "vue-router";
  79. import { useStore } from "vuex";
  80. import farmInfoPopup from "@/views/old_mini/home/components/farmInfoPopup.vue";
  81. import { convertPointToArray } from "@/utils/index";
  82. const store = useStore();
  83. const props = defineProps({
  84. gardenId: {
  85. type: [Number, String],
  86. default: null
  87. },
  88. from: {
  89. type: String,
  90. default: null
  91. }
  92. });
  93. // 定义emit事件
  94. const emit = defineEmits(['weatherExpanded','changeGarden']);
  95. const router = useRouter();
  96. const handleCommand = ({id, name}) => {
  97. farmName.value = name;
  98. farmId.value = id;
  99. // 更新默认农场标识
  100. const selectedFarm = farmList.value.find(farm => farm.id === id);
  101. isDefaultFarm.value = selectedFarm ? selectedFarm.defaultOption || false : false;
  102. // 保存用户选择的农场到 localStorage
  103. localStorage.setItem('selectedFarmId', id);
  104. localStorage.setItem('selectedFarmName', name);
  105. localStorage.setItem('selectedFarmPoint', selectedFarm.wkt);
  106. getLocationName();
  107. getWeatherData();
  108. emit('changeGarden',{id, name});
  109. };
  110. const isExpanded = ref(false);
  111. const toggleExpand = () => {
  112. isExpanded.value = !isExpanded.value;
  113. emit('weatherExpanded',isExpanded.value);
  114. };
  115. const farmId = ref(null);
  116. const farmName = ref("");
  117. const farmList = ref([]);
  118. const hasFarm = ref(true)
  119. const isDefaultFarm = ref(false); // 添加默认农场标识
  120. // 根据传入的gardenId设置农场(先刷新列表再设置)
  121. async function setFarmByGardenId(gardenIdValue) {
  122. if (!gardenIdValue) {
  123. return false;
  124. }
  125. // 先刷新农场列表,确保数据是最新的
  126. return new Promise((resolve) => {
  127. VE_API.farm.listByUserId().then(({data}) => {
  128. // const fullData = data.filter(item => item.userType === 2);
  129. const fullData = data;
  130. farmList.value = fullData || [];
  131. if (fullData && fullData.length > 0) {
  132. hasFarm.value = true;
  133. const targetFarm = fullData.find(farm => farm.id == gardenIdValue);
  134. if (targetFarm) {
  135. farmName.value = targetFarm.name;
  136. farmId.value = Number(gardenIdValue);
  137. isDefaultFarm.value = targetFarm.defaultOption || false;
  138. // 保存到 localStorage
  139. localStorage.setItem('selectedFarmId', farmId.value);
  140. localStorage.setItem('selectedFarmName', farmName.value);
  141. localStorage.setItem('selectedFarmPoint', targetFarm.wkt);
  142. getLocationName();
  143. getWeatherData();
  144. emit('changeGarden', { id: farmId.value, name: farmName.value });
  145. resolve(true);
  146. } else {
  147. resolve(false);
  148. }
  149. } else {
  150. farmList.value = [];
  151. hasFarm.value = false;
  152. getLocationName();
  153. getWeatherData();
  154. resolve(false);
  155. }
  156. }).catch(() => {
  157. resolve(false);
  158. });
  159. });
  160. }
  161. // 获取农场列表
  162. function getFarmList() {
  163. // 如果传入了 gardenId,优先使用 setFarmByGardenId(它会刷新列表并设置)
  164. if (props.gardenId) {
  165. setFarmByGardenId(props.gardenId).then((setSuccess) => {
  166. // 如果设置失败,使用已获取的列表数据执行默认逻辑(避免重复请求)
  167. if (!setSuccess && farmList.value && farmList.value.length > 0) {
  168. selectFarmFromList(farmList.value);
  169. } else if (!setSuccess) {
  170. // 如果列表为空,再次获取列表
  171. getFarmListWithoutGardenId();
  172. }
  173. });
  174. return;
  175. }
  176. // 如果没有传入 gardenId,执行正常逻辑
  177. getFarmListWithoutGardenId();
  178. }
  179. // 从列表中选择农场(使用已有列表数据)
  180. function selectFarmFromList(data) {
  181. // 使用 localStorage 中保存的农场选择
  182. const savedFarmId = localStorage.getItem('selectedFarmId');
  183. const savedFarmName = localStorage.getItem('selectedFarmName');
  184. if (savedFarmId && savedFarmName) {
  185. // 检查保存的农场是否还在当前列表中(名称以接口列表为准,避免改名后仍显示 localStorage 旧值)
  186. const savedFarm = data.find(farm => farm.id == savedFarmId);
  187. if (savedFarm) {
  188. farmName.value = savedFarm.name;
  189. farmId.value = Number(savedFarmId);
  190. isDefaultFarm.value = savedFarm.defaultOption || false;
  191. localStorage.setItem('selectedFarmPoint', savedFarm.wkt);
  192. localStorage.setItem('selectedFarmName', savedFarm.name);
  193. } else {
  194. // 如果保存的农场不在列表中,按优先级选择
  195. selectDefaultFarm(data);
  196. }
  197. } else {
  198. // 如果没有保存的选择,按优先级选择
  199. selectDefaultFarm(data);
  200. }
  201. getLocationName();
  202. getWeatherData();
  203. emit('changeGarden',{id: farmId.value, name: farmName.value});
  204. }
  205. // 获取农场列表(不处理传入的gardenId)
  206. function getFarmListWithoutGardenId() {
  207. VE_API.farm.listByUserId().then(({data}) => {
  208. // const fullData = data.filter(item => item.userType === 2);
  209. const fullData = data;
  210. farmList.value = fullData || [];
  211. if (fullData && fullData.length > 0) {
  212. hasFarm.value = true;
  213. selectFarmFromList(fullData);
  214. } else {
  215. farmList.value = [];
  216. hasFarm.value = false;
  217. getLocationName();
  218. getWeatherData();
  219. }
  220. })
  221. }
  222. // 监听父组件传入的gardenId变化
  223. watch(() => props.gardenId, (newGardenId) => {
  224. if (newGardenId) {
  225. // 直接调用 setFarmByGardenId,它会刷新列表并设置
  226. setFarmByGardenId(newGardenId);
  227. }
  228. }, { immediate: false });
  229. // 选择默认农场的逻辑
  230. function selectDefaultFarm(data) {
  231. // 首先查找 defaultOption 为 true 的农场
  232. const defaultFarm = data.find(farm => farm.defaultOption === true);
  233. if (defaultFarm) {
  234. // 如果有默认农场,选择它
  235. farmName.value = defaultFarm.name;
  236. farmId.value = defaultFarm.id;
  237. isDefaultFarm.value = true;
  238. localStorage.setItem('selectedFarmPoint', defaultFarm.wkt);
  239. } else {
  240. // 如果没有默认农场,选择第一个
  241. farmName.value = data[0].name;
  242. farmId.value = data[0].id;
  243. isDefaultFarm.value = data[0].defaultOption || false;
  244. localStorage.setItem('selectedFarmPoint', data[0].wkt);
  245. }
  246. // 保存到 localStorage
  247. localStorage.setItem('selectedFarmId', farmId.value);
  248. localStorage.setItem('selectedFarmName', farmName.value);
  249. localStorage.setItem('selectedFarmPoint', data[0].wkt);
  250. getLocationName();
  251. getWeatherData();
  252. }
  253. onActivated(() => {
  254. getFarmList();
  255. });
  256. // 暴露刷新方法供父组件调用
  257. defineExpose({
  258. refreshFarmList: getFarmList,
  259. toggleExpand
  260. });
  261. const locationName = ref("");
  262. const weatherData = ref(null);
  263. const currentWeather = ref({ temp: "--", text: "--", iconDay: "" });
  264. const MAP_KEY = "CZLBZ-LJICQ-R4A5J-BN62X-YXCRJ-GNBUT";
  265. function getLocationName() {
  266. const locationPoint = localStorage.getItem('selectedFarmPoint') || store.state.home.miniUserLocationPoint;
  267. const farmLocation = convertPointToArray(locationPoint);
  268. let formattedLocation = `${farmLocation[1]},${farmLocation[0]}`;
  269. const params = {
  270. key: MAP_KEY,
  271. location: formattedLocation,
  272. };
  273. VE_API.old_mini_map.location(params).then(({ result }) => {
  274. // locationVal.value = result.formatted_addresses.recommend;
  275. locationName.value = result?.address_component
  276. ? result.address_component.city + result.address_component.district
  277. : result?.address + "";
  278. });
  279. }
  280. const myFarmInfoRef = ref(null);
  281. /** 弹窗内修改农场名称等成功后,同步头部展示与下拉列表、本地缓存 */
  282. function onFarmBasicInfoSaved(payload) {
  283. if (!payload?.id || payload.name == null) {
  284. return;
  285. }
  286. const savedId = Number(payload.id);
  287. const idx = farmList.value.findIndex((f) => f.id == savedId);
  288. if (idx !== -1) {
  289. farmList.value[idx] = { ...farmList.value[idx], name: payload.name };
  290. }
  291. if (farmId.value == savedId) {
  292. farmName.value = payload.name;
  293. localStorage.setItem('selectedFarmName', payload.name);
  294. emit('changeGarden', { id: farmId.value, name: farmName.value });
  295. }
  296. }
  297. const handleFarmInfo = () => {
  298. // myFarmInfoRef.value.handleShow();
  299. router.push(`/farm_info?subjectId=${farmId.value}`);
  300. }
  301. const handleAddFarm = () => {
  302. router.push(`/create_farm?from=${props.from}&isReload=true`);
  303. }
  304. // 获取天气数据
  305. function getWeatherData() {
  306. const point = localStorage.getItem('selectedFarmPoint') || store.state.home.miniUserLocationPoint;
  307. if (!point) {
  308. return;
  309. }
  310. VE_API.old_mini_map.get7d({ point }).then(({ data }) => {
  311. if (data && data.daily && data.daily.length > 0) {
  312. weatherData.value = data;
  313. // 设置当前天气(第一天的数据)
  314. const today = data.daily[0];
  315. currentWeather.value = {
  316. temp: today.tempMax || today.tempMin || 26,
  317. text: today.textDay || "晴天",
  318. iconDay: today.iconDay
  319. };
  320. }
  321. }).catch(() => {
  322. // 获取天气数据失败,使用默认值
  323. });
  324. }
  325. // 获取当前日期和星期
  326. const currentDateText = computed(() => {
  327. const now = new Date();
  328. const month = String(now.getMonth() + 1).padStart(2, '0');
  329. const day = String(now.getDate()).padStart(2, '0');
  330. return `${month}/${day}`;
  331. });
  332. </script>
  333. <style lang="scss" scoped>
  334. .weather-info {
  335. width: 100%;
  336. // height: 58px;
  337. height: 70px;
  338. border-radius: 14px;
  339. background-color: #ffffff;
  340. padding: 10px 12px;
  341. box-sizing: border-box;
  342. transition: height 0.3s ease-in-out;
  343. overflow: hidden;
  344. &.is-garden {
  345. height: 85px;
  346. box-shadow: 0px 1px 5.5px 0px #00000005;
  347. border-radius: 8px;
  348. padding: 10px 12px;
  349. }
  350. &.expanded {
  351. height: 312px;
  352. background-image: linear-gradient(90deg, #e2f1fe 0%, #ffffff 80%);
  353. }
  354. &.bg-white{
  355. border-radius: 14px;
  356. background-image: linear-gradient(90deg, #e2f1fe 0%, #ffffff 80%);
  357. }
  358. .flex-center {
  359. display: flex;
  360. align-items: center;
  361. }
  362. .header {
  363. display: flex;
  364. align-items: flex-end;
  365. justify-content: space-between;
  366. .header-left {
  367. .address-select {
  368. .select-garden {
  369. width: fit-content;
  370. max-width: 170px;
  371. margin-right: 8px;
  372. margin-bottom: 10px;
  373. .el-dropdown-link {
  374. font-size: 15px;
  375. font-weight: 500;
  376. color: #000000;
  377. span {
  378. width: fit-content;
  379. max-width: 95%;
  380. margin-right: 4px;
  381. }
  382. .default-text {
  383. font-size: 12px;
  384. color: #fff;
  385. padding: 2px 4px;
  386. width: 44px;
  387. text-align: center;
  388. box-sizing: border-box;
  389. font-weight: 400;
  390. background-color: #2199f8;
  391. border-radius: 25px;
  392. }
  393. }
  394. }
  395. .btn-wrap{
  396. position: absolute;
  397. right: 10px;
  398. top: 8px;
  399. display: flex;
  400. gap: 8px;
  401. }
  402. .add-garden {
  403. font-size: 12px;
  404. color: #2199f8;
  405. padding: 3px 10px;
  406. border: 1px solid rgba(33, 153, 248, 0.5);
  407. border-radius: 25px;
  408. display: flex;
  409. align-items: center;
  410. }
  411. .gray-btn {
  412. color: #919191;
  413. border: 1px solid rgba(145, 145, 145, 0.5);
  414. }
  415. }
  416. .farm-name {
  417. font-size: 16px;
  418. color: #1D2129;
  419. }
  420. .temperature {
  421. .temperature-number {
  422. font-size: 40px;
  423. position: relative;
  424. margin-right: 14px;
  425. &::after {
  426. content: "°";
  427. font-size: 18px;
  428. position: absolute;
  429. right: -6px;
  430. top: 2px;
  431. }
  432. }
  433. .temperature-text {
  434. font-weight: 500;
  435. .temperature-text-time {
  436. font-size: 12px;
  437. font-weight: 400;
  438. }
  439. .temperature-text-more {
  440. font-size: 12px;
  441. color: #2199f8;
  442. margin-left: 10px;
  443. font-weight: 500;
  444. cursor: pointer;
  445. }
  446. }
  447. }
  448. }
  449. .header-right {
  450. width: 84px;
  451. height: 73px;
  452. }
  453. .weather-icon {
  454. i {
  455. font-size: 40px;
  456. color: #a7cffb;
  457. }
  458. }
  459. }
  460. .weather-chart-container {
  461. width: 100%;
  462. height: 100%;
  463. overflow: hidden;
  464. margin-top: 8px;
  465. .weather-chart-title {
  466. display: flex;
  467. justify-content: space-between;
  468. align-items: center;
  469. font-size: 12px;
  470. font-weight: 500;
  471. .weather-chart-title-more {
  472. color: #828282;
  473. cursor: pointer;
  474. padding: 3px 10px;
  475. border-radius: 25px;
  476. font-weight: 400;
  477. border: 1px solid rgba(130, 130, 130, 0.5);
  478. }
  479. }
  480. .weather-chart {
  481. margin-top: 5px;
  482. width: 100%;
  483. height: 180px;
  484. }
  485. }
  486. }
  487. </style>
  488. <style lang="scss">
  489. .select-garden-popper {
  490. max-height: calc(100vh - 200px);
  491. overflow-y: auto;
  492. &.el-dropdown__popper {
  493. .el-dropdown__list {
  494. max-width: 250px;
  495. }
  496. .el-dropdown-menu__item{
  497. background-color: transparent !important;
  498. color: #606266 !important;
  499. }
  500. .selected-active-garden {
  501. color: #2199f8 !important;
  502. background-color: rgba(33, 153, 248, 0.1) !important;
  503. font-weight: 500;
  504. }
  505. }
  506. .dropdown-default-text {
  507. font-size: 11px;
  508. color: #2199f8;
  509. margin-left: 8px;
  510. padding: 0 5px;
  511. background-color: rgba(33, 153, 248, 0.1);
  512. border-radius: 10px;
  513. font-weight: 400;
  514. }
  515. }
  516. </style>