微信小程序(一)天气预报

来源:互联网 发布:陀飞轮手表价格 知乎 编辑:程序博客网 时间:2024/05/16 08:41

一、准备工作

1、开发工具下载

            https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/download.html

            
         2、开发文档

               https://mp.weixin.qq.com/debug/wxadoc/dev/index.html

二 、开发

         1、最终页面展示

               
})
        
    2、目录结构
            
             


       3、  main.js内容:

 Page({
  data:{
    city:"",
    today:[],
    future:[]
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    this.loadInfo();
    
  },
  onReady:function(){
    // 页面渲染完成
    
  },
  onShow:function(){
    // 页面显示
   
  },
  onHide:function(){
    // 页面隐藏
    
  },
  onUnload:function(){
    // 页面关闭
    
  },
  loadInfo:function(){
    var page = this;
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: function(res) {
        var latitude = res.latitude
        var longitude = res.longitude
        console.log(latitude,longitude)
        page.loadCity(latitude,longitude)
      }
    })
  },
  loadCity:function(latitude,longtitude){
      var page = this;
      wx.request({
        url: 'http://api.map.baidu.com/geocoder/v2/?ak=zn19BItWZoNh4RtAM7VurCWAKGoCYKmA&location='+latitude+','+longtitude+'&output=json',
        header: {
            'content-type': 'application/json'
        },
        success: function(res) {
          console.log(res)
          var city = res.data.result.addressComponent.city + " " +res.data.result.addressComponent.district;
          console.log(city);
          page.setData({city:city});
          city = res.data.result.addressComponent.city;
          city =city.replace("市","");
          page.loadWeather(city);
        }
      })
  },
  loadWeather:function(city){
    var page = this;
    wx.request({
        url: 'http://wthrcdn.etouch.cn/weather_mini?city='+city,
        header: {
            'content-type': 'application/json'
        },
        success: function(res) {
          console.log(res)
          var future = res.data.data.forecast;
          var todayInfo = future.shift();
          var today = res.data.data;
          today.todayInfo = todayInfo;
          page.setData({today:today,future:future});
        }
    })
  }

 

4、  main.wxml内容:

    <view class="content">
    <view class="today">
        <view class = "today-date">
            <view>{{today.todayInfo.date}}</view>
        </view>
        <view class = "info">
            <view class="temp">{{today.wendu}}℃</view>
            <view class="weather">{{today.todayInfo.type}} {{today.todayInfo.fengxiang}} {{today.todayInfo.fengli}}</view>
            <view class= "information">{{today.ganmao}}</view>
            <view class ="city">{{city}}</view>
        </view>
    <view>
    
    <import src = "../template/itemtpl"/>
    <view class ="future">
        <block wx:for = "{{future}}" wx:key = "uniqu" >
            <template is = "future-item" data="{{item}}"/>
        </block>
    </view>
    
</view>

5、  main.wxss内容:

         page{
    height: 100%;
    width: 100%;
}
.content{
    /*font-family: 微软雅黑,宋体;
    font-size: 14px;*/
    position: relative ;
    font: 18px "微软雅黑,宋体";
    background-size: cover;
    height:100%;
    width: 100%;
    background-image:url("../assets/img/bg.png"); 
    color: #FFFFFF;  
}
.info{
    margin-top: 50rpx;
    padding: 20px;
    background: #FFFFFF;
    background-color: rgba(0,0,0,.5);
    box-shadow: 10px 10px 20px rgba(0, 0, 0, .5);
}
.today{
    padding-top: 50rpx;
    height: 50%;
}
.temp{
    font-size: 50px;
    text-align: center;  
}
.information{
    margin-top: 20rpx;
}
.city{
    color: white;
    font-size: 25px;
    text-align: right;
    margin-right: 10rpx;
    margin-top: 30rpx;
}
.weather{
    text-align: center;
}
.future{
    width: 100%;
    display: flex;
    flex-direction: row;
    position: absolute ;
    bottom: 60rpx;
    padding-left: 15rpx;
    background: #000000;
    background: rgba(0, 0, 0, .4);
    box-shadow: 10px 10px 20px rgba(0, 0, 0, 0.5);
    padding-top: 10rpx;
    padding-bottom: 20rpx;
}
.future-item{
    text-align: center;
    height: 400rpx;
    min-height: 100%;
    width: 165rpx;
    margin-left: 10rpx;
    border: 1px solid  coral;
    border-radius: 10px;
    padding-top: 10rpx;
}
.future-item view{
    margin-top: 10px;
}
.today-date{
    background: #FFFFFF;
    background: rgba(0, 0, 0, .4);
    font-size: 40px;
    box-shadow: 10px 10px 20px rgba(0, 0, 0, .5);
    text-align: center;
    padding: 20px;
}

6、  itemtpl.wxml内容:

<template name= "future-item">
    <view class = "future-item">
        <view>{{item.date}}</view>
        <view>{{item.type}}</view>
        <view>{{item.fengxiang}}</view>
        <view>{{item.low}}</view>
        <view>{{item.high}}</view>  
    </view>  
</template>

0 0
原创粉丝点击