vue前端页面获取实时天气

 首先获取用户定位的ip地址,我这边使用的是太平洋网络IP地址查询Web接口 (pconline.com.cn)

提供的接口,使用方法如下

<script>
    function testJson(obj) {
      sessionStorage.setItem('pro', obj.pro)
      sessionStorage.setItem('city', obj.city)
    }
  </script>
<script src="http://whois.pconline.com.cn/ipJson.jsp?callback=testJson"></script>

这时候我们已经获取到省份和地区市,我们可以通过存储的pro和city,注意实时和风天气必须要有开发者key

然后具体获取实时天气的,使用的是和风天气提供的接口开发文档 | 和风天气开发平台 (qweather.com)

getLocationId() {
    let param = {
    	key: defaultSettings.key,
		adm: sessionStorage.getItem('pro'),
		location: sessionStorage.getItem('city'),
	};
	getCity(param).then((res) => {
		if (res.code == 200) {
			this.locationId = res.location[0].id;
			this.getTem();
		}
	});
},
getTem() {
	let param = {
		key: defaultSettings.key,
		location: this.locationId,
	};
	getWeatehr(param).then((res) => {
		if (res.code == 200) {
			this.temp = res.now.temp
			this.temptext = res.now.text
			// this.weathericon = "qi-" + res.now.icon + '-fill'
			sessionStorage.setItem('temp', res.now.temp)
			sessionStorage.setItem('temptext', res.now.text)
		}
	});
},


/**
 * 获取城市id
 * @param {
 * key:'',
 * location:'绍兴',
 * adm:'浙江省'
 * } 
 * @returns 
 */
// export const getCity = p => get('https://geoapi.qweather.com/v2/city/lookup', p);

export const getCity = p => get('/qweather-city/v2/city/lookup', p);

/**
 * 获取天气
 * @param {
 *  key:'',
 * location:'101210507'
 * } 
 * @returns 
 */
// export const getWeatehr = p => get('https://devapi.qweather.com/v7/weather/now', p);

export const getWeatehr = p => get('/qweather-weather/v7/weather/now', p);

封装了它的接口,在本地使用,vue.config.js中配置并且转发使用

'/qweather-city': {
        target: 'https://geoapi.qweather.com',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/qweather-city': ''
        }
      },
      '/qweather-weather': {
        target: 'https://devapi.qweather.com',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/qweather-weather': ''
        }
}

getCity 方法获取到的是

上线时候,我使用的是ngnix转发,这样我们就能实时获取到天气内容啦

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实时获取天气信息,你可以使用Vue.js结合第三方天气API来实现。你可以在Vue组件中通过使用axios或fetch等工具发送网络请求来获取实时天气数据。首先,你需要获取到用户的IP地址,可以使用Vue源代码来获取当前IP地址。接下来,你可以使用获取到的IP地址来调用第三方天气API,并传递相关参数(如省份和地区市)来获取实时天气信息。然后,你可以将获取到的实时天气信息在Vue组件中展示出来,例如在标签页中展示温度、体感温度、风向和风速等信息。这样就能实现Vue2实时获取天气信息的功能了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [在Vue中调用和风天气api展示天气详情和生活指数](https://download.csdn.net/download/weixin_38537050/14886024)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [vue获取当前IP所在地天气.docx](https://download.csdn.net/download/weixin_39249427/13141592)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [vue前端页面获取实时天气](https://blog.csdn.net/willsoooo/article/details/126428966)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值