微信小程序獲取循環(huán)元素id以及wx.login登錄操作
更新時間:2017年08月17日 15:32:52 投稿:lqh
這篇文章主要介紹了微信小程序獲取循環(huán)元素id以及wx.login登錄操作的相關(guān)資料,這里提供實例幫助大家實現(xiàn)該功能,需要的朋友可以參考下
微信小程序獲取循環(huán)元素id以及wx.login登錄操作
通過點擊方法獲取循環(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ù)更新時間:{{item.date}})</text> <a catchtap="gotoresult" id="{{item.name}}" class="button">肉產(chǎn)類</a> </view>
上面的a標簽的id是通過循環(huán)來的,js能通過catchtap="gotoresult"來獲取當(dāng)前點擊的元素idjs里:
gotoresult:function(e){ var ep = e.target.id console.log(ep); }
小程序用戶登錄wx.login操作
js里:
wx.login({ success: function (res) { if (res.code) { //發(fā)起網(wǎng)絡(luò)請求 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里有用戶openid等私密信息 } }) } else { console.log('獲取用戶登錄態(tài)失敗!' + res.errMsg)//異常反饋 } } });
通過以上方式,可以向微信發(fā)送請求獲取傳回來的openid等信息;
小程序通過wx.checkSession可以判斷登錄是否過期
js里:
wx.checkSession({ success: function(){ //session 未過期,并且在本生命周期一直有效 }, fail: function(){ //登錄態(tài)過期 wx.login() //重新登錄 .... } })
如果登錄過期,就可以調(diào)用上面的we.login來進行登錄
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!