index.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <template>
  2. <!-- 天气 -->
  3. <view class="app-common-window bg-white radius shadow-default" v-if="state.weatherData.length != 0">
  4. <view class="weather radius">
  5. <view class="weather-header">
  6. <view class="iconfont oaIcon-address"></view>
  7. <view class="weather-header-location">{{ state.weatherData[0].location || "上海" }}</view>
  8. <view class="weather-header-day">今天</view>
  9. <view class="weather-header-text">{{ state.weatherData[0].text || "未知" }}</view>
  10. </view>
  11. <view class="weather-center">
  12. <view class="weather-center-temperature">{{ state.weatherData[0].today.low + "/" + state.weatherData[0].today.high || "0℃" }}</view>
  13. <image class="weather-center-image" :src="`${'/static/icons/white/' + state.weatherData[0].code.now + '.png'}`" mode="heightFix"></image>
  14. </view>
  15. </view>
  16. </view>
  17. </template>
  18. <script setup>
  19. import { onReady, onLoad, onShow, onNavigationBarButtonTap, onPullDownRefresh, onReachBottom } from "@dcloudio/uni-app";
  20. import { ref, onMounted, inject, shallowRef, reactive, getCurrentInstance, watchEffect, toRefs, toRef, watch } from "vue";
  21. const emit = defineEmits(["load", "refresh"]);
  22. const props = defineProps({});
  23. const state = reactive({
  24. weatherData: [],
  25. });
  26. /**
  27. * @获取天气信息
  28. */
  29. function getWeather(string) {
  30. uni.request({
  31. url: `https://widget-v3.seniverse.com/api/weather/7b8a7d89-f01d-4b14-bdec-5ae0b82c857f?unit=c&language=zh-Hans&location=${string}`,
  32. success: (res) => {
  33. if (res.statusCode == 200) {
  34. state.weatherData = res.data.results[0].data;
  35. } else {
  36. console.log("获取天气信息失败");
  37. }
  38. },
  39. fail: (err) => {
  40. console.log("获取天气信息失败", err);
  41. },
  42. });
  43. }
  44. onLoad((option) => {});
  45. onShow(() => {});
  46. // 暴露变量
  47. defineExpose({
  48. getWeather,
  49. });
  50. </script>
  51. <style lang="scss" scoped>
  52. .app-common-window {
  53. position: relative;
  54. margin-top: -20px;
  55. margin: -20px 20upx 20upx 20upx;
  56. z-index: 50;
  57. .weather {
  58. padding: 10px;
  59. background-image: url("@/static/images/index/weather.png");
  60. background-size: 100% 100%;
  61. background-position: center center;
  62. background-repeat: no-repeat;
  63. .weather-header {
  64. display: flex;
  65. margin-bottom: 10px;
  66. font-size: 14px;
  67. &-location {
  68. margin-right: 15px;
  69. }
  70. &-day {
  71. margin: 0 auto 0 0;
  72. }
  73. &-text {
  74. margin: 0;
  75. }
  76. .iconfont {
  77. margin: auto 5px auto 0;
  78. color: #0c83fa;
  79. }
  80. }
  81. .weather-center {
  82. display: flex;
  83. &-temperature {
  84. margin: auto auto auto 0;
  85. font-size: 18px;
  86. font-weight: 600;
  87. }
  88. &-image {
  89. margin: 0 0 0 0;
  90. height: 30px !important;
  91. }
  92. }
  93. }
  94. }
  95. </style>