page.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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, formatDate } 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 = {}, sentence = {} } = 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. openLink(info.link);
  211. }}>{formatMoney(info.price)} | 立即购买</div>
  212. <div className="free-over-desc">
  213. {(sentence.comments || []).map(row => {
  214. return [
  215. <div className="free-over-desc-title">{row.nickname} {formatDate(row.createTime, 'YYYY-MM-DD')}</div>,
  216. <div className="free-over-desc-content">{row.content}</div>,
  217. ];
  218. })}
  219. </div>
  220. </div>
  221. </div>
  222. );
  223. }
  224. renderMenu() {
  225. const { sentence = {}, articleMap } = this.state;
  226. const { chapters = [] } = sentence;
  227. let page = 1;
  228. const code = !!sentence.code;
  229. const message = '购买后访问';
  230. return (
  231. <div className="layout-menu">
  232. <div className="title">目录</div>
  233. <div
  234. className="close"
  235. onClick={() => {
  236. this.setState({ showMenu: false });
  237. }}
  238. >
  239. x
  240. </div>
  241. <div className="chapter">
  242. {chapters.map(chapter => {
  243. if (chapter.exercise) {
  244. return [<div className={'chapter-item trail'}>{chapter.title}</div>];
  245. }
  246. chapter.startPage = page;
  247. const _item = code ? (
  248. <div
  249. className={'chapter-item'}
  250. onClick={() => {
  251. this.jumpPage(chapter.startPage);
  252. }}
  253. >
  254. {chapter.title}
  255. <div className="page">{chapter.startPage}</div>
  256. </div>
  257. ) : (<Tooltip title={message}>
  258. <div className={'chapter-item trail'}>
  259. {chapter.title}
  260. <div className="page">{chapter.startPage}</div>
  261. </div>
  262. </Tooltip>
  263. );
  264. const list = [_item];
  265. if (chapter.value) {
  266. (articleMap[chapter.value] || []).forEach(article => {
  267. // 得到下一章节page
  268. page += article.pages;
  269. const item = code ? (
  270. <div
  271. className={'part-item'}
  272. onClick={() => {
  273. if (code) this.jumpPage(article.startPage);
  274. }}
  275. >
  276. {article.title}
  277. <div className="page">{article.startPage}</div>
  278. </div>
  279. ) : (<Tooltip title={message}>
  280. <div className={'part-item trail'}>
  281. {article.title}
  282. <div className="page">{article.startPage}</div>
  283. </div>
  284. </Tooltip>
  285. );
  286. list.push(item);
  287. });
  288. }
  289. return list;
  290. })}
  291. </div>
  292. </div>
  293. );
  294. }
  295. renderRight() {
  296. return (
  297. <div className="layout-right">
  298. <div
  299. className="m-b-1"
  300. onClick={() => {
  301. this.setState({ showMenu: true });
  302. }}
  303. >
  304. <Icon name="menu" />
  305. </div>
  306. <div
  307. className="m-b-1"
  308. onClick={() => {
  309. this.prevPage();
  310. }}
  311. >
  312. <Icon name="down_turn" />
  313. </div>
  314. <div
  315. className="m-b-1"
  316. onClick={() => {
  317. this.nextPage();
  318. }}
  319. >
  320. <Icon name="up_turn" />
  321. </div>
  322. </div>
  323. );
  324. }
  325. renderBottom() {
  326. const { showJump, currentPage, totalPage } = this.state;
  327. return (
  328. <div className="layout-bottom">
  329. <span className="per">{totalPage ? parseInt((currentPage * 100) / totalPage, 10) : 0}%</span>
  330. <span className="num">
  331. {currentPage}/{totalPage}
  332. </span>
  333. <span className="btn">
  334. <Assets
  335. name={showJump ? 'unfold_icon_down' : 'unfold_icon_up'}
  336. onClick={() => {
  337. this.setState({ showJump: !showJump });
  338. }}
  339. />
  340. <div hidden={!showJump} className="jump">
  341. <span className="text">当前页</span>
  342. <input
  343. className="input"
  344. value={this.state.inputPage}
  345. onChange={e => {
  346. this.page = Number(e.target.value);
  347. this.setState({ inputPage: e.target.value });
  348. }}
  349. />
  350. <Assets
  351. name="yes_icon"
  352. onClick={() => {
  353. if (this.page < 1 || this.page > totalPage) return;
  354. this.jumpPage(this.page);
  355. }}
  356. />
  357. </div>
  358. </span>
  359. </div>
  360. );
  361. }
  362. renderProgress() {
  363. const { currentPage, totalPage } = this.state;
  364. return (
  365. <div className="layout-progress">
  366. <Progress
  367. size="small"
  368. theme="theme"
  369. radius={false}
  370. progress={totalPage ? parseInt((currentPage * 100) / totalPage, 10) : 0}
  371. />
  372. </div>
  373. );
  374. }
  375. }