Weather_wev8.js 4.13 KB
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);
});