index.js 7.76 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"
wangshuo committed
4
import { getParam, http, SendMessageToApp, browser } 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
  popupInstance = null

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


zhanghaozhe committed
27
  componentDidMount() {
zhanghaozhe committed
28 29 30 31 32 33 34 35 36 37 38 39 40
    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
          }, {})
xuzhenghua committed
41 42 43 44 45 46 47 48
          const preheatLives = data.data['data_hot'].reduce((accu, current) => {
            if (!(current.date in accu)) {
              accu[current.date] = [current]
            } else {
              accu[current.date].push(current)
            }
            return accu
          }, {})
zhanghaozhe committed
49 50

          let tabs, today
zhanghaozhe committed
51
          if (this.props.isFormal) {
zhanghaozhe committed
52 53 54
            tabs = Object.keys(lives).map(item => ({title: item}))
            today = uniqBy(data.data['data_active'], value => value.date).findIndex(item => item['is_today'])
          } else {
xuzhenghua committed
55 56
            tabs = Object.keys(preheatLives).map(item => ({title: item}))
            today = uniqBy(data.data['data_hot'], value => value.date).findIndex(item => item['is_today'])
zhanghaozhe committed
57 58 59 60 61 62 63
          }


          this.setState({
            tabs,
            lives,
            today,
xuzhenghua committed
64
            preheatLives
zhanghaozhe committed
65 66 67 68 69 70 71 72 73
          })

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

      })
  }

zhanghaozhe committed
74
  toLiveRoom = id => {
zhanghaozhe committed
75
    const {history, isLogin} = this.props
zhanghaozhe committed
76
    if (this.state.isApp) {
zhanghaozhe committed
77
      if (isLogin) {
xuzhenghua committed
78
        SendMessageToApp('toLiveRoom', id)
zhanghaozhe committed
79
      } else {
xuzhenghua committed
80 81
        SendMessageToApp("toLogin")
      }
zhanghaozhe committed
82
    } else {
zhanghaozhe committed
83
      if (isLogin) {
xuzhenghua committed
84
        window.location.href = `${window.location.protocol}//www.julyedu.com/live/m_room/${id}`
zhanghaozhe committed
85
      } else {
xuzhenghua committed
86 87
        history.push('/passport')
      }
zhanghaozhe committed
88
    }
zhanghaozhe committed
89 90
  }

zhanghaozhe committed
91 92
  saveImage = () => {
    let version = getParam('version')
wangshuo committed
93
    version = typeof version === 'string' ? version.replace('.', '').replace('.', '').slice(0, 3) : ''
94
    const {QRCodeUrl} = this.state
zhanghaozhe committed
95
    if (version && parseInt(version) < 451) {
wangshuo committed
96
      Toast.info('当前不支持此功能,升级到最新版本app可以点击保存二维码!', 2, null, false)
zhanghaozhe committed
97
    } else {
98
      SendMessageToApp('generateQRCode', QRCodeUrl)
zhanghaozhe committed
99 100 101
    }
  }

zhanghaozhe committed
102
  makeSubscribe = id => {
zhanghaozhe committed
103 104 105 106
    const {user, history} = this.props
    if (user.hasError) {
      history.push('/passport/login')
    }
107
    http.get(`${API['base-api']}/sys/createLiveQrcode/${id}`)
zhanghaozhe committed
108 109
      .then(res => {
        const {data} = res
xuzhenghua committed
110
        if (data.errno == 200) {
111
          this.setState(() => ({
wangshuo committed
112
            QRCodeUrl: data.data.url,
113
          }))
zhanghaozhe committed
114
          QRCode.toDataURL(data.data.url, (err, url) => {
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
            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
134 135 136 137 138
          })
        } else {
          Toast.info(data.msg, 2, null, false)
        }
      })
zhanghaozhe committed
139 140
  }

zhanghaozhe committed
141
  render() {
FE committed
142
    const {tabs, lives, preheatLives, today} = this.state
zhanghaozhe committed
143
    return (
zhanghaozhe committed
144
      <div id={'live'}>
zhanghaozhe committed
145 146 147 148 149
        <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
150 151 152 153 154 155 156 157 158 159
        <div className="live-container">
          {
            today !== '' &&
            <Tabs
              tabs={tabs}
              tabBarBackgroundColor={'transparent'}
              tabBarActiveTextColor={'#5600DF'}
              tabBarInactiveTextColor={'#FFF604'}
              tabBarUnderlineStyle={{display: 'none'}}
              initialPage={today}
160
              swipeable={false}
zhanghaozhe committed
161 162
            >
              {
zhanghaozhe committed
163
                this.props.isFormal
zhanghaozhe committed
164 165 166 167 168
                  ? tabs.map((item, index) => {
                    const todayLives = lives[item.title]
                    return (
                      <div key={index}>
                        {
xuzhenghua committed
169
                          todayLives && todayLives.map((item, index) => {
zhanghaozhe committed
170
                            return (
zhanghaozhe committed
171 172 173 174 175 176 177 178 179 180 181 182 183
                              <LiveContent item={item} key={index} makeSubscribe={this.makeSubscribe}
                                           toLiveRoom={this.toLiveRoom}/>
                            )
                          })
                        }
                      </div>
                    )
                  })
                  : tabs.map((item, index) => {
                    const todayLives = preheatLives[item.title]
                    return (
                      <div key={index}>
                        {
xuzhenghua committed
184
                          todayLives && todayLives.map((item, index) => {
zhanghaozhe committed
185 186 187
                            return (
                              <LiveContent item={item} key={index} makeSubscribe={this.makeSubscribe}
                                           toLiveRoom={this.toLiveRoom}/>
zhanghaozhe committed
188 189 190 191 192 193 194 195 196 197 198
                            )
                          })
                        }
                      </div>
                    )
                  })
              }
            </Tabs>

          }
        </div>
zhanghaozhe committed
199 200 201 202 203
      </div>
    )
  }
}

zhanghaozhe committed
204
function LiveContent({item, makeSubscribe, toLiveRoom}) {
zhanghaozhe committed
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 230 231 232 233 234 235 236 237
  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
238 239 240
          ? <button className={'on-living'} onClick={() => {
            toLiveRoom(item['room_url'])
          }}>正在直播</button>
zhanghaozhe committed
241
          :
FE committed
242 243 244
          item['is_end']
            ? <button className={'subscribed'}>已结束</button>
            : item['is_subscribe']
zhanghaozhe committed
245 246 247 248 249 250 251 252
            ? <button className={'subscribed'}>已预约</button>
            : <button className={'subscribe'}
                      onClick={makeSubscribe.bind(this, item['live_id'])}>立即预约</button>
      }
    </div>
  )
}

zhanghaozhe committed
253
export default withRouter(Live)