index.js 8.91 KB
Newer Older
zhanghaozhe committed
1 2 3 4
import React, { Component } from 'react'
import { http } from "@/utils"
import './index.scss'
import { HeaderBar } from "@common/index"
zhanghaozhe committed
5
import { WhiteSpace, Toast } from "antd-mobile";
zhanghaozhe committed
6
import VList from '@/common/v-list-base'
zhanghaozhe committed
7
import { Popup } from "@common/index"
zhanghaozhe committed
8
import WithFullSize from "@/HOCs/WithFullSize"
9 10 11
import { connect } from "react-redux";
import { Link } from "react-router-dom";

zhanghaozhe committed
12

zhanghaozhe committed
13 14 15
function showToast(msg) {
  Toast.info(msg, 2, null, false)
}
zhanghaozhe committed
16 17 18

class LimitFree extends Component {

zhanghaozhe committed
19
  nav
zhanghaozhe committed
20
  state = {
zhanghaozhe committed
21 22
    tab: {},
    courses: [],
zhanghaozhe committed
23 24
    navItemStyle: {},
    tabActiveIndex: 0
zhanghaozhe committed
25 26 27
  }

  componentDidMount() {
zhanghaozhe committed
28
    document.title = '助力春招,好课限免--七月在线'
zhanghaozhe committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    this.getData()
  }

  getData = () => {
    Promise.all([http.get(`${API.home}/sys/category`), http.get(`${API.home}/sys/course`)])
      .then(res => {
        const [tab, courses] = res
        const {data: tabData, code: tabCode, msg: tabMsg} = tab.data
        const {data: coursesData, code: coursesCode, msg: coursesMsg} = courses.data
        if (tabCode == 200) {
          this.setState({
            tab: tabData
          })
        } else {
          showToast(tabMsg)
        }
        if (coursesCode === 200) {
          this.setState({
            courses: coursesData
          })
        } else {
          showToast(coursesMsg)
        }
      })
zhanghaozhe committed
53 54 55
  }


56 57
  handleClick = id => {
    this.props.history.push(`/detail?id=${id}`)
zhanghaozhe committed
58 59
  }

zhanghaozhe committed
60 61 62 63 64 65 66
  changeTab = (e, index) => {
    const {tabActiveIndex} = this.state
    if (tabActiveIndex !== index) {
      this.setState({
          tabActiveIndex: index
        }
      )
zhanghaozhe committed
67 68 69
    }
  }

zhanghaozhe committed
70
  getCourse = (courseId, vCourseId) => {
zhanghaozhe committed
71 72 73 74 75
    const {user, history} = this.props
    if (user.hasError) {
      history.push('/passport')
      return
    }
76
    http.post(`${API.home}/sys/limitFree/receive`, {
zhanghaozhe committed
77
      course_id: courseId
zhanghaozhe committed
78
    })
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
      .then(res => {
        const {code, msg} = res.data
        if (code === 200) {

          const instance = Popup({
            className: 'get-course-popup',
            closable: false,
            clickMaskClose: false,
            title: <div>
              <img src="https://julyedu-cdn.oss-cn-beijing.aliyuncs.com/time_limited_free/M/check.png" alt=""/>
              <div>课程有效期7天,快去学习吧~</div>
            </div>,
            content: <div className={'btns'}>
              <button onClick={() => {
                instance.close()
                this.getData()
              }}>知道了
              </button>
zhanghaozhe committed
97
              <button onClick={() => {
zhanghaozhe committed
98
                this.toPlay(vCourseId)
zhanghaozhe committed
99 100 101
                instance.close()
              }}>立即学习
              </button>
102 103 104 105 106 107 108 109
            </div>
          })

        } else {
          showToast(msg)
        }
      })

zhanghaozhe committed
110 111
  }

112 113 114 115 116 117 118 119 120 121
  toPlay = id => {
    this.props.history.push(`/play/video?id=${id}`)
  }

  formatTime = seconds => ({
    d: Math.floor(seconds / 60 / 60 / 24).toString().padStart(2, '0'),
    h: Math.floor(seconds / 60 / 60 % 24).toString().padStart(2, '0'),
    m: Math.floor(seconds / 60 % 60).toString().padStart(2, '0')
  })

zhanghaozhe committed
122
  render() {
zhanghaozhe committed
123
    const {tab, courses, navItemStyle, tabActiveIndex} = this.state
zhanghaozhe committed
124 125 126 127 128 129
    return (
      <div className='limit-free'>
        <HeaderBar arrow={true} title={'限时免费'}></HeaderBar>
        <div className="banner">
          <img src="https://julyedu-cdn.oss-cn-beijing.aliyuncs.com/time_limited_free/M/banner.png" alt=""/>
        </div>
zhanghaozhe committed
130
        <nav>
zhanghaozhe committed
131
          {/*<div className="prev-cover"></div>*/}
zhanghaozhe committed
132
          <ul ref={el => this.nav = el}>
zhanghaozhe committed
133
            {
zhanghaozhe committed
134
              tab && !!tab.length && tab.map((item, index) => {
zhanghaozhe committed
135
                return (
zhanghaozhe committed
136 137
                  <li key={index} className={index === tabActiveIndex ? 'active' : ''} style={navItemStyle}
                      onClick={e => this.changeTab(e, index)}>
zhanghaozhe committed
138
                    <a href={`#category${item.id}`} target={'_self'}>{item.category_name}</a>
zhanghaozhe committed
139 140 141 142
                  </li>
                )
              })
            }
zhanghaozhe committed
143
          </ul>
zhanghaozhe committed
144
          <div className="next-cover"></div>
zhanghaozhe committed
145 146 147 148
        </nav>
        <WhiteSpace/>
        <div className="course-list">
          <ul>
zhanghaozhe committed
149
            {
zhanghaozhe committed
150
              tab && !!tab.length && tab.map(category => {
zhanghaozhe committed
151
                return (
zhanghaozhe committed
152 153
                  <li key={category.id} className={'category'}>
                    <h2 id={`category${category.id}`}>
zhanghaozhe committed
154 155
                      <img src="https://julyedu-cdn.oss-cn-beijing.aliyuncs.com/time_limited_free/M/category-icon.png"
                           alt=""/>
zhanghaozhe committed
156
                      <span>{category.category_name}</span>
zhanghaozhe committed
157 158 159 160
                    </h2>
                    <ul className={'courses'}>
                      {
                        courses && courses.length && courses.map((item, index) => {
zhanghaozhe committed
161
                          if (item.category_id != category.id) {
zhanghaozhe committed
162 163 164 165 166 167
                            return null
                          }
                          /*
                          * course_status:
                          * 0未领取 1已领取未过期 2 已领取已过期 3 正常已购买
                          * */
zhanghaozhe committed
168
                          let des, bottom
zhanghaozhe committed
169 170
                          switch (item.course_status) {
                            case 0:
zhanghaozhe committed
171 172 173 174 175 176
                              des = <div className={'learner'}>
                                <i className='iconfont iconRectangleCopy4'/>
                                <span>{item.play_times}人学习</span>
                              </div>
                              bottom = <div className={'bottom'}>
                                <span className={'red'}>限时免费</span>
zhanghaozhe committed
177
                                <span className={'origin-price'}>¥{item.price0}</span>
zhanghaozhe committed
178 179
                                <button onClick={e => {
                                  e.stopPropagation()
zhanghaozhe committed
180
                                  this.getCourse(item.course_id, item.v_course_id)
zhanghaozhe committed
181 182
                                }}>免费领取
                                </button>
zhanghaozhe committed
183 184
                              </div>
                              break
zhanghaozhe committed
185
                            case 1:
186
                              const {d, h, m} = this.formatTime(item.course_expire)
zhanghaozhe committed
187 188
                              des = <div className={'remain-time'}>
                                <i className={'iconfont iconiconfront-21'}/>
189
                                <span>{d}{h}{m}分后过期</span>
zhanghaozhe committed
190
                              </div>
zhanghaozhe committed
191
                              bottom = <div className={'bottom'}>
zhanghaozhe committed
192
                                <span className={'purchased'}>已领取</span>
193
                                <StudyButton id={item.course_id}/>
zhanghaozhe committed
194
                              </div>
zhanghaozhe committed
195
                              break
zhanghaozhe committed
196 197 198 199 200 201 202 203
                            case 2:
                              des = <div className={'remain-time'}>
                                <i className={'iconfont iconiconfront-21'}/>
                                <span>{item.play_times}人学习</span>
                              </div>
                              bottom = <div className={'bottom'}>
                                <span className={'red'}>¥{item.price1}</span>
                                <span className={'origin-price'}>¥{item.price0}</span>
204
                                <Link to={`/detail?id=${item.course_id}`}>立即购买</Link>
zhanghaozhe committed
205 206
                              </div>
                              break
zhanghaozhe committed
207
                            case 3:
zhanghaozhe committed
208
                              des = <div className={'learner'}>
zhanghaozhe committed
209 210 211 212
                                <i className='iconfont iconRectangleCopy4'/>
                                <span>{item.play_times}人学习</span>
                              </div>
                              bottom = <div className="bottom">
zhanghaozhe committed
213
                                <span className={'purchased'}>已购买</span>
214
                                <StudyButton id={item.course_id}/>
zhanghaozhe committed
215
                              </div>
zhanghaozhe committed
216
                          }
zhanghaozhe committed
217

zhanghaozhe committed
218 219 220 221 222 223 224 225 226 227
                          const info = (
                            <div className='info'>
                              <div className='title'>{item.course_title}</div>
                              {des}
                              {bottom}
                            </div>
                          )
                          return (
                            <VList img={item.image_name}
                                   handleClick={this.handleClick}
228
                                   id={item.course_id}
zhanghaozhe committed
229 230 231 232 233 234 235 236
                                   info={info}
                                   key={index}
                            />
                          )
                        })
                      }
                    </ul>
                  </li>
zhanghaozhe committed
237

zhanghaozhe committed
238 239 240
                )
              })
            }
zhanghaozhe committed
241 242 243 244 245 246 247 248 249 250
          </ul>
        </div>
        <div className="no-more">
          -没有更多了-
        </div>
      </div>
    );
  }
}

251 252 253 254 255 256 257 258 259
function StudyButton({id}) {
  return <Link to={`/play/video?id=${id}`}>立即学习</Link>
}

export default connect(
  state => ({user: state.user}),
  null
)
(WithFullSize(LimitFree))