106 lines
5.1 KiB
JavaScript
106 lines
5.1 KiB
JavaScript
const { withDataCache } = require('../../utils/page-cache');
|
|
const { getLayout } = require('../../utils/layout');
|
|
// pages/discover/discover.js
|
|
const { api } = require('../../utils/request');
|
|
const { diaryEntry, albumEntry, memoryEntry } = require('../../utils/journal');
|
|
const { saveScrollPosition, resetScrollOnTabShow } = require('../../utils/scroll-pos');
|
|
|
|
Page(withDataCache({
|
|
onResize: function() { this.setData(getLayout()); },
|
|
data: {
|
|
stats: null, loading: true, loadError: false, refreshing: false, publishOpen: false,
|
|
activeFilter: 'diary', filters: [{value:'diary',label:'日记',icon:'stat-book'}, {value:'album',label:'相册',icon:'stat-album'}, {value:'memory',label:'记忆日',icon:'stat-timeline'}],
|
|
entries: [], albums: [], memories: [],
|
|
scrollTop: 0
|
|
},
|
|
|
|
onLoad: function() {
|
|
this.setData(getLayout());
|
|
this.setData({ scrollTop: 0 });
|
|
},
|
|
onShow: function() {
|
|
resetScrollOnTabShow(this, 'discover');
|
|
const tabBar = this.getTabBar && this.getTabBar();
|
|
if (tabBar) tabBar.syncActive();
|
|
this._syncPublishTabBar();
|
|
this.loadStats();
|
|
},
|
|
|
|
// 保存滚动位置
|
|
onScroll: function(e) {
|
|
this._scrollTop = e.detail.scrollTop;
|
|
saveScrollPosition('discover', e.detail.scrollTop);
|
|
},
|
|
|
|
onPullDownRefresh: async function() {
|
|
await this.loadStats(true);
|
|
wx.stopPullDownRefresh();
|
|
},
|
|
|
|
loadStats: async function() {
|
|
if (this._loading) return;
|
|
this._loading = true;
|
|
this.setData({loading: !this._hasCachedData, loadError:false});
|
|
|
|
try {
|
|
const [timeline, album, diary] = await Promise.all([
|
|
api.timelineList(), api.albumList(), api.diaryList()
|
|
]);
|
|
var photoCount = 0;
|
|
if (Array.isArray(album)) {
|
|
for (var i = 0; i < album.length; i++) {
|
|
photoCount += parseInt(album[i].photo_count || 0);
|
|
}
|
|
}
|
|
var stats = {
|
|
timeline: Array.isArray(timeline) ? timeline.length : 0,
|
|
album: photoCount,
|
|
diary: (diary && diary.total) ? diary.total : 0,
|
|
};
|
|
this.setData({ stats: stats, entries: ((diary && diary.items) || []).map(diaryEntry), albums: (Array.isArray(album) ? album : []).map(albumEntry), memories: (Array.isArray(timeline) ? timeline : []).map(memoryEntry) });
|
|
} catch (e) {
|
|
console.error(e);
|
|
this.setData({loadError:true});
|
|
} finally { this._loading = false; this.setData({loading:false}); }
|
|
},
|
|
|
|
onRefresh: async function() { this.setData({refreshing:true}); try { await this.loadStats(); } finally { this.setData({refreshing:false}); } },
|
|
selectFilter: function(e) { this.setData({activeFilter:e.currentTarget.dataset.filter}); },
|
|
_syncPublishTabBar: function() {
|
|
const tabBar = this.getTabBar && this.getTabBar();
|
|
if (tabBar && tabBar.setHidden) tabBar.setHidden(this.data.publishOpen);
|
|
},
|
|
showPublish: function() { this.setData({publishOpen:true}); this._syncPublishTabBar(); },
|
|
hidePublish: function() { this.setData({publishOpen:false}); this._syncPublishTabBar(); },
|
|
onHide: function() { this.hidePublish(); },
|
|
noop: function() {}, preventMove: function() {},
|
|
_publish: function(type) { this.hidePublish(); this._savePosBeforeLeave(); wx.navigateTo({url:'/pages/publish/publish?type=' + type}); },
|
|
writeDiary: function() { this._publish('diary'); },
|
|
uploadPhotos: function() { this.hidePublish(); this._savePosBeforeLeave(); wx.navigateTo({url:'/pages/albums/albums?upload=1'}); },
|
|
createAlbum: function() { this._publish('album'); },
|
|
createMemory: function() { this._publish('timeline'); },
|
|
openJournalEntry: function(e) { this._savePosBeforeLeave(); wx.navigateTo({url:'/pages/diary_detail/diary_detail?id=' + e.currentTarget.dataset.id}); },
|
|
openAlbum: function(e) { this._savePosBeforeLeave(); wx.navigateTo({url:'/pages/album/album?album_id=' + e.currentTarget.dataset.id}); },
|
|
// 跳转前保存当前滚动位置
|
|
_savePosBeforeLeave: function() {
|
|
saveScrollPosition('discover', this._scrollTop == null ? this.data.scrollTop : this._scrollTop);
|
|
},
|
|
|
|
// Tab 导航(替换,不留栈)
|
|
goHome: function() { this._savePosBeforeLeave(); wx.switchTab({ url: '/pages/index/index' }); },
|
|
goToDiaryFromDiscover: function() { this._savePosBeforeLeave(); wx.navigateTo({ url: '/pages/diary/diary?from=discover' }); },
|
|
goToProfile: function() { this._savePosBeforeLeave(); wx.switchTab({ url: '/pages/profile/profile' }); },
|
|
|
|
// 功能入口(推入栈,支持左滑返回)
|
|
goToTimeline: function() { this._savePosBeforeLeave(); wx.navigateTo({ url: '/pages/timeline/timeline' }); },
|
|
goToAlbum: function() { this._savePosBeforeLeave(); wx.navigateTo({ url: '/pages/albums/albums' }); },
|
|
goToSongs: function() { this._savePosBeforeLeave(); wx.navigateTo({ url: '/pages/lovewords/lovewords' }); },
|
|
goToCheckin: function() { this._savePosBeforeLeave(); wx.navigateTo({ url: '/pages/punch/punch' }); },
|
|
goToPet: function() { this._savePosBeforeLeave(); wx.navigateTo({ url: '/pages/pet/pet' }); },
|
|
goToPeriod: function() {
|
|
this.hidePublish();
|
|
this._savePosBeforeLeave();
|
|
wx.navigateTo({ url: '/pages/period/period' });
|
|
}
|
|
}, {"loadStats": ["timelineList", "albumList", "diaryList"]}));
|