欢迎光临,了解微信小程序开发,就上易用通!

小程序实战--天气预报

发布:2017-12-28 15:35浏览: 来源:网络 作者:tianshu

这个案例是仿UC中天气界面做的中间也有点出入,预留了显示当前城市名字和刷新图标的位置,自己可以写下,也可以添加搜索城市。值得注意的是100%这个设置好像已经不好使了,可以通过获取设备的高度通过数据绑定设置高度。地址:https://github.com/shuncaigao/Weather

界面主要分为四部分:

这里写图片描述

第一部分

这里写图片描述

这里是预留的一块可以自行添加补充下

<view class="newTopView">
<!--左边添加当前城市名字,点击跳转选择城市 右边添加刷新当前天气-->
</view>

第二部分:

这里写图片描述

 <view class="topView">
    <view class="degreeView">
    <!--当前温度-->
      <text class="degree">{{currentTemperature}}</text>
      <!--度数图标-->
      <image class="circle" src="../../image/circle.png"></image>
    </view>
    <view class="detailInfo">
      <view class="degreeView">
      <!--夜间天气情况-->
        <text class="detailInfoDegree">{{nightAirTemperature}}</text>
        <image class="detailInfoCircle" src="../../image/circle.png" />
        <text class="detailInfoLine">/</text>
        <!--白天天气-->
        <text class="detailInfoDegree">{{dayAirTemperature}}</text>
        <!-- style优先级比class高会覆盖class中相同属性 -->
        <image class="detailInfoCircle" style="margin-left: 57rpx; margin-right: 40rpx" src="../../image/circle.png" />
        <!-- 当前天气名字 -->
        <text class="detailInfoName">{{weather}}</text>
      </view>
    </view>
  </view>

第四部分:

这里写图片描述

 <!-- 底部view -->
  <view class="bottomView">
  <!--数据返回的不是数组 在js中拼接的数组-->
    <block wx:for-items="{{list}}">
      <view class="bottomItemView">
        <image class="bottomImage" src="{{item.day_weather_pic}}" style="margin-bottom: 15rpx;" />
        <text wx:if="{{item.weekday == 1}}" class="bottomText">星期一</text>
        <text wx:elif="{{item.weekday == 2}}" class="bottomText">星期二</text>
        <text wx:elif="{{item.weekday == 3}}" class="bottomText">星期三</text>
        <text wx:elif="{{item.weekday == 4}}" class="bottomText">星期四</text>
        <text wx:elif="{{item.weekday == 5}}" class="bottomText">星期五</text>
        <text wx:elif="{{item.weekday == 6}}" class="bottomText">星期六</text>
        <text wx:else="{{item.weekday == 7}}" class="bottomText">星期日</text>
        <view class="degreeView" style="margin-top: 20rpx;">
          <text class="detailInfoDegree">{{item.night_air_temperature}}</text>
          <image class="detailInfoCircle" src="../../image/circle.png" />
          <text class="detailInfoLine">/</text> 
          <text class="detailInfoDegree">{{item.day_air_temperature}}</text>
          <!-- style优先级比class高会覆盖class中相同属性 -->
          <image class="detailInfoCircle" style="margin-left: 57rpx; margin-right: 40rpx" src="../../image/circle.png" />
        </view>
      </view>

js

//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    //加载状态
    loadingHidden: false,
    //当前温度
    currentTemperature: '',
    //夜间温度
    nightAirTemperature: '',
    //白天温度
    dayAirTemperature: '',
    //当前天气
    weather: '',
    //污染指数
    aqi: '',
    //污染程度
    quality: '',
    //风力
    windPower: '',
    //风向
    windDirection: '',
    //因为数据返回不是数组所以要自己封装一个数组
    list: [],
    height: 0,


  },

  onLoad: function () {
    console.log('onLoad')
    var that = this

    //100%好像不好使 可以获取设备高度
    wx.getSystemInfo({
      success: function (res) {
        that.data.height = res.windowHeight;
      }
    })

    wx.getLocation({
      success: function (res) {
        //通过经纬度请求数据
        wx.request({
          //这个网站有免费API赶紧收藏
          url: 'https://route.showapi.com/9-5',
          data: {
            showapi_appid: '11697',
            showapi_sign: '6c0c15c5ec61454dac5288cea2d32881',
            //
            from: '5',
            lng: res.longitude,
            lat: res.latitude,
            //获取一周情况 0是不获取
            needMoreDay: '1',
            needIndex: '1'
          },
          success: function (res) {
            console.log(res)
            console.log(res.data.showapi_res_body.now.api)
            that.setData({
              //改变加载状态
              loadingHidden: true,

              currentTemperature: res.data.showapi_res_body.now.temperature,
              nightAirTemperature: res.data.showapi_res_body.f1.night_air_temperature,
              dayAirTemperature: res.data.showapi_res_body.f1.day_air_temperature,
              weather: res.data.showapi_res_body.now.weather,
              aqi: res.data.showapi_res_body.now.aqi,
              quality: res.data.showapi_res_body.now.aqiDetail.quality,
              windPower: res.data.showapi_res_body.now.wind_power,
              windDirection: res.data.showapi_res_body.now.wind_direction,
              //拼接数组
              list: [
                {
                  'day_weather_pic': res.data.showapi_res_body.f1.day_weather_pic,
                  'weekday': res.data.showapi_res_body.f1.weekday,
                  'day_air_temperature': res.data.showapi_res_body.f1.day_air_temperature,
                  'night_air_temperature': res.data.showapi_res_body.f1.night_air_temperature
                },
                {
                  'day_weather_pic': res.data.showapi_res_body.f2.day_weather_pic,
                  'weekday': res.data.showapi_res_body.f2.weekday,
                  'day_air_temperature': res.data.showapi_res_body.f2.day_air_temperature,
                  'night_air_temperature': res.data.showapi_res_body.f2.night_air_temperature
                },
                {
                  'day_weather_pic': res.data.showapi_res_body.f3.day_weather_pic,
                  'weekday': res.data.showapi_res_body.f3.weekday,
                  'day_air_temperature': res.data.showapi_res_body.f3.day_air_temperature,
                  'night_air_temperature': res.data.showapi_res_body.f3.night_air_temperature
                },
                {
                  'day_weather_pic': res.data.showapi_res_body.f4.day_weather_pic,
                  'weekday': res.data.showapi_res_body.f4.weekday,
                  'day_air_temperature': res.data.showapi_res_body.f4.day_air_temperature,
                  'night_air_temperature': res.data.showapi_res_body.f4.night_air_temperature
                },
                {
                  'day_weather_pic': res.data.showapi_res_body.f5.day_weather_pic,
                  'weekday': res.data.showapi_res_body.f5.weekday,
                  'day_air_temperature': res.data.showapi_res_body.f5.day_air_temperature,
                  'night_air_temperature': res.data.showapi_res_body.f5.night_air_temperature
                },
                {
                  'day_weather_pic': res.data.showapi_res_body.f6.day_weather_pic,
                  'weekday': res.data.showapi_res_body.f6.weekday,
                  'day_air_temperature': res.data.showapi_res_body.f6.day_air_temperature,
                  'night_air_temperature': res.data.showapi_res_body.f6.night_air_temperature
                },
                {
                  'day_weather_pic': res.data.showapi_res_body.f7.day_weather_pic,
                  'weekday': res.data.showapi_res_body.f7.weekday,
                  'day_air_temperature': res.data.showapi_res_body.f7.day_air_temperature,
                  'night_air_temperature': res.data.showapi_res_body.f7.night_air_temperature
                }

              ]
            })
          }
        })

      }
    })

  }
})

PS:

开发者工具无法显示问题:是因为View没有获得高度,默认个高度或者直接修改wxml中height高度即可





免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。