uni-app微信小程序微信登录(获取头像、个性签名等基本信息)

1.思路

微信小程序用微信授权登录的思路:官方在线文档
1.前端调用调uni.login() 获取临时登录凭证code ,并将获取的用户信息和code回传到后端;
2.后端调用 auth.code2Session 接口,换取 用户唯一标识 OpenID 、 用户在微信开放平台帐号下的唯一标识UnionID(若当前小程序已绑定到微信开放平台帐号) 和 会话密钥 session_key,然后后端根据openID和unionID生成特有的用户信息。

2.方法

因原有的方法获取的用户信息为灰度信息,及用户的信息包括头像、用户名等都不是用户的真实信息,getUserInfo改成getUserProfile接口。

<button v-if="canIUseGetUserProfile" @tap="getUserProfile" class="imgBox">
	<image class="ImgL" src="/static/icon/wechat.png" mode="aspectFit">
</button>
<button v-else open-type="getUserInfo" @getuserinfo="getUserInfo" class="imgBox">
	<image class="ImgL" src="/static/icon/wechat.png" mode="aspectFit">
</button>
data() {
	return {
		// 微信获取头像能力
		canIUseGetUserProfile: false,
		// 是否弹窗提示授权获取用户头像,防止频繁获取影响用户体验
		isGetUserProfile: false,
	};
},
onLoad() {
			// 判断此用户微信登录是否成功过
			if (uni.getStorageSync("isGetUserProfile") == true) {
				this.isGetUserProfile = true
			}
			// #ifdef MP-WEIXIN
			if (uni.getUserProfile) {
				if (this.isGetUserProfile == true) {
					this.canIUseGetUserProfile = false
				} else {
					this.canIUseGetUserProfile = true
				}
			}
			// #endif
		},
// #ifdef MP-WEIXIN
getUserProfile(e) {
	if (this.isGetUserProfile == false) {
		console.log("用于完善会员资料")
		uni.getUserProfile({
			desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
			success: (res) => {
				console.log(res.userInfo)   //用户基本信息(头像、个性签名等)
				this.userInfo = res.userInfo
				this.wxSilentLogin()  // 具体登录操作,参照uni.login 
			}
		})
	} 
},
// #endif

uni.login介绍官网

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值