index.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <!DOCTYPE html>
  2. <html>
  3. <head lang="en">
  4. <meta charset="UTF-8" />
  5. <title>天气</title>
  6. <script src="http://static.vbdsm.com/js/lib/jquery-1.8.0.min.js"></script>
  7. <script src="http://static.vbdsm.com/js/lib/jquery.query-2.1.7.js"></script>
  8. <script src="http://static.vbdsm.com/js/common.tools.js"></script>
  9. <script src="http://static.vbdsm.com/js/util.js"></script>
  10. <link href="weather.css" rel="stylesheet" />
  11. <style>
  12. body {
  13. overflow: hidden;
  14. margin-left: -6px;
  15. }
  16. </style>
  17. <script charset="UTF-8" type="text/javascript"></script>
  18. </head>
  19. <body>
  20. <div id="future_container">
  21. <div class="future_box">
  22. <img alt="天气" height="100px" src="./images/weather_icon/1.png" />
  23. <span class="info">城市</span>
  24. <span class="info">---</span>
  25. <span class="info">--</span>
  26. <span class="info">--</span>
  27. </div>
  28. </div>
  29. <script>
  30. var cid = $.query.get("auth_cid")
  31. $("body").append(
  32. '<script src="/system/lang/desktop/config.json?script=1&auth_cid=' + cid + '"><\/script>'
  33. )
  34. $(function () {
  35. //getWinXin()
  36. getGdWeather();
  37. })
  38. function getWinXin() {
  39. $.ajax({
  40. url: "http://weixin.jirengu.com/weather?jsoncallback=getWeather",
  41. type: "get",
  42. dataType: "json",
  43. xhrFields: {
  44. widthCredentials: true,
  45. },
  46. success: function (data) {
  47. if (data.status == "OK") {
  48. var weather = data.weather[0]
  49. var oSpan = document.getElementsByClassName("info")
  50. oSpan[0].innerHTML = weather.city_name
  51. oSpan[1].innerHTML = weather.now.text
  52. oSpan[2].innerHTML = weather.now.temperature + "°C"
  53. oSpan[3].innerHTML = weather.future[0].wind
  54. var code = weather.now.code
  55. //changeImg(response);
  56. var firstImg = document.getElementsByTagName("img")[0]
  57. firstImg.src =
  58. "http://weixin.jirengu.com/images/weather/code/" + encodeURI(code) + ".png"
  59. }
  60. },
  61. })
  62. }
  63. //高德地图获取天气
  64. function getAdCode(){
  65. return new Promise((resolve, reject)=>{
  66. $.ajax({
  67. url: "https://restapi.amap.com/v3/ip?key=ee001a52e68437f9a030eaa01c3a9646",
  68. type: 'get',
  69. dataType: 'json',
  70. success: function (data) {
  71. resolve(data);
  72. },
  73. error: function (data) {
  74. reject(data);
  75. }
  76. })
  77. })
  78. }
  79. function getGdWeather() {
  80. getAdCode().then((adcode)=>{
  81. if(adcode && adcode.status == "1"){
  82. $.ajax({
  83. url: `https://restapi.amap.com/v3/weather/weatherInfo?key=ee001a52e68437f9a030eaa01c3a9646&city=${adcode.adcode}&extensions=base`,
  84. type: 'get',
  85. dataType: 'json',
  86. success: function (data) {
  87. if(data && data.status == "1"){
  88. var weather = data.lives[0];
  89. let nowText = new Date(weather.reporttime).toLocaleDateString('zh').replaceAll('/', '-')
  90. var oSpan = document.getElementsByClassName('info');
  91. oSpan[0].innerHTML = weather.city;
  92. oSpan[1].innerHTML = nowText;
  93. oSpan[2].innerHTML = weather.temperature+ "°C";
  94. oSpan[3].innerHTML = weather.winddirection + " " + weather.windpower + "级";
  95. var firstImg = document.getElementsByTagName("img")[0];
  96. var code = getWeatherImage(weather.weather);
  97. console.error(code)
  98. firstImg.src = "./images/weather_icon/" + encodeURI(code) + ".png"
  99. }
  100. },
  101. error: function (data) {
  102. console.error(data)
  103. }
  104. })
  105. }
  106. })
  107. }
  108. const iconWeatherMap = {
  109. '风': ['有风', '平静', '微风', '和风', '清风', '强风/劲风', '疾风', '大风', '烈风', '风暴', '狂爆风', '飓风', '热带风暴', '龙卷风'],
  110. '多云': ['少云', '晴间多云', '多云'],
  111. '雪': ['雪', '阵雪', '小雪', '中雪', '大雪', '暴雪', '小雪-中雪', '中雪-大雪', '大雪-暴雪', '冷'],
  112. '雾': ['浮尘', '扬沙', '沙尘暴', '强沙尘暴', '雾', '浓雾', '强浓雾', '轻雾', '大雾', '特强浓雾'],
  113. '晴': ['晴', '热'],
  114. '雨夹雪': ['雨雪天气', '雨夹雪', '阵雨夹雪'],
  115. '雨': ['阵雨', '雷阵雨', '雷阵雨并伴有冰雹', '小雨', '中雨', '大雨', '暴雨', '大暴雨', '特大暴雨', '强阵雨', '强雷阵雨', '极端降雨', '毛毛雨/细雨', '雨', '小雨-中雨', '中雨-大雨', '大雨-暴雨', '暴雨-大暴雨', '大暴雨-特大暴雨', '冻雨'],
  116. '阴': ['阴', '霾', '中度霾', '重度霾', '严重霾', '未知']
  117. }
  118. function getWeatherImage(weather) {
  119. for (const weatherKey in iconWeatherMap) {
  120. if (Object.hasOwnProperty.call(iconWeatherMap, weatherKey)) {
  121. const weatherNames = iconWeatherMap[weatherKey]
  122. const findWeatherItem = weatherNames.find(name => weather === name)
  123. // 如果找了某一类的图标了,那重新赋值url
  124. if (findWeatherItem) {
  125. return weatherKey
  126. }
  127. }
  128. }
  129. }
  130. </script>
  131. </body>
  132. </html>