美文网首页
小程序:天气

小程序:天气

作者: 0清婉0 | 来源:发表于2021-03-24 19:41 被阅读0次

读书时,发现书上的和风天气接口有些过时了,

于是按着视频中周老师教的方法,修改了一些代码。

而且还要加一个函数,才可以获取到天气数据,之前书上说的地区名是拼音,但现在是数字,

最后想了想,又看了两遍视频,终于明白如何修改了。

并且还加了三天的实时天气信息,本来想加24小时的天气信息,但苦于有权限,无法增加。

代码中加粗文字就是修改和增加的

【index.js】

data: {

    region:['北京市','北京市','朝阳区'],

    locationid:'101010100',

    threeday:[],

    now:{

      temp:0,

      text:'未知',

      icon:'999',

      humidity:0,

      pressure:0,

      vis:0,

      windDir:0,

      windSpeed:0,

      windScale:0

    }

  },

  regionChange: function (e) {

    // console.log(e);

    this.setData({

      region:e.detail.value

    })

    this.getLocationid();

  },

  // 获取城市id

  getLocationid:function(){

    var that = this;

    wx.request({

      url: 'https://geoapi.qweather.com/v2/city/lookup',

      data:{

        location:that.data.region[2],

        adm1: that.data.region[1],

        key:'和风天气密钥'

      },

      success:function(res_id){

        // console.log(res_city.data.location[0].id);

        that.setData({

          locationid:res_id.data.location[0].id

        })

      }

    })

    this.getWeather();

    this.getthreeday();

  },

  // 获取天气数据

  getWeather: function () {

    var that = this;

    wx.request({

      url: 'https://devapi.qweather.com/v7/weather/now',

      data:{

        location:that.data.locationid,

        key:'和风天气密钥'

      },

      success:function(res){

        // console.log(res.data);

        that.setData({

          now:res.data.now

        })

      }

    })

  },

    // 获取3天天气

    getthreeday:function(){

      var that = this;

      wx.request({

        url: 'https://devapi.qweather.com/v7/weather/3d',

        data:{

          location:that.data.locationid,

          key:'和风天气密钥'

        },

        success:function(res){

          // console.log(res.data.daily)

          that.setData({

            threeday:res.data.daily

          })

        }

      })

 },

onLoad: function (options) { this.getLocationid() },

【index.xwml】

<div class="container">

  <!--地区-->

  <picker mode="region" bindchange="regionChange">

    <view>{{region}}</view>

  </picker>

  <!--天气信息-->

  <text>{{now.temp}}℃ {{now.text}}</text>

  <!--天气图标

  <image src="/images/weather_icon/{{now.icon}}.png" mode="widthFix"></image>-->

  <!--3日内信息-->

  <view class="detail detail2">

    <block class="detail_box" wx:for="{{threeday}}" wx:key="threeday_{{index}}">

      <view class="bar">

        <view class="box">{{item.fxDate}}</view>

        <view class="box"><image src="/images/weather_icon/{{item.iconDay}}.png" mode="widthFix"></image></view>

        <view class="box">{{item.tempMax}}</view>

        <view class="box">{{item.tempMin}}</view>

      </view>

    </block>

  </view>

  <!--天气信息-->

  <view class="detail">

    <view class="bar">

      <view class="box">温度</view>

      <view class="box">气压</view>

      <view class="box">能见度</view>

    </view>

    <view class="bar">

      <view class="box">{{now.humidity}} %</view>

      <view class="box">{{now.pressure}} hpa</view>

      <view class="box">{{now.vis}} km</view>

    </view>

    <view class="bar">

      <view class="box">风向</view>

      <view class="box">风速</view>

      <view class="box">风力</view>

    </view>

    <view class="bar">

      <view class="box">{{now.windDir}}</view>

      <view class="box">{{now.windSpeed}} km/h</view>

      <view class="box">{{now.windScale}} 级</view>

    </view>

  </view>

</div>

css基本没变化,省略了。

相关文章

  • 小程序:天气

    读书时,发现书上的和风天气接口有些过时了, 于是按着视频中周老师教的方法,修改了一些代码。 而且还要加一个函数,才...

  • 天气应用小程序-实战

    完整小程序项目实战 Sass + ES6 进行代码编写 通过构建工具编译成小程序可以识别的 WXSS 和 ES5,...

  • 一周学会小程序-日播天气

    前言:欢迎收看一周学会小程序系列2-日播天气。看了苹果的自带天气软件,发现很简单使用。在小程序上看了一下天气的小程...

  • 微信小程序—天气APP

    项目github地址https://github.com/huruji/Wechat-WeAPP,欢迎star 一...

  • 开源一个天气小程序:轻松天气

    前言 ⛅轻松天气 这次开源的小项目是一个微信小程序: 轻松天气 这是我 2017 年还在学校时自己做的一个小程序,...

  • 微信小程序之百度地图API

    最近给我的小程序增加了天气预报的功能,采用的是Baidu地图的小程序API。 百度地图微信小程序JavaScrip...

  • 极简天气之小夭天气

    1、利用周末两天的时间,参考小天气的风格,撸了一个天气小程序,【小夭天气】。 2、功能十分简单,查看当前地区的天气...

  • 微信小程序笔记二:天气应用--小好天气

    微信小程序笔记二:天气应用 源码github地址在此,记得点星:https://github.com/brando...

  • 微信小程序之天气预报

    历时3天,从微信小程序调研到开发完成,天气预报微信小程序终于开发完成。 2天用来浏览小程序官方文档。 1天用来编写...

  • 小程序-即时天气(已上线)-----分享

    由于公司打算要搞一个小程序app,所以趁闲先自己搞一个试试手,小程序搜索----即时天气(已上线)。 浅谈项目 这...

网友评论

      本文标题:小程序:天气

      本文链接:https://www.haomeiwen.com/subject/rnnhhltx.html