weatherInfo.vue 16 KB

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