微信小程序获取用户手机号授权

1、获取微信用户绑定的手机号,需要先在主页面调用wx.login接口
2、使用button按钮触发授权弹窗 <button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">获取手机号码</button>
3、通过参数【encryptedData】,【iv】,【sessionKey】请求后端接口,传入上面获取到的参数,后端进行解密然后返回用户手机号

getPhoneNumber(e){
    console.log(e.detail.errMsg);//
    console.log(e.detail.iv);//加密算法的初始向量
    console.log(e.detail.encryptedData);//包括敏感数据在内的完整用户信息的加密数据

	const _this = this
    if (e.detail.encryptedData && e.detail.iv) {
		wx.checkSession({ //监测登录是否过期
			success() {
				// authorLogin方法为请求的后台接口
				const res = await this.authorLogin({
                  catgory: _this.choosedSubjectData.categoryId,
                  encryptedData: e.detail.encryptedData,
                  iv: e.detail.iv,
                  openId: _this.openid,
                  terminal: 21,
                  promoterInfoId:_this.qudaoInfo,
                  appletLoginFrom,
                  type: 1,
                  unionid: uni.getStorageSync('unionid')
                })
                console.log(res); // 此时的res返回用户手机号
			},
            fail() { // session_key 已经失效,需要重新执行登录流程
                _this.getUserOpenId() //重新登录,getOpenId为后端接口,
            }
		})
	}
}
getUserOpenId() {
	uni.login({
    	success: data => {
    		// data.code作为参数传给给后台
        	console.log(data.code,1111)
        	// 此时传入加密的code码,后端会返回用户unionid、openid等信息
	        this.getOpenId({ code: data.code }).then(res => {
                uni.setStorageSync('unionid', res.data.unionid) //存入缓存待用
                resolve(res.data.openid)
            }).catch(err => {
            	reject(err)
            })
        },
        fail: err => {
             console.log('uni.login 接口调用失败,将无法正常使用开放接口等服务', err)
             reject(err)
        }
   })
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值