移动软件开发实验二

一、实验目标

1、掌握服务器域名配置和临时服务器部署;

2、掌握 wx.request 接口的用法。

二、实验步骤

js相关代码:

data: {
    region:['北京市','北京市','东城区'],
    regionID:101010100,
    icon:999,
    text:"未知",
    temp:0,
    hum:0,
    pressure:0,
    vis:0,
    windDir:0,
    windSpeed:0,
    windScale:0,
  },
​
  regionChange:function(e){
    this.setData({region:e.detail.value});
    this.getWeather();
  },
​
  getRegion:function(){
    var that=this;
    wx.request({
      url:'https://geoapi.qweather.com/v2/city/lookup?',
      data:{
        location:that.data.region[1],
        key:'35fc57b95350441e8c06735d5ee25e49'
      },
    success:function(res){
      console.log(res.data.location[0].id),
      that.data.regionID=res.data.location[0].id
    },
    })
  },
​
  getWeather:function(){
    var that=this;
    this.getRegion();
    wx.request({
      url:'https://devapi.qweather.com/v7/weather/now?',
      data:{
        location:that.data.regionID,
        key:'35fc57b95350441e8c06735d5ee25e49'
      },
      success:function(res){
        console.log(res.data);
        console.log(res.data.now.temp);
        that.setData({
          icon:res.data.now.icon,
          text:res.data.now.text,
          temp:res.data.now.temp,
          hum:res.data.now.humidity,
          pressure:res.data.now.pressure,
          vis:res.data.now.vis,
          windDir:res.data.now.windDir,
          windSpeed:res.data.now.windSpeed,
          windScale:res.data.now.windScale,
        })
      },
    })
  },
​
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.getWeather();
  },

wxml相关代码:

<view class="container"> 
  <picker mode="region"bindchange='regionChange'>
    <view>{{region}}</view>
  </picker>
  <text>{{temp}}℃ {{text}}</text>
  <image src="/images/weather_icon/images/weather_icon_s1_bw/{{icon}}.png" mode="widthFix"></image>
  <view class="detail">
    <view class="bar">
      <view class="box">湿度</view>
      <view class="box">气压</view>
      <view class="box">能见度</view>
    </view>
    <view class="bar">
      <view class="box">{{hum}}%</view>
      <view class="box">{{pressure}}hPa</view>
      <view class="box">{{vis}}Km</view>
    </view>
    <view class="bar">
      <view class="box">风向</view>
      <view class="box">风速</view>
      <view class="box">风力</view>
    </view>
    <view class="bar">
      <view class="box">{{windDir}}</view>
      <view class="box">{{windSpeed}}km</view>
      <view class="box">{{windScale}}级</view>
    </view>
  </view>
</view>

wxss相关代码:

.container{
  height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-around;
}
text{
  font-size: 80rpx;
  color: #3c5f81;
}
image{
  width: 220rpx;
}
.detail{
  width: 100%;
  display: flex;
  flex-direction: column;
}
.bar{
  display: flex;
  flex-direction: row;
  margin: 20rpx;
}
.box{
  width: 33.3%;
  text-align: center;
}

三、程序运行结果

初始数据

启动后数据

切换城市后数据

四、问题总结与体会

问题

由于开发工具版本的更新,原先的风和天气url的request申请从城市名变成了城市id,所以无法获取request。为了获取request,又调用了一个接口geoapi来实现从城市名到城市id的转换。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值