const pages = require('../../mixin/pages') const { getProductCategoryList } = require('../../api/common') let leftHeight = 0 let rightHeight = 0 let query = wx.createSelectorQuery() Page({ /** * 页面的初始数据 */ data: { ...pages.data(), listUrl: '/api/user/caigou/product/list', searchForm: { key_words: '', category_id: '' }, categoryList: [], navScrollLeft: -1, leftList: [], rightList: [] }, ...pages.methods, /** * 生命周期函数--监听页面加载 */ async onLoad(options) { await this.fetchProductCategoryList() this.fetchOrderList() }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { if (this.data.freshing) { return } this.setData({ freshing: true }) this.bindCallBack() }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { this.fetchOrderList() }, bindCallBack() { this.refreshOrderList() }, async fetchProductCategoryList() { try { const { status, data, msg } = await getProductCategoryList() if (status && Array.isArray(data) && data.length > 0) { const temp = data.map(item => { return { ...item, text: item.category_name } }) temp.unshift({ category_img_url: 'https://lanman-shining.oss-cn-hangzhou.aliyuncs.com/images/user/bashi635cd28a12481.png', category_name: '全部', id: 0 }) this.setData({ categoryList: temp, 'searchForm.category_id': temp[0].id }) } else { wx.showToast({ title: msg, icon: 'none' }) } } catch (err) {} }, handleNav(e) { const { item } = e.currentTarget.dataset if (this.data.searchForm.category_id !== item.id) { this.setData({ 'searchForm.category_id': item.id, navScrollLeft: 0 }, () => { this.refreshOrderList() }) } }, async isLeft(list) { const { leftList, rightList } = this.data for (const item of list) { if (leftHeight < rightHeight) { leftList.push(item) } else if (leftHeight === rightHeight) { leftList.length <= rightList ? leftList.push(item) : rightList.push(item) } else { rightList.push(item) } await this.getBoxHeight(leftList, rightList) } }, getBoxHeight(leftList, rightList) { return new Promise((resolve, reject) => { this.setData({ leftList, rightList }, () => { query.select('.waterfall-left').boundingClientRect() query.select('.waterfall-right').boundingClientRect() query.exec((res) => { leftHeight = res[0].height rightHeight = res[1].height resolve() }) }) }) } })