page.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import React from 'react';
  2. import './index.less';
  3. import { Tabs } from 'antd-mobile';
  4. import Page from '@src/containers/Page';
  5. import Money from '../../../components/Money';
  6. import Button from '../../../components/Button';
  7. import { FAQItem, CommentItem } from '../../../components/Item';
  8. import { Course } from '../../../stores/course';
  9. import { Main } from '../../../stores/main';
  10. import { Order } from '../../../stores/order';
  11. import Icon from '../../../components/Icon';
  12. export default class extends Page {
  13. initState() {
  14. return { tab: 'serviceContent', number: 1 };
  15. }
  16. initData() {
  17. const { id } = this.params;
  18. Main.getPromote().then(result => {
  19. this.promote = result.vs_list || [];
  20. Course.get(id).then(data => {
  21. this.setState({ data });
  22. this.changeNumber(data.minNumber || 1);
  23. });
  24. });
  25. }
  26. changeNumber(number) {
  27. const { data } = this.state;
  28. let price = data.price * number;
  29. let max = 0;
  30. let maxIndex = -1;
  31. this.promote.forEach((row, i) => {
  32. if (row.number <= number) {
  33. if (row.number > max) {
  34. max = number;
  35. maxIndex = i;
  36. }
  37. }
  38. });
  39. if (maxIndex >= 0) {
  40. price *= this.promote[maxIndex].percent / 100;
  41. }
  42. this.setState({ number, price });
  43. }
  44. buy() {
  45. const { number } = this.state;
  46. Order.speedPay({ productType: 'course', productId: this.params.id, number })
  47. .then((result) => {
  48. linkTo(`/pay/${result.id}`);
  49. });
  50. }
  51. renderText() {
  52. const { tab, data } = this.state;
  53. let content;
  54. switch (tab) {
  55. case 'serviceContent':
  56. case 'crowdContent':
  57. case 'processContent':
  58. content = <div dangerouslySetInnerHTML={{ __html: data[tab] }} />;
  59. break;
  60. case 'faq':
  61. content = <div>{(data.faqs || []).map(row => <FAQItem data={row} />)}</div>;
  62. break;
  63. case 'comment':
  64. content = <div>{(data.comments || []).map(row => <CommentItem data={row} />)}</div>;
  65. break;
  66. default:
  67. break;
  68. }
  69. return content;
  70. }
  71. renderView() {
  72. const { data = {}, tab, number, price } = this.state;
  73. return (
  74. <div>
  75. <div className="b-g" style={{ backgroundImage: `url(${data.cover})` }}>
  76. <div className="title">{data.title}</div>
  77. </div>
  78. {/* <div className="tip">访问{PcUrl}/course/detail/{data.id},试听该课程</div> */}
  79. <div className="detail">
  80. <Tabs
  81. page={tab}
  82. tabs={[
  83. { title: '服务介绍', key: 'serviceContent' },
  84. { title: '适合人群', key: 'crowdContent' },
  85. { title: '授课流程', key: 'processContent' },
  86. { title: 'FAQs', key: 'faq' },
  87. { title: '用户评价', key: 'comment' },
  88. ]}
  89. onChange={(value) => {
  90. this.setState({ tab: value.key });
  91. }}
  92. />
  93. {this.renderText()}
  94. </div>
  95. <div className="fixed">
  96. <div className="action">
  97. <div className="minus" onClick={() => this.changeNumber(number === 1 ? number : number - 1)}>
  98. <Icon type="minus" />
  99. </div>
  100. <div className="num">{number}</div>
  101. <div className="add" onClick={() => this.changeNumber(number + 1)}>
  102. <Icon type="plus" />
  103. </div>
  104. </div>
  105. <div className="fee">
  106. 总额: <Money value={price} size="lager" />
  107. </div>
  108. <Button width={110} className="f-r" radius onClick={() => {
  109. this.buy();
  110. }}>
  111. 立即购买
  112. </Button>
  113. </div>
  114. </div>
  115. );
  116. }
  117. }