小程式例子——擷取使用者唯一標識openid,小程式openid
1、效果展示
2、關鍵代碼
1)WXML檔案
擷取使用者唯一標識openidopenid:{{openid}}session_key:{{session_key}}
2)JS檔案
const APP_ID ='';//輸入小程式appidconst APP_SECRET ='';//輸入小程式app_secretvar OPEN_ID=''//儲存擷取到openidvar SESSION_KEY=''//儲存擷取到session_keyPage({ getOpenIdTap:function(){ var that=this; wx.login({ success:function(res){ wx.request({ //擷取openid介面 url: 'https://api.weixin.qq.com/sns/jscode2session', data:{ appid:APP_ID, secret:APP_SECRET, js_code:res.code, grant_type:'authorization_code' }, method:'GET', success:function(res){ console.log(res.data) OPEN_ID = res.data.openid;//擷取到的openid SESSION_KEY = res.data.session_key;//擷取到session_key console.log(OPEN_ID.length) console.log(SESSION_KEY.length) that.setData({ openid: res.data.openid.substr(0, 10) + '********' + res.data.openid.substr(res.data.openid.length - 8, res.data.openid.length), session_key: res.data.session_key.substr(0, 8) + '********' + res.data.session_key.substr(res.data.session_key.length - 6, res.data.session_key.length) }) } }) } }) }})
3)WXSS檔案
button{ margin: 10px; color: white; background: green;}view{ margin: 10px;}
3、原始碼擷取方式
百度雲連結:http://pan.baidu.com/s/1miqPiBY
4、在擷取使用者唯一標識openid過程中有遇到任何問題或者不明白的地方,可以添加我的進行諮詢,感謝支援!號:FutureJet
5、覺得不錯請打賞,您的十分滿意是筆者的無限動力。
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。