index.js 7.33 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 5
import { getParam, http, SendMessageToApp, browser } from "src/utils"
import { Popup } from "src/common/index"
zhanghaozhe committed
6 7
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
  popupInstance = null

zhanghaozhe committed
17 18
  state = {
    tabs: [],
xuzhenghua committed
19
    lives: [],
zhanghaozhe committed
20
    preheatLives: [],
xuzhenghua committed
21
    todayIndex: '',
wangshuo committed
22 23
    isApp: getParam('version'),
    QRCodeUrl: '',
zhanghaozhe committed
24 25 26
  }


zhanghaozhe committed
27
  componentDidMount() {
zhanghaozhe committed
28 29
    http.get(`${API.home}/sys/get_live_info`)
      .then(res => {
xuzhenghua committed
30 31 32
        const {data, code, msg} = res.data
        if (code == 200) {
          let tabs = uniqBy(data, item => item.date)
xuzhenghua committed
33
          let todayIndex = tabs.findIndex(item => item['is_today'])
xuzhenghua committed
34
          tabs = tabs.map(item => ({title: item.date}))
zhanghaozhe committed
35 36 37

          this.setState({
            tabs,
xuzhenghua committed
38 39
            lives: data,
            todayIndex: todayIndex < 0 ? 0 : todayIndex,
zhanghaozhe committed
40 41 42
          })

        } else {
xuzhenghua committed
43
          Toast.info(msg, 2, null, false)
zhanghaozhe committed
44 45 46 47 48
        }

      })
  }

xuzhenghua committed
49 50 51 52 53 54 55 56 57 58

  toLogin = () => {
    const {history} = this.props
    if (!getParam('version')) {
      history.push('/passport')
    } else {
      SendMessageToApp("toLogin")
    }
  }

zhanghaozhe committed
59
  toLiveRoom = id => {
zhanghaozhe committed
60
    const {history, isLogin} = this.props
xuzhenghua committed
61 62 63
    var _czc = _czc || []
    var name = '直播间id=' + id
    _czc.push(["_trackEvent", name, 'm端双十一大咖直播-正在直播'])
zhanghaozhe committed
64
    if (this.state.isApp) {
zhanghaozhe committed
65
      if (isLogin) {
xuzhenghua committed
66
        SendMessageToApp('toLiveRoom', id)
zhanghaozhe committed
67
      } else {
xuzhenghua committed
68 69
        SendMessageToApp("toLogin")
      }
zhanghaozhe committed
70
    } else {
zhanghaozhe committed
71
      if (isLogin) {
xuzhenghua committed
72
        window.location.href = `${window.location.protocol}//www.julyedu.com/live/m_room/${id}`
zhanghaozhe committed
73
      } else {
xuzhenghua committed
74 75
        history.push('/passport')
      }
zhanghaozhe committed
76
    }
zhanghaozhe committed
77 78
  }

zhanghaozhe committed
79 80
  saveImage = () => {
    let version = getParam('version')
wangshuo committed
81
    version = typeof version === 'string' ? version.replace('.', '').replace('.', '').slice(0, 3) : ''
82
    const {QRCodeUrl} = this.state
zhanghaozhe committed
83
    if (version && parseInt(version) < 451) {
wangshuo committed
84
      Toast.info('当前不支持此功能,升级到最新版本app可以点击保存二维码!', 2, null, false)
zhanghaozhe committed
85
    } else {
86
      SendMessageToApp('generateQRCode', QRCodeUrl)
zhanghaozhe committed
87 88 89
    }
  }

xuzhenghua committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  checkVideo = (url) => {
    let version = getParam('version');
    const {history, user} = this.props;
    if(!version) {
      if (user.hasError) {
        this.toLogin()
      }else{
        const vCourseId = url.split('/')[0];
        history.push(`/play/video?id=${vCourseId}`)
      }
    }else{
      Toast.info('当前版本不支持此功能,请前往h5/pc查看回放该直播', 2, null, false);
    }
  }

zhanghaozhe committed
105
  makeSubscribe = id => {
xuzhenghua committed
106
    const {user} = this.props
zhanghaozhe committed
107
    if (user.hasError) {
xuzhenghua committed
108
      this.toLogin()
zhanghaozhe committed
109
    }
xuzhenghua committed
110

xuzhenghua committed
111 112
    var name = '直播间id=' + id
    _czc.push(["_trackEvent", name, 'm端双十一大咖直播-立即预约'])
xuzhenghua committed
113

114
    http.get(`${API['base-api']}/sys/createLiveQrcode/${id}`)
zhanghaozhe committed
115 116
      .then(res => {
        const {data} = res
xuzhenghua committed
117
        if (data.errno == 200) {
118
          this.setState(() => ({
wangshuo committed
119
            QRCodeUrl: data.data.url,
120
          }))
zhanghaozhe committed
121
          QRCode.toDataURL(data.data.url, (err, url) => {
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
            if (!this.popupInstance) {
              this.popupInstance = Popup({
                title: '扫码关注“七月在线”服务号即可预约',
                content: (
                  <>
                    <img id={'live-qr-code'} src={url} alt=""/>
                    {
                      browser.isAndroidApp ? (
                        <button className={'save-image'} onClick={this.saveImage}>保存二维码</button>
                      ) : null
                    }
                  </>
                ),
                close: () => new Promise(resolve => {
                  this.popupInstance = null
                  resolve()
                })
              })
            }
zhanghaozhe committed
141
          })
xuzhenghua committed
142 143
        } else if(data.errno == 4030 || data.errno == 4040){
        }else {
zhanghaozhe committed
144 145 146
          Toast.info(data.msg, 2, null, false)
        }
      })
zhanghaozhe committed
147 148
  }

zhanghaozhe committed
149
  render() {
xuzhenghua committed
150
    const {tabs, lives, todayIndex} = this.state
zhanghaozhe committed
151
    return (
zhanghaozhe committed
152
      <div id={'live'}>
zhanghaozhe committed
153 154 155 156 157
        <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
158 159
        <div className="live-container">
          {
xuzhenghua committed
160
            todayIndex !== '' &&
zhanghaozhe committed
161 162 163 164 165 166
            <Tabs
              tabs={tabs}
              tabBarBackgroundColor={'transparent'}
              tabBarActiveTextColor={'#5600DF'}
              tabBarInactiveTextColor={'#FFF604'}
              tabBarUnderlineStyle={{display: 'none'}}
xuzhenghua committed
167
              initialPage={todayIndex}
168
              swipeable={false}
zhanghaozhe committed
169 170
            >
              {
xuzhenghua committed
171 172 173 174 175 176 177 178
                tabs.map((item, index) => {
                  const todayLives = lives.filter(liveItem => liveItem.date === item.title)
                  return (
                    <div key={index}>
                      {
                        todayLives && todayLives.map((item, index) => {
                          return (
                            <LiveContent item={item} key={index} makeSubscribe={this.makeSubscribe}
xuzhenghua committed
179
                                         toLiveRoom={this.toLiveRoom} checkVideo={this.checkVideo}/>
xuzhenghua committed
180 181 182 183 184 185
                          )
                        })
                      }
                    </div>
                  )
                })
zhanghaozhe committed
186 187 188 189 190
              }
            </Tabs>

          }
        </div>
zhanghaozhe committed
191 192 193 194 195
      </div>
    )
  }
}

xuzhenghua committed
196
function LiveContent({item, makeSubscribe, toLiveRoom, checkVideo}) {
zhanghaozhe committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
  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
230 231 232
          ? <button className={'on-living'} onClick={() => {
            toLiveRoom(item['room_url'])
          }}>正在直播</button>
zhanghaozhe committed
233
          :
FE committed
234
          item['is_end']
xuzhenghua committed
235
            ? item.transcribe_url === '' ? <button className={'subscribed'}>已结束</button> : <button className={'on-living'} onClick={() => checkVideo(item.transcribe_url)}>查看回放</button>
FE committed
236
            : item['is_subscribe']
zhanghaozhe committed
237 238 239 240 241 242 243 244
            ? <button className={'subscribed'}>已预约</button>
            : <button className={'subscribe'}
                      onClick={makeSubscribe.bind(this, item['live_id'])}>立即预约</button>
      }
    </div>
  )
}

zhanghaozhe committed
245
export default withRouter(Live)