当前日期获取加天气获取

 

 

一、获取当前日期

    getTime() {
      let yy = new Date().getFullYear();
      let mm = new Date().getMonth() + 1;
      let dd = new Date().getDate();
      let hh = new Date().getHours();
      let wk = new Date().getDay();
      let mf =
        new Date().getMinutes() < 10
          ? "0" + new Date().getMinutes()
          : new Date().getMinutes();
      var date = yy + " " + mm + "月" + dd + "日 " + hh + ":" + mf;
      let weeks = [
        "星期日",
        "星期一",
        "星期二",
        "星期三",
        "星期四",
        "星期五",
        "星期六",
      ];
      let week = weeks[wk];
      this.date = date;
      this.week = week;
    }

二、获取天气

 weatherType 用来区分显示哪个图标 因可能返回的天气太多 所以分了一下类

      getCity().then((res) => {
        var city = res.data.city;
        city.replace("市", "");
        getWeather({ city: city }).then((res) => {
          var lives = res.data.lives[0];
          if (lives.weather.indexOf("晴") !== -1 ) {
            this.weatherType = 1;
          } else if (lives.weather.indexOf("云") !== -1 || lives.weather.indexOf("阴") !== -1) {
            this.weatherType = 2;
          } else if (lives.weather.indexOf("雨") !== -1) {
            this.weatherType = 3;
          } else if (lives.weather.indexOf("雪") !== -1) {
            this.weatherType = 4;
          } else if (lives.weather.indexOf("风") !== -1) {
            this.weatherType = 5;
          }else if (lives.weather.indexOf("雾") !== -1 || lives.weather.indexOf("霾")) {
            this.weatherType = 6;
          }else {
            this.weatherType = 1;
          }
          this.weatherTitle = lives.weather;
        });
      });
export function getCity(params) {
    return request ({
        method: 'get',
        url: 'https://restapi.amap.com/v3/ip?key=你自己的key',
        data: params
    })
}
export function getWeather(params) {
    return request ({
        method: 'get',
        url: 'https://restapi.amap.com/v3/weather/weatherInfo?key=你自己的key&city='+ params.city,
        data: params
    })
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值