const memoryCards = require('../../data/memoryCards') const { getProgress } = require('../../utils/storage') const { getCollectedMemories } = require('../../utils/memoryCollection') const { chapterRoute } = require('../../utils/chapterRoute') const bridge = require('../../utils/platformBridge') const STATUS_TEXT = { guest: '游客阅读 · 仅存本机', offline: '离线存档 · 点此重试', pending: '本机已保存 · 等待同步', connecting: '正在连接存档…', syncing: '本机已保存 · 正在同步', synced: '阅读存档已同步', conflict: '存档有冲突 · 点此选择', 'auth-expired': '登录已失效 · 本机记录保留', 'storage-error': '本机未存成功 · 请检查空间', 'version-error': '存档版本不一致 · 仅本机阅读', 'sync-error': '存档未同步 · 请更新后重试', } const { getHomeLayoutMetrics, getHomeLayoutStyle, hasReadingProgress, } = require('./homeLayout') Page({ data: { tangStatusText: '正在读取存档…', coverOpened: false, completedCount: 0, lastChapter: 1, hasReadingProgress: false, memoryCount: 0, homeLayoutStyle: [ 'height:390px', '--home-top-inset:8px', '--home-right-inset:10px', '--home-bottom-inset:7px', '--home-left-inset:10px', ].join(';'), }, onLoad() { this.unsubscribeTang = bridge.subscribe(() => this.refreshTangStatus()) this.refreshTangStatus() this.applyHomeLayout() }, onShow() { this.refreshHomeProgress() this.refreshTangStatus() }, onUnload() { if (this.unsubscribeTang) this.unsubscribeTang() }, refreshHomeProgress() { const progress = getProgress() this.setData({ completedCount: progress.completedChapters.length, lastChapter: progress.lastChapter || 1, hasReadingProgress: hasReadingProgress(progress), memoryCount: getCollectedMemories(progress, memoryCards).length, }) this.applyHomeLayout() }, refreshTangStatus() { this.setData({ tangStatusText: STATUS_TEXT[bridge.getStatus()] || '本机阅读存档' }) }, async syncTang() { const scope = bridge.getScope() const visibleGeneration = this.__tangShowGeneration const stillActive = () => !this.__tangDead && this.__tangVisible && this.__tangShowGeneration === visibleGeneration && bridge.getScope() === scope if (!wx.getStorageSync('token')) { wx.showToast({ title: '请先在学堂登录,游客记录不会自动上传', icon: 'none' }) return } await bridge.open(true) if (!stillActive()) return if (bridge.getStatus() !== 'conflict') { this.refreshHomeProgress(); return } const conflictContext = bridge.getConflictContext() wx.showActionSheet({ itemList: ['采用云端存档', '用本机存档覆盖云端'], success: result => { if (!stillActive() || bridge.getConflictContext() !== conflictContext) return const choice = result.tapIndex === 0 ? 'cloud' : 'local' wx.showModal({ title: '确认阅读存档', content: choice === 'cloud' ? '将用云端进度替换当前本机进度。原记录会保留一份本机备份。' : '将用本机进度覆盖云端,其他设备的未同步阅读可能不在其中。原记录会保留一份本机备份。', confirmText: '确认使用', success: async answer => { if (answer.confirm && stillActive()) { await bridge.resolveConflict(choice, conflictContext) if (stillActive()) this.refreshHomeProgress() } }, }) }, }) }, returnToXuetang() { bridge.flush() wx.reLaunch({ url: '/tongji/pages/weekly' }) }, onResize(resizeInfo) { this.applyHomeLayout(resizeInfo) }, applyHomeLayout(resizeInfo = null) { let windowInfo = {} try { windowInfo = wx.getWindowInfo ? wx.getWindowInfo() : wx.getSystemInfoSync() } catch (error) { windowInfo = {} } const resizeSize = resizeInfo && resizeInfo.size ? resizeInfo.size : resizeInfo if (resizeSize) { windowInfo = { ...windowInfo, windowWidth: resizeSize.windowWidth || windowInfo.windowWidth, windowHeight: resizeSize.windowHeight || windowInfo.windowHeight, } } let menuRect = {} try { menuRect = wx.getMenuButtonBoundingClientRect ? wx.getMenuButtonBoundingClientRect() : {} } catch (error) { menuRect = {} } const metrics = getHomeLayoutMetrics(windowInfo, menuRect) this.setData({ homeLayoutStyle: getHomeLayoutStyle(metrics), }) }, startGame() { const chapter = this.data.lastChapter || 1 wx.navigateTo({ url: chapterRoute(chapter), }) }, revealCover() { if (this.data.coverOpened) return this.setData({ coverOpened: true }) }, keepCoverOpen() {}, openCatalog() { wx.navigateTo({ url: '/tang-detective/pages/catalog/catalog' }) }, openCast() { wx.navigateTo({ url: '/tang-detective/pages/cast/cast' }) }, openMemories() { wx.navigateTo({ url: '/tang-detective/pages/memories/memories' }) }, onShareAppMessage() { return { title: '唐侦探:在一桌饭里,看见被忽略的人', path: '/tang-detective/pages/home/home', } }, onShareTimeline() { return { title: '唐侦探:在一桌饭里,看见被忽略的人', query: '', } }, })