index.js 16.2 KB
Newer Older
zhanghaozhe committed
1
import React, { Component, useState } from 'react'
zhanghaozhe committed
2
import './bargain.scss'
zhanghaozhe committed
3
import { Flex, Toast } from "antd-mobile"
xuzhenghua committed
4
import {differenceInSeconds, differenceInMinutes, differenceInHours, differenceInDays} from "date-fns"
FE committed
5
import { getParam, http } from "@/utils"
xuzhenghua committed
6
import Ranking from "@/components/bargainMiddlePage/ranking"
zhanghaozhe committed
7 8
import { compose } from "redux"
import { withRouter } from 'react-router-dom'
FE committed
9 10 11 12
import { connect } from "react-redux";
import Mask from '@/common/Mask/index';
import BargainBindPhone from './../bindPhone/index';
import BargainConfirmBind from './../bindPhone/confirm';
zhanghaozhe committed
13 14

class Bargain extends Component {
xuzhenghua committed
15 16 17 18 19 20 21 22
    constructor(props) {
        super(props)
        this.state = {
            kanjiaIcon: require('./image/kanjia_icon.png'),
            info: '',
            outList: [],
            list: [],
            status: '',
xuzhenghua committed
23
            time: '',
FE committed
24
            barInfo: {},
25 26
            country: '86',
            price: 0, // 每次砍价砍掉的价格
FE committed
27 28 29
            sum: 0, // 累计砍掉的价格,
            bindInfo: {}, // 冲突信息
            formInfo: {}
xuzhenghua committed
30 31 32 33
        }
    }

    componentDidMount() {
xuzhenghua committed
34
        this.getBargainInfo();
FE committed
35
        this.judgePopupTypeFromCountry();
xuzhenghua committed
36 37
    }

FE committed
38 39
    // 选择区号后进入
    judgePopupTypeFromCountry = () => {
FE committed
40 41 42 43 44 45 46 47 48 49
        const { country, delCountryNum } = this.props;
        if(country.from && country.from === 'bargain') {
            this.setState({
                country: country.num,
                status: 3,
            });

            // 清除
            delCountryNum();
        }
FE committed
50 51
    }

xuzhenghua committed
52
    // 获取助理好友
FE committed
53 54
    getBargainRankList = (params = {}) => {
        http.post(`${API.home}/m/bargain/rankList`, params).then((res) => {
xuzhenghua committed
55 56 57
            if (res.data.code === 200) {
                this.setState({
                    list: res.data.data.list,
xuzhenghua committed
58
                    outList: res.data.data.list[0]
xuzhenghua committed
59 60 61 62 63
                })
            } else {
                Toast.info(res.data.msg, 2)
            }
        })
zhanghaozhe committed
64
    }
xuzhenghua committed
65 66

    //获取砍价信息
FE committed
67
    getBargainInfo = (isFetch = true) => {
xuzhenghua committed
68 69 70
        let data = {
            courseId: getParam('id')
        }
zhanghaozhe committed
71
        http.post(`${API.home}/m/bargain/courseDetail`, data).then((res) => {
FE committed
72 73
            const { code, data, msg = '' } = res.data;
            if (code === 200) {
xuzhenghua committed
74
                this.setState({
FE committed
75 76
                    barInfo: data,
                });
FE committed
77 78 79 80 81 82 83 84
                if(isFetch) {
                    // 砍价状态 0-砍价中,1砍价结束,待支付,2砍价过期(没有砍价记录没有砍价信息),3已购买
                    if (data.bargain_status === 0 || data.bargain_status === 1 ) {
                        this.getBargainRankList({
                            type: 0,
                            bargain_code: data.bargain_code
                        });
                    }
xuzhenghua committed
85
                }
xuzhenghua committed
86
            } else {
FE committed
87
                Toast.info(msg, 2);
xuzhenghua committed
88
            }
FE committed
89
        });
xuzhenghua committed
90 91 92 93 94 95 96 97
    }

    // 查看更多
    getMore = () => {
        this.setState({
            isShowMore: true
        })
    }
xuzhenghua committed
98

xuzhenghua committed
99 100 101 102 103 104 105
    // 自组件传给父组件的boxHide
    boxHide = (val) => {
        this.setState({isShowMore: val})
    }

    // 领取砍价神器
    toArtifact = () => {
xuzhenghua committed
106 107 108
        let data = {
            courseId: getParam('id')
        }
zhanghaozhe committed
109
        http.post(`${API.home}/m/bargain/receiveLimit`, data).then((res) => {
xuzhenghua committed
110 111
            if (res.data.code === 200) {
                this.setState({
FE committed
112 113
                    status: 1
                });
FE committed
114
                this.getBargainInfo(false);
xuzhenghua committed
115 116 117
            } else {
                Toast.info(res.data.msg, 2)
            }
xuzhenghua committed
118 119 120 121 122
        })
    }

    // 使用砍价神器
    useArtifact = () => {
xuzhenghua committed
123
        this.toKanjia(getParam('id'), 2, 0)
xuzhenghua committed
124 125 126 127 128 129 130
    }

    // 加入购物车
    toCart = () => {
        let data = {
            course_id: getParam('id')
        }
zhanghaozhe committed
131
        http.post(`${API.home}/m/cart/add`, data).then((res) => {
xuzhenghua committed
132 133 134 135 136 137 138
            if (res.data.code === 200 || res.data.code === 15001) {
                this.props.history.push('/shopcart')
            } else {
                Toast.info(res.data.msg, 2);
            }
        })
    }
FE committed
139

xuzhenghua committed
140 141
    // 继续砍价
    continueBargain = () => {
FE committed
142
        const { history } = this.props;
xuzhenghua committed
143 144
        const { barInfo = {} } = this.state;
        const code = barInfo.bargain_code;
FE committed
145
        history.push(`/bargain-middle-page?id=${getParam('id')}&bargaincode=${code}&is_originator=1`)
xuzhenghua committed
146
    }
xuzhenghua committed
147 148 149

    // 我要砍价
    iWantBargain = () => {
FE committed
150 151
        const { history, user } = this.props
        const uid = user && user.data && user.data.uid;
xuzhenghua committed
152
        if(!uid){
FE committed
153
            history.push('/passport/login');
xuzhenghua committed
154
        } else {
FE committed
155
            this.toKanjia(getParam('id'), 1, 0);
xuzhenghua committed
156
        }
xuzhenghua committed
157
    }
xuzhenghua committed
158

xuzhenghua committed
159 160 161 162 163 164
    // 砍价接口
    toKanjia = (id, type, uid) => {
        let data = {
            course_id: id,
            type: type,  // 1 用户自己砍价 2 使用砍价神器 3 好友助力砍价 4 好友第二次助力
            parent_uid: uid // 被助力人id 【自己本人操作传0】
FE committed
165
        };
FE committed
166
        const { history } = this.props;
zhanghaozhe committed
167
        http.post(`${API.home}/m/bargain/toBargain`, data).then((res) => {
FE committed
168 169
            const { data: { data, code } } = res;
            if (code === 200) {
FE committed
170 171 172

                // user_status 用户状态 1-关注公众号,2-绑定手机号 3-再砍一刀 (是发起人没有这个字段)
                if(data.user_status === 2) {
xuzhenghua committed
173
                    this.setState({
FE committed
174 175 176 177
                        status: 3,
                    });
                }else {
                    if(type === 2) {
xuzhenghua committed
178
                        this.setState({
FE committed
179
                            status: 2,
180 181 182 183 184 185 186
                            price: data.amount,
                            sum: data.bargain_price
                        });
                        this.getBargainInfo(false);
                        this.getBargainRankList({
                            type: 0,
                            bargain_code: data.bargain_code
FE committed
187 188
                        });
                    }else {
FE committed
189
                        history.push(`/bargain-middle-page?id=${getParam('id')}&bargaincode=${data.bargain_code}&is_originator=1`)
xuzhenghua committed
190
                    }
xuzhenghua committed
191 192 193 194
                }
            } else {
                Toast.info(res.data.msg, 2)
            }
xuzhenghua committed
195 196 197 198
        })
    }

    // 关闭弹窗
FE committed
199
    handleToHide = () => {
xuzhenghua committed
200
        this.setState({
FE committed
201 202 203 204 205 206 207 208 209 210 211
            status: ''
        });
    }

    // 绑定手机号--确认
    confirmBindPhone = (params, bindInfo) => {
        this.setState({
            status: 4,
            formInfo: params,
            bindInfo
        });
zhanghaozhe committed
212 213
    }

zhanghaozhe committed
214
    render() {
FE committed
215 216 217 218 219 220 221 222 223 224 225 226
        const { 
            list, 
            outList, 
            barInfo, 
            country, 
            price, 
            sum,
            status,
            formInfo,
            bindInfo
        } = this.state;
        const { user } = this.props
xuzhenghua committed
227
        const uid = user && user.data && user.data.uid
zhanghaozhe committed
228
        return (
zhanghaozhe committed
229
            <div className={'bargain-func'}>
xuzhenghua committed
230

xuzhenghua committed
231
                {/*bargain_status 砍价状态 0-砍价中,1砍价结束,待支付,2砍价过期(没有砍价记录没有砍价信息),3已购买*/}
zhanghaozhe committed
232
                {
FE committed
233
                    (barInfo.bargain_status === 2 || (getParam('id') === '139' && barInfo.bargain_status === 3) || !uid) &&
FE committed
234 235 236 237
                    <BargainIntro 
                        limitPeople={barInfo.limit_people} 
                        iWantBargain={this.iWantBargain}
                    />
xuzhenghua committed
238 239
                }
                {
xuzhenghua committed
240
                    (barInfo.bargain_status === 0 || barInfo.bargain_status === 1) && uid &&
xuzhenghua committed
241
                    <BargainStatus
xuzhenghua committed
242 243
                        info={barInfo}
                        outList={outList}
xuzhenghua committed
244 245 246
                        getMore={this.getMore}
                        toArtifact={this.toArtifact}
                        useArtifact={this.useArtifact}
xuzhenghua committed
247
                        continueBargain={this.continueBargain}
xuzhenghua committed
248 249 250
                    />
                }

FE committed
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
                {/* 绑定手机号 */}
                <Mask visible={status === 3} handleToHide={this.handleToHide}>
                    <BargainBindPhone 
                        country={country} 
                        handleToBargain={this.iWantBargain}
                        confirmBindPhone={this.confirmBindPhone}
                    />
                </Mask>

                {/* 绑定手机号--确认 */}
                <Mask visible={status === 4} handleToHide={this.handleToHide}>
                    <BargainConfirmBind 
                        data={formInfo} 
                        bindInfo={bindInfo}
                        handleToBargain={this.iWantBargain}
                    />
                </Mask>

                {/* 领取砍价神器 */}
                <Mask visible={status === 1} handleToHide={this.handleToHide}>
                    <Artifact useArtifact={this.useArtifact} />
                </Mask>

                {/* 使用砍价神器 */}
                <Mask visible={status === 2} handleToHide={this.handleToHide}>
                    <UseArtifact 
                        price={price}
                        sum={sum}
                        toCart={this.toCart}
                    />
                </Mask>
xuzhenghua committed
282

FE committed
283
                {/* 更多好友砍价 */}
xuzhenghua committed
284 285 286
                <Ranking
                    list={list}
                    icon={this.state.kanjiaIcon}
xuzhenghua committed
287
                    limitPeople={barInfo.limit_people}
xuzhenghua committed
288
                    isShowMore={this.state.isShowMore}
xuzhenghua committed
289 290
                    boxHide={this.boxHide}
                />
zhanghaozhe committed
291
            </div>
xuzhenghua committed
292
        )
zhanghaozhe committed
293 294 295
    }
}

FE committed
296
// 是砍价课程时,展示砍价按钮
xuzhenghua committed
297
function BargainIntro(props) {
zhanghaozhe committed
298 299 300
    return (
        <div className="intro-outer">
            <div className="intro-inner">
FE committed
301 302 303 304 305
                <Flex 
                    direction={'column'} 
                    justify={'center'} 
                    className={'intro-wrapper'}
                >
zhanghaozhe committed
306 307
                    <p>
                        邀请
xuzhenghua committed
308
                        <span className={'indicator'}>{props.limitPeople}</span>
zhanghaozhe committed
309 310 311 312 313
                        好友帮忙砍价可获得
                        <span className={'indicator'}>【砍价神器】</span>
                    </p>
                    <p>一刀绝杀,砍爆底价</p>
                    <Flex.Item>
FE committed
314
                        <button onClick={props.iWantBargain}>我要砍价</button>
zhanghaozhe committed
315 316 317 318 319 320 321
                    </Flex.Item>
                </Flex>
            </div>
        </div>
    )
}

xuzhenghua committed
322
function BargainStatus(props) {
xuzhenghua committed
323

xuzhenghua committed
324 325 326 327
    const [day, setDay] = useState(0)
    const [hour, setHour] = useState(0)
    const [min, setMin] = useState(0)
    const [sec, setSec] = useState(0)
xuzhenghua committed
328 329


xuzhenghua committed
330 331 332 333
    let thirdRow, btn
    // is_artifact	 0-再邀请多少人可以使用 1-可以使用未领取 2-已领取 3-已使用
    if (props.info.is_artifact === 0) {
        thirdRow =
xuzhenghua committed
334 335
            <div>再邀请<span className={'indicator'}>{props.info.invit_num}</span>位好友助力即可获得<span
                className={'indicator'}>【砍价神器】</span></div>
xuzhenghua committed
336 337 338 339 340 341 342
    } else if (props.info.is_artifact === 1) {
        thirdRow = <div>恭喜你获得<span className={'indicator'}>【砍价神器】</span></div>
        btn = <button onClick={props.toArtifact}>立即领取</button>
    } else if (props.info.is_artifact === 2) {
        thirdRow = <div>恭喜你获得<span className={'indicator'}>【砍价神器】</span></div>
        btn = <button onClick={props.useArtifact}>立即使用</button>
    }
xuzhenghua committed
343 344 345 346 347 348 349 350 351


    let date = props.info.end_time * 1000
    let now = Date.now()
    setInterval(() => {
        date -= 1000
        setSec(differenceInSeconds(new Date(date), now) % 60)
        setMin(differenceInMinutes(new Date(date), now) % 60)
        setHour(differenceInHours(new Date(date), now) % 24)
xuzhenghua committed
352
        setDay(differenceInDays(new Date(date), now) % 24)
xuzhenghua committed
353 354 355

    }, 1000)

xuzhenghua committed
356

zhanghaozhe committed
357 358
    return (
        <div className="status-outer">
xuzhenghua committed
359 360 361 362 363 364 365 366 367

            {
                props.info.bargain_status === 0 &&
                <div className="status-inner">

                    <Flex direction={'column'}>
                        <div className={'first-row'}>
                            <div>
                                已砍<span className={'indicator'}>{props.info.bargain_price}</span>
xuzhenghua committed
368 369 370
                                <span className={'time hour'}>{String(hour).padStart(2, 0)}</span> :&nbsp;
                                <span className={'time min'}>{String(min).padStart(2, 0)}</span> :&nbsp;
                                <span className={'time sec'}>{String(sec).padStart(2, 0)}</span>
xuzhenghua committed
371
                                <span className='over'>后砍价结束</span>
xuzhenghua committed
372 373
                            </div>
                            <div onClick={props.getMore}>{props.info.assist_num}位好友助力></div>
zhanghaozhe committed
374
                        </div>
xuzhenghua committed
375 376 377 378 379 380 381

                        <div className="sec-row">
                            <div>
                                <img
                                    src={props.outList.avatar_file}
                                    alt=""/>
                                <span className='name'>{props.outList.user_name}</span>
xuzhenghua committed
382
                            </div>
xuzhenghua committed
383 384 385 386 387
                            <div>
                                <div>砍掉<span className={'indicator'}>{props.outList.amount}</span></div>
                                <button onClick={props.continueBargain}>继续砍价</button>
                            </div>
                        </div>
xuzhenghua committed
388 389 390
                        <div className="third-row">
                            {thirdRow}
                            {btn}
zhanghaozhe committed
391
                        </div>
xuzhenghua committed
392 393 394
                    </Flex>
                </div>
            }
zhanghaozhe committed
395

xuzhenghua committed
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
            {
                props.info.bargain_status === 1 &&
                <div className="status-inner status-over">

                    <Flex direction={'column'}>
                        <div className={'first-row'}>
                            <div>
                                已砍<span className={'indicator'}>{props.info.bargain_price}</span>
                                <span className='bargain-over'>
                                    砍价结束
                                </span>
                            </div>
                            <div onClick={props.getMore}>{props.info.assist_num}位好友助力></div>
                        </div>
                        <div className="time-tobuy">
xuzhenghua committed
411
                            砍价金额将于
xuzhenghua committed
412 413 414 415 416 417
                            <span className={'time hour'}>{String(day).padStart(2, 0)}</span><span
                            className={'unit'}>  </span>
                            <span className={'time min'}>{String(hour).padStart(2, 0)}</span><span
                            className={'unit'}>  </span>
                            <span className={'time sec'}>{String(min).padStart(2, 0)}</span><span
                            className={'unit'}></span>
xuzhenghua committed
418
                            后清零,请尽快完成支付
xuzhenghua committed
419 420 421 422
                        </div>
                    </Flex>
                </div>
            }
zhanghaozhe committed
423 424 425 426
        </div>
    )
}

FE committed
427
// 领取砍价神器
xuzhenghua committed
428 429 430 431 432 433 434 435 436 437 438 439 440 441
function Artifact(props) {
    return (
        <div className='artifact-box'>
            <p className='top-tip'>恭喜你获得【砍价神器】!</p>
            <p className='middle-tip'>
                您可以使用
                <span className={'indicator'}>【砍价神器】</span>
                再砍一刀
            </p>
            <button className='use-artifact' onClick={props.useArtifact}>立即使用</button>
        </div>
    )
}

FE committed
442
// 使用砍价神器
xuzhenghua committed
443
function UseArtifact(props) {
444
    const { sum, price } = props;
xuzhenghua committed
445 446
    return (
        <div className='use-artifact-box'>
FE committed
447 448 449 450 451
            <img 
                className='top-img' 
                src={require('./image/kanjia_cg_icon.png')} 
                alt=""
            />
452
            <p className='top-tip'>厉害了,又砍掉了{price}元!</p>
xuzhenghua committed
453 454
            <p className='middle-tip'>
                你已经砍了
455
                <span className={'indicator'}>{sum}</span>
xuzhenghua committed
456 457
                没见过你这么能砍的人...
            </p>
FE committed
458
            <p className='btm-tip'>不能再砍了哦~</p>
xuzhenghua committed
459 460 461 462 463
            <button className='tubuy' onClick={props.toCart}>去支付</button>
        </div>
    )
}

xuzhenghua committed
464
export default compose(
xuzhenghua committed
465 466 467 468 469 470
    connect(
        state => ({
            user: state.user
        }),
        null
    ),
xuzhenghua committed
471 472
    withRouter
)(Bargain)