appid 和 secret 需要登录微信公众平台查找 开发-开发管理-开发设置-开发者ID
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
let that = this; //获取openid不需要授权
//根据code获取openid等信息
wx.login({
//获取code
success: function (res) {
var code = res.code; //返回code
console.log(code);
var appId = '你的appid';
var secret = '你的secret';
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appId + '&secret=' + secret + '&js_code=' + code + '&grant_type=authorization_code',
data: {},
header: {
'content-type': 'json'
},
success: function (res) {
var openid = res.data.openid //返回openid
console.log('openid为' + openid);
}
})
}
})
},