微信小程序獲取循環(huán)元素id以及wx.login登錄操作
通過(guò)點(diǎn)擊方法獲取循環(huán)數(shù)據(jù)元素的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">(數(shù)據(jù)更新時(shí)間:{{item.date}})</text> <a catchtap="gotoresult" id="{{item.name}}" class="button">肉產(chǎn)類(lèi)</a> </view>上面的a標(biāo)簽的id是通過(guò)循環(huán)來(lái)的,js能通過(guò)catchtap="gotoresult"來(lái)獲取當(dāng)前點(diǎn)擊的元素idjs里:
gotoresult:function(e){ var ep = e.target.id console.log(ep); }小程序用戶(hù)登錄wx.login操作
js里:
wx.login({ success: function (res) { if (res.code) { //發(fā)起網(wǎng)絡(luò)請(qǐng)求 wx.request({ url: 'https://api.weixin.qq.com/sns/jscode2session', //url: 'https://www.xxx你的服務(wù)器網(wǎng)站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里有用戶(hù)openid等私密信息 } }) } else { console.log('獲取用戶(hù)登錄態(tài)失敗!' + res.errMsg)//異常反饋 } } });通過(guò)以上方式,可以向微信發(fā)送請(qǐng)求獲取傳回來(lái)的openid等信息;
小程序通過(guò)wx.checkSession可以判斷登錄是否過(guò)期
js里:
wx.checkSession({ success: function(){ //session 未過(guò)期,并且在本生命周期一直有效 }, fail: function(){ //登錄態(tài)過(guò)期 wx.login() //重新登錄 .... }})如果登錄過(guò)期,就可以調(diào)用上面的we.login來(lái)進(jìn)行登錄
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選