index.js 5.9 KB
Newer Older
zhanghaozhe committed
1
import React, { Component } from 'react'
zhanghaozhe committed
2
import './index.scss'
zhanghaozhe committed
3
import { Tabs, Toast } from "antd-mobile"
zhanghaozhe committed
4
import { getParam, http, SendMessageToApp } from "@/utils"
zhanghaozhe committed
5 6 7
import { Popup } from "@common/index"
import QRCode from 'qrcode'
import { uniqBy } from 'lodash'
zhanghaozhe committed
8 9
import { connect } from 'react-redux'
import { withRouter } from 'react-router-dom'
zhanghaozhe committed
10

zhanghaozhe committed
11

zhanghaozhe committed
12
@connect(state => ({user: state.user}))
zhanghaozhe committed
13
class Live extends Component {
zhanghaozhe committed
14 15 16 17 18

  state = {
    tabs: [],
    lives: {},
    preheatLives: [],
zhanghaozhe committed
19 20
    today: '',
    isApp: getParam('version')
zhanghaozhe committed
21 22 23
  }


zhanghaozhe committed
24
  componentDidMount() {
zhanghaozhe committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    http.get(`${API.home}/sys/get_live_info`)
      .then(res => {
        const {data} = res
        if (data.code == 200) {

          const lives = data.data['data_active'].reduce((accu, current) => {
            if (!(current.date in accu)) {
              accu[current.date] = [current]
            } else {
              accu[current.date].push(current)
            }
            return accu
          }, {})

          let tabs, today
zhanghaozhe committed
40
          if (this.props.isFormal) {
zhanghaozhe committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
            tabs = Object.keys(lives).map(item => ({title: item}))
            today = uniqBy(data.data['data_active'], value => value.date).findIndex(item => item['is_today'])
          } else {
            tabs = data.data['data_hot'].map(item => ({title: item.date}))
            today = data.data['data_hot'].findIndex(item => item['is_today'])
          }


          this.setState({
            tabs,
            lives,
            today,
            preheatLives: data.data['data_hot']
          })

        } else {
          Toast.info(data.msg, 2, null, false)
        }

      })
  }

zhanghaozhe committed
63
  toLiveRoom = id => {
zhanghaozhe committed
64 65 66 67 68
    if (this.state.isApp) {
      SendMessageToApp('toLiveRoom', id)
    } else {
      window.location.href = `${window.location.protocol}//www.julyedu.com/live/m_room/${id}`
    }
zhanghaozhe committed
69 70
  }

zhanghaozhe committed
71
  makeSubscribe = id => {
zhanghaozhe committed
72 73 74 75
    const {user, history} = this.props
    if (user.hasError) {
      history.push('/passport/login')
    }
76
    http.get(`${API['base-api']}/sys/createLiveQrcode/${id}`)
zhanghaozhe committed
77 78
      .then(res => {
        const {data} = res
xuzhenghua committed
79
        if (data.errno == 200) {
zhanghaozhe committed
80
          QRCode.toDataURL(data.data.url, (err, url) => {
FE committed
81 82 83
            Popup({
              title: '扫码关注“七月在线”服务号即可预约',
              content: <img id={'live-qr-code'} src={url} alt=""/>
zhanghaozhe committed
84 85 86 87 88 89
            })
          })
        } else {
          Toast.info(data.msg, 2, null, false)
        }
      })
zhanghaozhe committed
90 91
  }

zhanghaozhe committed
92
  render() {
FE committed
93
    const {tabs, lives, preheatLives, today} = this.state
zhanghaozhe committed
94
    return (
zhanghaozhe committed
95
      <div id={'live'}>
zhanghaozhe committed
96 97 98 99 100
        <div className="title">
          <img src="https://julyedu-cdn.oss-cn-beijing.aliyuncs.com/active19_1111/title-decorate-left.png" alt=""/>
          <span>大咖直播</span>
          <img src="https://julyedu-cdn.oss-cn-beijing.aliyuncs.com/active19_1111/title-decorate-right.png" alt=""/>
        </div>
zhanghaozhe committed
101 102 103 104 105 106 107 108 109 110
        <div className="live-container">
          {
            today !== '' &&
            <Tabs
              tabs={tabs}
              tabBarBackgroundColor={'transparent'}
              tabBarActiveTextColor={'#5600DF'}
              tabBarInactiveTextColor={'#FFF604'}
              tabBarUnderlineStyle={{display: 'none'}}
              initialPage={today}
111
              swipeable={false}
zhanghaozhe committed
112 113
            >
              {
zhanghaozhe committed
114
                this.props.isFormal
zhanghaozhe committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
                  ? tabs.map((item, index) => {
                    const todayLives = lives[item.title]
                    return (
                      <div key={index}>
                        {
                          todayLives.map((item, index) => {
                            return (
                              <LiveContent item={item} key={index} makeSubscribe={this.makeSubscribe}/>
                            )
                          })
                        }
                      </div>
                    )
                  })
                  : preheatLives.map((item, index) => {
                    return (
                      <div key={index}>
                        {
                          preheatLives.map((item, index) => {
zhanghaozhe committed
134
                            return <LiveContent key={index} item={item} makeSubscribe={this.makeSubscribe} toLiveRoom={this.toLiveRoom}/>
zhanghaozhe committed
135 136 137 138 139 140 141 142 143 144
                          })
                        }
                      </div>
                    )
                  })
              }
            </Tabs>

          }
        </div>
zhanghaozhe committed
145 146 147 148 149
      </div>
    )
  }
}

zhanghaozhe committed
150
function LiveContent({item, makeSubscribe, toLiveRoom}) {
zhanghaozhe committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
  return (
    <div className="content">
      {
        item['is_teacher']
          ? <div className="tag teacher">讲师分享</div>
          : <div className="tag student">学员分享</div>
      }
      <div className="person-info">
        <div className="left">
          <img
            src={item.avatar}
            alt="头像"
            className="avatar"/>
        </div>
        <div className="right">
          <div className="name">讲师:{item['teacher']}</div>
          <div className="profession">{item['teacher_desc']}</div>
        </div>
      </div>
      <div className="title">{item.title}</div>
      <div className="time">直播时间:{item.time}</div>
      <div className="outline">
        <div className="outline-title">内容大纲:</div>
        <ul>
          {
            item['content'].map((content, index) => {
              return <li key={index}>{content}</li>
            })
          }
        </ul>
      </div>
      {
        item['on_live']
zhanghaozhe committed
184
          ? <button className={'on-living'} onClick={() => {toLiveRoom(item['live_id'])}}>正在直播</button>
zhanghaozhe committed
185
          :
FE committed
186 187 188
          item['is_end']
            ? <button className={'subscribed'}>已结束</button>
            : item['is_subscribe']
zhanghaozhe committed
189 190 191 192 193 194 195 196
            ? <button className={'subscribed'}>已预约</button>
            : <button className={'subscribe'}
                      onClick={makeSubscribe.bind(this, item['live_id'])}>立即预约</button>
      }
    </div>
  )
}

zhanghaozhe committed
197
export default withRouter(Live)