仅供大家参考学习
1、先说明一下官方:为优化用户体验,使用 wx.getUserInfo 接口直接弹出授权框的开发方式将逐步不再支持。
小程序与小游戏获取用户信息接口调整,请开发者注意升级。
2、使用 button 组件,并将 open-type 指定为 getUserInfo 类型,获取用户基本信息。文档
3、实现思路:写一个微信授权登录按钮让用户实现点击的功能,也就是实现了通过 button 组件去触发 getUserInof 接口。在用户进入微信小程序的时候,判断用户是否授权了,如果没有授权的话就显示授权按钮,让用户去执行授权的操作。如果已经授权了,则隐藏这个按钮,进入首页。
4、小程序源码说明
wx:if="{ {is_auth == 0}}"
class='auth-btn'
open-type="getUserInfo"
lang="zh_CN"
bindgetuserinfo="onGotUserInfo">
/**index.wxss**/.auth-btn{
width: 100%;
height: 100%;
position: fixed;
top: 0;
left: 0;
z-index: 99;
background-color: rgba(255, 255, 255, 0)
}
.auth-btn::after{
border: none;
}//index.js
var app = getApp();
Page({
data: {
is_auth: app.globalData.is_auth, //判断是否登录
},
onLoad: function (options) {
var that = this;
setTimeout(function () {
if (app.globalData.userInfo['NickName'] != 'undefined' || app.globalData.userInfo['HeadUrl'] != 'undefined') {
that.setData({
is_auth: 1,//判断是否登录
nickName: app.globalData.userInfo['NickName'],
HeadUrl: app.globalData.userInfo.HeadUrl,
});
}
}, 1000);
},
//授权用户信息
onGotUserInfo: function (e) {
//console.log(app.globalData.userInfo)
//console.log(app.globalData.is_auth)
var that = this;
if (e.detail.userInfo !== undefined) {
let info = e.detail.userInfo;
wx.request({
url: app.d.hostUrl + '/Api/Login/authlogin',
data: {
gender: info.gender,
NickName: info.nickName,
HeadUrl: info.avatarUrl,
openid: app.globalData.userInfo.openid,
save_user: 'save_user' //保存用户信息
},
header: {
'Content-Type': 'application/x-www-form-urlencoded'
},
m