Weather_wev8.js
4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
define(["mUtil", "Component", "weatherHelper", "amap"],function(mUtil, Component, weatherHelper) {
var Weather = function(options) {
var _weather = {};
Component.super(this, options);
this.type = "Weather";
this.tpl = this.type + "_html";
this.css = this.type + "_css";
var vm = this.viewModel = {
defalutUrl : weatherHelper.getImageUrl('未知'),
paramName : '',
cityType: '1',
city : "shanghai"
};
this.mounted = function(){
if(vm.cityType == '1' || (!$p(vm.paramName) && (!vm.city || /{\w+}/.test(vm.city)))){
var that = this;
AMap.plugin('AMap.CitySearch', function(){
var citysearch = new AMap.CitySearch();
citysearch.getLocalCity(function(status, result) {
if (status === 'complete' && result.info === 'OK' && result.bounds) {
vm.city = result.adcode;
vm.city = $p(vm.paramName) || vm.city;
that.loadData(vm.city.toLowerCase());
} else {
console.error(result.info);
}
});
});
}else {
vm.city = $p(vm.paramName) || vm.city;
this.loadData(vm.city.toLowerCase());
}
};
this.loadData = function(city){
var that = this, unit = 'c',
cityCode = weatherHelper.getWeatherCityCode(city);
var apiUrl = "https://restapi.amap.com/v3/weather/weatherInfo?key=fbbb3b85bc2133d171a6540deb92ca21&extensions=all&city=" + city;
reset(city, this.$el);
if(cityCode){
apiUrl = "https://restapi.amap.com/v3/weather/weatherInfo?key=fbbb3b85bc2133d171a6540deb92ca21&extensions=all&city=" + cityCode;
}
mUtil.ajax(apiUrl, function (data) {
var info = false;
if (data.status == "1" && data.count != "0") {
info = data.forecasts[0];
return _weather.configWeatherInfo(info);
}
_weather.configWeatherInfo(false);
}, {
error: function () {
_weather.configWeatherInfo(false);
}
});
};
function reset(city, $el){
var unit = 'c';
$(".wev-weather-next",$el).hide();
$(".wev-weather-curr img", $el).attr('src', weatherHelper.getImageUrl('未知'));
$(".wev-weather-curr span", $el).eq(1).text(city || "");
$(".wev-weather-curr span", $el).eq(0).text("-- °" + unit.toUpperCase());
}
_weather.configWeatherInfo = function(info){
var $el = this.$el, text, $currCity;
if(!info) {
$currCity = $(".wev-weather-curr span", $el).eq(1);
mUtil.getLabel(5279, "无法查询到#CITY#的天气信息。",function(msg){
$currCity.text(msg.replace("#CITY#",$currCity.text()));
});
return $currCity;
}
var futureDaysInfo = info.casts,
unit = 'c';
var $nextDay = $(".wev-weather-next", $el), temp;
$(".wev-weather-curr img", $el).attr('src', weatherHelper.getImageUrl(info.casts[0].dayweather));
$(".wev-weather-curr span", $el).eq(1).text(info.city);
$(".wev-weather-curr span", $el).eq(0).text((parseInt(info.casts[0].daytemp) + parseInt(info.casts[0].nighttemp)) / 2 + " °" + unit.toUpperCase());
$nextDay.show();
for (var i = 0, j = $nextDay.size(); i < j; i++) {
temp = futureDaysInfo[i+1];
$("img", $nextDay[i]).attr('src', weatherHelper.getImageUrl(temp.dayweather));
$("span", $nextDay[i]).eq(0).text(temp.date);
$("span", $nextDay[i]).eq(1).text((parseInt(temp.daytemp) + parseInt(temp.nighttemp)) / 2 + " °" + unit.toUpperCase());
}
};
_weather.configWeatherInfo = _weather.configWeatherInfo.bind(this);
};
return Component.init(Weather);
});