1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| <!DOCTYPE html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Weather API Test Page</title> <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/jquery@3.7.0/dist/jquery.min.js"></script> <script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.12.1/dist/cdn.min.js"></script> </head>
<body>
<div id="content">
<ul id="lives" x-data="lives" data-weather="今天天气"> <template x-for="item in liveslist" :key="item.reporttime"> <li> <span x-text="item.city"></span> <span x-text="item.weather"></span> <span x-text="item.temperature"></span> <span x-text="item.winddirection"></span> <span x-text="item.windpower"></span> <span x-text="item.humidity"></span> <span x-text="item.reporttime"></span> </li> </template> </ul>
<ul id="forecast" x-data="forecast" data-weather="预报天气"> <template x-for="forecastItem in forecastslist" :key="forecastItem.reporttime"> <template x-for="item in forecastItem.casts" :key="item.date"> <li> <span x-text="item.date"></span> <span x-text="item.week"></span> <span x-text="item.dayweather"></span> <span x-text="item.nightweather"></span> <span x-text="item.daytemp"></span> <span x-text="item.nighttemp"></span> <span x-text="item.daywind"></span> <span x-text="item.nightwind"></span> <span x-text="item.daypower"></span> <span x-text="item.nightpower"></span> </li> </template> </template> </ul> </div>
<script>
(function () { const KEY = "51aa9fcb4dcf672049c566e977b437c0";
function getCurrentCity() { return new Promise((resolve, reject) => { $.ajax({ method: 'get', data: { key: KEY, }, url: 'https://restapi.amap.com/v3/ip', success: function (data) { resolve({ position: data }); }, error: function (error) { reject(error); } }) }) };
function getWeather(adcode, type) { return new Promise((resolve, reject) => { $.ajax({ method: 'get', data: { key: KEY, city: adcode, extensions: type, }, url: 'https://restapi.amap.com/v3/weather/weatherInfo', success: function (data) { resolve({ [type]: data }); }, error: function (error) { reject(error); } }) }) };
async function handler() { const { position: { status = '', adcode = '', province = '', city = '' } } = await getCurrentCity(); const { all: weatherAll } = await getWeather(adcode, 'all'); const { base: weatherBase } = await getWeather(adcode, 'base');
const { forecasts = [] } = weatherAll; const { lives = [] } = weatherBase; return { weatherAll, weatherBase } };
const lives = { province: '省份名', city: '城市名', adcode: '区域编码', weather: '天气现象(汉字描述)', temperature: '实时气温,单位:摄氏度', winddirection: '风向描述', windpower: '风力级别,单位:级', humidity: '空气湿度', reporttime: '数据发布的时间', };
const forecast_casts = { date: '日期', week: '星期几', dayweather: '白天天气现象', nightweather: '晚上天气现象', daytemp: '白天温度', nighttemp: '晚上温度', daywind: '白天风向', nightwind: '晚上风向', daypower: '白天风力', nightpower: '晚上风力', };
document.addEventListener('alpine:init', () => { Alpine.data('lives', () => ({ liveslist: [], async init() { const { weatherBase } = await handler(); this.liveslist = weatherBase.lives || []; console.log('lives', weatherBase); } }));
Alpine.data('forecast', () => ({ forecastslist: [], async init() { const { weatherAll } = await handler(); this.forecastslist = weatherAll.forecasts || []; console.log('forecast', weatherAll.forecasts); },
})); });
})(); </script>
</body>
</html>
|