page.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import React from 'react';
  2. import './index.less';
  3. import Page from '@src/containers/Page';
  4. import Assets from '@src/components/Assets';
  5. import { getMap, formatTreeData } from '@src/services/Tools';
  6. import Footer from '../../../components/Footer';
  7. import { FaqModal, FinishModal } from '../../../components/OtherModal';
  8. import { CommentFalls, AnswerCarousel, Consultation, Contact } from '../../../components/Other';
  9. import Tabs from '../../../components/Tabs';
  10. import Filter from '../../../components/Filter';
  11. import { SingleItem, PackageItem } from '../../../components/Item';
  12. import UserPagination from '../../../components/UserPagination';
  13. import { Main } from '../../../stores/main';
  14. import { Course } from '../../../stores/course';
  15. import { User } from '../../../stores/user';
  16. export default class extends Page {
  17. initState() {
  18. return {
  19. list: [],
  20. tab: 'single',
  21. };
  22. }
  23. init() {
  24. Main.getPromote()
  25. .then(result => {
  26. this.setState({ promote: result });
  27. });
  28. Main.getBase()
  29. .then(result => {
  30. this.setState({ base: result });
  31. });
  32. Main.courseStruct().then(result => {
  33. const courseStruct = result.map(row => {
  34. return {
  35. level: row.level,
  36. title: `${row.titleZh}${row.titleEn}`,
  37. key: `${row.id}`,
  38. parentId: row.parentId,
  39. isExamination: row.isExamination,
  40. };
  41. });
  42. const courseStructSelect = courseStruct.filter(row => row.level === 1);
  43. const packageStructSelect = courseStructSelect.filter(row => row.isExamination);
  44. courseStructSelect.unshift({
  45. title: '全部',
  46. key: '',
  47. });
  48. packageStructSelect.unshift({
  49. title: '全部',
  50. key: '',
  51. });
  52. const courseStructTree = formatTreeData(courseStruct, 'key', 'title', 'parentId');
  53. const courseStructTreeMap = getMap(courseStructTree, 'key', 'children');
  54. this.setState({ courseStructSelect, courseStructTreeMap, packageStructSelect });
  55. });
  56. }
  57. initData() {
  58. const data = Object.assign(this.state, this.state.search);
  59. if (data.order) {
  60. data.sortMap = { [data.order]: data.direction };
  61. }
  62. data.filterMap = this.state.search;
  63. this.setState(data);
  64. const { tab } = this.state;
  65. switch (tab) {
  66. case 'single':
  67. this.refreshSingle();
  68. break;
  69. case 'package':
  70. this.refreshPackage();
  71. break;
  72. default:
  73. break;
  74. }
  75. }
  76. refreshSingle() {
  77. Main.listFaq({ page: 1, size: 100, channel: 'course-video_index' }).then(result => {
  78. this.setState({ faqs: result.list });
  79. });
  80. Main.listComment({ page: 1, size: 100, channel: 'course-video_index' }).then(result => {
  81. this.setState({ comments: result.list });
  82. });
  83. Course.listVideo(Object.assign({ structId: this.state.search.parentStructId }, this.state.search))
  84. .then(result => {
  85. this.setState({ list: result.list, total: result.total });
  86. });
  87. }
  88. refreshPackage() {
  89. Main.listFaq({ page: 1, size: 100, channel: 'course-package_index' }).then(result => {
  90. this.setState({ faqs: result.list });
  91. });
  92. Main.listComment({ page: 1, size: 100, channel: 'course-package_index' }).then(result => {
  93. this.setState({ comments: result.list });
  94. });
  95. Course.listPackage(Object.assign({}, this.state.search))
  96. .then(result => {
  97. this.setState({ list: result.list, total: result.total });
  98. });
  99. }
  100. onTabChange(tab) {
  101. const data = { tab };
  102. this.refreshQuery(data);
  103. }
  104. onFilter(key, value) {
  105. const { filterMap } = this.state;
  106. filterMap[key] = value;
  107. this.search(filterMap, false);
  108. this.initData();
  109. }
  110. onChangePage(page) {
  111. this.search({ page }, false);
  112. this.initData();
  113. }
  114. renderView() {
  115. const { number } = this.props.order;
  116. const { tab, promote = {}, base = {}, comments, faqs, showFaq, faq, showFinish } = this.state;
  117. return (
  118. <div>
  119. <div className="top content">
  120. <Tabs type="text" active={'online'} tabs={[{ title: '在线课程', key: 'online', path: '/course/online' }, { title: '1v1私教', key: 'vs', path: '/course/vs' }]} />
  121. <div className="f-r">
  122. <span className="t-2 m-r-1">{(promote.video || {}).text ? `优惠活动:${promote.video.text}` : ''}</span>
  123. <Assets name="cart" onClick={() => linkTo('/cart')} />
  124. <span className="t-2">( {number || 0} )</span>
  125. </div>
  126. </div>
  127. <div className="center">
  128. <div className="content">
  129. <Tabs
  130. type="division"
  131. theme="theme"
  132. size="small"
  133. space={2.5}
  134. width={100}
  135. border
  136. active={tab}
  137. tabs={[{ title: '单项购买', key: 'single' }, { title: '套餐购买', key: 'package' }]}
  138. onChange={key => this.onTabChange(key)}
  139. />
  140. {this[`renderTab${tab}`]()}
  141. </div>
  142. </div>
  143. <Consultation data={base.contact} />
  144. <CommentFalls list={comments} />
  145. <AnswerCarousel list={faqs} onFaq={() => User.needLogin().then(() => this.setState({ showFaq: true, faq: { channel: tab === 'single' ? 'course-video_index' : 'course-package_index' } }))} />
  146. <Contact data={base.contact} />
  147. <Footer />
  148. <FaqModal show={showFaq} defaultData={faq} onCancel={() => this.setState({ showFaq: false, faq: {} })} onConfirm={() => this.setState({ showFaq: false, faq: {}, showFinish: true })} />
  149. <FinishModal
  150. show={showFinish}
  151. onConfirm={() => this.setState({ showFinish: false })}
  152. />
  153. </div>
  154. );
  155. }
  156. renderTabsingle() {
  157. const { filterMap, list = [], courseStructSelect, courseStructTreeMap = {}, total, page } = this.state;
  158. const child = (courseStructTreeMap[filterMap.parentStructId] || []).map(row => {
  159. return {
  160. title: row.title,
  161. key: row.key,
  162. };
  163. });
  164. const filterList = [
  165. {
  166. key: 'parentStructId',
  167. children: courseStructSelect,
  168. }];
  169. if (child.length > 0) {
  170. child.unshift({
  171. title: '全部',
  172. key: '',
  173. });
  174. filterList.push({
  175. key: 'structId',
  176. children: child,
  177. });
  178. }
  179. return [
  180. <Filter
  181. filter={filterMap}
  182. list={filterList}
  183. onFilter={(key, value) => this.onFilter(key, value)}
  184. />,
  185. <div className="tab-1-list">
  186. {list.map((data) => {
  187. return <SingleItem data={data} />;
  188. })}
  189. </div>,
  190. total > 0 && list.length > 0 && (
  191. <UserPagination total={total} current={page} pageSize={this.state.search.size} onChange={p => this.onChangePage(p)} />
  192. ),
  193. ];
  194. }
  195. renderTabpackage() {
  196. const { filterMap, list = [], packageStructSelect, total, page } = this.state;
  197. return [
  198. <Filter
  199. filter={filterMap}
  200. list={[
  201. {
  202. key: 'structId',
  203. children: packageStructSelect,
  204. },
  205. ]}
  206. onFilter={(key, value) => this.onFilter(key, value)}
  207. />,
  208. <div className="tab-2-list">
  209. {list.map((data) => {
  210. return <PackageItem data={data} />;
  211. })}
  212. </div>,
  213. total > 0 && list.length > 0 && (
  214. <UserPagination total={total} current={page} pageSize={this.state.search.size} onChange={p => this.onChangePage(p)} />
  215. ),
  216. ];
  217. }
  218. }