自动定位天气信息
1.拿到当前电脑ip
2.高德接口获取ip的地址
3.通过和风天气获取城市id
4.通过id获取实时天气
高德API:https://lbs.amap.com/api/webservice/guide/api/ipconfig
和风天气:https://dev.qweather.com/docs/api/weather/weather-now/
拿到当前IP
一、在index.js里引入
<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
二、获取IP
const ip = returnCitySN["cip"]
通过IP定位当前城市
this.$axios.get(`https://restapi.amap.com/v3/ip?ip=${ip}&key=你的key`).then(res => {
console.log(res)
}
通过地址拿到和风天气对应的ID
this.$axios.get(`https://geoapi.qweather.com/v2/city/lookup?location=${res.data.city}&key=你的key`).then(res => {
console.log(res)
}
通过ID和实时天气
this.$axios.get(`https://devapi.qweather.com/v7/weather/now?location=${res.data.location[0].id}&key=你的key`).then(res => {
console.log(res)
})