用的好好的小程序突然間不能獲取用戶(hù)信息,本以為寫(xiě)法有問(wèn)題,嘗試以前的項(xiàng)目還是不行,新建一個(gè)項(xiàng)目發(fā)現(xiàn),api改了!
以前使用的open-type=getUserInfo已經(jīng)不再好使了,只能獲取默認(rèn)信息,看下官方回復(fù)
不推薦使用getUserInfo獲取用戶(hù)信息,預(yù)計(jì)自2021年4月13日起,getUserInfo將不再?gòu)棾鰪棿?,并直接返回匿名的用?hù)個(gè)人信息
推薦使用wx.getUserProfile獲取用戶(hù)信息,開(kāi)發(fā)者每次通過(guò)該接口獲取用戶(hù)個(gè)人信息均需用戶(hù)確認(rèn),開(kāi)發(fā)者妥善保管用戶(hù)快速填寫(xiě)的頭像昵稱(chēng),避免重復(fù)彈窗
可以看到官方推我們使用getUserProfile去獲取用戶(hù)信息,嘗試一下
//index.html
<!--index.wxml-->
<view class="container">
<view class="userinfo">
<block wx:if="{{!hasUserInfo}}">
<button bindtap="getUserProfile"> 獲取頭像昵稱(chēng) </button>
</block>
<block wx:else>
<image class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
<text class="userinfo-nickname">{{userInfo.nickName}}</text>
</block>
</view>
</view>
// index.js
// 獲取應(yīng)用實(shí)例
const app = getApp()
Page({
data: {
userInfo: {},
hasUserInfo: false,
canIUseGetUserProfile: false,
},
onLoad() {
if (wx.getUserProfile) {
this.setData({
canIUseGetUserProfile: true
})
}
},
getUserProfile(e) {
// 推薦使用wx.getUserProfile獲取用戶(hù)信息,開(kāi)發(fā)者每次通過(guò)該接口獲取用戶(hù)個(gè)人信息均需用戶(hù)確認(rèn),開(kāi)發(fā)者妥善保管用戶(hù)快速填寫(xiě)的頭像昵稱(chēng),避免重復(fù)彈窗
wx.getUserProfile({
desc: '展示用戶(hù)信息', // 聲明獲取用戶(hù)個(gè)人信息后的用途,后續(xù)會(huì)展示在彈窗中,請(qǐng)謹(jǐn)慎填寫(xiě)
success: (res) => {
console.log(res)
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
})
}
})
改了之后總體來(lái)看是比較有好的,可以不用再必須使用button按鈕觸發(fā)了,也不用再使用open-type