index.js 16.6 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"
zhanghaozhe committed
4 5
import Overlay from '../overlay'
import BargainInfo from './bargainInfo'
xuzhenghua committed
6
import {differenceInSeconds, differenceInMinutes, differenceInHours, differenceInDays} from "date-fns"
zhanghaozhe committed
7
import { api, getParam, http } from "@/utils"
xuzhenghua committed
8
import Ranking from "@/components/bargainMiddlePage/ranking"
zhanghaozhe committed
9 10
import { compose } from "redux"
import { withRouter } from 'react-router-dom'
xuzhenghua committed
11
import {connect} from "react-redux";
FE committed
12
import { Object } from 'core-js'
xuzhenghua committed
13

zhanghaozhe committed
14 15

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

    componentDidMount() {
xuzhenghua committed
36
        this.getBargainInfo();
FE committed
37
        this.judgePopupTypeFromCountry();
xuzhenghua committed
38 39
    }

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

        // 清除
        delCountryNum();
    }
  }

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


    //获取砍价信息
FE committed
71
    getBargainInfo = (isFetch = true) => {
xuzhenghua committed
72 73 74
        let data = {
            courseId: getParam('id')
        }
zhanghaozhe committed
75
        http.post(`${API.home}/m/bargain/courseDetail`, data).then((res) => {
FE committed
76 77
            const { code, data, msg = '' } = res.data;
            if (code === 200) {
xuzhenghua committed
78
                this.setState({
FE committed
79
                    barInfo: data,
xuzhenghua committed
80 81 82
                    // info: res.data.data,
                    // limitPeople: res.data.data.limit_people,
                    // bargainCode: res.data.data.bargain_code
FE committed
83
                });
FE committed
84 85 86 87 88 89 90 91
                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
92
                }
xuzhenghua committed
93
            } else {
FE committed
94
                Toast.info(msg, 2);
xuzhenghua committed
95
            }
FE committed
96
        });
xuzhenghua committed
97 98 99 100 101 102 103 104
    }

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

xuzhenghua committed
106 107 108 109 110 111 112
    // 自组件传给父组件的boxHide
    boxHide = (val) => {
        this.setState({isShowMore: val})
    }

    // 领取砍价神器
    toArtifact = () => {
xuzhenghua committed
113 114 115
        let data = {
            courseId: getParam('id')
        }
zhanghaozhe committed
116
        http.post(`${API.home}/m/bargain/receiveLimit`, data).then((res) => {
xuzhenghua committed
117 118 119
            if (res.data.code === 200) {
                this.setState({
                    isShowOverlay: true,
FE committed
120 121
                    status: 1
                });
FE committed
122
                this.getBargainInfo(false);
xuzhenghua committed
123 124 125
            } else {
                Toast.info(res.data.msg, 2)
            }
xuzhenghua committed
126 127 128 129 130
        })
    }

    // 使用砍价神器
    useArtifact = () => {
xuzhenghua committed
131
        this.toKanjia(getParam('id'), 2, 0)
xuzhenghua committed
132 133 134 135 136 137 138
    }

    // 加入购物车
    toCart = () => {
        let data = {
            course_id: getParam('id')
        }
zhanghaozhe committed
139
        http.post(`${API.home}/m/cart/add`, data).then((res) => {
xuzhenghua committed
140 141 142 143 144 145 146
            if (res.data.code === 200 || res.data.code === 15001) {
                this.props.history.push('/shopcart')
            } else {
                Toast.info(res.data.msg, 2);
            }
        })
    }
xuzhenghua committed
147 148
    // 继续砍价
    continueBargain = () => {
xuzhenghua committed
149 150
        const { barInfo = {} } = this.state;
        const code = barInfo.bargain_code;
xuzhenghua committed
151 152
        this.props.history.push(`/bargain-middle-page?id=${getParam('id')}&bargaincode=${code}&is_originator=1`)
    }
xuzhenghua committed
153 154 155

    // 我要砍价
    iWantBargain = () => {
FE committed
156 157
        const { history, user } = this.props
        const uid = user && user.data && user.data.uid;
xuzhenghua committed
158
        if(!uid){
FE committed
159
            history.push('/passport/login');
xuzhenghua committed
160
        } else {
FE committed
161
            this.toKanjia(getParam('id'), 1, 0);
xuzhenghua committed
162
        }
xuzhenghua committed
163
    }
xuzhenghua committed
164

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

                // user_status 用户状态 1-关注公众号,2-绑定手机号 3-再砍一刀 (是发起人没有这个字段)
                if(data.user_status === 2) {
xuzhenghua committed
179 180
                    this.setState({
                        isShowOverlay: true,
FE committed
181 182 183 184
                        status: 3,
                    });
                }else {
                    if(type === 2) {
xuzhenghua committed
185 186
                        this.setState({
                            isShowOverlay: true,
FE committed
187
                            status: 2,
188 189 190 191 192 193 194
                            price: data.amount,
                            sum: data.bargain_price
                        });
                        this.getBargainInfo(false);
                        this.getBargainRankList({
                            type: 0,
                            bargain_code: data.bargain_code
FE committed
195 196
                        });
                    }else {
FE committed
197
                        history.push(`/bargain-middle-page?id=${getParam('id')}&bargaincode=${data.bargain_code}&is_originator=1`)
xuzhenghua committed
198
                    }
xuzhenghua committed
199
                }
FE committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
                // if (type === 2) {
                //     this.setState({
                //         isShowOverlay: true,
                //         status: 2,
                //     })
                // } else {
                //     // user_status 用户状态 1-关注公众号,2-绑定手机号 3-再砍一刀 (是发起人没有这个字段)
                //     if (data.user_status === 2) {
                //         this.setState({
                //             isShowOverlay: true,
                //             status: 3,
                //         })
                //     } else {
                //         history.push(`/bargain-middle-page?id=${getParam('id')}&bargaincode=${data.bargain_code}&is_originator=1`)
                //     }
                // }
xuzhenghua committed
216 217 218
            } else {
                Toast.info(res.data.msg, 2)
            }
xuzhenghua committed
219 220 221
        })
    }

xuzhenghua committed
222

xuzhenghua committed
223 224 225 226 227 228
    // 关闭弹窗
    close = () => {
        this.setState({
            isShowOverlay: false,
            status: '',
        })
zhanghaozhe committed
229 230
    }

zhanghaozhe committed
231
    render() {
232
        const { list, outList, barInfo, country, price, sum } = this.state;
xuzhenghua committed
233 234
        const {user} = this.props
        const uid = user && user.data && user.data.uid
zhanghaozhe committed
235
        return (
zhanghaozhe committed
236
            <div className={'bargain-func'}>
xuzhenghua committed
237

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


                {
                    this.state.isShowOverlay &&
zhanghaozhe committed
258
                    <Overlay>
xuzhenghua committed
259 260 261
                        {/*绑定手机号*/}
                        {
                            this.state.status === 3 &&
FE committed
262
                            <BargainInfo country={country} iWantBargain={this.iWantBargain} toClose={this.close} />
xuzhenghua committed
263 264 265 266 267 268
                        }


                        {/*领取砍价神器*/}
                        {
                            this.state.status === 1 &&
xuzhenghua committed
269
                            <Artifact useArtifact={this.useArtifact}/>
xuzhenghua committed
270 271 272 273
                        }
                        {/*使用砍价神器*/}
                        {
                            this.state.status === 2 &&
274 275 276 277 278
                            <UseArtifact 
                                price={price}
                                sum={sum}
                                toCart={this.toCart}
                            />
xuzhenghua committed
279 280 281 282 283 284
                        }

                        {
                            this.state.status !== 0 &&
                            <i onClick={this.close} className={'iconfont iconiconfront-2 bargain-close'}></i>
                        }
xuzhenghua committed
285

zhanghaozhe committed
286
                    </Overlay>
xuzhenghua committed
287

zhanghaozhe committed
288
                }
xuzhenghua committed
289 290 291


                {/*更多好友砍价*/}
xuzhenghua committed
292 293 294
                <Ranking
                    list={list}
                    icon={this.state.kanjiaIcon}
xuzhenghua committed
295
                    limitPeople={barInfo.limit_people}
xuzhenghua committed
296
                    isShowMore={this.state.isShowMore}
xuzhenghua committed
297 298
                    boxHide={this.boxHide}
                />
xuzhenghua committed
299

zhanghaozhe committed
300
            </div>
xuzhenghua committed
301
        )
zhanghaozhe committed
302 303 304
    }
}

xuzhenghua committed
305
function BargainIntro(props) {
zhanghaozhe committed
306 307 308 309 310 311
    return (
        <div className="intro-outer">
            <div className="intro-inner">
                <Flex direction={'column'} justify={'center'} className={'intro-wrapper'}>
                    <p>
                        邀请
xuzhenghua committed
312
                        <span className={'indicator'}>{props.limitPeople}</span>
zhanghaozhe committed
313 314 315 316 317
                        好友帮忙砍价可获得
                        <span className={'indicator'}>【砍价神器】</span>
                    </p>
                    <p>一刀绝杀,砍爆底价</p>
                    <Flex.Item>
xuzhenghua committed
318
                        <button onClick={props.iWantBargain}>我要砍价</button>
zhanghaozhe committed
319 320 321 322 323 324 325
                    </Flex.Item>
                </Flex>
            </div>
        </div>
    )
}

xuzhenghua committed
326
function BargainStatus(props) {
xuzhenghua committed
327

xuzhenghua committed
328 329 330 331
    const [day, setDay] = useState(0)
    const [hour, setHour] = useState(0)
    const [min, setMin] = useState(0)
    const [sec, setSec] = useState(0)
xuzhenghua committed
332 333


xuzhenghua committed
334 335 336 337
    let thirdRow, btn
    // is_artifact	 0-再邀请多少人可以使用 1-可以使用未领取 2-已领取 3-已使用
    if (props.info.is_artifact === 0) {
        thirdRow =
xuzhenghua committed
338 339
            <div>再邀请<span className={'indicator'}>{props.info.invit_num}</span>位好友助力即可获得<span
                className={'indicator'}>【砍价神器】</span></div>
xuzhenghua committed
340 341 342 343 344 345 346
    } 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
347 348 349 350 351 352 353 354 355


    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
356
        setDay(differenceInDays(new Date(date), now) % 24)
xuzhenghua committed
357 358 359

    }, 1000)

xuzhenghua committed
360

zhanghaozhe committed
361 362
    return (
        <div className="status-outer">
xuzhenghua committed
363 364 365 366 367 368 369 370 371

            {
                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
372 373 374
                                <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
375
                                <span className='over'>后砍价结束</span>
xuzhenghua committed
376 377
                            </div>
                            <div onClick={props.getMore}>{props.info.assist_num}位好友助力></div>
zhanghaozhe committed
378
                        </div>
xuzhenghua committed
379 380 381 382 383 384 385

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

xuzhenghua committed
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
            {
                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
415
                            砍价金额将于
xuzhenghua committed
416 417 418 419 420 421
                            <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
422
                            后清零,请尽快完成支付
xuzhenghua committed
423 424 425 426
                        </div>
                    </Flex>
                </div>
            }
zhanghaozhe committed
427 428 429 430
        </div>
    )
}

zhanghaozhe committed
431

xuzhenghua committed
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
{/*领取砍价神器*/
}

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>
    )
}

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

zhanghaozhe committed
468

xuzhenghua committed
469
export default compose(
xuzhenghua committed
470 471 472 473 474 475
    connect(
        state => ({
            user: state.user
        }),
        null
    ),
xuzhenghua committed
476 477
    withRouter
)(Bargain)