index.js 20.3 KB
Newer Older
zhanghaozhe committed
1 2 3 4 5 6
import React, { Component } from 'react'
import { getParam, http } from "@/utils"
import { Toast } from 'antd-mobile'
import { Link } from 'react-router-dom'
import { getCourses } from "@/components/detail/actions"
import { connect } from "react-redux"
xuzhenghua committed
7
import './index.scss'
xuzhenghua committed
8
import Overlay from '../overlay'
xuzhenghua committed
9 10 11
import BindPhone from './../bindPhone/index';
import BargainConfirmBind from './../bindPhone/confirm';
import Mask from '@/common/Mask/index';
xuzhenghua committed
12 13 14 15 16

class BtnStatus extends Component {
    constructor(props) {
        super(props)
        this.state = {
xuzhenghua committed
17
            isbuy: 1,
18 19 20
            is_baoming: 0,
            group_status: 3,
            in_cart: false,
wangshuo committed
21
            countdown: 0,
xuzhenghua committed
22 23 24 25 26
            barInfo: {},
            bindPhone: false,
            bindConfrm: false,
            bindInfo: {}, // 冲突信息
            formInfo: {}, 
xuzhenghua committed
27 28 29
        }
    }

xuzhenghua committed
30
    componentDidMount() {
31
        // this.getBargainInfo()
32
        this.group = false;
xuzhenghua committed
33 34 35 36 37 38 39 40 41 42 43 44 45
    }


    // componentDidUpdate(prevProps) {
    //     let {courseInfo} = this.props
    //     let {courseInfo: prevCourseInfo} = prevProps
    //     if (courseInfo && courseInfo.is_bargain) {
    //         if (prevCourseInfo && courseInfo.is_bargain !== prevCourseInfo.is_bargain) {
    //             this.getBargainInfo()
    //         }
    //     }
    // }

46
    componentWillReceiveProps(nextProps) {
47
        const { data = {}, user = {}} = nextProps;
48
        if(data.is_bargain && user.data && user.data.uid) {
49 50
            this.getBargainInfo();
        }
51 52 53
        // if(nextProps.data && nextProps.data.is_bargain) {
        //     this.getBargainInfo()
        // }
54
        this.setState({
FE committed
55
            courseInfo: data,
wangshuo committed
56
            countdown: nextProps.countdown,
57 58 59
        });
    }

FE committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    // // 加入购物车 type:1 加入购物车,2加入购物车并跳转到购物车页面去支付
    // toCart = (type) => {
    //     const { history, addCourseToCart } = this.props;
    //     let data = {
    //         course_id: getParam('id')
    //     };
    //     http.post(`${API.home}/m/cart/add`, data).then((res) => {
    //         if (res.data.code === 200) {
    //             if (type === 1) {
    //                 Toast.info('已加入购物车', 2)
    //                 // this.props.getCourses()
    //                 // document.location.reload()
    //                 addCourseToCart();
    //             } else {
    //                 history.replace('/shopcart');ß
    //             }
    //         } else if (res.data.code === 15001) {
    //             history.replace('/shopcart');
    //         } else if (res.data.code === 4030) {
    //             history.replace('/passport');
    //         } else {
    //             Toast.info(res.data.msg, 2);
    //         }
    //     })
    // };
xuzhenghua committed
85

86 87 88 89 90 91
    // 返现课程的立即购买
    signUpNow = () => {
        if(this.props.user.hasError) {
            // 未登录 去登陆
            this.props.history.push('/passport');
        }else{
wangshuo committed
92
            let cidArr = JSON.stringify([Number(getParam('id'))]);
93
            http.get(`${API['base-api']}/m/cart/addtopreorder/${cidArr}?type=1`).then((res) => {
wangshuo committed
94
                if (res.data.errno === 0) {
95
                    this.props.history.push("/order?id=" + res.data.data[0], {type: 1});
96 97 98 99 100 101
                } else {
                    Toast.info(res.data.msg, 2);
                }
            })
        }
    };
xuzhenghua committed
102

wangshuo committed
103 104
    // 普通课程的立即报名 要模拟结算过程
    simpleCourse = () => {
105 106 107 108 109 110
        if(this.props.user.hasError) {
            // 未登录 去登陆
            this.props.history.push('/passport');
        }else{
            http.get(`${API['base-api']}/m/cart/addtopreorder/[${getParam('id')}]`).then((res) => {
                if (res.data.errno === 0) {
wangshuo committed
111
                    this.props.history.push(`/order?id=${getParam('id')}`, {simple: 1})
112 113 114 115 116
                } else {
                    Toast.info(res.data.msg, 2);
                }
            })
        }
wangshuo committed
117
    };
xuzhenghua committed
118

119 120 121 122 123
    // 格式化开课时间
    formatDate = (date) => {
        let ary = date.split('-');
        return `${ary[1]}${ary[2]}日开课`;
    };
xuzhenghua committed
124

xuzhenghua committed
125 126
    // 直接购买
    tobuy = () => {
127 128
        const {user} = this.props;
        const {barInfo} = this.state;
xuzhenghua committed
129 130 131
        const uid = user && user.data && user.data.uid
        if (!uid) {
            this.props.history.push('/passport/login')
xuzhenghua committed
132
        } else {
133
            if (barInfo.bargain_status === 2) {
134 135 136
                // this.toCart(2)
                // 新需求 不需要加入购物车 直接走普通课程的立即报名流程 跳到订单页
                this.simpleCourse();
xuzhenghua committed
137 138 139 140 141 142 143
            } else {
                // 取消砍价记录
                this.setState({
                    isShowOverlay: true,
                    bargainStatus: 1,
                })
            }
xuzhenghua committed
144
        }
145
    };
xuzhenghua committed
146

147 148 149 150 151 152 153 154 155
    // 一键开团
    keyToGroup = () => {
        if(this.props.user.hasError) {
            // 未登录 去登陆
            this.props.history.push('/passport');
            return;
        }
        this.props.history.push(`/order?id=${getParam('id')}`, {group: 1})
    };
xuzhenghua committed
156

157 158 159 160 161 162 163 164
    // 砍完价去支付
    bargainToOrder = () => {
        if(this.props.user.hasError) {
            // 未登录 去登陆
            this.props.history.push('/passport');
        }else{
            http.get(`${API['base-api']}/m/cart/addtopreorder/[${getParam('id')}]`).then((res) => {
                if (res.data.errno === 0) {
wangshuo committed
165
                    this.props.history.push(`/order?id=[${getParam('id')}]`, {bargain: 1});
166 167 168 169 170 171
                } else {
                    Toast.info(res.data.msg, 2);
                }
            })
        }
    };
xuzhenghua committed
172

xuzhenghua committed
173 174 175 176 177
    // 取消砍价
    cancel = () => {
        let data = {
            courseId: getParam('id')
        }
zhanghaozhe committed
178
        http.post(`${API.home}/m/bargain/cancel`, data).then((res) => {
xuzhenghua committed
179 180 181 182 183
            if (res.data.code === 200) {
                this.setState({
                    isShowOverlay: false,
                    bargainStatus: '',
                })
184 185 186 187 188 189 190
                http.get(`${API['base-api']}/m/cart/addtopreorder/[${getParam('id')}]`).then((res) => {
                    if (res.data.errno === 0) {
                        this.props.history.push(`/order?id=[${getParam('id')}]`, {simple: 1});
                    } else {
                        Toast.info(res.data.msg, 2);
                    }
                })
191
                // window.location.href = '/shopcart'
192
                // this.props.history.push(`/order?id=[${getParam('id')}]`, {simple: 1})
xuzhenghua committed
193 194 195 196 197
            } else {
                Toast.info(res.data.msg, 2)
            }
        })
    }
xuzhenghua committed
198

xuzhenghua committed
199 200 201 202 203 204 205 206
    close = () => {
        this.setState({
            isShowOverlay: false,
            bargainStatus: '',
        })
    }
    // 砍价接口
    toKanjia = () => {
FE committed
207 208 209
        const {user, history} = this.props;
        const uid = user && user.data && user.data.uid;
        const course_id = getParam('id');
xuzhenghua committed
210 211 212 213
        if (!uid) {
            this.props.history.push('/passport/login')
        } else {
            let data = {
FE committed
214
                course_id,
xuzhenghua committed
215 216 217
                type: 1,  // 1 用户自己砍价 2 使用砍价神器 3 好友助力砍价 4 好友第二次助力
                parent_uid: 0 // 被助力人id 【自己本人操作传0】
            }
218
            http.post(`${API.home}/m/bargain/toBargain`, data).then((res) => {
219
                const {data, code, msg} = res.data;
FE committed
220
                if (code === 200) {
xuzhenghua committed
221 222 223 224 225 226 227 228
                    // user_status 用户状态 1-关注公众号,2-绑定手机号 3-再砍一刀 (是发起人没有这个字段)
                    if(data.user_status === 2) {
                        this.setState({
                            bindPhone: true
                        })
                    }else {
                        history.push(`/bargain-middle-page?id=${course_id}&bargaincode=${data.bargain_code}&is_originator=1`)
                    }
xuzhenghua committed
229
                } else {
FE committed
230
                    Toast.info(msg, 2)
xuzhenghua committed
231 232 233 234 235
                }
            })
        }
    }

xuzhenghua committed
236 237 238 239 240 241 242 243 244 245
    // 开始学习
    toStudy=(vCourseId,isHaveVideo)=>{
        const { history } = this.props;
        if(isHaveVideo == 0){
            Toast.info('尚未开课,开课后立即上传课程~', 2)
        } else {
            history.push(`/play/video?id=${vCourseId}`)
        }
    }

xuzhenghua committed
246 247 248 249
    //获取砍价信息
    getBargainInfo = () => {
        const {user} = this.props
        const uid = user && user.data && user.data.uid
xuzhenghua committed
250
        let data = {
xuzhenghua committed
251
            courseId: getParam('id')
xuzhenghua committed
252
        }
wangshuo committed
253
        http.post(`${API.home}/m/bargain/courseDetail`, data).then((res) => {
xuzhenghua committed
254
            if (res.data.code === 200) {
xuzhenghua committed
255 256 257
                this.setState({
                    barInfo: res.data.data
                })
xuzhenghua committed
258 259 260 261 262
            } else {
                Toast.info(res.data.msg, 2)
            }
        })
    }
xuzhenghua committed
263

xuzhenghua committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
    // 付定金 付尾款
    expandPay = (info,type)=>{
        // type 等于1是定金 等于2是尾款
        const {user, history} = this.props;
        const uid = user && user.data && user.data.uid;
        if (!uid) {
            this.props.history.push('/passport/login')
        } else {
            if(type == 1){
                this.props.history.push(
                    `/deposit-order?oid=${getParam('id')}&source=${1}`,
                    {
                        id: getParam('id'),
                        isexpand: 1,
                        sourcenum: 1
                    }
                )
            } else {
                let timeStamp = Date.parse(new Date()) / 1000;
                if (timeStamp >= info.start_timestamp) {
                    this.props.history.push(
                        '/final-deposit-order?source=1',
                        {
                            id: getParam('id'),
                            sourcenum: 1

                        }
                    )
                } else {
                    Toast.info("付尾款时间将在" + info.final_start_time + "开启",2);
                }
            }
        }
    }

xuzhenghua committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
    // 隐藏弹窗
    handleToHide = (key) => {
        console.log(key);
        this.setState({
            [key]: false
        });
    }

    // 绑定手机号 -- 确认
    confirmBindPhone = (params, bindInfo) => {
        this.setState({
            bindPhone: false,
            bindConfrm: true,
            formInfo: params,
            bindInfo
        });
    }

xuzhenghua committed
317
    render() {
318
        // data 课程信息;barInfo 砍价信息
xuzhenghua committed
319 320 321 322 323 324 325 326 327 328
        const { user = {}, toCart, country } = this.props;
        const { 
            countdown,
            barInfo, 
            courseInfo: info = {}, 
            bindPhone, 
            bindConfrm,
            bindInfo,
            formInfo,
        } = this.state;
329
        const uid = user.data && user.data.uid;
xuzhenghua committed
330 331
        return (
            <div>
xuzhenghua committed
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
                {/* 绑定手机号 */}
                <Mask visible={bindPhone} handleToHide={() => this.handleToHide('bindPhone')}>
                    <BindPhone 
                        country={country}
                        handleToBargain={this.toKanjia}
                        confirmBindPhone={this.confirmBindPhone}
                    />
                </Mask>

                {/* 绑定手机号--确认 */}
                <Mask visible={bindConfrm} handleToHide={() => this.handleToHide('bindConfrm')}>
                    <BargainConfirmBind 
                        data={formInfo}
                        bindInfo={bindInfo}
                        handleToHide={() => this.handleToHide('bindConfrm')}
                        handleToBargain={this.toKanjia}
                    />
                </Mask>
                
xuzhenghua committed
351 352
                {/*正常购买*/}
                {
xuzhenghua committed
353
                    info.is_baoming === 0 && info.group_status !== 3 &&
xuzhenghua committed
354
                    <div className='btns-box'>
xuzhenghua committed
355
                        <a className='consult consult-s' href="https://q.url.cn/AB8aue?_type=wpa&qidian=true">
xuzhenghua committed
356 357
                            <i className='iconfont iconerji'></i>
                            <span>课程咨询</span>
xuzhenghua committed
358
                        </a>
xuzhenghua committed
359 360 361 362 363 364
                        {
                            info.in_cart &&
                            <Link to='/shopcart' className='btn btn-s bg-FCCD05'>去购物车结算</Link>
                        }
                        {
                            !info.in_cart &&
FE committed
365
                            <button className='btn btn-s bg-FCCD05' onClick={e => toCart(1)}>加入购物车</button>
xuzhenghua committed
366 367

                        }
wangshuo committed
368
                        <span className='btn btn-s bg-FD7700' onClick={e => this.simpleCourse()}>立即报名</span>
xuzhenghua committed
369 370 371
                    </div>
                }

xuzhenghua committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385
                {/* 定金课程 is_deposit 是否定金课程 0-否 1-付定金 2-付尾款*/}
                {
                    info.is_baoming === 0 && info.is_deposit != 0  &&
                    <div className='btns-box'>
                        <a className='consult consult-m' href="https://q.url.cn/AB8aue?_type=wpa&qidian=true">
                            <i className='iconfont iconerji' />
                            <span>课程咨询</span>
                        </a>
                        <div className='btn btn-m bg-FD7700' onClick={()=>this.expandPay(info.deposit_info,info.is_deposit)}>
                            {info.is_deposit == 1 ? '立即付定金':'立即付尾款'}
                        </div>
                    </div>
                }

xuzhenghua committed
386 387
                {/*已购买*/}
                {
xuzhenghua committed
388
                    info.is_baoming === 1 &&
xuzhenghua committed
389
                    <div className='btns-box'>
xuzhenghua committed
390
                        <a className='consult consult-m' href="https://q.url.cn/AB8aue?_type=wpa&qidian=true">
wangshuo committed
391
                            <i className='iconfont iconerji' />
xuzhenghua committed
392
                            <span>课程咨询</span>
xuzhenghua committed
393 394 395 396
                        </a>
                        <a className='btn btn-m bg-09f' onClick={() => this.toStudy(info.v_course_id,info.is_have_video)}>
                            开始学习
                        </a>
xuzhenghua committed
397 398 399 400
                    </div>
                }
                {/*拼团 未开团*/}
                {
401
                    info.is_baoming === 0 && info.group_status === 3 &&
xuzhenghua committed
402
                    <div className='btns-box'>
xuzhenghua committed
403
                        <a className='consult consult-s' href="https://q.url.cn/AB8aue?_type=wpa&qidian=true">
xuzhenghua committed
404 405
                            <i className='iconfont iconerji'></i>
                            <span>课程咨询</span>
xuzhenghua committed
406
                        </a>
407
                        <button className='btn btn-s bg-F4AAA7' onClick={this.simpleCourse}>
xuzhenghua committed
408 409 410 411
                            <span>{${info.price1}`}</span>
                            <span>直接购买</span>
                        </button>
                        <button className='btn btn-s bg-E02E24'>
412
                            <span onClick={this.keyToGroup}>
xuzhenghua committed
413 414
                                <span>{${info.pdd_group_info.price}`}</span>
                                <span>一键开团</span>
415
                            </span>
xuzhenghua committed
416 417
                        </button>
                    </div>
418

xuzhenghua committed
419 420 421
                }
                {/*拼团 已开团*/}
                {
422
                    info.is_baoming === 0 && info.group_status === 4 &&
xuzhenghua committed
423
                    <div className='btns-box'>
xuzhenghua committed
424
                        <a className='consult consult-s' href="https://q.url.cn/AB8aue?_type=wpa&qidian=true">
xuzhenghua committed
425 426
                            <i className='iconfont iconerji'></i>
                            <span>课程咨询</span>
xuzhenghua committed
427
                        </a>
428
                        <div className='btn btn-l bg-E02E24' onClick={this.props.invitedFriends}>
wangshuo committed
429
                            邀请好友参团 {countdown} 后结束
430
                        </div>
xuzhenghua committed
431 432 433 434 435
                    </div>
                }

                {/*砍价*/}
                {
436
                    info.is_baoming === 0 && this.props.data && this.props.data.is_bargain &&
xuzhenghua committed
437
                    <div className='btns-box'>
xuzhenghua committed
438
                        <a className='consult consult-s' href="https://q.url.cn/AB8aue?_type=wpa&qidian=true">
xuzhenghua committed
439 440
                            <i className='iconfont iconerji'></i>
                            <span>课程咨询</span>
xuzhenghua committed
441
                        </a>
xuzhenghua committed
442
                        <button className='btn btn-s bg-F4AAA7'>
xuzhenghua committed
443
                            <span>¥{info.price1}</span>
xuzhenghua committed
444
                            <span onClick={this.tobuy}>直接购买</span>
xuzhenghua committed
445 446
                        </button>
                        {
FE committed
447
                            (barInfo.bargain_status === 2|| (getParam('id') === '139' && barInfo.bargain_status === 3) || !uid) &&
xuzhenghua committed
448
                            <button className='btn btn-s bg-E02E24' onClick={this.toKanjia}>
xuzhenghua committed
449 450 451 452
                                我要砍价
                            </button>
                        }
                        {
453
                            (barInfo.bargain_status === 0 || barInfo.bargain_status === 1) && (uid) &&
xuzhenghua committed
454
                            <button className='btn btn-s bg-E02E24'>
455
                                <span>¥{barInfo.amount}</span>
456
                                <span onClick={this.bargainToOrder}>去支付</span>
xuzhenghua committed
457 458 459 460 461 462 463
                            </button>
                        }
                    </div>
                }

                {/*特殊课程*/}
                {
xuzhenghua committed
464
                    this.state.isbuy === 20 &&
xuzhenghua committed
465
                    <div className='btns-box'>
xuzhenghua committed
466
                        <a className='consult-l' href="https://q.url.cn/AB8aue?_type=wpa&qidian=true">
xuzhenghua committed
467 468
                            <i className='iconfont iconerji'></i>
                            <span>课程咨询</span>
xuzhenghua committed
469
                        </a>
xuzhenghua committed
470 471
                    </div>
                }
xuzhenghua committed
472

473 474 475 476
                {/*特训营课程 未登录 未报名*/}
                {
                    (info.is_aist && (this.props.user.hasError || info.is_baoming === 0)) &&
                    <div className='btns-box'>
xuzhenghua committed
477
                        <a className='consult consult-m' href="https://q.url.cn/AB8aue?_type=wpa&qidian=true">
478 479
                            <i className='iconfont iconerji'></i>
                            <span>课程咨询</span>
xuzhenghua committed
480
                        </a>
481 482 483 484 485 486 487 488

                        <button className='btn sign-up-now' onClick={e => this.signUpNow()}>
                            <span>立即报名</span>
                        </button>
                    </div>
                }
                {/*特训营课程 登陆且已报名*/}
                {
wangshuo committed
489
                    info.is_aist && !this.props.user.hasError && info.is_baoming === 1 &&
490
                    <div className='btns-box'>
xuzhenghua committed
491
                        <a className='consult consult-m' href="https://q.url.cn/AB8aue?_type=wpa&qidian=true">
492 493
                            <i className='iconfont iconerji'></i>
                            <span>课程咨询</span>
xuzhenghua committed
494
                        </a>
495 496 497 498

                        {
                            info.aist_start_time === "" ?
                                (
xuzhenghua committed
499
                                    <a className='btn btn-m bg-09f' onClick={() => this.toStudy(info.v_course_id,info.is_have_video)}>开始学习</a>
500 501 502 503 504 505 506 507 508
                                ) : (
                                    <button className='btn btn-m wait-open' >
                                        <span>{this.formatDate(info.aist_start_time)}</span>
                                    </button>
                                )
                        }
                    </div>
                }

xuzhenghua committed
509 510 511 512 513 514 515 516 517 518 519 520 521 522

                {
                    this.state.isShowOverlay &&
                    <Overlay>
                        {/*引导关注公众号*/}
                        {
                            this.state.bargainStatus === 1 &&
                            <CancelBargain close={this.close} cancel={this.cancel}/>
                        }

                        <i onClick={this.close} className={'iconfont iconiconfront-2 bargain-close'}></i>
                    </Overlay>

                }
xuzhenghua committed
523 524 525 526 527
            </div>
        )
    }
}

xuzhenghua committed
528 529 530 531 532 533 534 535 536 537 538 539 540 541

function CancelBargain(props) {
    return (
        <div className='cancel-bargain'>
            <p className='top-img'><i className='iconfont icondanseshixintubiao-8'></i></p>
            <p className='tip-mess'>您已发起砍价,直接购买将清除已砍金额。直接购买可使用优惠券~</p>
            <div className="btns">
                <button onClick={props.close}>取消</button>
                <button onClick={props.cancel}>确定</button>
            </div>
        </div>
    )
}

xuzhenghua committed
542
export default connect(
xuzhenghua committed
543 544 545
    state => ({
        user: state.user
    }),
xuzhenghua committed
546
    {getCourses}
xuzhenghua committed
547
)(BtnStatus)