uniapp微信授权登录

uniapp微信授权登录

想了好久这个问题,我朋友来一会就给我解决了,很厉害!@曹建宇

问题:

当点击按钮调用wx.authorize()方法获取用户个人信息的时候,会弹出个拒接or允许,它还没等我点击呢,就往下执行了,如下图:
uniapp微信授权登录_第1张图片

问题所在:

button按钮事件绑的是点击@click事件,要用@getuserinfo="getuserinfo"

问题解决:

正确绑定如下:
在这里插入图片描述
然后再执行
uniapp微信授权登录_第2张图片

说明这个API是同步的

uniapp微信授权登录_第3张图片

附完整代码:

<style>
	button {
		margin-top: 400rpx;
	}
</style>
<template>
	<view class="content">
		<button @getuserinfo="getuserinfo" open-type='getUserInfo' withCredentials="true">获取授权</button>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				
			}
		},
		onLoad() {
			
		},
		methods: {
			getuserinfo(){
				wx.authorize({
					scope: 'scope.userInfo',
					fail(res){
						uni.showToast({
							"title":"登录失败",
							"icon":"none"
						})
					},
					success(res){
						uni.showToast({
							"title":"登录成功"
						})
					}
				});
				//默认同步请求:执行完当前操作才能进行下面操作
				console.log("授权函数执行完毕!");
			}
		}
	}
</script>

你可能感兴趣的:(uniapp微信授权登录)