feat: 任务和社区页面
This commit is contained in:
+67
-9
@@ -1,16 +1,23 @@
|
||||
// pages/garden/index.js
|
||||
import { MOCK_PLANTS } from '../../utils/mockData';
|
||||
import request from '../../utils/request';
|
||||
|
||||
Page({
|
||||
data: {
|
||||
plants: [],
|
||||
dateString: '',
|
||||
greeting: ''
|
||||
greeting: '',
|
||||
|
||||
// Pagination
|
||||
currentPage: 1,
|
||||
pageSize: 6,
|
||||
total: 0,
|
||||
isLastPage: false,
|
||||
isLoading: false
|
||||
},
|
||||
|
||||
onLoad(options) {
|
||||
this.initTime();
|
||||
this.loadPlants();
|
||||
this.loadPlants(true);
|
||||
},
|
||||
|
||||
onShow() {
|
||||
@@ -20,12 +27,62 @@ Page({
|
||||
selected: 0
|
||||
})
|
||||
}
|
||||
// Refresh list in case new plant was added
|
||||
this.loadPlants();
|
||||
// Refresh list on show to ensure data is up-to-date
|
||||
// We use reset=true to reload from page 1
|
||||
this.loadPlants(true);
|
||||
},
|
||||
|
||||
loadPlants() {
|
||||
this.setData({ plants: MOCK_PLANTS });
|
||||
// Pull to refresh
|
||||
onPullDownRefresh() {
|
||||
this.loadPlants(true).then(() => {
|
||||
wx.stopPullDownRefresh();
|
||||
});
|
||||
},
|
||||
|
||||
// Infinite scroll
|
||||
onReachBottom() {
|
||||
if (!this.data.isLastPage && !this.data.isLoading) {
|
||||
this.loadPlants(false);
|
||||
}
|
||||
},
|
||||
|
||||
loadPlants(reset = false) {
|
||||
if (this.data.isLoading) return Promise.resolve();
|
||||
|
||||
this.setData({ isLoading: true });
|
||||
|
||||
const page = reset ? 1 : this.data.currentPage + 1;
|
||||
const payload = {
|
||||
current: page,
|
||||
pageSize: this.data.pageSize,
|
||||
keyword: ''
|
||||
};
|
||||
|
||||
return request.post('/plant/page', payload).then(res => {
|
||||
const list = res.list || [];
|
||||
const total = res.total || 0;
|
||||
|
||||
// Map backend data to UI structure: 1. Map imgList to images array with path resolution
|
||||
const mappedList = list.map(item => {
|
||||
let imageUrl = '';
|
||||
if (item.imgList && item.imgList.length > 0) {
|
||||
imageUrl = item.imgList[0].url;
|
||||
}
|
||||
return { ...item, images: [imageUrl] };
|
||||
});
|
||||
|
||||
this.setData({
|
||||
plants: reset ? mappedList : [...this.data.plants, ...mappedList],
|
||||
currentPage: page,
|
||||
total: total,
|
||||
isLastPage: (reset ? mappedList.length : this.data.plants.length + mappedList.length) >= total,
|
||||
isLoading: false
|
||||
});
|
||||
}).catch(err => {
|
||||
console.error('Load plants failed', err);
|
||||
this.setData({ isLoading: false });
|
||||
wx.showToast({ title: '加载失败', icon: 'none' });
|
||||
});
|
||||
},
|
||||
|
||||
initTime() {
|
||||
@@ -66,7 +123,8 @@ Page({
|
||||
},
|
||||
|
||||
onScrollLower() {
|
||||
console.log('Scroll to lower - loading more plants...');
|
||||
// In a real app, this would trigger pagination
|
||||
if (!this.data.isLastPage && !this.data.isLoading) {
|
||||
this.loadPlants(false);
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user