const chapters = require('../../data/chapters') const { getProgress, saveProgress, resetStoryProgress, getSettings, } = require('../../utils/storage') const { chapterRoute } = require('../../utils/chapterRoute') Page({ data: { chapters: [], fontScale: 'large', catalogListEnded: false, }, onShow() { const progress = getProgress() const settings = getSettings() const completed = new Set(progress.completedChapters) const lastChapter = Number(progress.lastChapter) || 1 const completedHotspots = progress.completedHotspots || {} this.setData({ chapters: chapters.map((chapter) => ({ ...chapter, completed: completed.has(chapter.chapterId), current: chapter.number === lastChapter, readingLabel: completed.has(chapter.chapterId) ? '重新翻看这一回' : chapter.number === lastChapter ? '上次读到这里' : '翻开这一回', eventCount: Array.isArray(completedHotspots[chapter.chapterId]) ? completedHotspots[chapter.chapterId].length : 0, })), lastChapter, fontScale: settings.fontScale === 'xlarge' ? 'xlarge' : 'large', catalogListEnded: false, }) }, markListEnd() { if (!this.data.catalogListEnded) { this.setData({ catalogListEnded: true }) } }, openChapter(event) { const chapter = Number(event.currentTarget.dataset.chapter) const progress = getProgress() const chapterMeta = chapters.find((item) => item.number === chapter) const replay = Boolean( chapterMeta && progress.completedChapters.includes(chapterMeta.chapterId), ) progress.lastChapter = chapter saveProgress(progress) wx.navigateTo({ url: chapterRoute(chapter, { replay }), }) }, restartStory() { wx.showModal({ title: '从第一回重新开始?', content: '关卡进度会清空;已经收藏的“我的桂香岁月”和大字设置会保留。', confirmText: '重新开始', cancelText: '先不清空', confirmColor: '#9f3028', success(result) { if (!result || !result.confirm) return if (!resetStoryProgress()) { wx.showToast({ title: '进度暂时没有清空,请稍后再试', icon: 'none', }) return } wx.redirectTo({ url: chapterRoute(1) }) }, }) }, goBack() { wx.reLaunch({ url: '/pages/home/home' }) }, onShareAppMessage() { return { title: '唐侦探:翻开十五回桂香故事', path: '/pages/catalog/catalog', } }, onShareTimeline() { return { title: '唐侦探:翻开十五回桂香故事', query: '', } }, })