CardWeather.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <template>
  2. <div class="weather-card">
  3. <div class="cloud-bg">
  4. <div class="date-time">
  5. <span>{{ currentDate }}</span>
  6. <span>{{ currentWeek }}</span>
  7. <span class="time">{{ currentTime }}</span>
  8. </div>
  9. <div class="temperature-wind">
  10. <div class="temperature">{{ curTemperature || '--' }}℃</div>
  11. <div class="wind">
  12. <div>
  13. 风速:
  14. <span class="wind-value">{{ curWindVelocity || '--' }} km/h</span>
  15. </div>
  16. <div>
  17. 风力:
  18. <span class="wind-value">{{ windSpeedLevel || '--' }}级</span>
  19. </div>
  20. </div>
  21. </div>
  22. <div class="weather-warning" :class="`weather-warning--${warningLevel}`" @click="handleClick">
  23. <SvgIcon iconName="weather-warning" :color="levelStyles[warningLevel].iconColor" width="20px" height="20px" />
  24. <Transition name="warning-text" mode="out-in">
  25. <span v-if="currentWarning" :key="currentIndex" class="weather-warning__text">
  26. {{ currentWarning.disasterName }}
  27. </span>
  28. <span v-else key="no-warning" class="weather-warning__text">今日无气象灾害预警</span>
  29. </Transition>
  30. <Transition name="arrow-icon" mode="out-in">
  31. <SvgIcon
  32. v-if="todayWarningInfo.length > 1"
  33. :key="currentIndex"
  34. class="arrow-icon"
  35. iconName="arrow-down"
  36. :color="levelStyles[warningLevel].arrowColor"
  37. width="16px"
  38. height="16px"
  39. />
  40. </Transition>
  41. </div>
  42. </div>
  43. </div>
  44. </template>
  45. <script setup lang="ts">
  46. import { computed, onMounted, onUnmounted, ref, watch } from 'vue';
  47. import dayjs from 'dayjs';
  48. import SvgIcon from '@/components/SvgIcon/SvgIcon.vue';
  49. import { SysDictDataDetail, queryDictTypeDetail } from '@/api/dict';
  50. import { getTodayDisasterWarnInfoList, getRealTimeWeatherData } from '@/api/disaster-overview';
  51. export interface DisasterWarningListType {
  52. disasterType: string;
  53. disasterName: string;
  54. }
  55. const currentDate = ref('');
  56. const currentWeek = ref('');
  57. const currentTime = ref('');
  58. let timer: NodeJS.Timeout;
  59. const weatherDisasterDic = ref<SysDictDataDetail[]>([]); // 气象灾害预警字典
  60. const todayWarningInfo = ref<DisasterWarningListType[]>([]); // 今日灾害预警信息
  61. const curTemperature = ref('0');
  62. const curWindVelocity = ref(0);
  63. const windSpeedLevel = ref(0);
  64. // 风速km/h等级换算表
  65. const windLevels = [
  66. { max: 1, level: 0 },
  67. { max: 5, level: 1 },
  68. { max: 11, level: 2 },
  69. { max: 19, level: 3 },
  70. { max: 28, level: 4 },
  71. { max: 38, level: 5 },
  72. { max: 49, level: 6 },
  73. { max: 61, level: 7 },
  74. { max: 74, level: 8 },
  75. { max: 88, level: 9 },
  76. { max: 102, level: 10 },
  77. { max: 117, level: 11 },
  78. { max: 133, level: 12 },
  79. { max: 149, level: 13 },
  80. { max: 166, level: 14 },
  81. { max: 183, level: 15 },
  82. { max: 201, level: 16 },
  83. { max: 220, level: 17 },
  84. { max: Infinity, level: 18 },
  85. ];
  86. const getWindLevel = (windSpeedKmh: number): number => {
  87. const matched = windLevels.find(({ max }) => windSpeedKmh <= max);
  88. return matched ? matched.level : 18;
  89. };
  90. watch(
  91. () => curWindVelocity.value,
  92. (newVal) => {
  93. windSpeedLevel.value = getWindLevel(newVal);
  94. },
  95. );
  96. // 更新时间函数
  97. const updateDateTime = () => {
  98. const now = dayjs();
  99. currentDate.value = now.format('MM月DD日');
  100. currentWeek.value = `星期${now.format('dd').slice(-1)}`;
  101. currentTime.value = now.format('HH:mm:ss');
  102. };
  103. // 获取实时天气数据
  104. const getRealTimeWeatherDataInfo = async () => {
  105. const res = await getRealTimeWeatherData();
  106. curTemperature.value = res?.temperature || '0';
  107. curWindVelocity.value = (res?.windVelocity || 0) * 3.6;
  108. };
  109. // 获取今日灾害预警信息
  110. const getTodayWarningInfo = async () => {
  111. todayWarningInfo.value = (await getTodayDisasterWarnInfoList())?.map((item) => ({
  112. disasterType: item.disasterType,
  113. disasterName:
  114. weatherDisasterDic.value.find((dic) => dic.itemCode === item.disasterType)?.itemValue || '未知预警信息',
  115. }));
  116. };
  117. // 当前显示的索引
  118. const currentIndex = ref(0);
  119. // 当前显示的预警项
  120. const currentWarning = computed(() => {
  121. return todayWarningInfo.value.length > 0 ? todayWarningInfo.value[currentIndex.value] : null;
  122. });
  123. // 预警等级样式配置
  124. const levelStyles = {
  125. blue: {
  126. iconColor: 'rgba(23, 119, 255, 1)',
  127. arrowColor: 'rgba(23, 119, 255, 0.8)',
  128. },
  129. yellow: {
  130. iconColor: 'rgba(250, 173, 20, 1)',
  131. arrowColor: 'rgba(250, 173, 20, 0.8)',
  132. },
  133. orange: {
  134. iconColor: 'rgba(255, 124, 77, 1)',
  135. arrowColor: 'rgba(255, 124, 77, 0.8)',
  136. },
  137. red: {
  138. iconColor: 'rgba(255, 77, 79, 1)',
  139. arrowColor: 'rgba(255, 77, 79, 0.8)',
  140. },
  141. common: {
  142. iconColor: 'rgba(0, 0, 0, 0.5)',
  143. arrowColor: 'rgba(0, 0, 0, 0.5)',
  144. },
  145. };
  146. // 根据类型判断预警等级
  147. const warningLevel = computed(() => {
  148. if (!currentWarning.value) return 'common';
  149. const type = currentWarning.value.disasterType.toLowerCase();
  150. if (type.includes('red')) return 'red';
  151. if (type.includes('orange')) return 'orange';
  152. if (type.includes('yellow')) return 'yellow';
  153. if (type.includes('blue')) return 'blue';
  154. return 'common'; // 默认通用
  155. });
  156. // 点击切换到下一个
  157. const handleClick = () => {
  158. if (todayWarningInfo.value.length <= 1) return;
  159. currentIndex.value = (currentIndex.value + 1) % todayWarningInfo.value.length;
  160. };
  161. onMounted(async () => {
  162. await queryDictTypeDetail('weather_warning').then((res) => {
  163. weatherDisasterDic.value = res.sysDictDataList;
  164. });
  165. updateDateTime();
  166. timer = setInterval(updateDateTime, 1000);
  167. getRealTimeWeatherDataInfo();
  168. getTodayWarningInfo();
  169. });
  170. onUnmounted(() => {
  171. clearInterval(timer);
  172. });
  173. </script>
  174. <style scoped lang="scss">
  175. .weather-card {
  176. background: linear-gradient(90deg, #b4ccff 0%, #e4fbf9 100%);
  177. border-radius: 8px;
  178. .cloud-bg {
  179. width: 100%;
  180. height: 100%;
  181. background-image: url('@/assets/images/disaster-overview/cloud-bg.png');
  182. background-repeat: no-repeat;
  183. background-position: left top;
  184. background-size: auto;
  185. border-radius: 8px;
  186. }
  187. }
  188. .date-time {
  189. padding: 16px 20px;
  190. font-weight: 400;
  191. font-size: 14px;
  192. color: #000000;
  193. display: flex;
  194. align-items: center;
  195. gap: 6px;
  196. .time {
  197. margin-left: auto;
  198. }
  199. }
  200. .temperature-wind {
  201. display: flex;
  202. align-items: center;
  203. gap: 32px;
  204. padding: 0 20px;
  205. .temperature {
  206. line-height: 67px;
  207. font-weight: bold;
  208. font-size: 56px;
  209. color: #0f3d7d;
  210. }
  211. .wind {
  212. display: flex;
  213. flex-direction: column;
  214. gap: 7px;
  215. font-weight: 400;
  216. font-size: 16px;
  217. color: #0f3d7d;
  218. }
  219. .wind-value {
  220. font-weight: 500;
  221. }
  222. }
  223. .weather-warning {
  224. width: 306px;
  225. height: 48px;
  226. margin: 17px 12px 12px 12px;
  227. border-radius: 8px;
  228. backdrop-filter: blur(10px);
  229. background: rgba(#8799b3, 0.15);
  230. display: flex;
  231. align-items: center;
  232. justify-content: center;
  233. gap: 8px;
  234. position: relative;
  235. cursor: pointer;
  236. &__text {
  237. line-height: 1;
  238. font-weight: 600;
  239. }
  240. .arrow-icon {
  241. position: absolute;
  242. right: 16px;
  243. top: 50%;
  244. transform: translateY(-50%);
  245. }
  246. // 蓝色预警
  247. &--blue {
  248. background-color: rgb(23 119 255 / 15%);
  249. .weather-warning__text {
  250. color: #1777ff;
  251. }
  252. }
  253. // 黄色预警
  254. &--yellow {
  255. background-color: rgb(250 173 20 / 15%);
  256. .weather-warning__text {
  257. color: #faad14;
  258. }
  259. }
  260. // 橙色预警
  261. &--orange {
  262. background-color: rgb(255 124 77 / 15%);
  263. .weather-warning__text {
  264. color: #ff7c4d;
  265. }
  266. }
  267. // 红色预警
  268. &--red {
  269. background-color: rgb(255 77 77 / 15%);
  270. .weather-warning__text {
  271. color: #ff4d4f;
  272. }
  273. }
  274. // 通用
  275. &--common {
  276. background-color: rgba(#8799b3, 0.15);
  277. .weather-warning__text {
  278. color: rgba(0, 0, 0);
  279. font-weight: 400;
  280. }
  281. }
  282. // 悬停效果(仅当有多个预警时)
  283. &:hover {
  284. opacity: 0.8;
  285. }
  286. }
  287. // 预警文本切换动画
  288. .warning-text-enter-active,
  289. .warning-text-leave-active,
  290. .arrow-icon-enter-active,
  291. .arrow-icon-leave-active {
  292. transition: all 0.3s ease;
  293. }
  294. .warning-text-enter-from,
  295. .arrow-icon-enter-from {
  296. opacity: 0;
  297. transform: translateY(-10px);
  298. }
  299. .warning-text-enter-to,
  300. .arrow-icon-enter-to {
  301. opacity: 1;
  302. transform: translateY(0);
  303. }
  304. .warning-text-leave-from,
  305. .arrow-icon-leave-from {
  306. opacity: 1;
  307. transform: translateY(0);
  308. }
  309. .warning-text-leave-to,
  310. .arrow-icon-leave-to {
  311. opacity: 0;
  312. transform: translateY(10px);
  313. }
  314. </style>