uni 小程序登录,授权登录模板,授权登录失败

18 篇文章 1 订阅
16 篇文章 0 订阅

记录下,之前一直拿到code后就不管了,后来加上iv、encryptedData 给后台,但是有时候解密会报错,就会显示授权登录失败,最后找到原因是先获取用户信息导致的

1.点击之后先登录获取code
2.获取code后再获取用户信息
3.传递数据
备注:先调用了getUserInfo获取了解密数据,然后又调用的login,就会刷新登录态,导致登录失败

<template>
	<view class="page">
		<view class="tk-column  ">
			<view class="title">
				<image src="../../static/logo.jpg" class="logo" mode="aspectFill"></image>
			</view>
			<view class="login-btn-box tk-column">
				<button class="btn-tk" @getuserinfo="do_login" open-type="getUserInfo">登录</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				wx_code: '',
			}
		},
		methods: {
			do_login(e) {
				let infoRes = e.detail;
				var that = this;


				//获取用户授权
				uni.getProvider({
					service: 'oauth',
					success: function(res) {
						// console.log(res);
						//微信端
						if (~res.provider.indexOf('weixin')) {
							console.log("weixin login");
							//微信登录
							uni.login({
								provider: 'weixin',
								success: function(loginRes) {
									console.log('loginRes.code---------' + loginRes.code);
									that.wx_code = loginRes.code;
									uni.getUserInfo({
										provider: 'weixin',
										withCredentials: true,
										success: function(userRes) {
											console.log(userRes);

											that.post_login(userRes);//这里拿最新一次获取的
										},
										fail: function(userRes) {

										}
									})

								},
								fail: function(loginRes) {
									console.log(loginRes);
								}
							});
						}
					}
				});
			},

			post_login(e) {
				// console.log(e);
				var that = this;
				let data = {
					method: 'user.nflogin',
					code: this.wx_code,
					edata: e.encryptedData,
					iv: e.iv
				};
				that.$api.post('', data).then(res => {
					let data = res.data;
					// console.log(res);
					uni.showToast({
						title: data.msg,
						icon: "none"
					});
					if (data.status) {
						// console.log(data.code);
						// console.log(data.data.token)
						// that.wx_getuser_show=false 
						//保存登录状态 
						// this.$store.state.hasLogin = true;
						uni.setStorageSync('token', data.data.token);
						uni.setStorageSync('hasLogin', true);
						that.get_userinfo();
					}

				})
			},
			//获取用户信息
			get_userinfo() {
				// console.log('get user info');
				var that = this;
				let data = {
					method:login,
					token: token,
				};
				that.$api.post('', data).then(res => {
					let data = res.data.data;
					uni.setStorageSync('userInfo', data);
					// console.log(data);
					uni.navigateBack({

					})


				})
			}

		}
	}
</script>


<style scoped lang="scss">
	.page {
		padding: 15upx;
		height: 100%;
	}

	.title {
		font-weight: bold;
		font-size: 46upx;
		text-align: center;
		line-height: 4rem;
	}

	.tk-column {}

	.login-btn-box {
		width: 100%;
		height: 300upx;
		align-self: center;
		justify-content: center;

		.btn-tk {
			align-items: center;
		}
	}

	.logo {
		width: 100%;
	}
</style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值