微信小程序獲取循環(huán)元素id以及wx.login登錄操作
微信小程序獲取循環(huán)元素id以及wx.login登錄操作
通過點(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ù)更新時間:{{item.date}})</text> <a catchtap="gotoresult" id="{{item.name}}" class="button">肉產(chǎn)類</a> </view>
上面的a標(biāo)簽的id是通過循環(huán)來的,js能通過catchtap="gotoresult"來獲取當(dāng)前點(diǎn)擊的元素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來進(jìn)行登錄
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
JavaScript執(zhí)行機(jī)制詳細(xì)介紹
這篇文章主要介紹了JavaScript執(zhí)行機(jī)制,想要搞懂JavaScript執(zhí)行機(jī)制,便與進(jìn)程與線程的概念脫不了干系,下面我們就來看看這JavaScript執(zhí)行機(jī)制的具體介紹吧,需要的朋友可以參考一下2021-12-12微信小程序中input標(biāo)簽詳解及簡單實(shí)例
這篇文章主要介紹了微信小程序中input標(biāo)簽詳解及簡單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05