index.js 4.8 KB
Newer Older
zhanghaozhe committed
1 2
import React from "react"
import { Link, withRouter } from "react-router-dom"
zhanghaozhe committed
3
import { Course } from "src/common/index"
zhanghaozhe committed
4
import "./index.scss"
5 6 7 8

// 课程模块儿公共组件
// 课程数量是奇数第一个课程需要横着展示沾满一行,课程数量是偶数一行显示两个

zhanghaozhe committed
9 10
const HomeCourseList = function ({ modules, history }) {
  const toDetail = (id) => {
11 12 13
    history.push(`/detail?id=${id}`)
  }

zhanghaozhe committed
14 15 16 17 18 19 20 21 22 23 24
  return (
    modules.length > 0 &&
    modules.map((module, i) => {
      return (
        <div key={i}>
          <Module module={module} toDetail={toDetail} />
          <p className="borderTop" />
        </div>
      )
    })
  )
25 26
}

zhanghaozhe committed
27
const Module = function CourseList({ module, toDetail }) {
28 29 30
  let filterList = []
  let isOdd = module.list.length % 2 === 0

zhanghaozhe committed
31
  if (module.name === "限时免费") {
32 33 34 35 36 37 38
    filterList = module.list
  } else {
    // 数量为奇数时,第一个课程显示大图(如后台未上传,前台显示小图),课程数量为偶数时,均显示小图

    if (isOdd) {
      filterList = module.list
    } else {
zhanghaozhe committed
39 40 41 42
      filterList =
        module.list[0].course_img === module.list[0].course_img_small
          ? module.list
          : module.list.slice(1)
43 44 45
    }
  }

zhanghaozhe committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
  return (
    <div className="category home-course-list">
      <h2 className="title">
        {module.name}
        {module.name === "限时免费" && <span className={"hot"}>hot</span>}
      </h2>

      {module.show_more === 1 && (
        <Link className="more" to="/classify">
          查看更多 &gt;
        </Link>
      )}
      {module.show_more === 2 ? (
        module.name === "限时免费" ? (
          <Link className="more" to={"/free"}>
            查看更多 &gt;
          </Link>
        ) : (
          <Link className="more" to={module.more_page}>
            查看更多 &gt;
          </Link>
        )
      ) : null}
      {/* <LazyLoad offset={50}> */}
      <ul className="index-course-detail">
        {module.name !== "限时免费" &&
          !isOdd &&
          module.list[0].course_img !== module.list[0].course_img_small && (
            <div
              className="category-vip"
              onClick={() => toDetail(module.list[0].course_id)}
            >
              <img src={module.list[0].course_img} alt="" />
            </div>
          )}
        {filterList.map((item, index) => {
          const top = item.is_limit_free ? null : (
            <div>
              {item.is_audition === true && (
                <span className="audition">
                  <i className={"iconfont iconerji"}></i>试
                </span>
              )}
              {item.is_aist && <span className="return_bash"></span>}
            </div>
          )

          const bottom = <Bottom course={item} />

          const status = item.is_limit_free ? null : (
            <div>
              {item.is_bargain && (
                <p className="course-status">砍价减{item.bargain_price}</p>
              )}
              {item.is_groupon && (
                <p className="course-status">拼团价{item.groupon_price}</p>
              )}
103
            </div>
zhanghaozhe committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
          )
          return (
            <Course
              key={index}
              top={top}
              data={item}
              bottom={bottom}
              status={status}
              img={item.course_img_small}
              title={item.course_title}
              id={item.course_id}
              toDetail={toDetail}
              className="text-overflow-2"
            />
          )
        })}
120
      </ul>
zhanghaozhe committed
121 122 123
      {/* </LazyLoad> */}
    </div>
  )
124 125 126
}

//限时免费
zhanghaozhe committed
127
function LimitFree({ course }) {
128
  /*
zhanghaozhe committed
129 130 131 132
   *
   * limit_free_status: 0-未领取 1-已领取 2-已过期
   *
   * */
133 134
  switch (course.limit_free_status) {
    case 0:
zhanghaozhe committed
135 136 137 138 139 140 141 142
      return (
        <Link to={`/detail?id=${course.course_id}`}>
          <p className={"course-price"}>
            <span className={"free"}>免费领取</span>
            <span className={"old"}>¥{course.price}</span>
          </p>
        </Link>
      )
143
    case 1:
zhanghaozhe committed
144
      return <div className={"isbuy"}>已领取</div>
145
    default:
zhanghaozhe committed
146 147 148 149 150 151
      return (
        <p className="course-price">
          <span className="new">¥{course.discounts_price}</span>
          <span className="old">¥{course.price}</span>
        </p>
      )
152 153 154 155
  }
}

//课程底部
zhanghaozhe committed
156
function Bottom({ course }) {
157 158
  if (course.is_buy) {
    if (course.is_limit_free && course.limit_free_status === 1) {
zhanghaozhe committed
159
      return <div class={"isbuy"}>已领取</div>
160
    } else {
zhanghaozhe committed
161
      return <div className={"isbuy"}>已购买</div>
162 163
    }
  } else {
zhanghaozhe committed
164 165 166 167
    return course.is_limit_free ? (
      <LimitFree course={course} />
    ) : (
      <p className="course-price">
168 169 170
        <span className="new">¥{course.discounts_price}</span>
        <span className="old">¥{course.price}</span>
      </p>
zhanghaozhe committed
171
    )
172 173 174
  }
}

zhanghaozhe committed
175
export default withRouter(HomeCourseList)