这篇文章主要介绍了微信小程序获取循环元素id以及wx.login登录操作的相关资料,这里提供实例帮助大家实现该功能,需要的朋友可以参考下
微信小程序获取循环元素id以及wx.login登录操作
通过点击方法获取循环数据元素的id例:
wxml里:
1 2 3 4 5 6 7 | <view id="list" wx:for="{{txt}}" >
<text id="L_name">{{item.name}}</text>
<text id="L_price">¥{{item.price}}/{{item.unit}}</text>
<text id="L_place">{{item.place}}</text>
<text id="L_date">(数据更新时间:{{item.date}})</text>
<a catchtap="gotoresult" id="{{item.name}}" class="button">肉产类</a>
</view>
|
上面的a标签的id是通过循环来的,js能通过catchtap="gotoresult"来获取当前点击的元素idjs里:
1 2 3 4 | gotoresult:function(e){
var ep = e.target.id
console.log(ep);
}
|
小程序用户登录wx.login操作
js里:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | wx.login({
success: function (res) {
if (res.code) {
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session',
data: {
appid:"你的appid",
secret: "获取的secret",
js_code: res.code,
grant_type:"authorization_code"
},
success:function(res){
message=res.data;
console.log(message.openid)
}
})
} else {
console.log('获取用户登录态失败!' + res.errMsg)//异常反馈
}
}
});
|
通过以上方式,可以向微信发送请求获取传回来的openid等信息;
小程序通过wx.checkSession可以判断登录是否过期
js里:
1 2 3 4 5 6 7 8 9 10 | wx.checkSession({
success: function(){
},
fail: function(){
wx.login()
....
}
})
|
如果登录过期,就可以调用上面的we.login来进行登录。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。