微信小程序獲取循環元素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來進行登錄
如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答