内容详情 您现在的位置是: 首页> Js
小程序登录、用户信息相关接口调整说明
发布时间:2021-01-22 19:30 已围观:2659
摘要回收wx.getUserInfo接口可获取用户授权的个人信息能力,使用 getUserProfile 接口替换
回收wx.getUserInfo接口可获取用户授权的个人信息能力,使用 getUserProfile 接口替换,官方文档如下:
https://developers.weixin.qq.com/community/develop/doc/000cacfa20ce88df04cb468bc52801?blockType=1
login.wxml
<view class="login_wrap">
<view class="img">
<image src="../../images/login-bg.png"></image>
</view>
<view class="tit1">你还没有登录哦</view>
<view class="tit2">登录立即开启新世界</view>
<view class="login" wx:if="{{canIUseGetUserProfile}}">
<button bindtap="getUserProfile">登录</button>
</view>
<view class="login" wx:else>
<button bindgetuserinfo="getUserInfo" openType="getUserInfo" wx:if="{{canIUse}}">登录</button>
<view style="font-size:16px;" wx:else>请升级微信版本</view>
</view>
</view>
login.js
data: {
canIUse: wx.canIUse("button.open-type.getUserInfo"),
canIUseGetUserProfile: wx.getUserProfile?true:false,//用户版本是否支持 新的获取用户信息函数
},
getUserProfile(t) {
var a = this;
wx.getUserProfile({
desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
success: (res) => {
console.log(res);
var userProfileData=res;
wx.login({
success: function(t) {
console.log(t)
var e = t.code;
wx.getSetting({
success: function(t) {
console.log(t),
console.log(userProfileData.userInfo),
dataApi.login({
code: e,
encryptedData: userProfileData.encryptedData,
iv: userProfileData.iv
}).then(function(t) {
console.log('dataApi',t)
wx.setStorageSync("uid", t.data.uid),
dataApi.userinfo({
uid: t.data.uid,
op: "getinfo"
}).then(function(t) {
console.log(t),
wx.setStorageSync("userinfo", t.data.info),
console.log(t.data.info.phone),
wx.reLaunch({
url: "../home/home"
});
}).catch(function(t) {
console.log(t);
});
}).catch(function(t) {
console.log(t), 1 == t.errno && wx.showToast({
icon: "none",
title: t.message
});
}), a.userInfoReadyCallback && a.userInfoReadyCallback(t);
}
});
}
})
},
fail :(res) => {
wx.showToast({
title: "授权失败",
icon: "none",
duration: 2e3
})
}
})
},
getUserInfo: function(t) {
var a = this;
t.detail.userInfo ? (wx.showLoading({
title: "加载中..."
}), wx.login({
success: function(t) {
var e = t.code;
console.log(t)
wx.getSetting({
success: function(t) {
console.log(t);
// t.authSetting["scope.userInfo"] && wx.getUserInfo({
wx.getUserInfo({
success: function(t) {
console.log(t);
t.userInfo, dataApi.login({
code: e,
encryptedData: t.encryptedData,
iv: t.iv
}).then(function(t) {
wx.setStorageSync("uid", t.data.uid), dataApi.userinfo({
uid: t.data.uid,
op: "getinfo"
}).then(function(t) {
console.log(t),
wx.setStorageSync("userinfo", t.data.info),
console.log(t.data.info.phone),
wx.reLaunch({
url: "../home/home"
});
}).catch(function(t) {
console.log(t);
}), wx.hideLoading();
}).catch(function(t) {
wx.hideLoading(), console.log(t), 1 == t.errno && wx.showToast({
icon: "none",
title: t.message
});
}), a.userInfoReadyCallback && a.userInfoReadyCallback(t);
}
});
}
});
}
})) : wx.showToast({
title: "授权失败",
icon: "none",
duration: 2e3
});
},
赞一个 (11)
上一篇: web前端使用腾讯云对象存储 COS
下一篇: 微信小程序支付流程操作及源码讲解