// js页面
const db = wx.cloud.database();
const user = db.collection('user')
Page({
data: {
userInfo: {},
hasUserInfo: false,
canIUseGetUserProfile: false,
},
onLoad() {
if (wx.getUserProfile) {
this.setData({
canIUseGetUserProfile: true
})
}
},
getUserProfile(e) {
wx.getUserProfile({
desc: '用于记录用户数据',
success: (res) => {
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
})
}
})
<!--wxml页面-->
<view class="container">
<view class="userinfo">
<block wx:if="{{!hasUserInfo}}">
<button wx:if="{{canIUseGetUserProfile}}" bindtap="getUserProfile"> 获取头像昵称 </button>
</block>
<block wx:else>
<image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
<text class="userinfo-nickname">{{userInfo.nickName}}</text>
</block>
</view>
</view>
需要用到云函数获取用户openid或者服务器端语言通过iv数据解密encryptedData得到openid实现用户信息和openid的绑定,客户端wx.getUserProfile不包含openid的
云函数获取openid参考
https://ask.csdn.net/questions/7612082
https://developers.weixin.qq.com/community/business/doc/000e26815e8de0db1ecae5a035b00d