|
|
function isInArray(arr, value) {
|
|
|
for (var k = 0; k < arr.length; k++) {
|
|
|
if (value == arr[i]) {
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
// 数组升序排序
|
|
|
function ascSort(a, b) {
|
|
|
return a - b;
|
|
|
}
|
|
|
const this_url = document.currentScript.src;
|
|
|
$(document).ready(function () {
|
|
|
var parsedUrl = new URL(this_url);
|
|
|
var cid = (parsedUrl.searchParams.get("cid"));
|
|
|
cid = cid ? cid : document.getElementById('cid').value;
|
|
|
$.get("https://api.openweathermap.org/data/2.5/weather?id=" + cid + "&appid=d79a45c23a77a304b8fd2cca3590b89d&units=metric", function (data, status) {
|
|
|
var cityName = document.getElementById('location');
|
|
|
cityName.innerHTML = data.name;
|
|
|
var times = new Date(data.dt * 1000);
|
|
|
var dates = (times.getDate() < 10 ? '0' + times.getDate() : times.getDate());
|
|
|
var weeks = times.getDay();
|
|
|
var cityTime = document.getElementById('time');
|
|
|
cityTime.innerHTML = times.toDateString().split(" ")[0];
|
|
|
var cityWeeks = document.getElementById('weeks');
|
|
|
var sys_time = new Date();
|
|
|
var sys_weeks = sys_time.getDay();
|
|
|
var week = "";
|
|
|
if (true) { };
|
|
|
if (weeks == sys_weeks) week = "Today";
|
|
|
cityWeeks.innerHTML = times.toDateString().split(" ")[1] + '.' + dates;
|
|
|
//天气类型
|
|
|
var cityWeather = document.getElementById('weatherCondition');
|
|
|
cityWeather.innerHTML = data.weather[0].main;
|
|
|
cityWeather.style.color = '#3b7fb0';
|
|
|
var max_temp = document.getElementById('tempSign');
|
|
|
var currentImg = "https://www.chinahighlights.com/pic/weather/big/" + data.weather[0].icon + ".png";
|
|
|
max_temp.innerHTML = "<img src=" + currentImg + " />";
|
|
|
max_temp.style.fontSize = "25px";
|
|
|
var humiditys = document.getElementById('weatherInfo');
|
|
|
var sunrizes = new Date(data.sys.sunrise * 1000);
|
|
|
var sunsets = new Date(data.sys.sunset * 1000);
|
|
|
var sunrize_hours = (sunrizes.getHours() < 10 ? '0' + sunrizes.getHours() : sunrizes.getHours()) + ':';
|
|
|
var sunrize_minutes = (sunrizes.getMinutes() < 10 ? '0' + sunrizes.getMinutes() : sunrizes.getMinutes());
|
|
|
var sunset_hours = (sunsets.getHours() < 10 ? '0' + sunsets.getHours() : sunsets.getHours()) + ':';
|
|
|
var sunset_minutes = (sunsets.getMinutes() < 10 ? '0' + sunsets.getMinutes() : sunsets.getMinutes());
|
|
|
var currentTemp = document.getElementById('todayTemperature');
|
|
|
currentTemp.innerHTML = Math.round(data.main.temp_max) + '°C';
|
|
|
var currentDesc = document.getElementById('descr');
|
|
|
currentDesc.innerHTML = data.weather[0].main;
|
|
|
var currentHumility = document.getElementById('humiditys');
|
|
|
currentHumility.innerHTML = "humidity: " + data.main.humidity + '%';
|
|
|
var currentWind = document.getElementById('winds');
|
|
|
currentWind.innerHTML = "wind: " + data.wind.speed + 'km/h';
|
|
|
});
|
|
|
//未来五天天气预报
|
|
|
$.get("https://api.openweathermap.org/data/2.5/forecast?id=" + cid + "&appid=d79a45c23a77a304b8fd2cca3590b89d&units=metric", function (data, status) {
|
|
|
var
|
|
|
rangeTmpture = [], // 获取一天的温度,排序,获得最大温度值和最小温度值
|
|
|
nowDate = new Date(data.list[0].dt_txt).getDate(); // 当天日期 用来和 dailyDay 比较,判断是否到了第二天
|
|
|
var futureForest = document.getElementsByClassName('futureWeather');
|
|
|
var futureTime = document.getElementById('futureTime');
|
|
|
var futureDays = document.getElementById('futureDays');
|
|
|
var futureImg = document.getElementById('futureImg');
|
|
|
var futureTemp = document.getElementById('futureTemp');
|
|
|
for (var j = 0, i = 0; j < data.list.length; j++) {
|
|
|
if (i > futureForest.length - 1) {
|
|
|
break;
|
|
|
}
|
|
|
var dailyTime = new Date(data.list[j].dt_txt);
|
|
|
var dailyDay = dailyTime.getDate();
|
|
|
var dailyMonth = (dailyTime.getMonth() + 1) + '-';
|
|
|
var daily_month_day = dailyMonth + dailyDay;
|
|
|
if (nowDate != dailyDay) {
|
|
|
// 到了第二天
|
|
|
rangeTmpture.sort(ascSort); // 将昨天的温度从小到大排列
|
|
|
var futureTimeLi = "<span>" + dailyTime.toDateString().split(" ")[1] + ' ' + dailyDay + "</span>";
|
|
|
futureTime.innerHTML = futureTimeLi;
|
|
|
var futureDaysLi = "<span>" + dailyTime.toDateString().split(" ")[0] + "</span>";
|
|
|
futureDays.innerHTML = futureDaysLi;
|
|
|
var urlFutureImg = "https://www.chinahighlights.com/pic/weather/small/" + data.list[j + 1].weather[0].icon + ".png";
|
|
|
var futureImgNew = "<img src=" + urlFutureImg + " />";
|
|
|
futureImg.innerHTML = futureImgNew;
|
|
|
var futureTempLi = "<span>" + parseInt(rangeTmpture[0]) + '°C' + '-' + parseInt(rangeTmpture[rangeTmpture.length - 1]) + '°C' + "</span>";
|
|
|
futureTemp.innerHTML = futureTempLi;
|
|
|
futureForest[i].innerHTML = futureTime.innerHTML + futureDays.innerHTML + futureImg.innerHTML + futureTemp.innerHTML;
|
|
|
i++;
|
|
|
nowDate = dailyDay;
|
|
|
rangeTmpture = []; // 清空温度数组
|
|
|
rangeTmpture.push(data.list[j].main.temp_max);
|
|
|
continue;
|
|
|
}
|
|
|
rangeTmpture.push(data.list[j].main.temp_max);
|
|
|
}
|
|
|
});
|
|
|
});
|