import React from 'react'; import { Tooltip } from 'antd'; import './index.less'; import Page from '@src/containers/Page'; import Icon from '../../../components/Icon'; import Progress from '../../../components/Progress'; import Assets from '../../../../../src/components/Assets'; import { Sentence } from '../../../stores/sentence'; import { Main } from '../../../stores/main'; import { formatMoney, formatDate } from '../../../../../src/services/Tools'; export default class extends Page { constructor(props) { super(props); this.page = 0; this.inited = false; this.timeout = null; this.articleMap = {}; this.pageLine = 100; } initState() { return { article: {}, showJump: false, showMenu: false, currentPage: 0, totalPage: 0 }; } init() { this.lastTime = new Date(); Main.getSentence().then(result => { this.setState({ info: result }); }); } initData() { const { chapter = 0, part = 0 } = this.state.search; let { page = 0 } = this.state.search; const handler = this.refreshSentence(); handler.then(() => { if (chapter > 0) { if (!part) { ({ page } = this.searchArticle(chapter, 1)); } else { ({ page } = this.searchArticle(chapter, part)); } } this.jumpPage(page); }); } refreshSentence() { if (this.inited) return Promise.resolve(); return Sentence.listArticle() .then(result => { const articleMap = {}; let totalPage = 0; let maxChapter = 0; let introduction = null; result.forEach(article => { if (article.chapter === 0) introduction = article; if (!articleMap[article.chapter]) { articleMap[article.chapter] = []; if (article.chapter > maxChapter) maxChapter = article.chapter; } article.startPage = totalPage + 1; article.endPage = totalPage + article.pages; totalPage += article.pages; articleMap[article.chapter].push(article); }); this.setState({ articleMap, totalPage, maxChapter }); return introduction; }) .then(introduction => { return Sentence.getInfo().then(result => { const map = {}; // 添加前言 if (introduction) { result.chapters.unshift({ title: introduction.title, value: 0, }); } result.chapters.forEach(row => { map[row.value] = row; }); this.setState({ sentence: result, chapterMap: map }); }); }) .then(() => { this.inited = true; }); } refreshArticle(articleId) { if (this.articleMap[articleId]) return Promise.resolve(this.articleMap[articleId]); return Sentence.detailArticle(articleId).then(result => { this.articleMap[articleId] = result; return result; }); } prevPage() { const { currentPage } = this.state; if (currentPage > 1) { this.jumpPage(currentPage - 1); } } nextPage() { const { sentence = {} } = this.state; const { currentPage, totalPage } = this.state; if (currentPage < totalPage) { this.jumpPage(currentPage + 1); } else { const code = !!sentence.code; if (!code) { this.setState({ article: null }); } } } jumpPage(targetPage) { // 计算哪篇文章 const { target, index, allow } = this.computeArticle(targetPage); if (!allow) { this.setState({ article: null }); return; } this.page = targetPage; this.setState({ inputPage: targetPage }); const { article = {} } = this.state; this.updateProgress(target, index, article); this.refreshArticle(target.id).then(row => { this.setState({ article: row, index, currentPage: targetPage }); }); } computeArticle(page) { const { articleMap, maxChapter, sentence } = this.state; let target = null; let index = 0; const allow = true || sentence.code || page <= sentence.trailPages; for (let i = 0; i <= maxChapter; i += 1) { const list = articleMap[i]; if (!list || list.length === 0) continue; for (let j = 0; j < list.length; j += 1) { const article = list[j]; if (article.endPage >= page) { target = article; index = page - article.startPage; // if (!sentence.code) { // if (!article.isTrail) { // allow = false; // } else if (index < article.trailStart - 1) { // allow = false; // } else if (index > article.trailEnd - 1) { // allow = false; // } // } return { target, index, allow }; } } } return { allow }; } searchArticle(chapter, part) { const { articleMap } = this.state; let target = null; let page = 0; if (!part) part = 1; const list = articleMap[chapter]; for (let j = 0; j < list.length; j += 1) { const article = list[j]; if (article.part === Number(part)) { target = article; page = article.startPage; // if (sentence.code) { // page = article.startPage; // } else { // // 试用章节页码 // page = article.startPage + article.trailPage - 1; // } break; } } return { target, page }; } updateProgress(target, index, current) { if (this.timeout) { clearTimeout(this.timeout); this.timeout = null; } const progress = ((index + 1) * 100) / target.pages; if (target.chapter === current.chapter && target.part === current.part) { Sentence.updateProgress(target.chapter, target.part, progress); } else { const now = new Date(); const time = (now.getTime() - this.lastTime.getTime()) / 1000; this.lastTime = now; Sentence.updateProgress(target.chapter, target.part, progress, time, current.chapter, current.part); } this.timeout = setTimeout(() => { // 最长5分钟阅读时间 Sentence.updateProgress(0, 0, 0, 5 * 60, target.chapter, target.part); this.lastTime = null; }, 5 * 60 * 1000); } renderView() { return (