const { api } = require("../../utils/api") // pages/garden/index.js Page({ /** * 页面的初始数据 */ data: { list: [], timer: null, name: '', leftList: [], rightList: [], leftHeight: 0, rightHeight: 0, }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { this.setData({leftList:[],rightList:[]}) this.fetchList() }, fetchList() { api('/plant/page', 'POST', { current: 0, pageSize: 999, name: this.data.name }, 'json').then(res => { if (res.code == 200) { if (res.data === null) { return } const tmps = res.data.list.map(e => { e.pic = e.imgList[0].url return e }) this.addItems(tmps) // this.setData({ // list: tmps // }) } }) }, addItems(newItems) { newItems.forEach(item => { // 动态获取图片尺寸 wx.getImageInfo({ src: item.pic, success: res => { const scale = res.width / res.height; const imgHeight = 345 / scale; // 假设宽度是 345rpx if (this.data.leftHeight <= this.data.rightHeight) { this.data.leftList.push(item); this.data.leftHeight += imgHeight; } else { this.data.rightList.push(item); this.data.rightHeight += imgHeight; } this.setData({ leftList: this.data.leftList, rightList: this.data.rightList, }); } }); }); }, goInfo(e){ const id = e.currentTarget.dataset.id wx.navigateTo({ url: '../index/info?id=' + id, }) }, onchange(e) { const value = e.detail.value if (this.data.timer != null) { clearTimeout(this.data.timer) } this.data.timer = setTimeout(() => { this.setData({ name: value }) this.fetchList() }, 500) }, goAdd() { wx.navigateTo({ url: '../add/index', }) }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })