liveRoom.js 11.9 KB
Newer Older
xuzhenghua committed
1 2
import React, {Component} from 'react'
import './index.scss'
FE committed
3
import {http, browser, validateTel} from '@/utils'
xuzhenghua committed
4
import {Toast} from 'antd-mobile'
xuzhenghua committed
5 6
import { Link } from "react-router-dom"

xuzhenghua committed
7 8 9 10 11

class LiveRoom extends Component {
    constructor(props) {
        super(props);
        this.state = {
xuzhenghua committed
12 13
            roomMess: '',
            iphone: '', // 手机号
xuzhenghua committed
14
            toSubscribe: false,  // 点击预约之后,判断判断时候关注公众号,选择预约方式
xuzhenghua committed
15
            isfollow: 1,  // 是否关注公众号  0没有 1有
xuzhenghua committed
16 17 18 19 20
            step: 0, // 手机号预约还是公众号预约
            success: false  // 预约成功
        }
    }

xuzhenghua committed
21
    // 课程直播信息
xuzhenghua committed
22
    componentDidMount() {
xuzhenghua committed
23
        // this.getRoom()
xuzhenghua committed
24 25 26 27 28
        if (browser.isWeixin) {
            this.isFollow()
        }
    }

xuzhenghua committed
29 30 31 32 33 34 35 36 37 38 39 40 41
    // // 课程直播信息
    // getRoom = () => {
    //     http.get(`${API.home}/m/live/middle/${this.props.roomMess.room_id}`).then((res) => {
    //         if (res.data.code === 200) {
    //             // this.setState({
    //             //     roomMess: res.data.data
    //             // })
    //         } else {
    //             Toast.info(res.data.msg, 2)
    //         }
    //
    //     })
    // }
xuzhenghua committed
42

xuzhenghua committed
43 44
    // 是否关注服务号
    isFollow = () => {
zhanghaozhe committed
45
        http.get(`${API.home}/m/live/isFollow`).then((res) => {
xuzhenghua committed
46 47 48 49 50 51 52 53 54 55 56 57
            if (res.data.code === 200) {
                this.setState({
                    isfollow: res.data.data.status
                })
            } else {
                Toast.info(res.data.msg, 2)
            }
        })
    }


// 直播间预约接口公共方法
xuzhenghua committed
58
    livePrepare = (room_id, type, source, mobile) => {
xuzhenghua committed
59 60 61
        let data = {
            room_id: room_id, // 直播间id
            type: type,       // 1-为微信服务通知预约 2-为手机号预约
xuzhenghua committed
62
            source: source,   //  1-为微信内置浏览器 2-为wap(微信外)
xuzhenghua committed
63 64
            mobile: mobile    // 手机号 当type为2时,该参数为必填
        };
zhanghaozhe committed
65
        http.post(`${API.home}/m/live/prepare`, data).then((res) => {
xuzhenghua committed
66 67 68 69 70 71 72
            /* status  0表示预约成功
                       1表示预约失败
                       2表示未关注服务号
                       3表示已预约
                       4表示手机号不能为空
            */
            if (res.data.code === 200) {
xuzhenghua committed
73
                if (res.data.data.status === 0) {
xuzhenghua committed
74 75 76 77 78 79 80 81 82 83 84 85 86
                    // 如果是微信服务通知预约成功 需要提示预留手机号。
                    if (type === 1) {
                        this.setState({
                            toSubscribe: true,
                            step: 2
                        })
                    } else {
                        // 手机号预约成功直接弹出'预约成功'
                        Toast.info(res.data.data.msg, 2)
                        this.setState({
                            success: true,
                            toSubscribe: false
                        })
xuzhenghua committed
87
                        this.props.getIndexData()
xuzhenghua committed
88
                    }
xuzhenghua committed
89

xuzhenghua committed
90
                } else if (res.data.data.status === 1) {
xuzhenghua committed
91
                    Toast.info(res.data.data.msg, 2)
xuzhenghua committed
92
                } else if (res.data.data.status === 3) {
xuzhenghua committed
93 94 95 96
                    Toast.info(res.data.data.msg, 2)
                    this.setState({
                        toSubscribe: false
                    })
xuzhenghua committed
97 98
                    this.props.getIndexData()

xuzhenghua committed
99
                } else if (res.data.data.status === 5) {
xuzhenghua committed
100
                    window.location.href = 'https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzIyMzMzMTIwNA==&scene=124#wechat_redirect';
xuzhenghua committed
101
                } else {
xuzhenghua committed
102
                    Toast.info(res.data.data.msg, 2)
xuzhenghua committed
103 104 105 106 107
                }

            }
        })
    }
xuzhenghua committed
108

xuzhenghua committed
109 110 111 112 113 114
// 关闭弹窗
    liveColse = () => {
        this.props.colseBox(false)
    }

// 点击预约
xuzhenghua committed
115
    toSubscribeFun = () => {
xuzhenghua committed
116 117 118 119 120
        this.props.colseBox(false)
        this.setState({
            toSubscribe: true,
            step: browser.isWeixin ? 0 : 1
        })
xuzhenghua committed
121
    }
xuzhenghua committed
122

xuzhenghua committed
123 124 125 126 127
// 获取手机号
    handleChange = (e) => {
        this.setState({
            iphone: e.target.value
        })
xuzhenghua committed
128
    }
xuzhenghua committed
129

xuzhenghua committed
130 131 132
// 手机号预约
    iphoneStep = () => {
        this.setState({
xuzhenghua committed
133
            step: 1
xuzhenghua committed
134
        })
xuzhenghua committed
135
    }
xuzhenghua committed
136

xuzhenghua committed
137 138
// 服务号预约
    serverStep = () => {
xuzhenghua committed
139
        if (this.state.isfollow == 2) {
xuzhenghua committed
140
            window.location.href = 'https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzIyMzMzMTIwNA==&scene=124#wechat_redirect';
xuzhenghua committed
141 142 143
        } else {
            this.livePrepare(this.props.roomMess.room_id, 1, 1, '')
        }
xuzhenghua committed
144
    }
xuzhenghua committed
145

xuzhenghua committed
146 147 148
// 手机号预约 || 预留手机号
    submit = () => {
        // 手机号预约
xuzhenghua committed
149
        if (this.state.step === 1) {
xuzhenghua committed
150 151 152 153 154
            if (!validateTel(this.state.iphone)) {
                Toast.info('请填写正确格式的手机号', 2)
            } else {
                this.livePrepare(this.props.roomMess.room_id, 2, 2, this.state.iphone)
            }
xuzhenghua committed
155
        }
xuzhenghua committed
156
        // 公众号预约预留手机号
xuzhenghua committed
157
        if (this.state.step === 2) {
xuzhenghua committed
158 159 160 161
            let data = {
                mobile: this.state.iphone,
                room_id: this.props.roomMess.room_id
            }
xuzhenghua committed
162
            http.post(`${API.home}/m/live/wxReservedTel`, data).then((res) => {
xuzhenghua committed
163 164 165 166 167
                if (res.data.code === 200) {
                    this.setState({
                        toSubscribe: false
                    })
                    Toast.info(res.data.msg, 2)
xuzhenghua committed
168
                    this.props.getIndexData()
xuzhenghua committed
169 170 171
                } else if(res.data.code === 410){
                    Toast.info('请输入手机号', 2)
                }else {
xuzhenghua committed
172 173 174
                    Toast.info(res.data.msg, 2)
                }
            })
xuzhenghua committed
175 176
        }
    }
xuzhenghua committed
177

xuzhenghua committed
178 179 180 181 182
// 取消预约 关闭弹窗
    subscribeColse = () => {
        this.setState({
            toSubscribe: false
        })
xuzhenghua committed
183 184 185 186
    }


    render() {
xuzhenghua committed
187
        const roomMess = this.props.roomMess
xuzhenghua committed
188 189 190 191 192 193 194 195 196 197 198
        return (
            <div className='index-box'>
                {/* 直播间预约 */}

                {
                    this.props.isShow === true &&

                    <div className='live-room-box'>
                        <div className='live-room-content'>
                            <div className='course-banner'>
                                <div className='left'><span></span></div>
xuzhenghua committed
199 200 201
                                <div className='center'>
                                    <img src={roomMess.live_img} alt=""/>
                                </div>
xuzhenghua committed
202 203 204
                                <div className='right'><span></span></div>
                            </div>
                            <div className='course-dec'>
xuzhenghua committed
205 206
                                <h2 className='title text-overflow-2'>{roomMess.live_title}</h2>
                                <p className='teacher text-overflow-2'>讲师:{roomMess.live_teacher_name}</p>
xuzhenghua committed
207
                                <p className='time'>时间:{roomMess.live_start_time}</p>
xuzhenghua committed
208 209 210 211 212
                            </div>
                            <div className='course-info'>
                                <ul>
                                    <li>简介:</li>
                                    <li className='text-overflow-2'>
xuzhenghua committed
213
                                        {roomMess.live_desc}
xuzhenghua committed
214 215 216 217 218
                                    </li>
                                </ul>
                            </div>
                            <div className='course-subscribe'>
                                {
xuzhenghua committed
219
                                    !roomMess.is_prepare && roomMess.is_free === 1 &&
xuzhenghua committed
220
                                    <button onClick={this.toSubscribeFun}>预约</button>
xuzhenghua committed
221 222
                                }
                                {
xuzhenghua committed
223 224 225 226
                                    roomMess.is_free === 0 &&
                                    <Link to={`/detail?id=${roomMess.course_id}`}>查看课程</Link>
                                }
                                {
xuzhenghua committed
227
                                    roomMess.is_prepare &&
xuzhenghua committed
228 229 230 231 232
                                    <span>直播尚未开始,敬请期待...</span>
                                }
                            </div>
                        </div>
                        <div className='live-room-close'>
xuzhenghua committed
233
                            <i onClick={this.liveColse} className={'iconfont iconiconfront-2 close'}></i>
xuzhenghua committed
234 235 236 237
                        </div>
                    </div>
                }
                {
xuzhenghua committed
238
                    this.state.toSubscribe &&
xuzhenghua committed
239 240 241
                    <div className='subscribe-box'>
                        {this.state.step === 0 &&
                        <div>
xuzhenghua committed
242
                            <div className='close-subscribe' onClick={this.subscribeColse}></div>
xuzhenghua committed
243 244
                            <div className='subscribe-content'>
                                <span className='title'>请选择即将直播时通知您的方式</span>
xuzhenghua committed
245 246
                                <button className={this.state.isfollow === 1 ? 'follow' : 'nofollow'}
                                        onClick={this.serverStep}>微信服务号通知
xuzhenghua committed
247 248
                                </button>
                                {
xuzhenghua committed
249
                                    this.state.isfollow === 2 &&
xuzhenghua committed
250 251 252
                                    <span className='tips'>关注服务号即代表预约成功</span>

                                }
xuzhenghua committed
253
                                <button className={this.state.isfollow === 1 ? 'follow' : 'nofollow'}
xuzhenghua committed
254
                                        onClick={this.iphoneStep}>&nbsp;手机短信通知&nbsp;&nbsp;
xuzhenghua committed
255 256 257 258 259 260
                                </button>
                            </div>
                        </div>
                        }
                        {this.state.step === 1 &&
                        <div>
xuzhenghua committed
261
                            <div className='close-subscribe' onClick={this.subscribeColse}></div>
xuzhenghua committed
262 263 264
                            <div className='iphone-content'>
                                <span className='title'>预约</span>
                                <p className='dec'>直播前会通过手机短信通知您,您可通过预留的手机号快捷登录参与学习:</p>
xuzhenghua committed
265 266 267 268 269 270
                                <input type="text" placeholder='请输入手机号'
                                       value={this.state.iphone}
                                       maxLength={11}
                                       onChange={this.handleChange.bind(this)}
                                />
                                <button className='submit' onClick={this.submit}>预约</button>
xuzhenghua committed
271 272 273 274 275
                            </div>
                        </div>
                        }
                        {this.state.step === 2 &&
                        <div>
xuzhenghua committed
276
                            <div className='close-subscribe' onClick={this.subscribeColse}></div>
xuzhenghua committed
277 278 279 280
                            <div className='iphone-content server-content'>
                                <p className='title'><i
                                    className={'iconfont icondanseshixintubiao-5'}></i><span>预约成功</span></p>
                                <p className='dec'>直播开始前会通过微信“服务通知”提醒您。 推荐预留手机号,届时可通过手机号登录直播 间直接学习:</p>
xuzhenghua committed
281 282 283 284 285 286
                                <input type="text" placeholder='请输入手机号'
                                       value={this.state.iphone}
                                       maxLength={11}
                                       onChange={this.handleChange.bind(this)}
                                />
                                <button className='submit' onClick={this.submit}>提交</button>
xuzhenghua committed
287 288 289 290 291
                            </div>
                        </div>
                        }
                    </div>
                }
xuzhenghua committed
292 293 294 295
                {/*{*/}
                    {/*this.state.success &&*/}
                    {/*<div className='success'>预约成功</div>*/}
                {/*}*/}
xuzhenghua committed
296 297 298 299 300 301 302
            </div>
        );
    }

}


xuzhenghua committed
303
export default LiveRoom
xuzhenghua committed
304 305 306