uniapp微信小程序一键登录授权获取用户信息

提示

1.当用户未授权过,调用该接口将直接报错

2.当用户授权过,可以使用该接口获取用户信息

微信授权步骤
在这里插入图片描述

授权

部分接口需要经过用户授权同意才能调用。我们把这些接口按使用范围分成多个 scope ,用户选择对 scope 来进行授权,当授权给一个 scope 之后,其对应的所有接口都可以直接使用。

此类接口调用时:

如果用户未接受或拒绝过此权限,会弹窗询问用户,用户点击同意后方可调用接口;
如果用户已授权,可以直接调用接口;
如果用户已拒绝授权,则不会出现弹窗,而是直接进入接口 fail 回调。请开发者兼容用户拒绝授权的场景。
获取用户授权设置
开发者可以使用 wx.getSetting 获取用户当前的授权状态。

打开设置界面

用户可以在小程序设置界面(「右上角」 - 「关于」 - 「右上角」 - 「设置」)中控制对该小程序的授权状态。
开发者可以调用 wx.openSetting 打开设置界面,引导用户开启授权。
提前发起授权请求
开发者可以使用 wx.authorize 在调用需授权 API 之前,提前向用户发起授权请求

<!-- 微信授权登录全程代码实现 -->
<template>
	<view>
		<view v-if="isloading">
			<!-- isloding是用来记录当前用户是否是第一次授权使用的 -->
			<view>
				<view class='header'>
					<image src='../../static/img/wx_login.png'></image>
				</view>
				<view class='content'>
					<view>申请获取以下权限</view>
					<text>获得你的公开信息(昵称,头像、地区等)</text>
				</view>

				<button class='bottom' type='primary' open-type="getUserInfo" withCredentials="true" lang="zh_CN"
					@getuserinfo="getuserinfo">
					授权登录
				</button>
			</view>
		</view>
		<!-- #endif -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				SessionKey: '',
				OpenId: '',
				nickName: null,
				avatarUrl: null,
				isloading: uni.getStorageSync('isloading') || true //默认为true
			};
		},
		methods: {
			//第一授权获取用户信息===》按钮触发
			getuserinfo() {
				let that = this;
				uni.getUserInfo({
					provider: 'weixin',
					success: function(infoRes) {
						let nickName = infoRes.userInfo.nickName; //获取用户登录昵称
						let avatarUrl = infoRes.userInfo.avatarUrl; //获取用户头像
						try {
							uni.setStorageSync('isloading', false); //记录是否第一次授权  false:表示不是第一次授权
							that.updateUserInfo();
						} catch (e) {}
					},
					fail(res) {}
				});
			}, //登录
			login() {
				let that = this;
				uni.showLoading({
					title: '登录中...'
				});

				// 1.wx获取登录用户code
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						let code = loginRes.code;
						if (!that.isloading) {
							//非第一次授权获取用户信息
							uni.getUserInfo({
								provider: 'weixin',
								success: function(infoRes) { //获取用户信息后向调用信息更新方法
									let nickName = infoRes.userInfo.nickName; //昵称
									let avatarUrl = infoRes.userInfo.avatarUrl; //头像
									that.updateUserInfo(); //调用更新信息方法
								}
							});
						}

						//2.将用户登录code传递到后台置换用户SessionKey、OpenId等信息
						uni.request({
							url: '服务器地址',//接入后端提供的登录接口
							data: {
								code: code,//传入参数code获取登录凭证
							},
							method: 'GET',
							header: {
								'content-type': 'application/json'
							},//请求头
							success: (res) => {
								//openId、或SessionKdy存储//隐藏loading
								uni.hideLoading();
							}
						});
					},
				});
			},
			//向后台更新信息
			updateUserInfo() {
				let _this = this;
				uni.request({
					url: 'url', //服务器端地址
					data: {
						appKey: this.$store.state.appKey,//Vuex封装app密钥
						customerId: _this.customerId,//自定义id
						nickName: _this.nickName,//昵称
						headUrl: _this.avatarUrl//头像
					},
					method: 'POST',
					header: {
						'content-type': 'application/json'
					},
					success: (res) => {
						if (res.data.state == "success") {
							uni.reLaunch({ //信息更新成功后跳转到小程序首页
								url: '/pages/index/index'
							});
						}
					}

				});
			}
		},
		onLoad() { //默认加载
			this.login();
		}
	}
</script>

<style>
	.header {
		margin: 90rpx 0 90rpx 50rpx;
		border-bottom: 1px solid #ccc;
		text-align: center;
		width: 650rpx;
		height: 300rpx;
		line-height: 450rpx;
	}

	.header image {
		width: 200rpx;
		height: 200rpx;
	}

	.content {
		margin-left: 50rpx;
		margin-bottom: 90rpx;
	}

	.content text {
		display: block;
		color: #9d9d9d;
		margin-top: 40rpx;
	}

	.bottom {
		border-radius: 80rpx;
		margin: 70rpx 50rpx;
		font-size: 35rpx;
	}
</style>

欢迎关注博主,为你解忧疑难!!

  • 7
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
uniapp微信小程序一键登录可以通过使用button组件中的open-type属性来实现。具体步骤如下: 1. 在button组件中设置open-type属性为getUserInfo,同时绑定getuserinfo事件,例如: ```html <button type="default" open-type="getUserInfo" @getuserinfo="wxLogin">一键登录微信小程序</button> ``` 2. 在对应的方法wxLogin中,可以通过event参数获取到用户的信息,包括用户的头像、昵称等。可以将这些信息传递给后端进行处理。 另外,如果需要获取用户的手机号信息,可以使用open-type属性为getPhoneNumber,并绑定getphonenumber事件。具体步骤如下: 1. 在button组件中设置open-type属性为getPhoneNumber,同时绑定getphonenumber事件,例如: ```html <button shape="circle" type="primary" link="true" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信一键登录</button> ``` 2. 在对应的方法getPhoneNumber中,可以通过event参数获取到用户的手机号信息。同样,可以将这些信息传递给后端进行处理。 需要注意的是,为了保证前后端的appid一致,以及确保使用的appid是经过认证的,避免出现错误或调用不通的情况。 #### 引用[.reference_title] - *1* [uni-app实现微信小程序一键登录](https://blog.csdn.net/qq_45797421/article/details/118339987)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [uni-app中使用微信一键登录](https://blog.csdn.net/weixin_49296337/article/details/124755651)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@逆风boy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值