当前位置:   article > 正文

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

uinapp微信小程序注册表单
<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>
		
  • 1
  • 2
  • 3
  • 4
//获取手机号
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);
			}
		}) 
		
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/812910
推荐阅读
相关标签
  

闽ICP备14008679号