liveRoom.js 11.1 KB
Newer Older
xuzhenghua committed
1 2
import React, {Component} from 'react'
import './index.scss'
xuzhenghua committed
3 4
import {http, api, browser, validateTel} from '@/utils'
import {Toast} from 'antd-mobile'
xuzhenghua committed
5 6 7 8 9

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

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

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

        })
xuzhenghua committed
39 40
    }

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


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

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

            }
        })
    }
xuzhenghua committed
102

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

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

xuzhenghua committed
117 118 119 120 121
// 获取手机号
    handleChange = (e) => {
        this.setState({
            iphone: e.target.value
        })
xuzhenghua committed
122
    }
xuzhenghua committed
123

xuzhenghua committed
124 125 126
// 手机号预约
    iphoneStep = () => {
        this.setState({
xuzhenghua committed
127
            step: 1
xuzhenghua committed
128
        })
xuzhenghua committed
129
    }
xuzhenghua committed
130

xuzhenghua committed
131 132 133 134 135 136 137
// 服务号预约
    serverStep = () => {
        if (this.state.isfollow === 0) {
            window.location.href = 'https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzIyMzMzMTIwNA==&scene=126&bizpsid=0&subscene=0#wechat_redirect';
        } else {
            this.livePrepare(this.props.roomMess.room_id, 1, 1, '')
        }
xuzhenghua committed
138
    }
xuzhenghua committed
139

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

xuzhenghua committed
169 170 171 172 173
// 取消预约 关闭弹窗
    subscribeColse = () => {
        this.setState({
            toSubscribe: false
        })
xuzhenghua committed
174 175 176 177
    }


    render() {
xuzhenghua committed
178
        const roomMess = this.state.roomMess
xuzhenghua committed
179 180 181 182 183 184 185 186 187 188 189
        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
190 191 192
                                <div className='center'>
                                    <img src={roomMess.live_img} alt=""/>
                                </div>
xuzhenghua committed
193 194 195
                                <div className='right'><span></span></div>
                            </div>
                            <div className='course-dec'>
xuzhenghua committed
196 197 198
                                <h2 className='title'>{roomMess.live_title}</h2>
                                <p className='teacher'>讲师:{roomMess.live_teacher_name}</p>
                                <p className='time'>时间:{roomMess.start_date}</p>
xuzhenghua committed
199 200 201 202 203
                            </div>
                            <div className='course-info'>
                                <ul>
                                    <li>简介:</li>
                                    <li className='text-overflow-2'>
xuzhenghua committed
204
                                        {roomMess.live_desc}
xuzhenghua committed
205 206 207 208 209
                                    </li>
                                </ul>
                            </div>
                            <div className='course-subscribe'>
                                {
xuzhenghua committed
210 211
                                    roomMess.is_prepare === 0 && roomMess.course_id === 0 &&
                                    <button onClick={this.toSubscribe}>预约</button>
xuzhenghua committed
212 213
                                }
                                {
xuzhenghua committed
214
                                    roomMess.is_prepare === 1 &&
xuzhenghua committed
215 216 217 218 219
                                    <span>直播尚未开始,敬请期待...</span>
                                }
                            </div>
                        </div>
                        <div className='live-room-close'>
xuzhenghua committed
220
                            <i onClick={this.liveColse} className={'iconfont iconiconfront-2 close'}></i>
xuzhenghua committed
221 222 223 224
                        </div>
                    </div>
                }
                {
xuzhenghua committed
225
                    this.state.toSubscribe &&
xuzhenghua committed
226 227 228
                    <div className='subscribe-box'>
                        {this.state.step === 0 &&
                        <div>
xuzhenghua committed
229
                            <div className='close-subscribe' onClick={this.subscribeColse}></div>
xuzhenghua committed
230 231
                            <div className='subscribe-content'>
                                <span className='title'>请选择即将直播时通知您的方式</span>
xuzhenghua committed
232 233
                                <button className={this.state.isfollow === 1 ? 'follow' : 'nofollow'}
                                        onClick={this.serverStep}>微信服务号通知
xuzhenghua committed
234 235
                                </button>
                                {
xuzhenghua committed
236
                                    this.state.isfollow === 0 &&
xuzhenghua committed
237 238 239
                                    <span className='tips'>关注服务号即代表预约成功</span>

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

}


xuzhenghua committed
290
export default LiveRoom
xuzhenghua committed
291 292 293