courselist.js 9.74 KB
Newer Older
2  
xuzhenghua committed
1 2 3
import React, { Component } from 'react'
import { VList } from '../../common'
import { Tabs, WhiteSpace, Toast } from 'antd-mobile'
xuzhenghua committed
4
import './courselist.scss'
xuzhenghua committed
5
import HeaderSearch from '../../common/HeaderSearch/index'
zhanghaozhe committed
6 7
import { http, getParam } from "src/utils"
import Loading from 'src/common/Loading'
2  
xuzhenghua committed
8
import { connect } from 'react-redux';
FE committed
9
import { StickyContainer, Sticky } from "react-sticky";
2  
xuzhenghua committed
10 11 12


function stopScroll(e) {
zhanghaozhe committed
13
  e.preventDefault()
2  
xuzhenghua committed
14
}
xuzhenghua committed
15

16
@connect(({user}) => ({
zhanghaozhe committed
17
  user
18
}))
xuzhenghua committed
19
class Classify extends Component {
zhanghaozhe committed
20 21 22 23 24 25 26 27 28 29 30
  constructor(props) {
    super(props)
    this.state = {
      ispull: false,
      display: 'none',
      arr: [{basics: []}, {advanced: []}],
      allClass: [],
      data: [],
      activeTab: decodeURIComponent(getParam('name')),
      isLoading: true,
      top: 44
xuzhenghua committed
31 32
    }

zhanghaozhe committed
33
  }
FE committed
34

zhanghaozhe committed
35 36 37
  componentDidMount() {
    this.getTabs()
    this.getList()
xuzhenghua committed
38

zhanghaozhe committed
39 40 41 42 43
    const el = document.querySelector('.search-nav');
    this.setState({
      top: el.offsetHeight
    });
  }
2  
xuzhenghua committed
44

zhanghaozhe committed
45 46 47
  componentWillUnmount() {
    document.removeEventListener('touchmove', stopScroll)
  }
2  
xuzhenghua committed
48

xuzhenghua committed
49

zhanghaozhe committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  // 获取tabs接口
  getTabs = () => {
    let data = 0
    http.get(`${API.home}/m/course/classify/${data}`)
      .then((res) => {
        const _this = this
        if (res.data.code === 200) {
          if (res.data.data.common.length > 0) {
            let arr = ['basics', 'advanced']
            let arr2 = [{basics: []}, {advanced: []}]
            let arr3 = []
            arr.forEach(function (item, index) {
              arr2[item] = res.data.data.common[index]
              res.data.data.common[index].list.forEach(function (item, index) {
                arr3.push({'title': item.c_name, 'id': item.c_id})
              })
xuzhenghua committed
66
            })
zhanghaozhe committed
67 68 69
            _this.setState({
              arr: arr2,
              allClass: arr3
xuzhenghua committed
70
            })
zhanghaozhe committed
71 72 73 74
          }
        } else {
          Toast.info(res.data.msg, 2)
        }
xuzhenghua committed
75

zhanghaozhe committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
      })
      .catch(err => {
        console.log(err)
      })
  }

  // 获取课程接口
  getList = () => {
    const _this = this
    _this.setState((state, props) => ({
      isLoading: true
    }));
    http.get(`${API.home}/m/course/list/${getParam('id')}`).then((res) => {
      if (res.data.code === 200) {
        _this.setState({
          data: res.data.data,
          isLoading: false
xuzhenghua committed
93
        })
zhanghaozhe committed
94 95 96
      }
    })
  }
xuzhenghua committed
97

xuzhenghua committed
98

zhanghaozhe committed
99 100 101 102 103 104 105 106
  // 点击横向滚动tab查询
  ontabclick = (tab) => {
    this.props.history.push(`/courselist?id=${tab.id}&name=${tab.title}`)
    this.getList()
    this.setState({
      activeTab: decodeURIComponent(getParam('name'))
    });
  }
xuzhenghua committed
107

zhanghaozhe committed
108 109 110 111 112 113 114 115 116 117 118
  // 上下展示
  pulldown = () => {
    this.setState(status => ({
      ispull: !status.ispull,
      display: status.ispull ? 'none' : 'block'
    }), () => {
      this.state.ispull ? document.addEventListener('touchmove', stopScroll, {
        passive: false
      }) : document.removeEventListener('touchmove', stopScroll)
    });
  }
xuzhenghua committed
119

zhanghaozhe committed
120 121 122 123 124 125 126 127 128 129
  // 弹窗里面tab点击查询
  labelclick = (item) => {
    this.props.history.push(`/courselist?id=${item.c_id}&name=${item.c_name}`)
    this.getList()
    this.setState(status => ({
      ispull: !status.ispull,
      display: status.ispull ? 'none' : 'block',
      activeTab: decodeURIComponent(getParam('name'))
    }))
  }
xuzhenghua committed
130

zhanghaozhe committed
131 132 133 134 135 136
  toCourseDetail = (id) => {
    const {dispatch, history} = this.props;
    // dispatch(getCourses(id, () => {
    history.push(`/detail?id=${id}`)
    // }));
  }
137

zhanghaozhe committed
138 139 140
  toClassify = () => {
    this.props.history.replace('/classify');
  }
xuzhenghua committed
141

zhanghaozhe committed
142 143 144 145 146 147 148 149 150
  render() {
    const {user = {}} = this.props;
    let isLogin = user.data && user.data.uid ? true : false;
    const bottom = (
      <i className={'iconfont iconiconfront-69 pull-down'}></i>
    )
    const top = (
      <i className={'iconfont iconiconfront-71 pull-down'}></i>
    )
xuzhenghua committed
151

zhanghaozhe committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    let page = this.state.allClass.findIndex((item) => item.title === this.state.activeTab)
    return (
      <div className='class-child'>
        <HeaderSearch
          isLogin={isLogin}
          toHref={this.toClassify}
        />
        <Loading isLoading={this.state.isLoading}>
          <div className='class-content'>
            {/* <WhiteSpace/> */}
            <div onClick={this.pulldown.bind(this)}>
              {this.state.ispull ? top : bottom}
            </div>
            <StickyContainer>
              {/* <Tabs
2  
xuzhenghua committed
167 168 169 170 171 172 173
                                tabs={this.state.allClass}
                                animated={false}
                                page={page}
                                onChange={(tab) => this.ontabclick(tab)}
                                renderTabBar={props => <div className={'custom-render-bar'}>
                                    <Tabs.DefaultTabBar {...props}/>
                                </div>}
FE committed
174
                            > */}
zhanghaozhe committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
              <Tabs
                tabs={this.state.allClass}
                animated={false}
                page={page}
                onChange={(tab) => this.ontabclick(tab)}
                renderTabBar={props => {
                  return (
                    <Sticky>
                      {({style}) => {
                        return (
                          <div style={{...style, top: `${this.state.top}px`, zIndex: 1}}>
                            <Tabs.DefaultTabBar {...props} />
                          </div>
                        )
                      }}
                    </Sticky>
                  )
                }}
              >
                <div className='tabs'>
                  <ul>
                    {this.state.data && this.state.data.length > 0 && this.state.data.map((item, index) => {
                      const Info = (
                        <div className="info">
xuzhenghua committed
199
                          <p className='title text-overflow-2'
zhanghaozhe committed
200 201 202
                             onClick={() => this.toCourseDetail(item.course_id)}>
                            {item.course_title}
                          </p>
xuzhenghua committed
203
                          <p className='contact text-overflow-1'>{item.desc}</p>
zhanghaozhe committed
204 205
                          <div className='des'>
                            {
zhanghaozhe committed
206 207 208 209 210 211 212 213
                              item.is_restricted ?
                                <LimitFree course={item}/>
                                : item.is_buy
                                ? <span className="isbuy">已购买</span>
                                : <p className="course-price">
                                  <span className="new">¥{item.price1}</span>
                                  <span className="old">¥{item.price0}</span>
                                </p>
zhanghaozhe committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
                            }
                          </div>
                        </div>
                      )
                      const status = (
                        !item.is_buy &&
                        <div>
                          {item.bargain_num === 0 && item.groupon_num !== 0 &&
                          <p className='course-status'>拼团减{item.groupon_num}</p>
                          }
                          {item.bargain_num !== 0 && item.groupon_num === 0 &&
                          <p className='course-status'>砍价减{item.bargain_num}</p>
                          }
                          {
                            item.is_aist && <span className='return_cash'></span>
                          }
                        </div>
                      )
                      return (
                        <VList
                          key={index}
                          status={status}
                          img={item.image_name}
                          id={item.course_id}
                          info={Info}
                          toDetail={this.toCourseDetail}
                        />
                      )
                    })}
                  </ul>
                </div>
              </Tabs>
2  
xuzhenghua committed
246

zhanghaozhe committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
            </StickyContainer>
            <WhiteSpace/>
          </div>
          <div className='mbc-box' style={{display: this.state.display}}>
            {
              this.state.arr.basics &&
              <div className="tabcontent">
                <ClassCourse activeTab={this.state.activeTab} data={this.state.arr.basics.list}
                             title={this.state.arr.basics.name} labelclick={this.labelclick}/>
                <ClassCourse activeTab={this.state.activeTab} data={this.state.arr.advanced.list}
                             title={this.state.arr.advanced.name} labelclick={this.labelclick}/>
              </div>
            }
          </div>
        </Loading>
      </div>
    )
  }
xuzhenghua committed
265 266 267

}

xuzhenghua committed
268
function ClassCourse(props) {
zhanghaozhe committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
  return (
    <div className="class-course">
      <p className='course-items-title'>{props.title}</p>
      <div className='items-box'>
        {
          props.data && props.data.length > 0 && props.data.map((item, index) => {
            return (
              <span className={props.activeTab === item.c_name ? 'active-label' : 'item-label'}
                    key={index} onClick={e => props.labelclick(item)}>{item.c_name}</span>
            )
          })
        }
      </div>
    </div>
  )
xuzhenghua committed
284 285
}

zhanghaozhe committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
function LimitFreeStatus({course}) {
  /*
  *  limit-free-status: 0-未领取 1-已领取 2-已过期
  *
  * */
  switch (course.limit_free_status) {
    case 0:
      return <div className="limit-free">
        <span>限时免费</span>
        <span>¥{course.price0}</span>
      </div>
    case 1:
      return <div>已领取</div>
    case 2:
      return <p className="course-price">
        <span className="new">¥{course.price1}</span>
        <span className="old">¥{course.price0}</span>
      </p>
  }
}

function LimitFree({course}) {
  if (course.is_buy) {
    if (course.limit_free_status === 1) {
      return <div className={'isbuy'}>已领取</div>
    } else {
      return <div className={'isbuy'}>已购买</div>
    }
  } else {
    return <LimitFreeStatus course={course}/>
  }
}

xuzhenghua committed
319
export default Classify;