var myWeather = (function(){ var funShun = "https://api.thinkpage.cn/v3/weather/now.json?location=Fushun&callback=showWeather&" return { el : {}, init : function(){ var that = this; $.extend(that.el,{ ‘nowWeather‘ : $(‘.now-weather‘), ‘nowTemperature‘ : $(‘.now-temperature‘) }) }, getUrl : function(){ var time = Math.round(new Date().getTime()/1000); var str = "ts=" + time + "&ttl=30&uid=UF4C86B626"; var hash = CryptoJS.HmacSHA1(str, "nvczde0rd9tkkr3o"); var base = hash.toString(CryptoJS.enc.Base64); var sig = encodeURIComponent(base); var url = funShun + str + "&sig=" + sig; return url; }, getWeather : function(){ var that = this; console.log(that.el.nowWeather) $.ajax({ url:that.getUrl(), jsonp : ‘callback‘, jsonpCallback : ‘showWeather‘, success : function(data){ function showWeather(data){ that.el.nowWeather.html(data.results[0].now.text) that.el.nowTemperature.html(data.results[0].now.temperature) $.cookie(‘weather‘, JSON.stringify(data)) } eval(data); } }) }, rundev : function(){ this.init(); if($.cookie(‘weather‘) !=‘‘){ var weather = $.parseJSON($.cookie(‘weather‘)) this.el.nowWeather.html(weather.results[0].now.text) this.el.nowTemperature.html(weather.results[0].now.temperature) }else{ this.getWeather(); } } } })() myWeather.rundev();
时间: 2024-10-08 04:39:14