uniapp小程序使用getUserProfile登录(获取昵称统一为‘微信用户’+头像为灰色头像)

一、微信授权登录按钮

<view class="name" v-if="!hasLogin">
							    <u-button @click="getUserProfile()" size="small" class="authorizationBtn" type="primary">
										微信授权登录
								</u-button>
							</view>

二、微信授权登录弹窗

<view>
			<!-- 登录弹框 -->
			<u-popup :show="phoneModalShow" mode="center" border-radius="14" >
				<view class="sq_box">
					<view>授权登录</view>
					<view>授权获取您的手机号</view>
					<view>
						<view class="agreement_warp">
							<view>登录代表您已同意</view>
							<view @click="agreenmentDetailHandle" style="color: #ff431e;">隐私政策</view>
						</view>
						<u-button style="width: 100%;"  type="success"
							@getphonenumber="getphonenumber" open-type="getPhoneNumber">
							微信快捷授权登录
						</u-button>
					</view>
				</view>
			</u-popup>
		</view>

三、数据定义

hasLogin: false,
phoneModalShow: false, // 手机号隐藏层

四、登录方法

//获取个人信息
			async getUserProfile() {
				//调用微信获取code
				uni.login({
					provider:'weixin',
					success: (logRes) => {
						console.log(logRes.code)
						this.code = logRes.code
					}
				});
				uni.getUserProfile({
					desc:'WeiXin'
				}).then(res => {
					if (res && res.length > 1) {
						let userMsgHave = JSON.parse(res[1].rawData)
						console.log(JSON.parse(res[1].rawData));
						uni.showLoading({
							title: '登录加载中'
						});
						if(res[1].errMsg == "getUserProfile:ok"){
							console.log("codeLogin", this.code);
							let url = 'wx/code/' + this.code
							this.request(url,"POST",null).then(res =>{
								if(res.data.code == 200){
									this.userRawData = userMsgHave; // 存储用户信息
									this.LoginCodeMsg.openId = res.data.openId
									this.LoginCodeMsg.sessionKey = res.data.sessionKey
									this.phoneModalShow = true;
									uni.hideLoading();
								}else{
									uni.showToast({
										title: '微信登录失败',
										icon: 'none'
									})
									uni.hideLoading();
								}
							})
						}else{
							uni.showToast({
								title:'登录失败请重试',
								icon:'error',
								duration:2000
							})
							uni.hideLoading();
						}
					}
				})
			},
			//获取手机号
			getphonenumber(e){
				let phoneMsg = {
					encryptedData: e.detail.encryptedData,
					iv: e.detail.iv,
					openId: this.LoginCodeMsg.openId,
					sessionKey: this.LoginCodeMsg.sessionKey,
					avatar: this.userRawData.avatarUrl,
					nickName: this.userRawData.nickName,
					sex: this.userRawData.gender
				}
				this.request('wx/login','POST',phoneMsg).then(res =>{
					this.phoneModalShow = false
					if(res.data.code == 200){
						this.isLogin = true
						uni.showToast({
							title:'登录成功',
							icon:'none'
						})
						this.hasLogin = true
						this.userId = res.data.busInfo.userId
						this.userInfo.level = res.data.busInfo.userType
						this.userInfo.nickName = res.data.busInfo.nickName
						this.userInfo.mobiler = res.data.busInfo.phoneNumber
						this.userInfo.avatar = res.data.busInfo.avatar
						console.log(JSON.stringify(this.userInfo))
						console.log(JSON.stringify(res.data.busInfo))
						//缓存登录状态和用户信息
						this.userInfo.token = res.data.token
						this.$store.state.busInfo = res.data.busInfo;
						this.$store.state.hasLogin = this.isLogin;
						this.$store.state.userInfo = this.userInfo;
						this.$store.state.token = res.data.token;
						//将当前代理的个人信息存入缓存中
						uni.setStorageSync('busInfo',res.data.busInfo)
						//token存缓存
						uni.setStorageSync('token',res.data.token)
						setTimeout(function (){
							uni.navigateBack()
						},2000)
						//加载顶部数据
						this.initTopShouYiData()
					}else{
						uni.showToast({
							title:'登录失败',
							icon:'error',
							duration:2000
						})
					}
				})
			},

//退出方法
			logout(){
				uni.showModal({
					title:'提示',
					content:'请确认是否退出',
					success: (res) => {
						if(res.confirm){
							this.$store.state.hasLogin = false;
							this.$store.state.userInfo = null;
							this.$store.state.busUser = null;
							uni.showToast({
								title:'退出登录成功',
								icon:'none'
							})
							uni.redirectTo({
								url:'/pages/index/index'
							})
						}
					}
				})
			}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值