index.js 3.22 KB
Newer Older
zhanghaozhe committed
1 2
import React, { Component } from 'react'
import { WingBlank } from 'antd-mobile'
FE committed
3 4

import './index.scss'
zhanghaozhe committed
5
import { getParam, http } from "@/utils"
FE committed
6 7

// 课程页面滚动广告
zhanghaozhe committed
8
class Barrage extends Component {
FE committed
9 10 11

    timer
    barageTimer
zhanghaozhe committed
12

FE committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
    constructor(props) {
        super(props)
        this.state = {
            list: [],
            isShowBarrage: false
        }
    }

    setupBarrage = () => {
        const periods = [
            {start: 0, end: 7, interval: 60000},
            {start: 7, end: 8, interval: 30000},
            {start: 8, end: 23, interval: 15000},
            {start: 23, end: 24, interval: 30000},
        ]
        const now = new Date(Date.now()).getHours()
        for (let period of periods) {
zhanghaozhe committed
30
            if (now >= period.start && now <= period.end) {
FE committed
31 32 33 34 35 36 37 38 39 40 41
                this.timer = setTimeout(() => {
                    this.getList()
                }, period.interval)
                break
            }
        }
    }

    componentDidUpdate(prevProps) {
        const {isShow} = this.props
        const {isShow: prevIsShow} = prevProps
zhanghaozhe committed
42 43 44 45 46
        if (prevIsShow !== isShow && isShow !== undefined) {
            if (isShow) {
                setTimeout(() => {
                    this.getList()
                }, 5000)
FE committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
            }
            this.componentDidUpdate = null
        }
    }


    componentWillUnmount() {
        this.timer && clearTimeout(this.timer);
        this.barageTimer && clearTimeout(this.barageTimer)
    }

    // 获取课程接口
    getList = () => {
        let data = {
            course_id: getParam('id')
        }
        http.post(`${API.home}/m/course/barrages`, data).then((res) => {
            if (res.data.code === 200) {
                this.setState({
                    list: res.data.data,
                    isShowBarrage: true
                }, () => {
                    this.barageTimer = setTimeout(() => {
                        this.setState({isShowBarrage: !this.state.isShowBarrage})
zhanghaozhe committed
71
                        this.setupBarrage()
FE committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
                    }, 5000)
                });
            }
        })
    }

    render() {
        return (
            <WingBlank>
                <div className="my-carousel" style={{opacity: this.state.isShowBarrage ? 1 : 0}}>
                    {
                        this.state.list &&
                        <div className="v-item text-overflow-one">
                            <img src={this.state.list.avatar} alt=""/>
                            {this.state.list.user_name} {this.state.list.live_msg}
                        </div>
                    }
                </div>
zhanghaozhe committed
90
                {/*
FE committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
                <Carousel className="my-carousel"
                          vertical
                          dots={false}
                          autoplay
                          infinite
                >
                    {
                        this.state.list &&
                        <div className="v-item text-overflow-one">
                            <img src={this.state.list.avatar} alt=""/>
                            {this.state.list.user_name} {this.state.list.live_msg}
                        </div>
                    }

                </Carousel>
*/}
            </WingBlank>
        )
    }
}

zhanghaozhe committed
112
export default Barrage