此文档介绍如何通过Vue.js获取全球城市的实时天气和空气质量数据。
地区例如:曼谷(泰国),巴黎(法国),伦敦(英国),迪拜(阿联酋),新加坡,吉隆坡(马来西亚),纽约(美国),伊斯坦布尔(土耳其),东京(日本),安塔利亚(土耳其)

接口基本信息

请求方式: GET

http://m7200.tianqiapis.com

请求参数:
参数名必选类型说明备注(示例)
appidstring用户appid注册开发账号
appsecretstring用户appsecret
versionstring接口版本标识today 全球实况天气 (返回内容更少,速度更快)
day 全球实况+48小时+15日天气
querystring城市名称如: 北京、东京、New York支持坐标查询如:query=36.68,116.99
ipstringIP地址查询IP所在城市天气
languagestring语言默认中文,更多语言联系客服
英文: en 日文: jp
德语: de 法语: fr
韩语: kr 俄语: ru
葡萄牙: pt 西班牙: es
萨特阿拉伯: sa 意大利: it
泰语: th 越南: vi-VN
菲律宾: tl-PH 印度尼西亚: id-ID
希腊: el-GR 波兰: pl-PL
土耳其: tr 罗马尼亚: ro
巴基斯坦: ur-PK 伊朗: fa-IR
unitstring温度单位摄氏度m,华氏度f
响应数据说明
参数名类型说明备注
updateTimeStringUTC时间
updateTimeFormatString更新时间
timeZoneString所在时区
cityString城市名称
countryString国家名称
longitudeString经度
latitudeString纬度
dayString今日天气情况含每小时详细预报等
 ┗ iconString天气现象代码查看Icon汇总表
 ┗ feelsLikeString体感温度单位为c摄氏度或f华氏度
 ┗ temperatureString温度单位为c摄氏度或f华氏度
 ┗ temperatureMaxSince7amString最高温度单位为c摄氏度或f华氏度
 ┗ phraseString天气情况例如:小阵雨
 ┗ altimeterString气压m百帕 f英寸
 ┗ barometerTrendString气压趋势例如:升温
 ┗ humidityString相对湿度0~100,单位为百分比
 ┗ dewPointString露点温度
 ┗ visibilityString能见度单位为km公里或mi英里
 ┗ windSpeedString风速单位为km/h公里每小时或mph英里每小时
 ┗ windDirCompassString风向标例如:东南偏东
 ┗ windDirDegreesString风向角度范围0~360,0为正北,90为正东,180为正南,270为正西
 ┗ uvIndexString紫外线等级
 ┗ uvDescriptionString紫外线等级描述0~2: 低
3~5: 中等
6~7: 强
8~9: 很强
>10: 极强
 ┗ sunriseString日出时间06:28
 ┗ sunsetString日落时间19:14
 ┗ moonriseString月出时间22:14
 ┗ moonsetString月落时间11:40
 ┗ moonIconString月相icon
 ┗ moonPhraseString月相如:Waning Gibbous
 ┗ narrativeString天气情况描述如:Partly cloudy. Highs 25 to 27C and lows 17 to 19C.
 ┗ aqiString空气质量指数
 ┗ ┗ AIRString空气质量指数
amount: 今日空气质量, index: 等级描述, pp: 主要污染物
 ┗ ┗ COString一氧化碳
amount: 浓度µg/m3, index: 数值, category: 等级描述
 ┗ ┗ NO2String二氧化氮
amount: 浓度µg/m3, index: 数值, category: 等级描述
 ┗ ┗ O3String臭氧
amount: 浓度µg/m3, index: 数值, category: 等级描述
 ┗ ┗ PM10String10微米以下细颗粒物
amount: 浓度µg/m3, index: 数值, category: 等级描述
 ┗ ┗ PM2.5String2.5微米以下细颗粒物
amount: 浓度µg/m3, index: 数值, category: 等级描述
 ┗ ┗ SO2String二氧化硫
amount: 浓度µg/m3, index: 数值, category: 等级描述

Vue集成示例

1. 创建API服务文件
// src/services/weatherApi.js
import axios from 'axios';

const WEATHER_API_BASE_URL = 'http://m7200.tianqiapis.com/';

export const weatherService = {
  // 获取城市天气信息
  getWeatherByCity(cityName, appId, appSecret) {
    return axios.get(WEATHER_API_BASE_URL, {
      params: {
        version: 'today',
        unit: 'm',
        language: 'zh',
        query: cityName,
        appid: appId,
        appsecret: appSecret
      }
    });
  },
  
  // 解析天气数据
  parseWeatherData(response) {
    if (response.data.errcode !== 0) {
      throw new Error(response.data.errmsg);
    }
    
    const data = response.data;
    const day = data.day;
    
    return {
      basicInfo: {
        city: data.city,
        country: data.country,
        updateTime: data.updateTimeFormat,
        timeZone: data.timeZone
      },
      weather: {
        temperature: day.temperature,
        feelsLike: day.feelsLike,
        humidity: day.humidity,
        windSpeed: day.windSpeed,
        windDirection: day.windDirCompass,
        condition: day.phrase,
        icon: day.icon,
        sunrise: day.sunrise,
        sunset: day.sunset,
        narrative: day.narrative
      },
      aqi: {
        overall: day.aqi.AIR,
        details: {
          co: day.aqi.CO,
          no2: day.aqi.NO2,
          o3: day.aqi.O3,
          pm10: day.aqi.PM10,
          pm25: day.aqi['PM2.5'],
          so2: day.aqi.SO2
        }
      }
    };
  }
};
2. 创建Vue组件使用天气数据
<template>
  <div class="weather-container">
    <div v-if="loading" class="loading">加载中...</div>
    <div v-else-if="error" class="error">{{ error }}</div>
    <div v-else class="weather-info">
      <h2>{{ weatherData.basicInfo.city }}天气</h2>
      <p>更新于: {{ weatherData.basicInfo.updateTime }}</p>
      
      <div class="current-weather">
        <div class="temperature">{{ weatherData.weather.temperature }}°C</div>
        <div class="condition">{{ weatherData.weather.condition }}</div>
        <div class="feels-like">体感温度: {{ weatherData.weather.feelsLike }}°C</div>
      </div>
      
      <div class="details">
        <div>湿度: {{ weatherData.weather.humidity }}%</div>
        <div>风速: {{ weatherData.weather.windSpeed }} m/s, {{ weatherData.weather.windDirection }}</div>
        <div>日出: {{ weatherData.weather.sunrise }} | 日落: {{ weatherData.weather.sunset }}</div>
      </div>
      
      <div class="aqi">
        <h3>空气质量</h3>
        <div>总体: {{ weatherData.aqi.overall.index }} ({{ weatherData.aqi.overall.amount }})</div>
        <div>主要污染物: {{ weatherData.aqi.overall.pp }}</div>
      </div>
    </div>
  </div>
</template>

<script>
import { weatherService } from '@/services/weatherApi';

export default {
  name: 'WeatherWidget',
  props: {
    city: {
      type: String,
      default: '洛杉矶'
    },
    appId: String,
    appSecret: String
  },
  data() {
    return {
      loading: false,
      error: null,
      weatherData: null
    };
  },
  mounted() {
    this.fetchWeatherData();
  },
  methods: {
    async fetchWeatherData() {
      this.loading = true;
      this.error = null;
      
      try {
        const response = await weatherService.getWeatherByCity(
          this.city, 
          this.appId, 
          this.appSecret
        );
        this.weatherData = weatherService.parseWeatherData(response);
      } catch (error) {
        this.error = error.message || '获取天气数据失败';
        console.error('Weather API error:', error);
      } finally {
        this.loading = false;
      }
    }
  },
  watch: {
    city() {
      this.fetchWeatherData();
    }
  }
};
</script>

<style scoped>
.weather-container {
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.loading, .error {
  text-align: center;
  padding: 20px;
}

.current-weather {
  text-align: center;
  margin: 20px 0;
}

.temperature {
  font-size: 3rem;
  font-weight: bold;
}

.condition {
  font-size: 1.5rem;
  margin-bottom: 10px;
}

.details, .aqi {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #eee;
}
</style>
在应用中使用组件
<template>
  <div id="app">
    <h1>全球天气查询</h1>
    <input v-model="selectedCity" placeholder="输入城市名称">
    <WeatherWidget 
      :city="selectedCity" 
      :app-id="appId" 
      :app-secret="appSecret" 
    />
  </div>
</template>

<script>
import WeatherWidget from './components/WeatherWidget.vue';

export default {
  name: 'App',
  components: {
    WeatherWidget
  },
  data() {
    return {
      selectedCity: '洛杉矶',
      appId: '您的appid', // 需要申请
      appSecret: '您的appsecret' // 需要申请
    };
  }
};
</script>

接口优势分析

数据全面性 - 包含基本天气信息、湿度、能见度、气压、降雨概率、日出日落、月初月落、空气质量指数(包含多种污染物详细指标(PM2.5, PM10, SO2, NO2, O3, CO))等

多语言支持 - 支持24多种语言查询和返回结果,方便国内外开发者使用

单位灵活性 - 支持公制单位,符合国内使用习惯

标准化数据结构 - 返回JSON格式数据,结构清晰易于解析

地理位置精确 - 提供经纬度坐标,可用于地图集成

包含时区信息 - 便于时间转换

更新及时 - 数据包含更新时间戳,确保信息新鲜度

全球覆盖 - 支持全球城市查询,适用于国际化应用

注意事项

  1. 需要申请appid和appsecret才能正常使用接口
  2. 注意API调用频率限制,避免过度请求
  3. 考虑添加错误处理和加载状态以提升用户体验
  4. 对于生产环境,建议通过后端代理调用API以避免CORS问题

更多推荐