昨天看了三個獲取用戶信息的接口,但是挺奇怪的,貌似是要跟其他接口配合使用
所以今天來看另外三個(后來發(fā)現(xiàn)其實就是一個wx.canIUse,這里注意一下,只不過為了可讀性,我們可以寫一寫明確的訪問請求)微信接口
- wx.canIUse
- canIUseGetUserProfile
- canIUseOpenData
為什么今天看這三個呢?
<block wx:if="{{canIUseOpenData}}">
<view class="userinfo-avatar" bindtap="bindViewTap">
<open-data type="userAvatarUrl"></open-data>
</view>
<open-data type="userNickName"></open-data>
</block>
<block wx:elif="{{!hasUserInfo}}">
<button wx:if="{{canIUseGetUserProfile}}" bindtap="getUserProfile"> 獲取頭像昵稱 </button>
<button wx:elif="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 獲取頭像昵稱 </button>
<view wx:else> 請使用1.4.4及以上版本基礎庫 </view>
</block>
<block wx:else>
<image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
<text class="userinfo-nickname">{{userInfo.nickName}}</text>
</block>
這里有個,if,elseif,else結構,通過判斷canIUseGetUerProfile和另外兩個接口的布爾值來決定后續(xù)要不要使用上一篇介紹的那三個獲取用戶信息的接口。
然后看對應的Typescript部分。
canIUse: wx.canIUse('button.open-type.getUserInfo'),
canIUseGetUserProfile: false,
// canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需嘗試獲取用戶信息可改為false
然后再看文檔中關于
boolean wx.canIUse(string schema)的部分
其中參數(shù)部分的寫法為
${API}.${method}.${param}.${option} 或者 ${component}.${attribute}.${option}
參數(shù)說明
-
${API}代表 API 名字 -
${method}代表調用方式,有效值為return, success, object, callback -
${param}代表參數(shù)或者返回值 -
${option}代表參數(shù)的可選值或者返回值的屬性 -
${component}代表組件名字 -
${attribute}代表組件屬性 -
${option}代表組件屬性的可選值
這樣就對上了,一種學習思路