page.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. import React from 'react';
  2. import { Tooltip } from 'antd';
  3. import './index.less';
  4. import Page from '@src/containers/Page';
  5. import Icon from '../../../components/Icon';
  6. import Progress from '../../../components/Progress';
  7. import Assets from '../../../../../src/components/Assets';
  8. import { Sentence } from '../../../stores/sentence';
  9. export default class extends Page {
  10. constructor(props) {
  11. super(props);
  12. this.page = 0;
  13. this.inited = false;
  14. this.timeout = null;
  15. this.articleMap = {};
  16. this.pageLine = 100;
  17. }
  18. init() {
  19. this.lastTime = new Date();
  20. }
  21. initState() {
  22. return { showJump: false, showMenu: false, currentPage: 0, totalPage: 0 };
  23. }
  24. initData() {
  25. const { chapter = 0, part = 0 } = this.state.search;
  26. let { page = 0 } = this.state.search;
  27. const handler = this.refreshSentence();
  28. handler.then(() => {
  29. if (chapter > 0) {
  30. if (!part) {
  31. ({ page } = this.searchArticle(chapter, 1));
  32. } else {
  33. ({ page } = this.searchArticle(chapter, part));
  34. }
  35. }
  36. this.jumpPage(page);
  37. });
  38. }
  39. refreshSentence() {
  40. if (this.inited) return Promise.resolve();
  41. return Sentence.listArticle().then(result => {
  42. const articleMap = {};
  43. let totalPage = 0;
  44. let maxChapter = 0;
  45. let introduction = null;
  46. result.forEach((article) => {
  47. if (article.chapter === 0) introduction = article;
  48. if (!articleMap[article.chapter]) {
  49. articleMap[article.chapter] = [];
  50. if (article.chapter > maxChapter) maxChapter = article.chapter;
  51. }
  52. article.startPage = totalPage + 1;
  53. article.endPage = totalPage + article.pages;
  54. totalPage += article.pages;
  55. articleMap[article.chapter].push(article);
  56. });
  57. this.setState({ articleMap, totalPage, maxChapter });
  58. return introduction;
  59. }).then((introduction) => {
  60. return Sentence.getInfo().then(result => {
  61. const map = {};
  62. // 添加前言
  63. if (introduction) {
  64. result.chapters.unshift({
  65. title: introduction.title,
  66. value: 0,
  67. });
  68. }
  69. result.chapters.forEach((row) => {
  70. map[row.value] = row;
  71. });
  72. this.setState({ sentence: result, chapterMap: map });
  73. });
  74. }).then(() => {
  75. this.inited = true;
  76. });
  77. }
  78. refreshArticle(articleId) {
  79. if (this.articleMap[articleId]) return Promise.resolve(this.articleMap[articleId]);
  80. return Sentence.detailArticle(articleId).then(result => {
  81. this.articleMap[articleId] = result;
  82. return result;
  83. });
  84. }
  85. prevPage() {
  86. const { currentPage } = this.state;
  87. if (currentPage > 1) {
  88. this.jumpPage(currentPage - 1);
  89. }
  90. }
  91. nextPage() {
  92. const { currentPage, totalPage } = this.state;
  93. if (currentPage < totalPage) {
  94. this.jumpPage(currentPage + 1);
  95. }
  96. }
  97. jumpPage(targetPage) {
  98. // 计算哪篇文章
  99. const { target, index, allow } = this.computeArticle(targetPage);
  100. if (!allow) {
  101. // todo 无法访问:非试用
  102. return;
  103. }
  104. this.page = targetPage;
  105. this.setState({ inputPage: targetPage });
  106. const { article = {} } = this.state;
  107. this.updateProgress(target, index, article);
  108. this.refreshArticle(target.id).then((row) => {
  109. this.setState({ article: row, index, currentPage: targetPage });
  110. });
  111. }
  112. computeArticle(page) {
  113. const { articleMap, maxChapter, sentence } = this.state;
  114. let target = null;
  115. let index = 0;
  116. const allow = true || sentence.code || page <= sentence.trailPages;
  117. for (let i = 0; i <= maxChapter; i += 1) {
  118. const list = articleMap[i];
  119. if (!list || list.length === 0) continue;
  120. for (let j = 0; j < list.length; j += 1) {
  121. const article = list[j];
  122. if (article.endPage >= page) {
  123. target = article;
  124. index = page - article.startPage;
  125. // if (!sentence.code) {
  126. // if (!article.isTrail) {
  127. // allow = false;
  128. // } else if (index < article.trailStart - 1) {
  129. // allow = false;
  130. // } else if (index > article.trailEnd - 1) {
  131. // allow = false;
  132. // }
  133. // }
  134. return { target, index, allow };
  135. }
  136. }
  137. }
  138. return { allow };
  139. }
  140. searchArticle(chapter, part) {
  141. const { articleMap } = this.state;
  142. let target = null;
  143. let page = 0;
  144. if (!part) part = 1;
  145. const list = articleMap[chapter];
  146. for (let j = 0; j < list.length; j += 1) {
  147. const article = list[j];
  148. if (article.part === Number(part)) {
  149. target = article;
  150. page = article.startPage;
  151. // if (sentence.code) {
  152. // page = article.startPage;
  153. // } else {
  154. // // 试用章节页码
  155. // page = article.startPage + article.trailPage - 1;
  156. // }
  157. break;
  158. }
  159. }
  160. return { target, page };
  161. }
  162. updateProgress(target, index, current) {
  163. if (this.timeout) {
  164. clearTimeout(this.timeout);
  165. this.timeout = null;
  166. }
  167. const now = new Date();
  168. const time = (now.getTime() - this.lastTime.getTime()) / 1000;
  169. this.lastTime = now;
  170. const progress = (index + 1) * 100 / target.pages;
  171. Sentence.updateProgress(target.chapter, target.part, progress, time, current.chapter, current.page);
  172. this.timeout = setTimeout(() => {
  173. // 最长5分钟阅读时间
  174. Sentence.updateProgress(0, 0, 0, 5 * 60, target.chapter, target.part);
  175. }, 5 * 60 * 1000);
  176. }
  177. renderView() {
  178. return (
  179. <div className="layout">
  180. {this.renderBody()}
  181. {this.renderRight()}
  182. {this.renderBottom()}
  183. {this.renderProgress()}
  184. </div>
  185. );
  186. }
  187. renderBody() {
  188. const { showMenu, article, index, chapterMap = {} } = this.state;
  189. return article && (
  190. <div className="layout-body">
  191. <div className="crumb">千行长难句解析 >> {(chapterMap[article.chapter] || {}).title}</div>
  192. {article.chapter > 0 && <div className="title">{article.title}</div>}
  193. <div className="overload" style={{ top: index * -20 * this.pageLine }}>
  194. <div className="text" dangerouslySetInnerHTML={{ __html: article.content }} />
  195. </div>
  196. {showMenu && this.renderMenu()}
  197. </div>
  198. );
  199. }
  200. renderMenu() {
  201. const { sentence = {}, articleMap } = this.state;
  202. const { chapters = [] } = sentence;
  203. let page = 1;
  204. const code = !!sentence.code;
  205. const message = '购买后访问';
  206. return (
  207. <div className="layout-menu">
  208. <div className="title">目录</div>
  209. <div className="close" onClick={() => {
  210. this.setState({ showMenu: false });
  211. }}>x</div>
  212. <div className="chapter">
  213. {chapters.map(chapter => {
  214. if (chapter.exercise) {
  215. return [<div className={'chapter-item trail'}>
  216. {chapter.title}
  217. </div>];
  218. }
  219. chapter.startPage = page;
  220. const list = code ? [<div className={'chapter-item'} onClick={() => {
  221. this.jumpPage(chapter.startPage);
  222. }}>
  223. {chapter.title}<div className="page">{chapter.startPage}</div>
  224. </div>] : [<Tooltip title={message}><div className={'chapter-item trail'}>
  225. {chapter.title}<div className="page">{chapter.startPage}</div>
  226. </div></Tooltip>];
  227. if (chapter.value) {
  228. (articleMap[chapter.value] || []).forEach((article) => {
  229. // 得到下一章节page
  230. page += article.pages;
  231. const item = code ? <div className={'part-item'} onClick={() => {
  232. if (code) this.jumpPage(article.startPage);
  233. }}>
  234. {article.title}<div className="page">{article.startPage}</div>
  235. </div> : <Tooltip title={message}><div className={'part-item trail'}>
  236. {article.title}<div className="page">{article.startPage}</div>
  237. </div></Tooltip>;
  238. list.push(item);
  239. });
  240. }
  241. return list;
  242. })}
  243. </div>
  244. </div>
  245. );
  246. }
  247. renderRight() {
  248. return (
  249. <div className="layout-right">
  250. <div className="m-b-1" onClick={() => {
  251. this.setState({ showMenu: true });
  252. }}>
  253. <Icon name="menu" />
  254. </div>
  255. <div className="m-b-1" onClick={() => {
  256. this.prevPage();
  257. }}>
  258. <Icon name="down_turn" />
  259. </div>
  260. <div className="m-b-1" onClick={() => {
  261. this.nextPage();
  262. }}>
  263. <Icon name="up_turn" />
  264. </div>
  265. </div>
  266. );
  267. }
  268. renderBottom() {
  269. const { showJump, currentPage, totalPage } = this.state;
  270. return (
  271. <div className="layout-bottom">
  272. <span className="per">{totalPage ? parseInt(currentPage * 100 / totalPage, 10) : 0}%</span>
  273. <span className="num">{currentPage}/{totalPage}</span>
  274. <span className="btn">
  275. <Assets name={showJump ? 'unfold_icon_down' : 'unfold_icon_up'} onClick={() => {
  276. this.setState({ showJump: !showJump });
  277. }} />
  278. <div hidden={!showJump} className="jump">
  279. <span className="text">当前页</span>
  280. <input className="input" value={this.state.inputPage} onChange={(e) => {
  281. this.page = Number(e.target.value);
  282. this.setState({ inputPage: e.target.value });
  283. }} />
  284. <Assets name="yes_icon" onClick={() => {
  285. if (this.page < 1 || this.page > totalPage) return;
  286. this.jumpPage(this.page);
  287. }} />
  288. </div>
  289. </span>
  290. </div>
  291. );
  292. }
  293. renderProgress() {
  294. const { currentPage, totalPage } = this.state;
  295. return (
  296. <div className="layout-progress">
  297. <Progress size="small" theme="theme" radius={false} progress={totalPage ? parseInt(currentPage * 100 / totalPage, 10) : 0} />
  298. </div>
  299. );
  300. }
  301. }