赞
踩
<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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。