1、小程序獲取微信openId
wx.login({
success: res => {
// 發送 res.code 到后台換取 openId, sessionKey, unionId
// var appid = 'wx958dfed141094376'; //填寫微信小程序appid
// var secret = '0e0caf2901e855bb18718c8ce479cd6d'; //填寫微信小程序secret
if (res.code) {
//調用request請求api轉換登錄憑證
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session',
data: {
//小程序唯一標識
appid: 'wx958dfed141094376',
//小程序的 app secret
secret: '0e0caf2901e855bb18718c8ce479cd6d',
grant_type: 'authorization_code',
js_code: res.code
},
method: 'GET',
header: { 'content-type': 'application/json' },
success: function (res) {
// console.log(res.data.openid) //獲取openid
this.globalData.openId = res.data.openid;
}
})
}
}
})
2、需要設置如下圖所示
