微信小程序獲取今日天氣預報api 免費接口

如果是測試, 請勾選配置 不校驗合法域名、web-view(業務域名)、TLS 版本以及 HTTPS 證書

如果正式使用, 請添加安全域名兩個 (ip.tianqiapi.com 和 tianqiapi.com)

有問題的加我qq 445899710, 提供源代碼, 效果如圖

 


//index.js
//獲取應用實例
const app = getApp()
 
Page({
  data: {
    weather: [],//實況天氣
    weatherweek:[],//七日天氣
  },
  onLoad: function () {
    this.getapi();
  },
  getapi:function(){
    var _this = this;
    // 獲取IP地址
    wx.request({
      url: 'https://ip.tianqiapi.com/',
      data: {
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res);
        // 根據IP獲取天氣數據
        _this.weathertoday(res.data.ip);
        _this.weatherweekday(res.data.ip);
      }
    });
  },
  // 天氣api實況天氣
  weathertoday:function(ip){
    var _this = this;
    wx.request({
      url: 'https://www.tianqiapi.com/api/?version=v6',
      data: {
        'ip': ip
      },
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        _this.setData({
          weather: res.data
        });
        console.log(_this.data.weather)
      }
    });
  },
  // 天氣api實況天氣
  weatherweekday: function (ip) {
    var _this = this;
    wx.request({
      url: 'https://www.tianqiapi.com/api/?version=v1',
      data: {
        'ip': ip
      },
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        _this.setData({
          weatherweek: res.data
        });
        console.log(_this.data.weatherweek)
      }
    });
  }
})

小程序源碼下載: https://pan.baidu.com/s/1jIPAtU1Q-EH6I18ud9pNvw 提取碼 s8tm

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章