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: '获取天气信息失败!'
});
}
});
}
})
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