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

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

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

zhanghaozhe committed
12 13 14 15 16
  constructor(props) {
    super(props)
    this.state = {
      list: [],
      isShowBarrage: false,
FE committed
17
    }
zhanghaozhe committed
18
  }
FE committed
19

zhanghaozhe committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
  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()
        }, period.interval)
        break
      }
FE committed
35
    }
zhanghaozhe committed
36
  }
FE committed
37

zhanghaozhe committed
38 39 40 41 42 43 44 45 46 47
  componentDidUpdate(prevProps) {
    const { isShow } = this.props
    const { isShow: prevIsShow } = prevProps
    if (prevIsShow !== isShow && isShow !== undefined) {
      if (isShow) {
        setTimeout(() => {
          this.getList()
        }, 5000)
      }
      this.componentDidUpdate = null
FE committed
48
    }
zhanghaozhe committed
49
  }
FE committed
50

zhanghaozhe committed
51 52 53 54
  componentWillUnmount() {
    this.timer && clearTimeout(this.timer)
    this.barageTimer && clearTimeout(this.barageTimer)
  }
FE committed
55

zhanghaozhe committed
56 57 58 59
  // 获取课程接口
  getList = () => {
    let data = {
      course_id: getParam("id"),
FE committed
60
    }
zhanghaozhe committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    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 })
              this.setupBarrage()
            }, 5000)
          }
        )
      }
    })
  }
FE committed
78

zhanghaozhe committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
  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>
        {/*
FE committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
                <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>
*/}
zhanghaozhe committed
110 111 112
      </WingBlank>
    )
  }
FE committed
113 114
}

zhanghaozhe committed
115
export default Barrage