一.获取用户openID
WXML:
<view class="container">
<view class="userinfo">
<block wx:if="{{!hasUserInfo}}">
<button wx:if="{{canIUseGetUserProfile}}" bindtap="getUserProfile"> 获取头像昵称 </button>
<button wx:else open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 获取头像昵称 </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>
js:
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
})
}
})
},
})
页面产生点击事件时调用,每次请求都会弹出授权窗口,用户同意后返回 userInfo
二.根据用户openID获取数据
getmyinfo:function(){
let that=this
db.collection('POST').where(_.or([
{openID:res.result.openid}, //判断是否是组织创建者
]))
.get({
success(res){
console.log("请求成功",res)
that.setData({
datalist: res.data
})
},
fail(res){
console.log("请求失败",res)
}
})
},
由于数据库中存放的是FIleID,因此需要在获得数据后再根据FilID将信息展示出来。
三.显示信息
WXML:
<view wx:for='{{testList}}' wx:for-item="item">
<image src="{{item.postimages}}"></image>
</view>
js:
const db = wx.cloud.database()
const $ = db.command.aggregate
const _ = db.command
Page({
data: {
testList:[],
},
onLoad: function() {
//获取云端数据库的数据
const db = wx.cloud.database()
db.collection('POST').get({
success:res=> {
console.log(res.data)
this.setData({
testList:res.data,
})
},
fail: console.error
})
}
})
如此就可以将存储中的图片显示出来