index.js 3.14 KB
Newer Older
FE committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 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 103 104 105 106 107 108 109
import React, {Component} from 'react'
import {WingBlank} from 'antd-mobile'

import './index.scss'
import {api, getParam, http} from "@/utils"

// 课程页面滚动广告
class Carouselw extends Component {

    timer
    barageTimer
    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) {
            if(now >= period.start && now <= period.end){
                this.timer = setTimeout(() => {
                    this.getList()
                    this.setupBarrage()
                }, period.interval)
                break
            }
        }
    }

    componentDidUpdate(prevProps) {
        const {isShow} = this.props
        const {isShow: prevIsShow} = prevProps
        if(prevIsShow !== isShow && isShow !== undefined){
            if(isShow){
                this.setupBarrage()
            }
            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})
                    }, 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>
{/*
                <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>
        )
    }
}

export default Carouselw