uniapp获取手机号流程

1`. view视图代码

<button class="allow-btn" open-type="getPhoneNumber" @getphonenumber="getphonenumber">允许</button>`
  1. js代码
getphonenumber(e) {
				this.$emit('getphonenumber',e);
				console.log(e,"获取手机号信息");
				console.log(e.detail.errMsg);
				console.log(e.detail.iv);
				console.log(e.detail.encryptedData);
				 
				 
			}
  1. 调用授权code
// 获取微信授权
			getWxCode() { 
				let that = this;
				uni.login({
					provider: 'weixin',
					timeout: 3000,
					success: res => {
						console.log(res,"登录的结果值");
						console.log(res.code, "code码")
						if (res.code) {
							//uni.setStorageSync('code', res.code)
							 that.code=res.code;

						}
					},
					fail(err) {
						console.log(err)
					}
				})
			},

4.把加密值给后端进行解密

getphonenumber(e){ 
				console.log(e.detail.errMsg);
				console.log(e.detail.iv);
				console.log(e.detail.encryptedData);
				wx.checkSession({
				  success () {
				    //session_key 未过期,并且在本生命周期一直有效
				  },
				  fail () {
				    // session_key 已经失效,需要重新执行登录流程
				    this.getWxCode() //重新登录
				  }
				})
				//-----------------是否授权,授权通过进入主页面,授权拒绝则停留在登陆界面
				    if (e.detail.errMsg == 'getPhoneNumber:user deny') { //用户点击拒绝
				         uni.showToast({
							 title:"你点击了拒绝",
							 icon:"none"
						 })
				    } else { //允许授权执行跳转 
						 this.iv=e.detail.iv;
						 this.encryptedData=e.detail.encryptedData;
						 // this.getWxCode();
						 this.gettoken(this.code)
				    }
				  
			},
selPhonenumber(data){
				
				 
				console.log(data,"获取手机号信息");
				let  that=this;
				this.$api.sendRequest({
					url:"/rest/getPhoneNo",
					data:data,
					success:res=>{
						console.log(res,"返回手机号信息")
						if(res.resultCode==200){ 
							that.memberPhone=res.data.phoneNumber;
							uni.setStorageSync('phoneNumber',that.memberPhone);
							that.$refs['phonepopup'].close();
							 that.shopTimeshow();
							 
						}else{
							 
							 uni.showModal({
							 	title:"错误提示",
								content:JSON.stringify(data)
							 })
						}
						 
					}
				})
			},

解密出来的值如下:
在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你的美,让我痴迷

你的好,我会永远记住你的。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值