page.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. import React from 'react';
  2. import { Link } from 'react-router-dom';
  3. import './index.less';
  4. import Page from '@src/containers/Page';
  5. import Assets from '@src/components/Assets';
  6. import { getMap, formatPercent, formatDate } from '@src/services/Tools';
  7. import Footer from '../../../components/Footer';
  8. import { FaqModal, CommentModal, FinishModal, CourseNoteModal, AskCourseModal } from '../../../components/OtherModal';
  9. import { Contact, AnswerCarousel, Comment } from '../../../components/Other';
  10. import Tabs from '../../../components/Tabs';
  11. import { Icon as GIcon } from '../../../components/Icon';
  12. import Button from '../../../components/Button';
  13. import IconButton from '../../../components/IconButton';
  14. import UserTable from '../../../components/UserTable';
  15. import ProgressText from '../../../components/ProgressText';
  16. import { OpenText } from '../../../components/Open';
  17. import Video from '../../../components/Video';
  18. import { Main } from '../../../stores/main';
  19. import { Course } from '../../../stores/course';
  20. import { User } from '../../../stores/user';
  21. import { Order } from '../../../stores/order';
  22. import { Question } from '../../../stores/question';
  23. import { My } from '../../../stores/my';
  24. export default class extends Page {
  25. initState() {
  26. this.columns = [
  27. {
  28. title: '学习内容',
  29. key: 'title',
  30. render: (text, record) => {
  31. const { no } = this.state;
  32. if (no === record.no) {
  33. // 当前正在
  34. }
  35. return `课时 ${record.no}: ${text}`;
  36. },
  37. },
  38. {
  39. title: '预习作业',
  40. key: 'paper',
  41. render: text => {
  42. text = text || {};
  43. const progress = text.report ? formatPercent(text.report.userNumber, text.report.questionNumber) : 0;
  44. const times = text.paper ? text.paper.times : 0;
  45. return (
  46. <div>
  47. <div className="v-a-m d-i-b">
  48. <ProgressText width={50} size="small" times={times} progress={progress} unit="次" />
  49. </div>
  50. {!text.report && (
  51. <IconButton
  52. className="m-l-2"
  53. type="start"
  54. tip="Start"
  55. onClick={() => {
  56. User.needLogin().then(() => {
  57. Question.startLink('preview', text);
  58. });
  59. }}
  60. />
  61. )}
  62. {text.report && !text.report.isFinish && (
  63. <IconButton
  64. className="m-l-2"
  65. type="continue"
  66. tip="Continue"
  67. onClick={() => {
  68. User.needLogin().then(() => {
  69. Question.continueLink('preview', text);
  70. });
  71. }}
  72. />
  73. )}
  74. {text.report && !!text.report.isFinish && (
  75. <IconButton
  76. className="m-l-2"
  77. type="restart"
  78. tip="Restart"
  79. onClick={() => {
  80. User.needLogin().then(() => {
  81. Question.restart('preview', text);
  82. });
  83. }}
  84. />
  85. )}
  86. {text.report && !!text.report.isFinish && (
  87. <IconButton
  88. className="m-l-5"
  89. type="report"
  90. tip="Report"
  91. onClick={() => {
  92. User.needLogin().then(() => {
  93. Question.reportLink('preview', text);
  94. });
  95. }}
  96. />
  97. )}
  98. </div>
  99. );
  100. },
  101. },
  102. {
  103. title: '进度',
  104. key: 'progress',
  105. render: (text, record) => {
  106. const { paper = {} } = record;
  107. return `${paper.paper && paper.paper.times > 0 ? `${paper.paper.times}次+` : ''}${
  108. paper.report ? formatPercent(paper.report.userNumber, paper.report.questionNumber, false) : '0%'}`;
  109. },
  110. },
  111. {
  112. title: '最近学习',
  113. key: 'lastTime',
  114. render: (text, record) => {
  115. const { paper = {} } = record;
  116. return paper.report && formatDate(paper.report.updateTime, 'YYYY-MM-DD HH:mm:ss');
  117. },
  118. },
  119. {
  120. title: '笔记',
  121. key: 'note',
  122. render: (text, record) => {
  123. return <GIcon name="note" active={record.note} />;
  124. },
  125. },
  126. {
  127. title: '问答',
  128. key: 'ask',
  129. render: (text, record) => {
  130. return (
  131. <Link to={`/course/answer/${record.courseId}?tab=my&courseNoId=${record.id}`}>{`${record.answerNumber ||
  132. 0}/${record.askNumber || 0}`}</Link>
  133. );
  134. },
  135. },
  136. ];
  137. return {
  138. tab: '1',
  139. rightTab: '1',
  140. key: '1',
  141. add: false,
  142. list: [{ key: '1' }, { key: '2' }, { key: '3' }],
  143. data: {},
  144. position: 0,
  145. };
  146. }
  147. init() {
  148. Main.dataStruct().then(result => {
  149. const dataStructSelect = result.map(row => {
  150. return {
  151. title: `${row.titleZh}${row.titleEn}`,
  152. key: row.id,
  153. };
  154. });
  155. const dataStructMap = getMap(dataStructSelect, 'key');
  156. this.setState({ dataStructSelect, dataStructMap });
  157. });
  158. Main.getBase().then(result => {
  159. this.setState({ base: result });
  160. });
  161. }
  162. formatRecord(row) {
  163. row.paperMap = {};
  164. if (row.papers) {
  165. row.papers.forEach(paper => {
  166. if (paper.courseNo) row.paperMap[paper.courseNo] = paper;
  167. });
  168. }
  169. row.progressMap = {};
  170. if (row.progress) {
  171. row.progress.forEach(progress => {
  172. row.progressMap[progress.courseNoId] = progress;
  173. });
  174. }
  175. row.courseNoMap = {};
  176. row.courseTime = 0;
  177. if (row.courseNos) {
  178. row.courseNos.forEach(no => {
  179. row.courseNoMap[no.id] = no;
  180. row.courseTime += no.time;
  181. no.paper = row.paperMap[no.id];
  182. no.progress = row.progressMap[no.id];
  183. });
  184. }
  185. if (row.currentNo) {
  186. row.currentCourseNo = row.courseNoMap[row.currentNo];
  187. } else {
  188. row.currentNo = 0;
  189. }
  190. return row;
  191. }
  192. initData() {
  193. const { id } = this.params;
  194. Course.detail(id).then(result => {
  195. result = this.formatRecord(result);
  196. result.have = true;
  197. this.setState({ data: result });
  198. // 选择课时
  199. if (this.state.search.no) {
  200. this.onChangeItem(this.state.search.no);
  201. } else {
  202. this.onChangeItem(1);
  203. }
  204. this.refreshNote();
  205. });
  206. }
  207. refreshAsk(position) {
  208. const { id } = this.params;
  209. const { item = {} } = this.state;
  210. Course.listAsk(Object.assign({ page: 1, size: 1000, courseId: id, courseNoId: item.id, position })).then(result => {
  211. this.setState({ asks: result.list });
  212. });
  213. }
  214. refreshNote() {
  215. const { id } = this.params;
  216. const { data } = this.state;
  217. if (!data.have) return;
  218. My.listCourseNote({ courseId: id, page: 1, size: data.courseNos.length })
  219. .then((result) => {
  220. this.noteMap = getMap(result.list, 'courseNoId');
  221. data.courseNos.forEach((row) => {
  222. const note = this.noteMap[row.id];
  223. if (note) row.note = true;
  224. });
  225. this.setState({ data });
  226. });
  227. }
  228. onChangeRightTab(rightTab) {
  229. this.setState({ rightTab });
  230. }
  231. onChangeTab(tab) {
  232. this.setState({ tab });
  233. }
  234. onChangeItem(key) {
  235. key = Number(key);
  236. this.changeQuery({ no: key });
  237. this.setState({ no: key });
  238. const { data, item } = this.state;
  239. const index = key - 1;
  240. const timelineSelect = [];
  241. const current = data.courseNos[index];
  242. if (current) {
  243. const max = current.time;
  244. let start = 0;
  245. let end = start + 5;
  246. while (start < max) {
  247. timelineSelect.push({
  248. title: `${start} - ${end}min`,
  249. key: `${start}`,
  250. });
  251. start += 5;
  252. end = Math.min(start + 5, max);
  253. }
  254. }
  255. // 切换播放,记录进度
  256. if (item) {
  257. this.updateProgress(item.id, this.lastSecond, item.time, true);
  258. }
  259. this.setState({ item: current, timelineSelect });
  260. }
  261. onTimeUpdate(second) {
  262. const { position, item = {}, data } = this.state;
  263. if (!data.have) {
  264. // 如果是试用,则按秒数增加
  265. second += (item.startTrail || 0) * 60;
  266. }
  267. const minute = parseInt(second / 60, 10);
  268. const nowPosition = parseInt(minute / 5, 10) * 5;
  269. if (nowPosition !== position) {
  270. this.refreshAsk(position);
  271. this.setState({ position: nowPosition });
  272. // 定时更新进度
  273. this.updateProgress(item.id, second, item.time);
  274. }
  275. this.lastSecond = second;
  276. }
  277. playVideo() {
  278. // 开始计时
  279. this.lastTime = new Date();
  280. }
  281. pauseVideo() {
  282. // 停止计时
  283. const now = new Date();
  284. this.time += (now.getTime() - this.lastTime.getTime()) / 1000;
  285. this.lastTime = null;
  286. }
  287. next() {
  288. const { data, item } = this.state;
  289. if (data.courseNos.length === item.no) {
  290. return;
  291. }
  292. this.onChangeItem(item.no + 1);
  293. }
  294. onVideoAction(key) {
  295. const { rightTab, showTab, showAsk, showNote, item = {} } = this.state;
  296. switch (key) {
  297. case 'ask':
  298. return this.setState({ showAsk: !showAsk });
  299. case 'note':
  300. return this.setState({ showNote: !showNote, note: this.noteMap ? this.noteMap[item.id] || {} : {} });
  301. case 'answer':
  302. return this.setState({ showTab: rightTab === '1' ? !showTab : true, rightTab: '1' });
  303. case 'list':
  304. return this.setState({ showTab: rightTab === '2' ? !showTab : true, rightTab: '2' });
  305. default:
  306. return '';
  307. }
  308. }
  309. updateProgress(courseNoId, currentTime, totalTime, record) {
  310. if (!this.lastTime) return;
  311. const { id } = this.params;
  312. const now = new Date();
  313. this.time += (now.getTime() - this.lastTime.getTime()) / 1000;
  314. this.lastTime = now;
  315. const progress = formatPercent(currentTime, totalTime);
  316. if (record || this.time > 600) {
  317. // 最长5分钟记录一次
  318. Course.noProgress(id, courseNoId, progress, this.time, courseNoId);
  319. this.time = 0;
  320. } else {
  321. Course.noProgress(id, courseNoId, progress, null, null);
  322. }
  323. }
  324. buy() {
  325. const { data } = this.props;
  326. User.needLogin().then(() => {
  327. Order.speedPay({ productType: 'course', productId: data.id }).then(result => {
  328. User.needPay(result).then(() => {
  329. this.refresh();
  330. });
  331. });
  332. });
  333. }
  334. add() {
  335. const { data } = this.props;
  336. User.needLogin().then(() => {
  337. Order.addCheckout({ productType: 'course', productId: data.id }).then(() => {
  338. this.setState({ add: true });
  339. });
  340. });
  341. }
  342. viewAsk(id) {
  343. Course.askView(id);
  344. }
  345. setVideo(video) {
  346. this.video = video;
  347. }
  348. renderView() {
  349. const { base = {}, data = {}, item = {}, add, rightTab, showTab, showAsk, showNote, dataStructMap = {}, showComment, comment = {}, showFaq, faq = {}, showFinish, note = {}, ask = {}, timelineSelect = [] } = this.state;
  350. const { courseNos = [] } = data;
  351. return (
  352. <div>
  353. <div className="top content t-8">
  354. 千行课堂 > 全部课程 > {data.parentStructId > 0 ? `${(dataStructMap[data.parentStructId] || {}).title} >` : ''}{' '}
  355. {(dataStructMap[data.structId] || {}).title} > {data.title} > <span className="t-1">课程详情</span>
  356. </div>
  357. <div className="center content">
  358. <div className="t-1 t-s-20">
  359. {data.title}
  360. <div className="action f-r">
  361. {!data.have && <Button className="m-r-1" radius size="lager" onClick={() => this.buy()}>
  362. 立即购买
  363. </Button>}
  364. {!data.have && <Button theme="default" radius size="lager" disabled={data.add || add} onClick={() => this.add()}>
  365. <Assets name="add" />
  366. </Button>}
  367. {data.have && <Button className="m-r-1" radius size="lager" onClick={() => linkTo('/my/course')}>
  368. 我的课程
  369. </Button>}
  370. </div>
  371. </div>
  372. <div className="t-2 m-b-1">授课老师:{data.teacher}</div>
  373. <div className={'detail'}>
  374. <div className="left">
  375. {data.have && <div hidden={(item.paper && item.paper.times > 0)} className="left-top">
  376. <span className="d-i-b m-r-1">预习作业</span>
  377. <span className="d-i-b m-r-2">
  378. <ProgressText width={480} size="small" progress={item.report ? formatPercent(item.report.userNumber, item.report.questionNumber) : 0} />
  379. </span>
  380. <Button className="f-r" radius onClick={() => (item.report ? Question.continueLink('preview', item) : Question.startLink('preview', item))}>
  381. 做题
  382. </Button>
  383. </div>}
  384. <div className="video-layout">
  385. {item && <Video
  386. key={item.id}
  387. src={item.resource || '/1.mp4'}
  388. width={750}
  389. height={467}
  390. ref={ref => this.setVideo(ref)}
  391. btnList={[
  392. { title: '提问', key: 'ask', show: data.have, active: showAsk, pause: true },
  393. {
  394. key: 'answer',
  395. render(active) {
  396. return <Assets name={active ? 'question_on' : 'question_off'} />;
  397. },
  398. full: true,
  399. show: true,
  400. active: showTab && rightTab === '1',
  401. },
  402. { title: '笔记', key: 'note', show: data.have, active: showNote, pause: true },
  403. { title: '课表', key: 'list', show: true, full: true, active: showTab && rightTab === '2' },
  404. ]}
  405. onPlay={() => this.playVideo()}
  406. onPause={() => this.pauseVideo()}
  407. onNext={() => this.next()}
  408. onAction={key => this.onVideoAction(key)}
  409. onTimeUpdate={time => this.onTimeUpdate(time)}
  410. onFullChange={() => this.setState({ showTab: true, rightTab: '1' })}
  411. >
  412. <div hidden={!showTab} className="video-fixed tab-warpper">
  413. <Tabs
  414. type="division"
  415. theme="gray"
  416. space={2.5}
  417. active={rightTab}
  418. tabs={[{ title: '精选问答', key: '1' }, { title: '课时列表', key: '2' }]}
  419. onChange={key => this.onChangeRightTab(key)}
  420. />
  421. <div className="tab-body">{this[`renderRightTab${rightTab}`]()}</div>
  422. </div>
  423. </Video>}
  424. </div>
  425. </div>
  426. <div className={`detail-right ${data.have && !(item.paper && item.paper.times > 0) ? 'have' : ''} tab-warpper`}>
  427. <Tabs
  428. type="division"
  429. theme="gray"
  430. space={2.5}
  431. active={rightTab}
  432. tabs={[{ title: '精选问答', key: '1' }, { title: '课时列表', key: '2' }]}
  433. onChange={key => this.onChangeRightTab(key)}
  434. />
  435. <div className="tab-body">{this[`renderRightTab${rightTab}`]()}</div>
  436. </div>
  437. </div>
  438. {data.have && <UserTable columns={this.columns} list={courseNos} />}
  439. </div>
  440. <div hidden={data.have} className="bottom">
  441. <div className="content">{this.renderTab()}</div>
  442. </div>
  443. <Contact data={base.contact} />
  444. <Footer />
  445. <AskCourseModal getContainer={() => (showTab ? document.getElementById(this.video.state.id) : document.body)} show={showAsk} defaultData={ask} course={data} courseNo={item} selectList={timelineSelect} onConfirm={() => this.setState({ showAsk: false })} onCancel={() => this.setState({ showAsk: false })} />
  446. <CourseNoteModal getContainer={() => (showTab ? document.getElementById(this.video.state.id) : document.body)} show={showNote} defaultData={note} course={data} courseNos={courseNos} noteMap={this.noteMap} onConfirm={() => {
  447. this.setState({ showNote: false });
  448. this.refreshNote();
  449. }} onCancel={() => this.setState({ showNote: false })} />
  450. <CommentModal
  451. show={showComment}
  452. defaultData={comment}
  453. onConfirm={() => this.setState({ showComment: false, commnet: {}, showFinish: true })}
  454. onCancel={() => this.setState({ showComment: false, commnet: {} })}
  455. onClose={() => this.setState({ showComment: false, commnet: {} })}
  456. />
  457. <FaqModal show={showFaq} defaultData={faq} onCancel={() => this.setState({ showFaq: false, faq: {} })} onConfirm={() => this.setState({ showFaq: false, faq: {}, showFinish: true })} />
  458. <FinishModal
  459. getContainer={() => document.getElementById(this.video.state.id)}
  460. show={showFinish}
  461. onConfirm={() => this.setState({ showFinish: false })}
  462. />
  463. </div >
  464. );
  465. }
  466. renderRightTab1() {
  467. const { asks = [], data = {}, position } = this.state;
  468. return [
  469. <div className="all-answer">
  470. <span className="d-i-b b m-r-5" />
  471. <span className="d-i-b t-6">{position}:00~{position + 5}:00</span>
  472. <a className="f-r d-i-b t-4 c-p" href={`/course/answer/${data.id}`} target="_blank">全部问答 ></a>
  473. </div>,
  474. <div className="answer-layout">
  475. {asks.map(item => {
  476. return (
  477. <div className="answer-item">
  478. <div>
  479. <div className="small-tag">提问</div>
  480. </div>
  481. <div className="desc">
  482. <OpenText>{item.content}</OpenText>
  483. </div>
  484. {item.answerStatus > 0 && (
  485. <div>
  486. <div className="small-tag">回答</div>
  487. </div>
  488. )}
  489. {item.answerStatus > 0 && (
  490. <div className="desc">
  491. <OpenText onOpen={() => this.viewAsk(item.id)}>{item.answer}</OpenText>
  492. </div>
  493. )}
  494. </div>
  495. );
  496. })}
  497. </div>,
  498. ];
  499. }
  500. renderRightTab2() {
  501. const { data = {}, no } = this.state;
  502. const { courseNos = [] } = data;
  503. return (
  504. <div className="item-layout">
  505. {courseNos.map(item => {
  506. return (
  507. <div className={`item ${item.no === no ? 'active' : ''}`} onClick={() => this.onChangeItem(item.no)}>
  508. <span className="t-1">课时{item.no}</span>
  509. <span className="t-2">{item.title}</span>
  510. </div>
  511. );
  512. })}
  513. </div>
  514. );
  515. }
  516. renderTab() {
  517. const { tab } = this.state;
  518. return [
  519. <div className="bottom">
  520. <div className="content">
  521. <Tabs
  522. type="full"
  523. border
  524. active={tab}
  525. tabs={[
  526. { title: '课程介绍', key: '1' },
  527. { title: '授课大纲', key: '2' },
  528. { title: '小助手', key: '3' },
  529. { title: 'FAQs', key: '4' },
  530. { title: '优惠信息', key: '5' },
  531. { title: '学员评价', key: '6' },
  532. ]}
  533. onChange={key => this.onChangeTab(key)}
  534. />
  535. {this[`renderTab${tab}`]()}
  536. </div>
  537. </div>,
  538. ];
  539. }
  540. renderTab1() {
  541. const { data = {} } = this.state;
  542. return (
  543. <div className="tab-layout">
  544. <div className="tab-title">老师资历</div>
  545. <div className="tab-desc" dangerouslySetInnerHTML={{ __html: data.teacherContent }} />
  546. <div className="tab-title">基本参数</div>
  547. <div className="tab-desc" dangerouslySetInnerHTML={{ __html: data.baseContent }} />
  548. <div className="tab-title">授课重点</div>
  549. <div className="tab-desc" dangerouslySetInnerHTML={{ __html: data.pointContent }} />
  550. <div className="tab-title">适合人群</div>
  551. <div className="tab-desc" dangerouslySetInnerHTML={{ __html: data.crowdContent }} />
  552. </div>
  553. );
  554. }
  555. renderTab2() {
  556. const { data } = this.state;
  557. return (
  558. <div className="tab-layout">
  559. <div className="tab-desc" dangerouslySetInnerHTML={{ __html: data.syllabusContent }} />
  560. </div>
  561. );
  562. }
  563. renderTab3() {
  564. return (
  565. <div className="tab-layout">
  566. <div className="qr-layout">
  567. <Assets name="qrcode" className="m-r-2 v-a-t" />
  568. <div className="p-l-1 d-i-b t-l">
  569. <div className="t-1">千行小助手:</div>
  570. <div className="t-1 m-b-2">1232104-310431</div>
  571. <div className="t-2 t-s-12">微信扫码添加千行小助手为好友,</div>
  572. <div className="t-2 t-s-12">咨询课程,了解更多信息</div>
  573. </div>
  574. </div>
  575. </div>
  576. );
  577. }
  578. renderTab4() {
  579. const { faqs, data = {} } = this.state;
  580. return (
  581. <div className="tab-layout">
  582. <AnswerCarousel
  583. hideBtn
  584. list={faqs}
  585. onFaq={() => User.needLogin().then(() => this.setState({ showFaq: true, faq: { channel: 'course-video', position: data.id } }))}
  586. />
  587. </div>
  588. );
  589. }
  590. renderTab5() {
  591. const { data = {} } = this.state;
  592. return (
  593. <div className="tab-layout">
  594. <div className="tab-desc" dangerouslySetInnerHTML={{ __html: data.promoteContent }} />
  595. </div>
  596. );
  597. }
  598. renderTab6() {
  599. const { data = {}, comments = [] } = this.state;
  600. return (
  601. <div className="tab-layout">
  602. {data.have && <div className="m-b-1 t-r">
  603. <Button width={100} radius onClick={() => User.needLogin().then(() => this.setState({ showComment: true, comment: { channel: 'course-video', position: data.id } }))}>
  604. 写评论
  605. </Button>
  606. </div>}
  607. {(comments || []).map(item => {
  608. return <Comment data={item} />;
  609. })}
  610. </div>
  611. );
  612. }
  613. }