page.js 11 KB

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