const { api } = require("../../utils/api") // pages/garden/index.js Page({ /** * 页面的初始数据 */ data: { list: [], timer: null, name: '', leftList: [], rightList: [], leftHeight: 0, rightHeight: 0, hotList: [], info: null, needCare: 0, inviteCode: '' }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { const code = options.inviteCode if (code) { api('/personal/inviteCode/accept', 'GET', { inviteCode: code }) } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { this.fetchList() this.init() }, init() { const tmp = wx.getStorageSync('weather') if (tmp) { this.setData({ info: tmp }) } else { wx.getFuzzyLocation({ type: 'wgs84', success(res) { const data = { latitude: res.latitude + "", longitude: res.longitude + "" } api('/auth/getLocation', 'GET', data).then(res => { if (res.code === 200) { wx.setStorageSync('city', res.data) api('/auth/getWeather', 'GET', res.data).then(res => { if (res.code === 200) { wx.setStorageSync('weather', res.data) this.setData({info:res.data}) } }) } }) }, fail(err) { console.log(err); } }) } const inviteCode = wx.getStorageSync('inviteCode') if (inviteCode) { this.setData({ inviteCode: inviteCode }) } api('/plant/needCare', 'GET').then(res => { if (res.code === 200) { this.setData({ needCare: res.data }) } }) }, goTask() { wx.switchTab({ url: '../garden/index', }) }, 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 } if (res.data.list.length === 0) { this.fetchHotList() return } this.setData({ leftList: [], rightList: [] }) const tmps = res.data.list.map(e => { e.pic = e.imgList[0].url e.cover = e.imgList[0] return e }) this.addItems(tmps) this.setData({ list: tmps }) } }) }, fetchHotList() { api('/library/list', 'POST', { isHot: 0, current: 0, pageSize: 6 }, 'json').then(res => { if (res.code === 200) { const data = res.data.list.map(e => { e.pic = e.imgList[0].url return e }) this.setData({ hotList: data }) } }) }, addItems(newItems) { let h1 = 0; let h2 = 0; newItems.forEach((item, index) => { // 动态获取图片尺寸 if (index % 2 === 0) { this.data.leftList.push(item); h1 += item.cover.height / item.cover.width; } else { this.data.rightList.push(item); h2 += item.cover.height / item.cover.width; } this.setData({ leftList: this.data.leftList, rightList: this.data.rightList, }); }); }, goInfo(e) { console.log(e); const id = e.currentTarget.dataset.id wx.navigateTo({ url: '../garden/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() { const tmp = wx.getStorageSync('user') if (tmp.phone.length === 0) { wx.navigateTo({ url: '../login/index', }) return } wx.navigateTo({ url: '../add/index', }) }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { return { title: '发现一个宝藏养花神器', imageUrl: "https://res.catter.cn/pub/2025/12/08/20251208112039694.png", path: '/pages/index/index?inviteCode=' + this.data.inviteCode }; }, onShareTimeline() { return { title: '发现一个宝藏养花神器', imageUrl: "https://res.catter.cn/pub/2025/12/08/20251208112039694.png", path: '/pages/index/index?inviteCode=' + this.data.inviteCode }; } })