一、2021年4月小程序登錄、用戶信息相關接口調整處理
官方參考鏈接:
https://developers.weixin.qq.com/community/develop/doc/000cacfa20ce88df04cb468bc52801
- 2021年4月28日24時後發佈的小程序新版本,無法通過wx.getUserInfo與<button open-type="getUserInfo"/>獲取用戶個人信息(頭像、暱稱、性別與地區),將直接獲取匿名數據(包括userInfo與encryptedData中的用戶個人信息),獲取加密後的openID與unionID數據的能力不做調整。此前發佈的小程序版本不受影響,但如果要進行版本更新則需要進行適配。
- 新增getUserProfile接口(基礎庫2.10.4版本開始支持),可獲取用戶頭像、暱稱、性別及地區信息,開發者每次通過該接口獲取用戶個人信息均需用戶確認。具體接口文檔:《getUserProfile接口文檔》
二、小程序頁面中處理
1.wxml: bindtap="getUserInfo"
<view class="empower">
<button class="grant" bindtap="getUserInfo">授權</button>
<!-- <button class="grant" open-type="getUserInfo" bindgetuserinfo="getUserInfo">授權</button> -->
<navigator class="cancel" url="/pages/index/index" open-type="reLaunch">取消授權</navigator>
</view>
2.js:
//登陸處理
getUserInfo: function () {
var _this = this;
var info = {};
//獲取配置
wx.getUserProfile({
desc: '用於登錄使用',
success: function (data) {
var userInfo = data.userInfo;
//獲取code
wx.login({
success: res => {
userInfo.code = res.code;
userInfo.recommendID = wx.getStorageSync("RecommendID");
var RecommendID = wx.getStorageSync("RecommendID");
userInfo.RecommendID = RecommendID;
wx.showLoading({
title: '正在登錄',
})
//後臺登錄驗證,此處去後臺解密獲取用戶信息
account.checkUser(userInfo, res => {
//初始化位置操作
account.initLocation();
wx.showToast({
title: '登錄成功',
success: () => {
// wx.reLaunch({
// url: '/pages/index/index',
// })
wx.navigateBack({})
}
})
});
}
})
},
fail: function (e) {
console.info(e);
if (e.errMsg.indexOf('deny') != -1) {
//wx.openSetting();
wx.showModal({
title: '登陸失敗',
content: '授權失敗,將影響部分高級功能體驗哦',
showCancel: false,
success: res => {}
})
}
}
});
},
獲取到用戶資料:
更多: