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)
        }
   })
}
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐