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

缺乏、安全感 2022-05-31 08:48 274阅读 0赞

概述

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

详细

代码下载:http://www.demodashi.com/demo/10634.html

一、准备工作

1、注册微信小程序

QQ截图20170717112223.png

2、注册和风天气账号

QQ截图20170717112355.png

3、注册百度地图开放平台(小程序应用)

QQ截图20170717112300.png

4、在小程序设置中设置request合法域名

QQ截图20170717112727.png

二、程序实现

项目代码截图:

image.png

具体实现如下:

1、前端页面的实现

  1. <!--index.wxml-->
  2. <image src="../../assets/day.jpg" class="bg"></image>
  3. <view class="container">
  4. <view class="nowWeather">
  5. <view class="city w">{
  6. {city}} {
  7. {district}}</view>
  8. <view class="road w">{
  9. {street}}</view>
  10. <view class="temp w b">{
  11. {tmp}}°</view>
  12. <view class="weather w">{
  13. {txt}} | 空气 {
  14. {qlty}}</view>
  15. </view>
  16. <view class="weahterDetail">
  17. <view class="">
  18. <view class="w center">{
  19. {dir}}</view>
  20. <view wx:if="{
  21. {sc == '微风'}}" class="w b center f50">微风</view>
  22. <view wx:else class="w b center f50">{
  23. {sc}}级</view>
  24. </view>
  25. <view class="l"></view>
  26. <view class="">
  27. <view class="w center">相对湿度</view>
  28. <view class="w b center f50">{
  29. {hum}}%</view>
  30. </view>
  31. <view class="l"></view>
  32. <view class="">
  33. <view class="w center">体感温度</view>
  34. <view class="w b center f50">{
  35. {fl}}°</view>
  36. </view>
  37. </view>
  38. </view>
  39. <view wx:for="{
  40. {daily_forecast}}" wx:for-index="i" wx:for-item="item">
  41. <view class="hor forcast">
  42. <view class="center">{
  43. {day[i]}}</view>
  44. <view class="hor">
  45. <image class="img" src="../../assets/icons/{
  46. {item.cond.code_d}}.png"></image>
  47. <view class="center">{
  48. {item.cond.txt_d}}|{
  49. {qlty}}</view>
  50. </view>
  51. <view class="center">{
  52. {item.tmp.min}}°/ {
  53. {item.tmp.max}}°</view>
  54. </view>
  55. </view>

2、css实现

  1. /**index.wxss**/
  2. /**common css**/
  3. .w{
  4. color: white;
  5. }
  6. .b{
  7. font-weight: bold;
  8. }
  9. .l{
  10. border: 1rpx solid #fff;
  11. }
  12. .center{
  13. text-align: center;
  14. margin: auto 0;
  15. }
  16. .hor{
  17. display: flex;
  18. }
  19. .f50{
  20. font-size: 50rpx;
  21. }
  22. /**index css**/
  23. .bg {
  24. width: 100%;
  25. height: 700rpx;
  26. }
  27. .temp{
  28. font-size: 170rpx;
  29. }
  30. .container {
  31. position: absolute;
  32. left: 0;
  33. top: 0;
  34. width: 100%;
  35. padding: 0;
  36. margin: 0;
  37. height: 700rpx;
  38. display: block;
  39. }
  40. .nowWeather{
  41. padding: 60rpx;
  42. }
  43. .weahterDetail{
  44. width: 100%;
  45. display: flex;
  46. flex-direction: row;
  47. justify-content: space-around;
  48. position: absolute;
  49. bottom: 50rpx;
  50. }
  51. .forcast{
  52. padding: 30rpx;
  53. margin-left: 16rpx;
  54. margin-right: 16rpx;
  55. border-bottom: 1rpx solid #eee;
  56. justify-content: space-around;
  57. }
  58. .img{
  59. width: 60rpx;
  60. height: 60rpx;
  61. margin-right: 16rpx;
  62. }

3、js实现动态数据绑定

  1. //index.js
  2. //获取应用实例
  3. var app = getApp()
  4. var day = ["今天","明天","后天"];
  5. Page({
  6. data: {
  7. day : day,
  8. },
  9. onLoad: function () {
  10. console.log('onLoad')
  11. var that = this
  12. that.getLocation();
  13. },
  14. //获取经纬度方法
  15. getLocation: function () {
  16. var that = this
  17. wx.getLocation({
  18. type: 'wgs84',
  19. success: function (res) {
  20. var latitude = res.latitude
  21. var longitude = res.longitude
  22. console.log("lat:" + latitude + " lon:" + longitude);
  23. that.getCity(latitude, longitude);
  24. }
  25. })
  26. },
  27. //获取城市信息
  28. getCity: function (latitude, longitude) {
  29. var that = this
  30. var url = "https://api.map.baidu.com/geocoder/v2/";
  31. var params = {
  32. ak: "1G50Crx5QIKWy5o4R5Q1ONFSgmFIxfIR",
  33. output: "json",
  34. location: latitude + "," + longitude
  35. }
  36. wx.request({
  37. url: url,
  38. data: params,
  39. success: function (res) {
  40. var city = res.data.result.addressComponent.city;
  41. var district = res.data.result.addressComponent.district;
  42. var street = res.data.result.addressComponent.street;
  43. that.setData({
  44. city: city,
  45. district: district,
  46. street: street,
  47. })
  48. var descCity = city.substring(0, city.length - 1);
  49. that.getWeahter(descCity);
  50. },
  51. fail: function (res) { },
  52. complete: function (res) { },
  53. })
  54. },
  55. //获取天气信息
  56. getWeahter: function (city) {
  57. var that = this
  58. var url = "https://free-api.heweather.com/v5/weather"
  59. var params = {
  60. city: city,
  61. key: "894fc2a749104d679fa022c3e71afe83"
  62. }
  63. wx.request({
  64. url: url,
  65. data: params,
  66. success: function (res) {
  67. var tmp = res.data.HeWeather5[0].now.tmp;
  68. var txt = res.data.HeWeather5[0].now.cond.txt;
  69. var code = res.data.HeWeather5[0].now.cond.code;
  70. var qlty = res.data.HeWeather5[0].aqi.city.qlty;
  71. var dir = res.data.HeWeather5[0].now.wind.dir;
  72. var sc = res.data.HeWeather5[0].now.wind.sc;
  73. var hum = res.data.HeWeather5[0].now.hum;
  74. var fl = res.data.HeWeather5[0].now.fl;
  75. var daily_forecast = res.data.HeWeather5[0].daily_forecast;
  76. that.setData({
  77. tmp: tmp,
  78. txt: txt,
  79. code: code,
  80. qlty: qlty,
  81. dir: dir,
  82. sc: sc,
  83. hum: hum,
  84. fl: fl,
  85. daily_forecast: daily_forecast
  86. })
  87. },
  88. fail: function (res) { },
  89. complete: function (res) { },
  90. })
  91. }
  92. })

三、运行效果

导入到微信web开发者工具,运行即可:

运行后,界面如下:

QQ截图20170714144814.png

注:本文著作权归作者,由demo大师(http://www.demodashi.com)宣传,拒绝转载,转载需要作者授权

发表评论

表情:
评论列表 (有 0 条评论,274人围观)

还没有评论,来说两句吧...

相关阅读