uniapp开发微信小程序登陆注册

<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" class="confirm-btn" v-show="btnShow" :disabled="disabled1">微信一键登录</button>
<view class="login-hint" v-show="btnShow1">请完成微信授权后继续使用</view>
<button open-type="getUserInfo" class="confirm-btn" @getuserinfo="goLoginBtn" withCredentials="true"  v-show="btnShow1" :disabled="disabled2">授权微信信息</button>
		
//获取手机号
getPhoneNumber(e){
		console.log(e)
		let that=this;
		let encryptedData=e.detail.encryptedData;
		let iv=e.detail.iv;
		that.disabled1=true;
		wx.checkSession({//微信检查登陆状态是否过期
	    	success: function(res){
				wx.login({//微信登陆
					success(res){
						let code=res.code;
						console.log(res.code)
						if(code) {
							weChatGetOpenId(code).then(re =>{//登陆接口请求
								console.log(re)
								that.openId=re.openid;
								let userInfo=re.userInfo;
								if(userInfo!==null){
									uni.setStorageSync('userInfo',userInfo);
									let token=re.userInfo.token;
									uni.setStorageSync('token',token);
									that.disabled1=false;//获取用户
									that.$api.msg('登录成功');
									setTimeout(function(){
										uni.switchTab({
											url: "index/index"
										});
									},300)
								}else{
									let sessionKey=re.session_key;
									getPhoneNumber(encryptedData,iv,sessionKey).then(re =>{//获取手机号接口请求
										that.phone=JSON.parse(re).phoneNumber;
										that.btnShow=false;//用户登陆按钮
										that.btnShow1=true;//获取用户信息按钮
										that.disabled1=false;//登陆按钮禁用
									}).catch( re=>{
										that.disabled1=false;//登陆按钮禁用
									}); 
								}
								
							}).catch( re=>{
								console.log(re);
								that.disabled1=false;//登陆按钮禁用
							}); 
						}
					},
					fail(re) {
						console.log(re);
						that.disabled1=false;//登陆按钮禁用
					}
				})
	    	},
			fail: function(res){//检查登陆过期
				wx.login({//登陆过期重新微信登陆
					success(res){
						let code=res.code;
						if(code) {
							weChatGetOpenId(code).then(re =>{//小程序登陆接口请求
								that.openId=re.openid;
								let userInfo=re.userInfo;
								if(userInfo!==null){
									uni.setStorageSync('userInfo',userInfo);
									let token=re.userInfo.token;
									uni.setStorageSync('token',token);
									that.disabled1=false;//登陆按钮禁用
									that.btnShow1=true;//获取用户信息按钮
									that.$api.msg('登录成功');
									setTimeout(function(){
										uni.switchTab({
											url: "index/index"
										});
									},300)
								}else{
									let sessionKey=re.session_key;
									getPhoneNumber(encryptedData,iv,sessionKey).then(re =>{//获取手机号接口请求
										that.phone=JSON.parse(re).phoneNumber;
										that.btnShow=false;//用户登陆按钮
										that.btnShow1=true;//获取用户信息按钮
									}).catch( re=>{
										console.log(re);
									}); 
								}
							}).catch( re=>{
								that.btnShow=false;//用户登陆按钮
								console.log(re);
							}); 
						}
					},
					fail(re) {
						that.btnShow=false;//用户登陆按钮
						console.log(re);
					}
				})
	    }
	  })
	},
	//登录操作
	goLoginBtn(){
		let that=this;
		let phone=this.phone;
		let openid=this.openId;
		that.btnShow1=true;//获取用户信息按钮
		that.disabled2=true;//获取用户信息
		uni.getUserInfo({//获取用户信息
			success: (re) => {
				console.log(re)
				let nickName=re.userInfo.nickName;
				let avatarUrl=re.userInfo.avatarUrl;
				let gender=re.userInfo.gender;
				// let parentUserId="";
				let unionId="";
				//注册账号接口请求
				registered(avatarUrl,gender,nickName,openid,that.parentUserId,phone,unionId).then(re =>{
					let userInfo=re;
					uni.setStorageSync('userInfo',userInfo);
					let token=userInfo.token;
					uni.setStorageSync('token',token);
					that.disabled1=false;//用户登陆按钮
					that.$api.msg('登录成功');
					that.btnShow1=false;//获取用户信息按钮
					uni.switchTab({
						url: "index/index"
					});
				}).catch( re=>{
					that.disabled2=false;//获取用户信息
					console.log(re);
				}); 
			},
			fail(re) {
				that.disabled2=false;//获取用户信息
				console.log(re);
			}
		}) 
		
	}

注意:先获取用户手机号(要先检查用户登陆信息是否过期),再获取用户信息
微信小程序文档:https://developers.weixin.qq.com/miniprogram/dev/api/open-api/login/wx.checkSession.html
uniapp文档:https://uniapp.dcloud.io/component/button

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值