当前位置:   article > 正文

实时天气组件(纯前端实现)_vue-skycons

vue-skycons

天气查询是一个简单的HTTP接口,根据用户输入的adcode,查询目标区域当前/未来的天气情况,数据来源是中国气象局。

官方使用说明:

第一步,申请”web服务 API”密钥(Key);

链接: 首页 | 高德控制台 (amap.com)

第二步,拼接HTTP请求URL,第一步申请的Key需作为必填参数一同发送;
第三步,接收HTTP请求返回的数据(JSON或XML格式),解析数据。

如无特殊声明,接口的输入参数和输出数据编码全部统一为UTF-8。

详细流程步骤:

1. 先在高德天气控制台中创建出自己的应用,并选择Web服务,记住好key

2.在自己的vue项目页面中导入axios和Skycon,记得要先npm
  1. import axios from 'axios';
  2. import Skycon from 'vue-skycons';
3. 准备好数据
  1. export default {
  2. components: { Skycon },
  3. data() {
  4. return {
  5. key: 'ef51caedee9eccc60accb02fcb8*****', //自己的key,建议自己申请
  6. weather: {}, //用于存放天气相关的数据
  7. iconCondition: null, //icon值
  8. }
  9. },
4.mounted()方法自动加载
  1. mounted() {
  2. this.getLocationInfo();
  3. },
5.编写div
  1. <!--天气系统-->
  2. <div class="flex-center" style="margin-left: 650px; cursor: pointer;"
  3. @click="navTo('https://weather.cma.cn/web/weather/57516.html')">
  4. <div style="height: 60px; margin-top: -40px; margin-right: 6px;">
  5. <div style="height: 25px;">{{ formatDate(weather) }} </div>
  6. <div style="height: 25px; font-weight: bold; font-size: 20px; margin-left: 5px; color: Orange;">{{
  7. weather.city }}</div>
  8. </div>
  9. <skycon v-if="iconCondition" :condition="iconCondition" color="CornflowerBlue" size="40" />
  10. <div v-if="iconCondition" class="flex-center font-22" style="margin-left: 6px;">
  11. <span style="color: MediumTurquoise; margin-right: 6px;">{{ weather.temperature }}℃ </span>
  12. <span>{{ weather.weather }}</span>
  13. </div>
  14. </div>
6. 编写方法
  1. methods: {
  2. // 获取用户位置信息
  3. async getLocationInfo() {
  4. const params = {
  5. key: this.key,
  6. };
  7. const { data } = await axios.get('https://restapi.amap.com/v3/ip', { params });
  8. // data.adcode值为获取天气需要的city值
  9. this.getWeather(data.adcode);
  10. },
  11. // 获取天气详情
  12. async getWeather(adcode) {
  13. const params = {
  14. key: this.key,
  15. city: adcode,
  16. };
  17. const { data } = await axios.get(`https://restapi.amap.com/v3/weather/weatherInfo`, { params });
  18. this.weather = data.lives[0];
  19. this.iconCondition = this.setWeatherIcon(data.lives[0]?.weather);
  20. },
  21. //设置时间只显示年月日
  22. formatDate(weather) {
  23. const date = new Date(weather.reporttime);
  24. const year = date.getFullYear();
  25. const month = (1 + date.getMonth()).toString().padStart(2, '0');
  26. const day = date.getDate().toString().padStart(2, '0');
  27. return `${year}-${month}-${day}`;
  28. },
  29. // 设置icon
  30. setWeatherIcon(weather) {
  31. // 只处理了基础的天气,可以继续精细化处理
  32. if (weather === '晴') {
  33. return 'clear-day';
  34. } else if (weather.includes('云')) {
  35. return 'partly-cloudy-day';
  36. } else if (weather.includes('风')) {
  37. return 'wind';
  38. } else if (weather.includes('雨')) {
  39. return 'rain';
  40. } else if (weather.includes('雪')) {
  41. return 'snow';
  42. } else if (weather.includes('雾')) {
  43. return 'fog';
  44. }
  45. return 'cloudy';
  46. },
  47. }

到这里页面上就会显示出一个很好看的实时定位的天气小组件啦

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/901528
推荐阅读
相关标签
  

闽ICP备14008679号