实训第五天:我的页面布局,API接口(一言,天气预报)

本文介绍了在微信小程序中使用open-data组件获取并显示用户信息,以及通过API接口实现一言和天气预报的功能。展示了相关代码段和运行效果截图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.不用不用脚本和弹窗获取手机信息,用微信公众平台的组件开发能力 open-data用于展示微信开放的数据。
部分代码:

<view class="container">

<view class='bg'>

<view class='usericon'>

<open-data type="userAvatarUrl"></open-data></view>

<open-data class="text" type="userNickName"></open-data>

</view>

<view class='system'>

<view id='info'><text>设备信息</text></view>

<view>

<text >手机型号:</text>

<text class='color'>{{model}}</text>

</view>

<view>

<text >手机版本号</text>

<text class='color'>{{system}}</text>

</view>

<view>

<text>微信版本:</text>

<text class='color'>{{version}}</text>

</view>

<view>

<text >手机屏幕分辨率:</text>

<text class='color'>{{screenWidth}} * {{screenHeight}}</text>

</view>

<view>

<text>地区</text>

<view class='color'>

<open-data type="userProvince" lang="en"></open-data>

<open-data class="ml_10" type="userCity" lang="en"></open-data>

</view>

</view>

</view>

</view>

再对各个信息格式进行设定,最后运行截图:


2.请求和响应:API  网络发起请求一言和天气预报的API接口获得网站信息
首先在微信公众平台填好自己的微信小程序信息,且将两个平台的API接口填到开发设置的服务器域名中,实现小程序能适应两个平台的数据。
一言请求:
 

click:function(){

var _this = this;

//发起一个网络请求

wx.request({

url: 'https://v1.hitokoto.cn/', //仅为示例,并非真实的接口地址

data: {

c: "a"

},

header: {

'content-type': 'application/json' // 默认值

},

success(res) {

// console.log(res.data)

_this.setData({

yiyan: res.data

})

}

});
一言实现页面(wxml页面):
<text class="color">{{yiyan.hitokoto}}</text>

<text class="color">---{{yiyan.from}}</text>

<button id="id" bindtap='click'>一言 </button>
天气预报请求:

wx.request({

url: 'https://www.tianqiapi.com/api/', //仅为示例,并非真实的接口地址

method:"get",

dataType:"json",

data: {

version: "v1",

city:'湛江'

},

header: {

'content-type': 'application/json' // 默认值

},

success(res) {

console.log(res.data)

_this.setData({

weather:res.data,

flag:true

})

console.log(res.data);

}

}),

天气预报实现页面部分代码:

<view class="week"> {{weather.update_time}} 更新</view>

<view id='img'><image class="photoes" src='../../images/{{weather.data[0].wea_img}}.png'></image></view>

<view class="week"> {{weather.data[0].tem}},{{weather.data[0].wea}}</view>

<view class="futherweather">

<text class="humity" decode="{{true}}"> 湿度:{{weather.data[0].humidity}}&nbsp;&nbsp;&nbsp;&nbsp; </text>

<text decode="{{true}}"> 空气指数:{{weather.data[0].air}}&nbsp;&nbsp;&nbsp;&nbsp; </text>

<text>空气质量:{{weather.data[0].air_level}}</text>

</view>

<view>

<text class="futureweather">生活小贴士:{{weather.data[0].index[3].desc}}</text>

<text class="futureweather">{{weather.data[0].index[0].desc}}</text> </view>

<text>\n</text>
页面运行截图:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值