| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <!DOCTYPE html>
- <html>
- <head lang="en">
- <meta charset="UTF-8" />
- <title>天气</title>
- <script src="http://static.vbdsm.com/js/lib/jquery-1.8.0.min.js"></script>
- <script src="http://static.vbdsm.com/js/lib/jquery.query-2.1.7.js"></script>
- <script src="http://static.vbdsm.com/js/common.tools.js"></script>
- <script src="http://static.vbdsm.com/js/util.js"></script>
- <link href="weather.css" rel="stylesheet" />
- <style>
- body {
- overflow: hidden;
- margin-left: -6px;
- }
- </style>
- <script charset="UTF-8" type="text/javascript"></script>
- </head>
- <body>
- <div id="future_container">
- <div class="future_box">
- <img alt="天气" height="100px" src="./images/weather_icon/1.png" />
- <span class="info">城市</span>
- <span class="info">---</span>
- <span class="info">--</span>
- <span class="info">--</span>
- </div>
- </div>
- <script>
- var cid = $.query.get("auth_cid")
- $("body").append(
- '<script src="/system/lang/desktop/config.json?script=1&auth_cid=' + cid + '"><\/script>'
- )
- $(function () {
- //getWinXin()
- getGdWeather();
- })
- function getWinXin() {
- $.ajax({
- url: "http://weixin.jirengu.com/weather?jsoncallback=getWeather",
- type: "get",
- dataType: "json",
- xhrFields: {
- widthCredentials: true,
- },
- success: function (data) {
- if (data.status == "OK") {
- var weather = data.weather[0]
- var oSpan = document.getElementsByClassName("info")
- oSpan[0].innerHTML = weather.city_name
- oSpan[1].innerHTML = weather.now.text
- oSpan[2].innerHTML = weather.now.temperature + "°C"
- oSpan[3].innerHTML = weather.future[0].wind
- var code = weather.now.code
- //changeImg(response);
- var firstImg = document.getElementsByTagName("img")[0]
- firstImg.src =
- "http://weixin.jirengu.com/images/weather/code/" + encodeURI(code) + ".png"
- }
- },
- })
- }
- //高德地图获取天气
- function getAdCode(){
- return new Promise((resolve, reject)=>{
- $.ajax({
- url: "https://restapi.amap.com/v3/ip?key=ee001a52e68437f9a030eaa01c3a9646",
- type: 'get',
- dataType: 'json',
- success: function (data) {
- resolve(data);
- },
- error: function (data) {
- reject(data);
- }
- })
- })
- }
- function getGdWeather() {
- getAdCode().then((adcode)=>{
- if(adcode && adcode.status == "1"){
- $.ajax({
- url: `https://restapi.amap.com/v3/weather/weatherInfo?key=ee001a52e68437f9a030eaa01c3a9646&city=${adcode.adcode}&extensions=base`,
- type: 'get',
- dataType: 'json',
- success: function (data) {
- if(data && data.status == "1"){
- var weather = data.lives[0];
- let nowText = new Date(weather.reporttime).toLocaleDateString('zh').replaceAll('/', '-')
- var oSpan = document.getElementsByClassName('info');
- oSpan[0].innerHTML = weather.city;
- oSpan[1].innerHTML = nowText;
- oSpan[2].innerHTML = weather.temperature+ "°C";
- oSpan[3].innerHTML = weather.winddirection + " " + weather.windpower + "级";
- var firstImg = document.getElementsByTagName("img")[0];
- var code = getWeatherImage(weather.weather);
- console.error(code)
- firstImg.src = "./images/weather_icon/" + encodeURI(code) + ".png"
- }
- },
- error: function (data) {
- console.error(data)
- }
- })
- }
- })
- }
- const iconWeatherMap = {
- '风': ['有风', '平静', '微风', '和风', '清风', '强风/劲风', '疾风', '大风', '烈风', '风暴', '狂爆风', '飓风', '热带风暴', '龙卷风'],
- '多云': ['少云', '晴间多云', '多云'],
- '雪': ['雪', '阵雪', '小雪', '中雪', '大雪', '暴雪', '小雪-中雪', '中雪-大雪', '大雪-暴雪', '冷'],
- '雾': ['浮尘', '扬沙', '沙尘暴', '强沙尘暴', '雾', '浓雾', '强浓雾', '轻雾', '大雾', '特强浓雾'],
- '晴': ['晴', '热'],
- '雨夹雪': ['雨雪天气', '雨夹雪', '阵雨夹雪'],
- '雨': ['阵雨', '雷阵雨', '雷阵雨并伴有冰雹', '小雨', '中雨', '大雨', '暴雨', '大暴雨', '特大暴雨', '强阵雨', '强雷阵雨', '极端降雨', '毛毛雨/细雨', '雨', '小雨-中雨', '中雨-大雨', '大雨-暴雨', '暴雨-大暴雨', '大暴雨-特大暴雨', '冻雨'],
- '阴': ['阴', '霾', '中度霾', '重度霾', '严重霾', '未知']
- }
- function getWeatherImage(weather) {
- for (const weatherKey in iconWeatherMap) {
- if (Object.hasOwnProperty.call(iconWeatherMap, weatherKey)) {
- const weatherNames = iconWeatherMap[weatherKey]
- const findWeatherItem = weatherNames.find(name => weather === name)
- // 如果找了某一类的图标了,那重新赋值url
- if (findWeatherItem) {
- return weatherKey
- }
- }
- }
- }
- </script>
- </body>
- </html>
|