前几天,在实习的公司里,要使用vue+webpack开发一个门店拜访的app,被分到了编写天气预报组件,然后就找了阿里云的免费天气预报API,传送门:https://market.aliyun.com/products/57126001/cmapi014302.html
不过要使用阿里云服务需要先注册一个阿里云的账号,这里就不赘述了,注册完以后购买该免费服务,然后就会获得AppKey,AppSecret和AppCode。
然后我们可以看看调用接口需要的请求参数:
名称 | 类型 | 是否必须 | 描述 |
---|---|---|---|
city | STRING | 可选 | 城市(city,cityid,citycode三者任选其一) |
citycode | STRING | 可选 | 城市天气代号(city,cityid,citycode三者任选其一) |
cityid | STRING | 可选 | 城市ID(city,cityid,citycode三者任选其一) |
ip | STRING | 可选 | IP |
location | STRING | 可选 | 经纬度 纬度在前,,分割 如:39.983424,116.322987 |
以上五个参数我选择了直接使用城市名来进行测试,具体代码如下:1
2
3
4
5
6
7
8
9
10
11$.ajax({
type: "GET",
url: "http://jisutqybmf.market.alicloudapi.com/weather/query",
data: {city: '杭州'},
success: function (data) {
console.log(data); //请求成功后操作
},
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "APPCODE /*此处是自己的APPCODE*/");
}
});
如果发送请求失败,返回的错误码如下:
错误码 | 错误信息 | 描述 |
---|---|---|
201 | City and city ID and city code are empty | 城市和城市ID和城市代号都为空 |
202 | City does not exist | 城市不存在 |
203 | There is no weather information in this city | 此城市没有天气信息 |
210 | No information | 没有信息 |
在返回结果中天气是有图片的,天气图片可以点此下载
需要特别注意的是接口流量限制情况(每个用户ID):30次流控/分钟。当然,如果是付费版就不存在限流的问题了。
(2018.9.14)好吧,还是要说这是真的坑,我今天发现,这个接口出错了:
既然上一个API出错了,那就得换一个,还是在阿里云市场找了另一款天气预报API:全国天气预报查询/免费版/易源数据,当然,调用方式是大同小异的,只需要更改调用地址:http://saweather.market.alicloudapi.com/area-to-weather
:1
2
3
4
5
6
7
8
9
10
11$.ajax({
type: "GET",
url: "http://saweather.market.alicloudapi.com/area-to-weather",
data: {area: '杭州'},
success: function (data) {
console.log(data);//请求成功后操作
},
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "APPCODE /*此处是自己的appcode*/");
}
});
然后调用该接口所需要的参数如下:
名称 | 类型 | 是否必须 | 描述 |
---|---|---|---|
area | STRING | 可选 | 地区名称。名称和id必须输入一个。如果都输入,以areaid为准。 |
areaid | STRING | 可选 | 地区id |
need3HourForcast | STRING | 可选 | 是否需要每小时数据的累积数组。由于本系统是半小时刷一次实时状态,因此实时数组最大长度为48。每天0点长度初始化为0. 1为需要 0为不 |
needAlarm | STRING | 可选 | 是否需要天气预警。1为需要,0为不需要。 |
needHourData | STRING | 可选 | 是否需要每小时数据的累积数组。由于本系统是半小时刷一次实时状态,因此实时数组最大长度为48。每天0点长度初始化为0. |
needIndex | STRING | 可选 | 是否需要返回指数数据,比如穿衣指数、紫外线指数等。1为返回,0为不返回。 |
needMoreDay | STRING | 可选 | 是否需要返回7天数据中的后4天。1为返回,0为不返回。 |
然后正常调用返回结果如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21{"showapi_res_error":"","showapi_res_id":"dc9235c7860e4776985a362365812bec","showapi_res_code":0,"showapi_res_body":
{"time":"20180914113000","cityInfo":{"c4":"hangzhou","c17":"+8","c5":"杭州","c6":"zhejiang","latitude":30.319,"c7":"浙江",
"c1":"101210101","c12":"310000","c2":"hangzhou","c15":"43","c8":"china","c9":"中国","longitude":120.165,"c3":"杭州",
"c11":"0571","c16":"AZ9571","c10":"1"},"f2":{"day_wind_power":"0-3级 <5.4m/s","night_wind_power":"0-3级 <5.4m/s",
"night_weather_code":"01","day_weather":"多云","sun_begin_end":"05:43|18:06","air_press":"1014 hPa",
"day_weather_code":"01","jiangshui":"15%","night_weather_pic":"http://app1.showapi.com/weather/icon/night/01.png","night_weather":"多云",
"day":"20180915","day_wind_direction":"东北风","night_wind_direction":"东北风","weekday":6,"ziwaixian":"中等",
"night_air_temperature":"24","day_weather_pic":"http://app1.showapi.com/weather/icon/day/01.png","day_air_temperature":"32"},"now":
{"wind_direction":"东北风","aqi":"105","weather_pic":"http://app1.showapi.com/weather/icon/day/01.png","wind_power":"1级",
"temperature_time":"16:30","weather_code":"01","temperature":"32","sd":"58%","aqiDetail":{"quality":"轻度污染","aqi":"105","pm10":"70","area":"杭州",
"co":"0.718","o3":"203","so2":"7","no2":"18","primary_pollutant":"臭氧1小时","o3_8h":"115","num":"365","pm2_5":"48"},"weather":"多云"},"f3":
{"day_wind_power":"0-3级 <5.4m/s","night_wind_power":"0-3级 <5.4m/s","night_weather_code":"01","day_weather":"阵雨",
"sun_begin_end":"05:44|18:04","air_press":"1014 hPa",
"day_weather_code":"03","jiangshui":"71%","night_weather_pic":"http://app1.showapi.com/weather/icon/night/01.png","night_weather":"多云",
"day":"20180916","day_wind_direction":"东北风","night_wind_direction":"东北风",
"weekday":7,"ziwaixian":"弱","night_air_temperature":"24","day_weather_pic":"http://app1.showapi.com/weather/icon/day/03.png","day_air_temperature":"29"},"ret_code":0,"f1":
{"day_wind_power":"0-3级 <5.4m/s","night_wind_power":"0-3级 <5.4m/s","night_weather_code":"01","day_weather":"多云",
"sun_begin_end":"05:43|18:07","air_press":"1014 hPa",
"day_weather_code":"01","jiangshui":"8%","night_weather_pic":"http://app1.showapi.com/weather/icon/night/01.png","night_weather":"多云",
"day":"20180914","day_wind_direction":"东北风","night_wind_direction":"东北风","weekday":5,"ziwaixian":"中等",
"night_air_temperature":"24","day_weather_pic":"http://app1.showapi.com/weather/icon/day/01.png","day_air_temperature":"33"}}}
在返回结果中的天气图片可以点此下载
当然,这个API提供的天气图片可以直接通过链接显示,比如,返回结果需要的图片是01,那么地址就是http://app1.showapi.com/weather/icon/day/01.png,以此类推。
这个API是30分钟更新一次。需要特别注意的是免费版限制并发1秒1次,可根据地名、经纬度GPS、IP查询对应地区的预报。