Home >WeChat Applet >Mini Program Development >Example of how WeChat applet obtains loop element id and wx.login login operation

Example of how WeChat applet obtains loop element id and wx.login login operation

黄舟
黄舟Original
2017-09-12 09:49:253047browse

This article mainly introduces the relevant information of the WeChat applet to obtain the loop element id and the wx.login login operation. Here are examples to help you implement this function. Friends who need it can refer to it

WeChat applet obtains the id of the loop element and the wx.login login operation

Get the id of the loop data element through the click method. Example:

In 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>

The id of the a tag above is obtained through looping. JS can obtain the currently clicked element id through catchtap="gotoresult". In js:


gotoresult:function(e){
  var ep = e.target.id
  console.log(ep);
 }

Mini program user login wx.login operation

js:


##

wx.login({
   success: function (res) {
    if (res.code) {
     //发起网络请求
     wx.request({
      url: &#39;https://api.weixin.qq.com/sns/jscode2session&#39;,
      //url: &#39;https://www.xxx你的服务器网站xxxxxx.cn/&#39;,
      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(&#39;获取用户登录态失败!&#39; + res.errMsg)//异常反馈
    }
   }
  });

Through the above method, you can send a request to WeChat to obtain the information Returned openid and other information;

The applet can determine whether the login has expired through wx.checkSession


In js:


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

If the login expires, you can call we.login above to log in

The above is the detailed content of Example of how WeChat applet obtains loop element id and wx.login login operation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn