1
0

page.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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. this.setState({ data: result });
  197. // 选择课时
  198. if (this.state.search.no) {
  199. this.onChangeItem(this.state.search.no);
  200. } else {
  201. this.onChangeItem(1);
  202. }
  203. this.refreshNote();
  204. });
  205. }
  206. refreshAsk(position) {
  207. const { id } = this.params;
  208. const { item } = this.state;
  209. Course.listAsk(Object.assign({ page: 1, size: 1000, courseId: id, courseNoId: item.id, position })).then(result => {
  210. this.setState({ asks: result.list });
  211. });
  212. }
  213. refreshNote() {
  214. const { id } = this.params;
  215. const { data } = this.state;
  216. if (!data.have) return;
  217. My.listCourseNote({ courseId: id, page: 1, size: data.courseNos.length })
  218. .then((result) => {
  219. this.noteMap = getMap(result.list, 'courseNoId');
  220. data.courseNos.forEach((row) => {
  221. const note = this.noteMap[row.id];
  222. if (note) row.note = true;
  223. });
  224. this.setState({ data });
  225. });
  226. }
  227. onChangeRightTab(rightTab) {
  228. this.setState({ rightTab });
  229. }
  230. onChangeTab(tab) {
  231. this.setState({ tab });
  232. }
  233. onChangeItem(key) {
  234. key = Number(key);
  235. this.changeQuery({ no: key });
  236. this.setState({ no: key });
  237. const { data, item } = this.state;
  238. const index = key - 1;
  239. const timelineSelect = [];
  240. const current = data.courseNos[index];
  241. if (current) {
  242. const max = current.time;
  243. let start = 0;
  244. let end = start + 5;
  245. while (start < max) {
  246. timelineSelect.push({
  247. title: `${start} - ${end}min`,
  248. key: `${start}`,
  249. });
  250. start += 5;
  251. end = Math.min(start + 5, max);
  252. }
  253. }
  254. // 切换播放,记录进度
  255. if (item) {
  256. this.updateProgress(item.id, this.lastSecond, item.time, true);
  257. }
  258. this.setState({ item: current, timelineSelect });
  259. }
  260. onTimeUpdate(second) {
  261. const { position, item, data } = this.state;
  262. if (!data.have) {
  263. // 如果是试用,则按秒数增加
  264. second += item.startTrail * 60;
  265. }
  266. const minute = second / 60;
  267. const nowPosition = (minute / 5) * 5;
  268. if (nowPosition !== position) {
  269. this.refreshAsk(position);
  270. this.setState({ position: nowPosition });
  271. // 定时更新进度
  272. this.updateProgress(item.id, second, item.time);
  273. }
  274. this.lastSecond = second;
  275. }
  276. playVideo() {
  277. // 开始计时
  278. this.lastTime = new Date();
  279. }
  280. pauseVideo() {
  281. // 停止计时
  282. const now = new Date();
  283. this.time += (now.getTime() - this.lastTime.getTime()) / 1000;
  284. this.lastTime = null;
  285. }
  286. next() {
  287. const { data, item } = this.state;
  288. if (data.courseNos.length === item.no) {
  289. return;
  290. }
  291. this.onChangeItem(item.no + 1);
  292. }
  293. onVideoAction(key) {
  294. const { rightTab, showTab, showAsk, showNote, item } = this.state;
  295. switch (key) {
  296. case 'ask':
  297. return this.setState({ showAsk: !showAsk });
  298. case 'note':
  299. return this.setState({ showNote: !showNote, note: this.noteMap ? this.noteMap[item.id] || {} : {} });
  300. case 'answer':
  301. return this.setState({ showTab: rightTab === '1' ? !showTab : true, rightTab: '1' });
  302. case 'list':
  303. return this.setState({ showTab: rightTab === '2' ? !showTab : true, rightTab: '2' });
  304. default:
  305. return '';
  306. }
  307. }
  308. updateProgress(courseNoId, currentTime, totalTime, record) {
  309. if (!this.lastTime) return;
  310. const { id } = this.params;
  311. const now = new Date();
  312. this.time += (now.getTime() - this.lastTime.getTime()) / 1000;
  313. this.lastTime = now;
  314. const progress = formatPercent(currentTime, totalTime);
  315. if (record || this.time > 600) {
  316. // 最长5分钟记录一次
  317. Course.noProgress(id, courseNoId, progress, this.time, courseNoId);
  318. this.time = 0;
  319. } else {
  320. Course.noProgress(id, courseNoId, progress, null, null);
  321. }
  322. }
  323. buy() {
  324. const { data } = this.props;
  325. User.needLogin().then(() => {
  326. Order.speedPay({ productType: 'course', productId: data.id }).then(result => {
  327. User.needPay(result).then(() => {
  328. this.refresh();
  329. });
  330. });
  331. });
  332. }
  333. add() {
  334. const { data } = this.props;
  335. User.needLogin().then(() => {
  336. Order.addCheckout({ productType: 'course', productId: data.id }).then(() => {
  337. this.setState({ add: true });
  338. });
  339. });
  340. }
  341. viewAsk(id) {
  342. Course.askView(id);
  343. }
  344. setVideo(video) {
  345. this.video = video;
  346. }
  347. renderView() {
  348. const { base = {}, data = {}, item = {}, add, progress, rightTab, showTab, showAsk, showNote, dataStructMap = {}, showComment, comment = {}, showFaq, faq = {}, showFinish, note = {}, ask = {}, timelineSelect = [] } = this.state;
  349. const { courseNos = [] } = data;
  350. return (
  351. <div>
  352. <div className="top content t-8">
  353. 千行课堂 > 全部课程 > {data.parentStructId > 0 ? `${(dataStructMap[data.parentStructId] || {}).title} >` : ''}{' '}
  354. {(dataStructMap[data.structId] || {}).title} > {data.title} > <span className="t-1">课程详情</span>
  355. </div>
  356. <div className="center content">
  357. <div className="t-1 t-s-20">
  358. {data.title}
  359. <div className="action f-r">
  360. {!data.have && <Button className="m-r-1" radius size="lager" onClick={() => this.buy()}>
  361. 立即购买
  362. </Button>}
  363. {!data.have && <Button theme="default" radius size="lager" disabled={data.add || add} onClick={() => this.add()}>
  364. <Assets name="add" />
  365. </Button>}
  366. {data.have && <Button className="m-r-1" radius size="lager" onClick={() => linkTo('/my/course')}>
  367. 我的课程
  368. </Button>}
  369. </div>
  370. </div>
  371. <div className="t-2 m-b-1">授课老师:{data.teacher}</div>
  372. <div className="detail">
  373. <div className="left">
  374. {data.have && <div hidden={(item.paper && item.paper.times > 0)} className="left-top">
  375. <span className="d-i-b m-r-1">预习作业</span>
  376. <span className="d-i-b m-r-2">
  377. <ProgressText width={480} size="small" progress={item.report ? formatPercent(item.report.userNumber, item.report.questionNumber) : 0} />
  378. </span>
  379. <Button className="f-r" radius onClick={() => (item.report ? Question.continueLink('preview', item) : Question.startLink('preview', item))}>
  380. 做题
  381. </Button>
  382. </div>}
  383. <div className="video-layout">
  384. {item && <Video
  385. key={item.id}
  386. src={item.resource}
  387. duration={10}
  388. ref={ref => this.setVideo(ref)}
  389. btnList={[
  390. { title: '提问', key: 'ask', show: data.have, active: showAsk, pause: true },
  391. {
  392. key: 'answer',
  393. render(active) {
  394. return <Assets name={active ? 'question_on' : 'question_off'} />;
  395. },
  396. full: true,
  397. active: showTab && rightTab === '1',
  398. },
  399. { title: '笔记', key: 'note', show: data.have, active: showNote, pause: true },
  400. { title: '课表', key: 'list', full: true, active: showTab && rightTab === '2' },
  401. ]}
  402. onPlay={() => this.playVideo()}
  403. onPause={() => this.pauseVideo()}
  404. onNext={() => this.next()}
  405. onAction={key => this.onVideoAction(key)}
  406. onTimeUpdate={time => this.onTimeUpdate(time)}
  407. onFullChange={() => this.setState({ showTab: true, rightTab: '1' })}
  408. >
  409. <div hidden={!showTab} className="video-fixed tab-warpper">
  410. <Tabs
  411. type="division"
  412. theme="gray"
  413. space={2.5}
  414. active={rightTab}
  415. tabs={[{ title: '精选问答', key: '1' }, { title: '课时列表', key: '2' }]}
  416. onChange={key => this.onChangeRightTab(key)}
  417. />
  418. <div className="tab-body">{this[`renderRightTab${rightTab}`]()}</div>
  419. </div>
  420. </Video>}
  421. </div>
  422. </div>
  423. <div className={`right ${progress > 0 ? 'progress' : ''} tab-warpper`}>
  424. <Tabs
  425. type="division"
  426. theme="gray"
  427. space={2.5}
  428. active={rightTab}
  429. tabs={[{ title: '精选问答', key: '1' }, { title: '课时列表', key: '2' }]}
  430. onChange={key => this.onChangeRightTab(key)}
  431. />
  432. <div className="tab-body">{this[`renderRightTab${rightTab}`]()}</div>
  433. </div>
  434. </div>
  435. {data.have && <UserTable columns={this.columns} list={courseNos} />}
  436. </div>
  437. <div hidden={data.have} className="bottom">
  438. <div className="content">{this.renderTab()}</div>
  439. </div>
  440. <Contact data={base.contact} />
  441. <Footer />
  442. <AskCourseModal show={showAsk} defaultData={ask} course={data} courseNo={item} selectList={timelineSelect} onConfirm={() => this.setState({ showAsk: false })} onCancel={() => this.setState({ showAsk: false })} />
  443. <CourseNoteModal show={showNote} defaultData={note} course={data} courseNos={courseNos} noteMap={this.noteMap} onConfirm={() => {
  444. this.setState({ showNote: false });
  445. this.refreshNote();
  446. }} onCancel={() => this.setState({ showNote: false })} />
  447. <CommentModal
  448. show={showComment}
  449. defaultData={comment}
  450. onConfirm={() => this.setState({ showComment: false, showFinish: true })}
  451. onCancel={() => this.setState({ showComment: false })}
  452. onClose={() => this.setState({ showComment: false })}
  453. />
  454. <FaqModal show={showFaq} defaultData={faq} onCancel={() => this.setState({ showFaq: false })} onConfirm={() => this.setState({ showFaq: false, showFinish: true })} />
  455. <FinishModal
  456. show={showFinish}
  457. onConfirm={() => this.setState({ showFinish: false })}
  458. />,
  459. </div >
  460. );
  461. }
  462. renderRightTab1() {
  463. const { asks = [], data = {}, position } = this.state;
  464. return [
  465. <div className="all-answer">
  466. <span className="d-i-b b m-r-5" />
  467. <span className="d-i-b t-6">{position}:00~{position + 5}:00</span>
  468. <a className="f-r d-i-b t-4 c-p" href={`/course/answer/${data.id}`} target="_blank">全部问答 ></a>
  469. </div>,
  470. <div className="answer-layout">
  471. {asks.map(item => {
  472. return (
  473. <div className="answer-item">
  474. <div>
  475. <div className="small-tag">提问</div>
  476. </div>
  477. <div className="desc">
  478. <OpenText>{item.content}</OpenText>
  479. </div>
  480. {item.answerStatus > 0 && (
  481. <div>
  482. <div className="small-tag">回答</div>
  483. </div>
  484. )}
  485. {item.answerStatus > 0 && (
  486. <div className="desc">
  487. <OpenText onOpen={() => this.viewAsk(item.id)}>{item.answer}</OpenText>
  488. </div>
  489. )}
  490. </div>
  491. );
  492. })}
  493. </div>,
  494. ];
  495. }
  496. renderRightTab2() {
  497. const { data = {}, no } = this.state;
  498. const { courseNos = [] } = data;
  499. return (
  500. <div className="item-layout">
  501. {courseNos.map(item => {
  502. return (
  503. <div className={`item ${item.no === no ? 'active' : ''}`} onClick={() => this.onChangeItem(item.no)}>
  504. <span className="t-1">课时{item.no}</span>
  505. <span className="t-2">{item.title}</span>
  506. </div>
  507. );
  508. })}
  509. </div>
  510. );
  511. }
  512. renderTab() {
  513. const { tab } = this.state;
  514. return [
  515. <div className="bottom">
  516. <div className="content">
  517. <Tabs
  518. type="full"
  519. border
  520. active={tab}
  521. tabs={[
  522. { title: '课程介绍', key: '1' },
  523. { title: '授课大纲', key: '2' },
  524. { title: '小助手', key: '3' },
  525. { title: 'FAQs', key: '4' },
  526. { title: '优惠信息', key: '5' },
  527. { title: '学员评价', key: '6' },
  528. ]}
  529. onChange={key => this.onChangeTab(key)}
  530. />
  531. {this[`renderTab${tab}`]()}
  532. </div>
  533. </div>,
  534. ];
  535. }
  536. renderTab1() {
  537. const { data = {} } = this.state;
  538. return (
  539. <div className="tab-layout">
  540. <div className="tab-title">老师资历</div>
  541. <div className="tab-desc" dangerouslySetInnerHTML={{ __html: data.teacherContent }} />
  542. <div className="tab-title">基本参数</div>
  543. <div className="tab-desc" dangerouslySetInnerHTML={{ __html: data.baseContent }} />
  544. <div className="tab-title">授课重点</div>
  545. <div className="tab-desc" dangerouslySetInnerHTML={{ __html: data.pointContent }} />
  546. <div className="tab-title">适合人群</div>
  547. <div className="tab-desc" dangerouslySetInnerHTML={{ __html: data.crowdContent }} />
  548. </div>
  549. );
  550. }
  551. renderTab2() {
  552. const { data } = this.state;
  553. return (
  554. <div className="tab-layout">
  555. <div className="tab-desc" dangerouslySetInnerHTML={{ __html: data.syllabusContent }} />
  556. </div>
  557. );
  558. }
  559. renderTab3() {
  560. return (
  561. <div className="tab-layout">
  562. <div className="qr-layout">
  563. <Assets name="qrcode" className="m-r-2 v-a-t" />
  564. <div className="p-l-1 d-i-b t-l">
  565. <div className="t-1">千行小助手:</div>
  566. <div className="t-1 m-b-2">1232104-310431</div>
  567. <div className="t-2 t-s-12">微信扫码添加千行小助手为好友,</div>
  568. <div className="t-2 t-s-12">咨询课程,了解更多信息</div>
  569. </div>
  570. </div>
  571. </div>
  572. );
  573. }
  574. renderTab4() {
  575. const { faqs, data = {}, showFaq, faq } = this.state;
  576. return (
  577. <div className="tab-layout">
  578. <AnswerCarousel
  579. hideBtn
  580. list={faqs}
  581. onFaq={() => this.setState({ showFaq: true, faq: { channel: 'course-video', position: data.id } })}
  582. />
  583. <FaqModal show={showFaq} defaultData={faq} onCancel={() => this.setState({ showFaq: false })} onConfirm={() => this.setState({ showFaq: false })} />
  584. </div>
  585. );
  586. }
  587. renderTab5() {
  588. const { data = {} } = this.state;
  589. return (
  590. <div className="tab-layout">
  591. <div className="tab-desc" dangerouslySetInnerHTML={{ __html: data.promoteContent }} />
  592. </div>
  593. );
  594. }
  595. renderTab6() {
  596. const { data = {}, comments = [] } = this.state;
  597. return (
  598. <div className="tab-layout">
  599. {data.have && <div className="m-b-1 t-r">
  600. <Button width={100} radius onClick={() => User.needLogin().then(() => this.setState({ showComment: true, comment: { channel: 'course-video', position: data.id } }))}>
  601. 写评论
  602. </Button>
  603. </div>}
  604. {(comments || []).map(item => {
  605. return <Comment data={item} />;
  606. })}
  607. </div>
  608. );
  609. }
  610. }