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

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

class LimitFree extends Component {
zhanghaozhe committed
17
  nav
zhanghaozhe committed
18
  state = {
zhanghaozhe committed
19 20
    tab: {},
    courses: [],
zhanghaozhe committed
21
    navItemStyle: {},
zhanghaozhe committed
22
    tabActiveIndex: 0,
zhanghaozhe committed
23 24 25
  }

  componentDidMount() {
zhanghaozhe committed
26
    document.title = "助力春招,好课限免--七月在线"
zhanghaozhe committed
27 28 29 30
    this.getData()
  }

  getData = () => {
zhanghaozhe committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    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
57 58
  }

zhanghaozhe committed
59
  handleClick = (id) => {
60
    this.props.history.push(`/detail?id=${id}`)
zhanghaozhe committed
61 62
  }

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

zhanghaozhe committed
72
  getCourse = (courseId, vCourseId) => {
zhanghaozhe committed
73
    const { user, history } = this.props
zhanghaozhe committed
74
    if (user.hasError) {
zhanghaozhe committed
75
      history.push("/passport")
zhanghaozhe committed
76 77
      return
    }
zhanghaozhe committed
78 79 80 81 82 83
    http
      .post(`${API.home}/sys/limitFree/receive`, {
        course_id: courseId,
      })
      .then((res) => {
        const { code, msg } = res.data
84 85
        if (code === 200) {
          const instance = Popup({
zhanghaozhe committed
86
            className: "get-course-popup",
87 88
            closable: false,
            clickMaskClose: false,
zhanghaozhe committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
            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>
                <button
                  onClick={() => {
                    this.toPlay(vCourseId)
                    instance.close()
                  }}
                >
                  立即学习
                </button>
              </div>
            ),
118 119 120 121 122
          })
        } else {
          showToast(msg)
        }
      })
zhanghaozhe committed
123 124
  }

zhanghaozhe committed
125
  toPlay = (id) => {
126 127 128
    this.props.history.push(`/play/video?id=${id}`)
  }

zhanghaozhe committed
129 130 131 132 133 134 135 136 137 138
  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"),
139 140
  })

zhanghaozhe committed
141
  render() {
zhanghaozhe committed
142
    const { tab, courses, navItemStyle, tabActiveIndex } = this.state
zhanghaozhe committed
143
    return (
zhanghaozhe committed
144 145
      <div className="limit-free">
        <HeaderBar arrow={true} title={"限时免费"}></HeaderBar>
zhanghaozhe committed
146
        <div className="banner">
zhanghaozhe committed
147 148 149 150
          <img
            src="https://julyedu-cdn.oss-cn-beijing.aliyuncs.com/time_limited_free/M/banner.png"
            alt=""
          />
zhanghaozhe committed
151
        </div>
zhanghaozhe committed
152
        <nav>
zhanghaozhe committed
153
          {/*<div className="prev-cover"></div>*/}
zhanghaozhe committed
154 155 156 157
          <ul ref={(el) => (this.nav = el)}>
            {tab &&
              !!tab.length &&
              tab.map((item, index) => {
zhanghaozhe committed
158
                return (
zhanghaozhe committed
159 160 161 162 163 164 165 166 167
                  <li
                    key={index}
                    className={index === tabActiveIndex ? "active" : ""}
                    style={navItemStyle}
                    onClick={(e) => this.changeTab(e, index)}
                  >
                    <a href={`#category${item.id}`} target={"_self"}>
                      {item.category_name}
                    </a>
zhanghaozhe committed
168 169
                  </li>
                )
zhanghaozhe committed
170
              })}
zhanghaozhe committed
171
          </ul>
zhanghaozhe committed
172
          <div className="next-cover"></div>
zhanghaozhe committed
173
        </nav>
zhanghaozhe committed
174
        <WhiteSpace />
zhanghaozhe committed
175 176
        <div className="course-list">
          <ul>
zhanghaozhe committed
177 178 179
            {tab &&
              !!tab.length &&
              tab.map((category) => {
zhanghaozhe committed
180
                return (
zhanghaozhe committed
181
                  <li key={category.id} className={"category"}>
zhanghaozhe committed
182
                    <h2 id={`category${category.id}`}>
zhanghaozhe committed
183 184 185 186
                      <img
                        src="https://julyedu-cdn.oss-cn-beijing.aliyuncs.com/time_limited_free/M/category-icon.png"
                        alt=""
                      />
zhanghaozhe committed
187
                      <span>{category.category_name}</span>
zhanghaozhe committed
188
                    </h2>
zhanghaozhe committed
189 190 191 192
                    <ul className={"courses"}>
                      {courses &&
                        courses.length &&
                        courses.map((item, index) => {
zhanghaozhe committed
193
                          if (item.category_id != category.id) {
zhanghaozhe committed
194 195 196
                            return null
                          }
                          /*
zhanghaozhe committed
197 198 199
                           * course_status:
                           * 0未领取 1已领取未过期 2 已领取已过期 3 正常已购买
                           * */
zhanghaozhe committed
200
                          let des, bottom
zhanghaozhe committed
201 202
                          switch (item.course_status) {
                            case 0:
zhanghaozhe committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
                              des = (
                                <div className={"learner"}>
                                  <i className="iconfont iconRectangleCopy4" />
                                  <span>{item.play_times}人学习</span>
                                </div>
                              )
                              bottom = (
                                <div className={"bottom"}>
                                  <span className={"red"}>限时免费</span>
                                  <span className={"origin-price"}>
                                    ¥{item.price0}
                                  </span>
                                  <button
                                    onClick={(e) => {
                                      e.stopPropagation()
                                      this.getCourse(
                                        item.course_id,
                                        item.v_course_id
                                      )
                                    }}
                                  >
                                    免费领取
                                  </button>
                                </div>
                              )
zhanghaozhe committed
228
                              break
zhanghaozhe committed
229
                            case 1:
zhanghaozhe committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
                              const { d, h, m } = this.formatTime(
                                item.course_expire
                              )
                              des = (
                                <div className={"remain-time"}>
                                  <i className={"iconfont iconiconfront-21"} />
                                  <span>
                                    {d}{h}{m}分后过期
                                  </span>
                                </div>
                              )
                              bottom = (
                                <div className={"bottom"}>
                                  <span className={"purchased"}>已领取</span>
                                  <StudyButton id={item.course_id} />
                                </div>
                              )
zhanghaozhe committed
247
                              break
zhanghaozhe committed
248
                            case 2:
zhanghaozhe committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
                              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>
                                  <Link to={`/detail?id=${item.course_id}`}>
                                    立即购买
                                  </Link>
                                </div>
                              )
zhanghaozhe committed
266
                              break
zhanghaozhe committed
267
                            case 3:
zhanghaozhe committed
268 269 270 271 272 273 274 275 276 277 278 279
                              des = (
                                <div className={"learner"}>
                                  <i className="iconfont iconRectangleCopy4" />
                                  <span>{item.play_times}人学习</span>
                                </div>
                              )
                              bottom = (
                                <div className="bottom">
                                  <span className={"purchased"}>已购买</span>
                                  <StudyButton id={item.course_id} />
                                </div>
                              )
zhanghaozhe committed
280
                          }
zhanghaozhe committed
281

zhanghaozhe committed
282
                          const info = (
zhanghaozhe committed
283 284
                            <div className="info">
                              <div className="title">{item.course_title}</div>
zhanghaozhe committed
285 286 287 288 289
                              {des}
                              {bottom}
                            </div>
                          )
                          return (
zhanghaozhe committed
290 291 292 293 294 295
                            <VList
                              img={item.image_name}
                              handleClick={this.handleClick}
                              id={item.course_id}
                              info={info}
                              key={index}
zhanghaozhe committed
296 297
                            />
                          )
zhanghaozhe committed
298
                        })}
zhanghaozhe committed
299 300 301
                    </ul>
                  </li>
                )
zhanghaozhe committed
302
              })}
zhanghaozhe committed
303 304
          </ul>
        </div>
zhanghaozhe committed
305
        <div className="no-more">-没有更多了-</div>
zhanghaozhe committed
306
      </div>
zhanghaozhe committed
307
    )
zhanghaozhe committed
308 309 310
  }
}

zhanghaozhe committed
311
function StudyButton({ id }) {
312 313 314 315
  return <Link to={`/play/video?id=${id}`}>立即学习</Link>
}

export default connect(
zhanghaozhe committed
316
  (state) => ({ user: state.user }),
317
  null
zhanghaozhe committed
318
)(WithFullSize(LimitFree))