微信小程序获取头像,昵称,unionid ,手机号
HTML
<button @click="getPersonUserInfo">去授权</button> <button open-type="getPhoneNumber" @getphonenumber="getMobileInfo">获取手机号授权</button>
JS:
getMobileInfo(e) { uni.login({ async success({code}) { const userInfo = await that.$apis.login.getMobileInfo({ "jsCode": code, "ivStr": e.detail.iv, "encryptedData": e.detail.encryptedData }) console.log(userInfo) // 用户信息 } }) }, getPersonUserInfo(e){ console.log(e); uni.getUserProfile({ //每次都会有授权弹框 desc:'登录', success({userInfo}){ console.log('userInfo',userInfo) uni.login({ async success({code}) { uni.request({ url: 'https://api.weixin.qq.com/sns/jscode2session?appid=xxxxxxxxxxxxxx&secret=xxxxxxxxxx&js_code=' + code + '&grant_type=authorization_code', success(res) { console.log(res.data.unionid) }) } }) } }) },