[番外] 来个 Weather App (续)

前置作业-HTML & CSS

使用 Google 查 Weather App 会有很多很多的参考,练习不怕没有画面,挑一个喜欢的来复刻或是模仿!

画面的雏形:
demo

改为黄昏时间的查询 成为追光者

参考 code:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Sunset App</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="./style.css">
  </head>
  <body>
    <section class="search">
      <input type="text">
    </section>
    <section>
      <div class="info-container">
        <div class="info-item">
          <p class="time"> <img src="./img/sea.png" alt="sunset"> 18:30</p>
        </div>
        <div class="info-item">
          <p class="name">Taipei, TW</p>
          <p class="date">2021/09/26</p>
          <p class="des">多云</p>
        </div>
      </div>
    </section>
  </body>
</html>
* {
  margin: 0;
  padding: 0;
}
img {
  display: inline-block;
}

body {
  font-family: 'Noto Sans', sans-serif;
  font-size: 24px;
  background-image: url(./img/sunset_1127-3503.jpg);
  background-repeat: no-repeat;
  background-size: cover;
  height: 100vh;
  color: #fff;
  padding: 20px;
}

section {
  text-align: center;
}

.search {
  margin-bottom: 40px;
  
}

.search input {
  font-size: 20px;
  border-radius: 10px;
  line-height: 1.5;
  outline: none;
  padding:10px;
}

/* info */
.info-item {
  margin-bottom: 20px;
}

.info-item .time {
  font-size: 60px;
  display: flex;
  justify-content: center;
  align-items: center;
}

Vue 起来

vue-cli 建立专案

$ vue create sunset-app

产生专案架构
new project

将 HTML, CSS 以及图档搬进专案中

  • HTML & CSS --> src/App.vue
  • 图档 --> src/assets

Vue 实体内容

data

包含 API 的URL, Key, 输入的城市 以及 回传的资料

  data () {
    return {
      apiKey: 'xxxxxxx',
      url: 'https://api.openweathermap.org/data/2.5/weather',
      searchCity: '', 
      weather: {} // 存 Response
    }
  }

methods

getWeather 打API

    getWeather (e) {
      if (e.key == "Enter") 
        fetch(`${this.url}?q=${this.searchCity}&units=metric&APPID=${this.apiKey}&lang=zh_tw`)
          .then(res => {
            return res.json();
          }).then(this.setResults);
      }
    },

setResults 存资料

 setResults(data) {
  this.weather = data;
  this.weather.sunset = this.convertTimestampToTime(data.sys.sunset);
 },

convertTimestampToTime 转换时间

convertTimestampToTime (timestamp) {
  timestamp = timestamp.length == 13 ? timestamp : timestamp*1000;
  let date = new Date(timestamp);
  return `${date.getHours()}:${date.getMinutes()}`;
}

模板绑定资料

搜寻框

<input 
    type="text" 
    v-model="searchCity"
    placeholder="搜寻地区"
    @keypress="getWeather"
>

显示结果

<div class="info-container" v-if="typeof weather.main != 'undefined'">
    <div class="info-item">
      <p class="time"> <img src="./assets/sea.png" alt="sunset"> {{weather.sunset}}</p>
    </div>
    <div class="info-item" >
      <p class="name">{{weather.name}}, {{weather.sys.country}}</p>
      <p class="des">{{weather.weather[0].description}}</p>
    </div>
</div>

<<:  Day14-D3 的 Force 原力

>>:  那些被忽略但很好用的 Web API / 简易文字编辑器

Day 24:专案06 - 股市趋势图01 | 单月股市API、Pandas

各位早安,今天是第24天,但其实爬虫的技巧大致上已经教得差不多了,而且我猜会看我的文章的人,应该都想...

[Day5] 策略买卖讯号回测

延续前一天的程序码,首先先把图片里的程序码搬到箭头的地方,固定前面放函数後面放程序,这样看起来比较清...

终章 - 资安碎碎念与心得

终章 - 资安碎碎念与心得 其实原本还有很多想打的鬼故事, 但碍於尺度与很难去识别化,最後还是觉得不...

Day 15 再手动安装个 Python3 容器看看

来安装个 Python3 的容器吧~ 虽说日後要搭建一系列的服务,并让开发者可以直观理解,可以使用 ...

Day30 Flutter Camera、播放影片

铁人赛完赛了... 很抱歉没有完成一开始订的完成一个旅游App 的目标,最终只能算是完成Flutte...