index.js 4.18 KB
Newer Older
zhanghaozhe committed
1
import React, { PureComponent } from 'react'
2 3
import { Tag } from '../../../common'
import Course from '@/common/course-base'
zhanghaozhe committed
4
import { http } from '@/utils'
zhanghaozhe committed
5 6
import { Toast } from "antd-mobile";
import classnames from 'classnames'
zhanghaozhe committed
7
import { isEmpty } from 'lodash'
zhanghaozhe committed
8 9 10 11 12 13


import './free-courses.scss'

const Bottom = (props) => (
    <div className='bottom'>
zhanghaozhe committed
14
        <Tag className={props.className}>{props.text}</Tag>
zhanghaozhe committed
15 16 17 18
        <span>{props.audience}人观看</span>
    </div>
)

zhanghaozhe committed
19 20 21 22
class FreeCourse extends PureComponent {
    state = {
        courses: [],
        live: [],
zhanghaozhe committed
23 24
        page: 1,
        num: 10
zhanghaozhe committed
25 26
    }

zhanghaozhe committed
27 28
    componentDidMount() {
        this.getFreeCourses()
zhanghaozhe committed
29
            .then(res => {
zhanghaozhe committed
30 31
                let data = res.data
                if (data.code == 200) {
zhanghaozhe committed
32
                    this.setState({
zhanghaozhe committed
33
                        courses: data.data,
zhanghaozhe committed
34 35
                    })
                } else {
zhanghaozhe committed
36
                    Toast.info(data.msg)
zhanghaozhe committed
37 38
                }
            })
zhanghaozhe committed
39
        this.getFreeLive()
zhanghaozhe committed
40
            .then(res => {
zhanghaozhe committed
41 42
                let data = res.data
                if (data.code == 200) {
zhanghaozhe committed
43
                    this.setState({
zhanghaozhe committed
44
                        live: isEmpty(data.data) ? [] : data.data
zhanghaozhe committed
45
                    })
zhanghaozhe committed
46
                } else {
zhanghaozhe committed
47
                    Toast.info(data.msg, 2, null, false)
zhanghaozhe committed
48 49 50
                }
            })
    }
zhanghaozhe committed
51

52 53 54 55
    handleClick = id => {
        this.props.history.push(`/play?id=${id}`)
    }

zhanghaozhe committed
56
    getFreeCourses = () => {
zhanghaozhe committed
57
        return http.get(`${API.home}/m/free_course/${this.state.page}/${this.state.num}`)
zhanghaozhe committed
58 59 60
    }

    getFreeLive = () => {
zhanghaozhe committed
61
        return http.get(`${API.home}/m/live/free_list`)
zhanghaozhe committed
62 63
    }

zhanghaozhe committed
64 65 66 67 68 69 70 71 72
    toLive = live => {
        const {room_id, live_status} = live
        if (live_status) {
            window.location.assign(`http://www-test.julyedu.com/live/m_room/${room_id}`)
        } else {
            Toast.info('直播即将开始,敬请期待', 2, null, false)
        }
    }

zhanghaozhe committed
73 74 75 76
    render() {
        return (
            <ul className='free-courses'>
                {
zhanghaozhe committed
77 78 79 80 81 82 83 84
                    this.state.live.map((item, index) => {
                        const Bottom = (
                            <div className="bottom">
                                <div className="animation-box">
                                    {new Array(4).fill('a').map((item, index) => {
                                        return <i key={index} className={classnames('column', `column-${index + 1}`)}/>
                                    })}
                                </div>
zhanghaozhe committed
85
                                <div className="time">{`${item['live_start_time']}`}</div>
zhanghaozhe committed
86 87
                            </div>
                        )
zhanghaozhe committed
88 89 90 91 92

                        const LiveStatus = (
                            item['live_status'] ? <Tag className={'tag-soon top'}>即将开始</Tag> :
                                <Tag className={'tag-playing top'}>正在直播</Tag>
                        )
zhanghaozhe committed
93 94
                        return (
                            <Course
zhanghaozhe committed
95
                                img={item['live_cover']}
zhanghaozhe committed
96
                                title={item['live_title']}
zhanghaozhe committed
97
                                top={LiveStatus}
zhanghaozhe committed
98 99 100
                                bottom={Bottom}
                                key={item['live_id']}
                                className={'live'}
zhanghaozhe committed
101
                                handleClick={this.toLive.bind(this, item)}
102
                                id={index}
zhanghaozhe committed
103 104 105 106
                            />
                        )
                    })
                }
zhanghaozhe committed
107 108 109 110 111
                {
                    this.state.courses.map((item, index) => (
                        <Course
                            img={item.logo}
                            title={item['video_course_name']}
112
                            handleClick={this.handleClick}
zhanghaozhe committed
113 114 115 116 117
                            bottom={
                                <Bottom audience={item['play_times']} className={'tag-category'} text={item.category}/>
                            }
                            id={item['v_course_id']}
                            key={index}
zhanghaozhe committed
118
                            className={'course-item'}
zhanghaozhe committed
119 120 121
                        />
                    ))
                }
zhanghaozhe committed
122 123 124
            </ul>
        )
    }
zhanghaozhe committed
125 126
}

zhanghaozhe committed
127

zhanghaozhe committed
128
export default FreeCourse