设为首页 加入收藏

TOP

小程序开发之——简简天气(二)
2017-10-11 14:02:05 】 浏览:5848
Tags:程序开发 简简 天气
om
: 0.1em; } .box-date { font-size: 0.8em; text-align: center; vertical-align: middle; padding-top: 0.1em; padding-bottom: 0.1em; } .flex-weather { flex-direction: row; display: -webkit-flex; align-items: center; text-align: center; margin-top: 0.1em; margin-bottom: 0.3em; } .flex-item-3 { width: 33.33%; padding: 0.1em; } .pm25 { font-size: 1.5em; margin-left: 1em; margin-right: 1em; border-radius: 0.1em; } .weather-text { font-size: 0.9em; margin-bottom: 0.1em; margin-top: 0.1em; }

逻辑层 index.js

var Bmap = require("../../bmap-wx.js");
var util = require("../../util.js");
var app = getApp();
Page({
  data: {
    weather: {},
    device: app.globalData.deviceInfo
  },
  onLoad: function () {
    var that = this;
    var bmap = new Bmap.BMapWX({ ak: "需要填写你的ak,免费申请"});
    bmap.weather({
      success:function(data){
        that.setData({ weather: util.formatWeather(data)});
      },
      fail: function(data){
        wx.showToast({
          title: '获取天气信息失败!'
        });
      }
    });
  }
})

用来处理json的工具类 util.js

function formatWeather(res) {
  var weathers = {
    today: {},
    list: []
  };
  console.log(res);
  weathers.today.city = res.currentWeather[0]["currentCity"];
  weathers.today.pm25 = res.currentWeather[0]["pm25"];
  weathers.today.temperature = res.currentWeather[0]["temperature"];
  weathers.today.weather = res.currentWeather[0]["weatherDesc"];
  weathers.today.wind = res.currentWeather[0]["wind"];
  var today_date = res.currentWeather[0]["date"].split(/[ :()]/);

  weathers.today.c_temperature = today_date[4];
  weathers.today.date = today_date[1] + " " + today_date[0];
  var weatherArray = res.originalData.results[0].weather_data;
  for (var i = 0; i < weatherArray.length; i++) {
    var weather = {};
    var date = new Date((Date.parse(res.originalData.date) / 1000 + (86400 * i)) * 1000);
    var month = date.getMonth() + 1;
    month = month > 9 ? month + "月" : "0" + month + "月";
    weather.date = month + date.getDate() + "日" + " " + weatherArray[i]["date"].split(" ")[0];
    weather.temperature = weatherArray[i]["temperature"];
    weather.weather = weatherArray[i]["weather"];
    weather.wind = weatherArray[i]["wind"];
    var day_pic = weatherArray[i]["dayPictureUrl"];
    weather.icon_day = day_pic.substring(day_pic.lastIndexOf("/") + 1, day_pic.length);
    var night_pic = weatherArray[i]["nightPictureUrl"];
    weather.icon_night = night_pic.substring(night_pic.lastIndexOf("/") + 1, night_pic.length);
    weathers.list.push(weather);
  }
  if (new Date().getHours() > 18) {
    weathers.today.icon = weathers.list[0].icon_night;
  } else {
    weathers.today.icon = weathers.list[0].icon_day;
  }
  if (weathers.today.pm25 > 300){
    weathers.today.pm_color = "#6d001d";
  }else if (weathers.today.pm25 > 200) {
    weathers.today.pm_color = "#884898";
  } else if (weathers.today.pm25 > 150) {
    weathers.today.pm_color = "#f00";
  } else if (weathers.today.pm25 > 100) {
    weathers.today.pm_color = "#fc0";
  }else{
    weathers.today.pm_color = "#0f0";
  }

 weathers.index = [];
 var indexArray = res.originalData.results[0].index;
 for(va
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Android项目目录结构 下一篇小程序开发之——简简天气

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目