微信小程序获取循环元素id以及wx.login登录操作

时间:2019-03-31
本文章向大家介绍微信小程序获取循环元素id以及wx.login登录操作,主要包括微信小程序获取循环元素id以及wx.login登录操作使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

微信小程序获取循环元素id以及wx.login登录操作

通过点击方法获取循环数据元素的id例:

wxml里:

<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里:

gotoresult:function(e){

  var ep = e.target.id


  console.log(ep);


 }

小程序用户登录wx.login操作

js里:

wx.login({
   success: function (res) {
    if (res.code) {
     //发起网络请求
     wx.request({
      url: 'https://api.weixin.qq.com/sns/jscode2session',
      //url: 'https://www.xxx你的服务器网站xxxxxx.cn/',
      data: {
       appid:"你的appid",
       secret: "获取的secret",
       js_code: res.code,
       grant_type:"authorization_code"
      },
      success:function(res){
       message=res.data;
       console.log(message.openid)//返回的res里有用户openid等私密信息
      }
     })
    } else {
     console.log('获取用户登录态失败!' + res.errMsg)//异常反馈
    }
   }
  });

通过以上方式,可以向微信发送请求获取传回来的openid等信息;

小程序通过wx.checkSession可以判断登录是否过期

js里:

wx.checkSession({
 success: function(){
  //session 未过期,并且在本生命周期一直有效
 },
 fail: function(){
  //登录态过期
  wx.login() //重新登录
  ....
 }
})

如果登录过期,就可以调用上面的we.login来进行登录

 如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!