首先来一个按钮
<button type="primary" open-type="getUserInfo" lang="zh_CN" bindtap="primary" bindgetuserinfo="YuYueSubmit" > 用户信息获取 </button>
然后来一个js
YuYueSubmit: function (e) {
console.log(e); //此处用户所有信息
wx.login({
success: function (code) {
console.log(code.code)
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session',
data: {
appid: 'xxxxxxxxx', //此处是小程序appid
secret: "70916b1a757dfa2affcxxxx", //此处是小程序秘钥
js_code: code.code, //此处登录返回的code
grant_type: "authorization_code" //此处是固定写法
},
method: 'GET',
success: function (data) {
console.log(data.data.openid); //此处是返回的openid
}
})
}
})
},
其中 url为微信官方的的请求地址,需要在后端将此地址进行封装。从而在此后端接受到返回的信息,返回给前端。
本地调式的话,只需要打开微信小程序工具中的本地域名校验即可使用,微信官方的URl。