《冷暖预知》天气小程序

学无止境,以玩儿玩儿的心态去学习!

前一天晚上写的,写的不太好,第二天马上修改了,如有差错,请多指教。

花半天时间完成简单的小程序应用。适合小程序初学者。

步骤:

  1. 申请小程序帐号: 小程序注册入口, 具体操作按照官网步骤执行,相信你会看的很明白的(-
  2. 安装微信开发者工具,打开工具填写信息:①项目目录为自己要开发小程序的位置②AppId在微信管理后台的设置-开发设置中③项目名称自己起,填写完成点击完成;
  3. 看到默认的初始小程序Hello Horld是不是很兴奋,以上步骤不是我们今天研究的重点,下面进行我们的关键:
  4. 在index.wxml中写我们的结构,index.wxss中写css样式,在index.js中写我们的逻辑内容。前提是要有css3和javascript的基础哦!!!
  5. 在index.js中定义两个方法:getLocation()获取用户的地理位置,getWeather()获取天气的方法;
  6. 和风天气提供免费天气接口(无偿打广告,哈哈~~),免费版只能获取3天的天气情况,开发文档

废话不多说,直接上代码~~~

代码

1.页面结构 index.wxml部分

  1. <!--index.wxml-->
  2. <view class="container">
  3. <view class="weather yesterday">
  4. <view>
  5. <view class='date'>今天</view>
  6. <view class='location'>{{basic.location}}/{{basic.parent_city}}</view>
  7. <view class='tmp'>{{today.tmp_min}}℃~{{today.tmp_max}}℃</view>
  8. <view class='cond_txt'>{{today.cond_txt_d}}</view>
  9. </view>
  10. <view>
  11. <view class='weather_icon'>
  12. <image src='{{todyIcon}}'></image>
  13. </view>
  14. <view class='lastUpdateDate'>最后更新:{{update}}</view>
  15. </view>
  16. </view>
  17. <view class="weather today">
  18. <view>
  19. <text>明天</text>
  20. <view class='location'>{{basic.location}}/{{basic.parent_city}}</view>
  21. <view class='tmp'>{{tomorrow.tmp_min}}℃~{{tomorrow.tmp_max}}℃</view>
  22. <view class='cond_txt'>{{tomorrow.cond_txt_d}}</view>
  23. </view>
  24. <view>
  25. <view class='weather_icon'>
  26. <image src='{{tomorrowIcon}}'></image>
  27. </view>
  28. <view class='lastUpdateDate'>最后更新:{{update}}</view>
  29. </view>
  30. </view>
  31. <view class="weather tomorrow">
  32. <view>
  33. <text>后天</text>
  34. <view class='location'>{{basic.location}}/{{basic.parent_city}}</view>
  35. <view class='tmp'>{{afterTomor.tmp_min}}℃~{{afterTomor.tmp_max}}℃</view>
  36. <view class='cond_txt'>{{afterTomor.cond_txt_d}}</view>
  37. </view>
  38. <view>
  39. <view class='weather_icon'>
  40. <image src='{{afterTomorIcon}}'></image>
  41. </view>
  42. <view class='lastUpdateDate'>最后更新:{{update}}</view>
  43. </view>
  44. </view>
  45. </view>

2.页面样式index.wxss部分

  1. /**index.wxss**/
  2. .container {
  3. height: 100%;
  4. width: 100%;
  5. display: flex;
  6. flex-direction: column;
  7. align-items: center;
  8. justify-content: space-between;
  9. padding: 10px 15px;
  10. box-sizing: border-box;
  11. }
  12. .weather{
  13. height: 110px;
  14. width: 100%;
  15. margin-bottom: 10px;
  16. border-radius: 5px;
  17. color: #FFF;
  18. padding: 5PX 15px;
  19. display: flex;
  20. font-size: 14px;
  21. box-sizing: border-box;
  22. }
  23. .weather>view{
  24. flex: 1;
  25. }
  26. .weather>view>view{
  27. margin: 5px 0;
  28. }
  29. .yesterday{
  30. background-color: #30BCAF;
  31. }
  32. .today{
  33. background-color: #78A4be;
  34. }
  35. .tomorrow{
  36. background-color: #FCB654;
  37. }
  38. .location,.cond_txt{
  39. font-size: 14px;
  40. }
  41. .date,.tmp{
  42. font-weight: bold;
  43. }
  44. .weather_icon{
  45. text-align: center;
  46. height: 65px;
  47. }
  48. .weather_icon image{
  49. width: 75px;
  50. height: 100%;
  51. }
  52. .lastUpdateDate{
  53. font-size: 10px;
  54. text-align: center;
  55. }

3.页面行为index.js部分

  1. //index.js
  2. //获取应用实例
  3. const app = getApp()
  4. Page({
  5. data: {
  6. update: '',
  7. basic:{},
  8. today:{},
  9. tomorrow:{},
  10. afterTomor:{},
  11. todyIcon:'../../imgs/weather/999.png',
  12. tomorrowIcon:'../../imgs/weather/999.png',
  13. afterTomorIcon:'../../imgs/weather/999.png'
  14. },
  15. onShow: function () {
  16. this.getLocation();
  17. },
  18. //事件处理函数
  19. bindViewTap: function() {
  20. wx.navigateTo({
  21. url: '../logs/logs'
  22. })
  23. },
  24. getLocation:function(){
  25. var that = this;
  26. wx.getLocation({
  27. type: 'wgs84',
  28. success: function (res) {
  29. var latitude = res.latitude
  30. var longitude = res.longitude
  31. that.getWeatherInfo(latitude, longitude);
  32. }
  33. })
  34. },
  35. getWeatherInfo: function (latitude, longitude){
  36. var _this = this;
  37. var key = '';//你自己的key
  38. //需要在微信公众号的设置-开发设置中配置服务器域名
  39. var url = 'https://free-api.heweather.com/s6/weather?key='+key+'&location=' + longitude + ',' + latitude;
  40. wx.request({
  41. url: url,
  42. data: {},
  43. method: 'GET',
  44. success: function (res) {
  45. var daily_forecast_today = res.data.HeWeather6[0].daily_forecast[0];//今天预报
  46. var daily_forecast_tomorrow = res.data.HeWeather6[0].daily_forecast[1];//明天天预报
  47. var daily_forecast_afterTomor = res.data.HeWeather6[0].daily_forecast[2];//后天预报
  48. var basic = res.data.HeWeather6[0].basic;
  49. var update = res.data.HeWeather6[0].update.loc;//更新时间
  50. _this.setData({
  51. update:update,
  52. basic:basic,
  53. today: daily_forecast_today,
  54. tomorrow:daily_forecast_tomorrow,
  55. afterTomor: daily_forecast_afterTomor,
  56. todyIcon: '../../imgs/weather/' + daily_forecast_today.cond_code_d+'.png', //在和风天气中下载天气的icon图标,根据cond_code_d显示相应的图标
  57. tomorrowIcon: '../../imgs/weather/' + daily_forecast_tomorrow.cond_code_d+'.png',
  58. afterTomorIcon: '../../imgs/weather/' + daily_forecast_afterTomor.cond_code_d+'.png'
  59. });
  60. }
  61. })
  62. }
  63. })

效果

大功搞成,这样就有了自己的天气预报了,天气变冷,大家注意身体哦,身体是革命的本钱!!!

扫码查看小程序

美食垂类小程序

微信小程序+和风天气完成天气预报的更多相关文章

  1. 微信小程序项目实战之天气预报

    概述 微信小程序项目实战之天气预报 详细 代码下载:http://www.demodashi.com/demo/10634.html 一.准备工作 1.注册微信小程序 2.注册和风天气账号 3.注册百 ...

  2. 微信小程序踩坑集合

    1:官方工具:https://mp.weixin.qq.com/debug/w ... tml?t=1476434678461 2:简易教程:https://mp.weixin.qq.com/debu ...

  3. 近期热门微信小程序demo源码下载汇总

    近期微信小程序demo源码下载汇总,乃小程序学习分析必备素材!点击标题即可下载: 即速应用首发!原创!电商商场Demo 优质微信小程序推荐 -秀人美女图 图片下载.滑动翻页 微信小程序 - 新词 GE ...

  4. 鸿蒙的远程交互组件应用及微信小程序的远程交互组件应用

    注:鸿蒙的远程交互组件应用相对复杂 ,访问网络时,首先要配置网络权限,华为官方文档有问题,在此引用我老师配置的模板,见附件 过程:1.导入鸿蒙的网络请求模块fetch 2.发起对服务器的请求(在这过程 ...

  5. 微信小程序学习指南

    作者:初雪链接:https://www.zhihu.com/question/50907897/answer/128494332来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  6. 微信小程序实战:天气预报

    接触微信小程序也有一段时间了,以天气预报练一下手. 主要实现了以下功能: (1) 首页图标式菜单,便于以后扩展功能 (2)首页顶部滚动消息 (3)页面右上角三点菜单转发功能,便于小程序的传播 (4)天 ...

  7. 【微信小程序】——实战开发之和风(含demo)

    微信小程序之和风 序 凑了一把微信小程序的热闹!12月,小程序正式发布,很火,但随之而来的是各种冷水,唱衰之调随处可见.但作为一个小前端,岂能有新技术却弃之不顾之理,更何况是微信出品的?抱着学习和研究 ...

  8. 两天撸一个天气应用微信小程序

    更新说明: I.气象数据由百度地图开放平台修改为了和风天气,需要注册账号获取 key: II.d0e51c8 版本之后为小程序云开发版本,若未开通云开发功能,为不影响小程序正常运行,可以将版本号回退到 ...

  9. 微信小程序开发实战-天气小程序

    园龄6年8个月了,还一篇文章都没写过,惭愧! 最近周末做了个天气预报小程序,在这里整理一下开发过程和注意点,给对小程序开发感兴趣的伙伴们提供点参考. 废话不多说,先上图最终效果: 下面进入正文: 第一 ...

随机推荐

  1. ASP.NET MVC 设置区域默认定向

    public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "m_defau ...

  2. 详细介绍Spring 5的那些新特性与增强

    Spring5 是一个重要的版本,距离SpringFramework4差不多四年.在此期间,大多数增强都是在 SpringBoot 项目中完成的.在本文中,我们将很快了解到Spring5发行版中的一些 ...

  3. Django_ajax

    AJAX(Asynchronous Javascript And XML)翻译成中文就是"异步Javascript和XML".即使用Javascript语言与服务器进行异步交互,传 ...

  4. Java DB 访问之(四) spring mvc 组合mybatis

    说明 本项目采用 maven 结构,主要演示了 spring mvc + mybatis,controller 获取数据后以json 格式返回数据. 项目结构 包依赖 与说明 pom文件: <p ...

  5. SpringBoot(一)走进Springboot的世界

    什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员 ...

  6. 关于int *a[常量]与int (*a)[常量]的分析与区分(详解)

    前言: 小伙伴私信我说,int *a[常量]与int (*a)[常量]这个区分不开,C指针,确实是C中最难的部分,也是学C++,JAVA,包括你以后上岗用的非常频繁的东西,在这里我就简单论述一下吧,具 ...

  7. HDU4355-Party All the Time-三分

    Party All the Time Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. 状压dp初探

    写了几道状压...然后就一直在颓废... 2064: 分裂 http://www.lydsy.com/JudgeOnline/problem.php?id=2064 初始的为正,最后的为负,假设我们能 ...

  9. c语言基础学习08_内存管理

    =============================================================================涉及到的知识点有:一.内存管理.作用域.自动变 ...

  10. flume1.8 Channel类型介绍(四)

    1. Flume Channel Channels是events在agent上进行的存储库.Source添加events,Sink移除events. 1.1 Memory Channel(内存Chan ...