微信小程序实现天气预报功能(附源码)


Posted in Javascript onDecember 10, 2020

前言

最近在学小程序开发,刚好学到天气预报功能的制作,于是给大家分享下。

效果图

微信小程序实现天气预报功能(附源码) 

微信小程序实现天气预报功能(附源码) 

天气API获取

这里我用的是和风天气的API,打开官网注册或者登陆你的账号

微信小程序实现天气预报功能(附源码)

进入控制台,新建应用

微信小程序实现天气预报功能(附源码)

微信小程序实现天气预报功能(附源码)

这是刚刚我们创建好的应用,点击添加KEY

微信小程序实现天气预报功能(附源码)

选择WebAPI

微信小程序实现天气预报功能(附源码)

这注册好我们的API了

微信小程序实现天气预报功能(附源码)

微信小程序后台域名配置

登陆小程序后台,分别点击开发和开发设置

微信小程序实现天气预报功能(附源码)

点击修改,将我们API的域名添加到request合法域名里面https://free-api.heweather.net

微信小程序实现天气预报功能(附源码)

页面代码

.wxml

<view class="header">
 <view class="top">
 <view class="city">
  {{city}}
 </view>
 <view class="search">
  <input placeholder="输入城市名" bindinput="bindKeyInput" placeholder-style="color:white"></input>
  <view class="bt_search" bindtap="search">
  <icon type="search" size="18" color="white"></icon>
  </view>
 </view>
 </view>

 <view class="center">
 <view class="tmp">
  {{tmp}}°
 </view>
 <image mode="widthFix" class="cond-image" src="https://moyv.top/wechat/images/weather/{{imgsrc}}.png">
 </image>
 </view>
 <view class="bottom">
 <view>{{wind_dir}} {{wind_sc}}级</view>
 <view>湿度 {{hum}}%</view>
 <view>气压 {{pres}}Pa</view>
 </view>
</view>
<view class="container">
 <view class="hourly_title">24小时预报</view>
 <scroll-view scroll-x="true" class="hourly">
 <view class="h_item" wx:for="{{hourly}}" wx:key="index">
  <text class="h_time">{{item.time}}</text>
  <view class="h_img">
  <image mode="widthFix" src="https://moyv.top/wechat/images/weather/{{item.imgsrc}}.png"></image>
  </view>
  <text class="h_tmp">{{item.tmp}}°</text>
  <text class="h_wind_dir">{{item.wind_dir}}</text>
  <text class="h_wind_sc">{{item.wind_sc}}级</text>
 </view>

 </scroll-view>
 <view class="hourly_title">7天预报</view>
 <scroll-view scroll-x="true" class="daily">
 <view class="d_item" wx:for="{{daily_forecast}}" wx:key="index">
  <text class="d_txt">{{item.d_txt}}</text>
  <text class="d_date">{{item.d_date}}</text>
  <view class="h_img">
  <image mode="widthFix" src="https://moyv.top/wechat/images/weather/{{item.imgsrc_d}}.png"></image>
  </view>
  <text class="h_tmp">{{item.tmp_min}}°~{{item.tmp_max}}°</text>
  <view class="h_img">
  <image mode="widthFix" src="https://moyv.top/wechat/images/weather/{{item.imgsrc_n}}.png"></image>
  </view>
  <text class="d_wind_dir">{{item.wind_dir}}</text>
  <text class="d_wind_sc">{{item.wind_sc}}级</text>
 </view>

 </scroll-view>
</view>

<view class="footer">
-天气数据来自和风天气api-
</view>

.wxss

page {
 background-color: #f6f6f6;
}

.header {
 background-color:#64c8fa; 
 /* background-image: linear-gradient(to right, #64a0f8, #64c8fa); */
 height: 450rpx;
 padding-top: 32rpx;
 text-align: center;
}

.top {
 display: flex;
 justify-content: space-between;
 align-content: center;
 align-items: center;
}

.city {
 text-align: center;
 color: white;
 display: inline-block;
 font-size: 52rpx;
 margin-left: 32rpx;
}

.search {
 margin-right: 32rpx;
 border-radius: 8rpx;
 display: inline-flex;
 justify-content: center;
 align-content: center;
 align-items: center;
 background-color: rgba(0, 0, 0, 0.1);
 height: 70rpx;
}

.search input {
 width: 200rpx;
 padding: 18rpx 32rpx;
 text-align: left;
 color: white;
 display: inline-block;
}

.bt_search {
 border-radius: 0 8rpx 8rpx 0;
 height: 100%;
 background-color: rgba(0, 0, 0, 0.1);
 display: inline-flex;
 justify-content: center;
 align-content: center;
 align-items: center;
}

.bt_search icon {
 margin: 8rpx 18rpx;
}

.center {
 display: flex;
 justify-content: space-between;
 align-content: center;
 align-items: center;
}

.tmp {
 margin-left: 18rpx;
 display: inline-block;
 font-size: 180rpx;
 color: white;
}
.cond-image{
 width: 200rpx;
 margin-right: 32rpx;
 margin-top: 32rpx;
}
.bottom{
 display: flex;
 justify-content: space-between;
 align-content: center;
 align-items: center;
}
.bottom view{
 color: white;
 margin: 32rpx;
}

.hourly_title{
 font-weight: bold;
 font-size: 42rpx;
 padding: 18rpx 32rpx;
}
.hourly {
 width: 718rpx;
 margin: 0 18rpx;
 border-radius: 18rpx;
 box-shadow: 0.1rem 0.1rem 0.5rem rgba(0, 0, 0, 0.15);
 white-space: nowrap;
 background-color: white;
}
.h_item {
 margin: 18rpx 0;
 display: inline-block;
 width: 143.5rpx;
 text-align: center;
 font-size: 28rpx;
}

.h_img {
 margin: 64rpx 0;
}

.h_img image {
 width: 60rpx;
}

.h_item text {
 display: block;
}

.h_time {
 color: gray;
}

.h_wind_dir {
 margin-top: 32rpx;
}

.h_wind_sc {
 color: gray;
}

.h_tmp {
 color: #027aff;
}

.daily {
 width: 718rpx;
 white-space: nowrap;
 margin: 0 18rpx;
 background-color: white;
 border-radius: 18rpx;
 box-shadow: 0.1rem 0.1rem 0.5rem rgba(0, 0, 0, 0.15);
}

.d_item {
 
 margin: 18rpx 0;
 display: inline-block;
 width: 179.5rpx;
 text-align: center;
 font-size: 28rpx;
}

.d_item text {
 display: block;
}

.d_date {
 color: gray;
}

.d_wind_dir {
 margin-top: 32rpx;
}

.d_wind_sc {
 color: gray;
}

.footer{
 font-size: 28rpx;
 color: gray;
 text-align: center;
 margin-top: 50rpx;
 margin-bottom: 18rpx;
}

.js

Page({

 /**
 * 页面的初始数据
 */
 data: {
 search_city: '',
 imgsrc:100
 },
 /**
 * 根据城市获取天气预报
 */
 getWeather(city) {
 let that = this
 //获取实况天气
 wx.request({
  url: 'https://free-api.heweather.net/s6/weather/now?key=你后台的key&location=' + city,
  success: function(res) {
  if (res.data.HeWeather6[0].status == 'unknown location') {
   wx.showToast({
   title: '抱歉!没有该城市的天气预报',
   icon: 'none',
   duration: 2000
   })
   return;
  }
  console.log(res)
  that.setData({
   city: city,
   tmp: res.data.HeWeather6[0].now.tmp,
   imgsrc: res.data.HeWeather6[0].now.cond_code,
   wind_dir: res.data.HeWeather6[0].now.wind_dir,
   wind_sc: res.data.HeWeather6[0].now.wind_sc,
   hum: res.data.HeWeather6[0].now.hum,
   pres: res.data.HeWeather6[0].now.pres
  })

  //获取24小时天气预报
  wx.request({
   url: 'https://free-api.heweather.net/s6/weather/hourly?key=你后台的key&location=' + city,
   success: function(res) {
   var arr = res.data.HeWeather6[0].hourly
   var hourly = []
   for (var i = 0; i < arr.length; i++) {
    hourly[i] = {
    "imgsrc": arr[i].cond_code,
    "tmp": arr[i].tmp,
    "time": arr[i].time.substring(11),
    "wind_dir": arr[i].wind_dir,
    "wind_sc": arr[i].wind_sc
    }
   }
   that.setData({
    hourly: hourly
   })

   var weekArray = new Array("周日", "周一", "周二", "周三", "周四", "周五", "周六");
   //获取未来7天天气预报
   wx.request({
    url: 'https://free-api.heweather.net/s6/weather/forecast?key=你后台的key&location=' + city,
    success: function(result) {
    //console.log(result)
    var arr = result.data.HeWeather6[0].daily_forecast
    var daily_forecast = []
    for (var i = 0; i < arr.length; i++) {
     daily_forecast[i] = {
     d_txt: i == 0 ? "今天" : weekArray[new Date(arr[i].date).getDay()],
     d_date: arr[i].date.substring(5),
     imgsrc_d: arr[i].cond_code_d,
     imgsrc_n: arr[i].cond_code_n,
     wind_dir: arr[i].wind_dir,
     wind_sc: arr[i].wind_sc,
     tmp_max: arr[i].tmp_max,
     tmp_min: arr[i].tmp_min,
     cond_txt_d: arr[i].cond_txt_d
     }
    }
    that.setData({
     daily_forecast: daily_forecast
    })
    }
   })

   }
  })

  }
 })
 },
 bindKeyInput(e) {
 this.setData({
  search_city: e.detail.value
 })
 },
 search() {
 this.getWeather(this.data.search_city)
 },
 /**
 * 生命周期函数--监听页面加载
 */
 onLoad: function(options) {
 this.getWeather("广州")
 },
})

.json

{
 "usingComponents": {},
 "navigationBarTitleText": "天气预报"
}

注意问题(必看)

由于我的项目有用到天气预报的逐小时预报7天预报,和风天气又必须实名才能获取到此数据,所以请登录和风天气后台进行实名认证

微信小程序实现天气预报功能(附源码)

到此这篇关于微信小程序实现天气预报功能(附源码)的文章就介绍到这了,更多相关小程序实现天气预报内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Javascript 相关文章推荐
Javascript控制页面链接在新窗口打开具体方法
Aug 16 Javascript
JavaScript中使用typeof运算符需要注意的几个坑
Nov 08 Javascript
深入剖析JavaScript编程中的对象概念
Oct 21 Javascript
跟我学习javascript的call(),apply(),bind()与回调
Nov 16 Javascript
JS采用绝对定位实现回到顶部效果完整实例
Jun 20 Javascript
使用vue.js实现联动效果的示例代码
Jan 10 Javascript
bootstrap table实现双击可编辑、添加、删除行功能
Sep 27 Javascript
JavaScript中正则表达式使数字、中文或指定字符高亮显示
Oct 31 Javascript
Node.js实现用户评论社区功能(体验前后端开发的乐趣)
May 09 Javascript
VUE兄弟组件传值操作实例分析
Oct 26 Javascript
vue实现分页加载效果
Dec 24 Javascript
vue中使用vue-print.js实现多页打印
Mar 05 Javascript
微信小程序向Java后台传输参数的方法实现
Dec 10 #Javascript
微信小程序反编译的实现
Dec 10 #Javascript
微信小程序用户登录和登录态维护的实现
Dec 10 #Javascript
关于小程序优化的一些建议(小结)
Dec 10 #Javascript
详解vue-cli项目在IE浏览器打开报错解决方法
Dec 10 #Vue.js
echarts柱状图背景重叠组合而非并列的实现代码
Dec 10 #Javascript
javascript实现放大镜功能
Dec 09 #Javascript
You might like
php三维数组去重(示例代码)
2013/11/26 PHP
php 启动报错如何解决
2014/01/17 PHP
PHP中$_SERVER使用说明
2015/07/05 PHP
PHP页面间传递值和保持值的方法
2016/08/24 PHP
php读取本地json文件的实例
2018/03/07 PHP
分享一个我自己写的ToolTip提示插件(附源码)
2013/01/20 Javascript
JQuery中对Select的option项的添加、删除、取值
2013/08/25 Javascript
JS实现将人民币金额转换为大写的示例代码
2014/02/13 Javascript
jQuery获取和设置表单元素的方法
2014/02/14 Javascript
jQuery1.9.1源码分析系列(十六)ajax之ajax框架
2015/12/04 Javascript
基于JS如何实现给字符加千分符(65,541,694,158)
2016/08/03 Javascript
JQuery遍历元素的父辈和祖先的方法
2016/09/18 Javascript
深入理解jQuery.data() 的实现方式
2016/11/30 Javascript
详解用node编写自己的cli工具
2017/05/23 Javascript
js实现数字从零慢慢增加到指定数字示例
2019/11/07 Javascript
Ant Design Vue table中列超长显示...并加提示语的实例
2020/10/31 Javascript
VUE+Element实现增删改查的示例源码
2020/11/23 Vue.js
[16:19]教你分分钟做大人——风暴之灵
2015/03/11 DOTA
[14:19]2018年度COSER大赛-完美盛典
2018/12/16 DOTA
Python实现批量压缩图片
2018/01/25 Python
在双python下设置python3为默认的方法
2018/10/31 Python
python批量修改图片尺寸,并保存指定路径的实现方法
2019/07/04 Python
Python基本语法之运算符功能与用法详解
2019/10/22 Python
python绘制彩虹图
2019/12/16 Python
python多线程使用方法实例详解
2019/12/30 Python
python 制作简单的音乐播放器
2020/11/25 Python
HTML5标签使用方法详解
2015/11/27 HTML / CSS
我们是伦敦女孩:WalG
2018/01/08 全球购物
美国购买汽车零件网站:Buy Auto Parts
2018/04/02 全球购物
LORAC官网:美国彩妆品牌
2019/08/27 全球购物
庆祝教师节活动方案
2014/01/31 职场文书
2014年共青团工作总结
2014/12/10 职场文书
Java 超详细讲解十大排序算法面试无忧
2022/04/08 Java/Android
python使用shell脚本创建kafka连接器
2022/04/29 Python
MySQL的表级锁,行级锁,排它锁和共享锁
2022/07/15 MySQL