|
@@ -1,18 +1,25 @@
|
1
|
1
|
const SERVER_KEY = 'a30b66a26ea1890fd656cf11cb2647fb';
|
2
|
2
|
|
3
|
|
-export default function getWeather(city) {
|
4
|
|
- const url = `https://restapi.amap.com/v3/weather/weatherInfo?key=${SERVER_KEY}&city=${city}&extensions=all`;
|
5
|
|
-
|
6
|
|
- return fetch(url)
|
|
3
|
+export default function getWeather() {
|
|
4
|
+ // 先获取当前位置
|
|
5
|
+ return fetch(`https://restapi.amap.com/v3/ip?key=${SERVER_KEY}`)
|
7
|
6
|
.then((res) => res.json())
|
8
|
7
|
.then((res) => {
|
9
|
|
- if (res.info === 'OK') {
|
10
|
|
- return res.forecasts;
|
11
|
|
- } else {
|
12
|
|
- return Promise.reject(res.info);
|
|
8
|
+ if (res.adcode) {
|
|
9
|
+ const url = `https://restapi.amap.com/v3/weather/weatherInfo?key=${SERVER_KEY}&city=${res.adcode}&extensions=all`;
|
|
10
|
+
|
|
11
|
+ return fetch(url)
|
|
12
|
+ .then((res) => res.json())
|
|
13
|
+ .then((res) => {
|
|
14
|
+ if (res.info === 'OK') {
|
|
15
|
+ return res.forecasts;
|
|
16
|
+ } else {
|
|
17
|
+ return Promise.reject(res.info);
|
|
18
|
+ }
|
|
19
|
+ })
|
|
20
|
+ .catch((err) => {
|
|
21
|
+ console.log(err.message);
|
|
22
|
+ });
|
13
|
23
|
}
|
14
|
|
- })
|
15
|
|
- .catch((err) => {
|
16
|
|
- console.log(err.message);
|
17
|
24
|
});
|
18
|
25
|
}
|