小程序手机号快捷登录

 首先,小程序手机号快捷登录普遍适用于各种小程序直接,之前上网搜过有的能直接拿来用,但是会有地狱回调问题,我这里写的代码可以直接拿来用

创建一个button按钮,用户点击之后可以调取手机号api

<button  v-show="z"class="btn" open-type="getPhoneNumber" type="primary" 
 @getphonenumber="getUserInfo">微信用户登录</button>

getUserInfo(e) {
                    //检查token是否过期
					uni.checkSession({
						success:res=> {
							console.log(res)
						},
						fail:res=> {
							uni.login({
							    success: res => {
								console.log(res)
								this.code=res.code
							    }
							  })
						},
						complete:res=>{
							myRequest({
								url: "/api/login/login",
								method:"post",
								data:{
									code:this.code,
									encryptedData:e.detail.encryptedData,
									iv:e.detail.iv
								}
							}).then(res=>{
								if(res.data.code==200){
                                    //设置登录状态
									uni.setStorageSync("is_login",1)
									uni.setStorageSync("uid",res.data.data.id)
									uni.setStorageSync("if_real",res.data.data.if_real)
									uni.setStorageSync("name",res.data.data.real_name)
									uni.setStorageSync("phonenum",res.data.data.mobile)
									uni.setStorageSync("idcard",res.data.data.number)
									uni.showToast({
										title:"登录成功",
										duration:2000
									})
									if(res.data.data.if_real==0){
										uni.navigateTo({
											url:"/pages/personal/personal"
										})
									}else{	
										setTimeout(function (){
											uni.switchTab({
											      url: '/pages/index/index'
											  });
										},2000)
									}
								}
								
							})
						}
					})
				//触发获取手机号加密信息
				console.log(e)

			  // let that = this
			  // encryptedData = e.detail.encryptedData
			  // iv = e.detail.iv	
			
			      //将code,encryptedData,iv传给后台进行解密
			}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值