index.js 13.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

zhanghaozhe committed
12 13

class Bargain extends Component {
xuzhenghua committed
14 15 16 17 18 19 20 21 22 23
    constructor(props) {
        super(props)
        this.state = {
            isShowOverlay: false,
            kanjiaIcon: require('./image/kanjia_icon.png'),
            info: '',
            outList: [],
            list: [],
            limitPeople: '',
            status: '',
xuzhenghua committed
24 25
            bargainCode: '',
            time: ''
xuzhenghua committed
26 27 28 29 30 31 32 33 34 35 36 37 38
        }
    }

    componentDidMount() {
        this.getBargainInfo()
    }

    // 获取助理好友
    getBargainRankList = (id, type) => {
        let data = {
            courseId: id,
            type: type
        }
zhanghaozhe committed
39
        http.post(`${API.home}/m/bargain/rankList`, data).then((res) => {
xuzhenghua committed
40 41 42
            if (res.data.code === 200) {
                this.setState({
                    list: res.data.data.list,
xuzhenghua committed
43
                    outList: res.data.data.list[0]
xuzhenghua committed
44 45 46 47 48
                })
            } else {
                Toast.info(res.data.msg, 2)
            }
        })
zhanghaozhe committed
49
    }
xuzhenghua committed
50 51 52 53 54 55 56


    //获取砍价信息
    getBargainInfo = () => {
        let data = {
            courseId: getParam('id')
        }
zhanghaozhe committed
57
        http.post(`${API.home}/m/bargain/courseDetail`, data).then((res) => {
xuzhenghua committed
58 59 60
            if (res.data.code === 200) {
                this.setState({
                    info: res.data.data,
xuzhenghua committed
61 62
                    limitPeople: res.data.data.limit_people,
                    bargainCode: res.data.data.bargain_code
xuzhenghua committed
63
                })
xuzhenghua committed
64
                if (res.data.data.bargain_status === 0) {
xuzhenghua committed
65
                    this.getBargainRankList(getParam('id'), 0)
xuzhenghua committed
66
                }
xuzhenghua committed
67 68 69 70 71 72 73 74 75 76 77 78
            } else {
                Toast.info(res.data.msg, 2)
            }
        })
    }

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

xuzhenghua committed
80 81 82 83 84 85 86
    // 自组件传给父组件的boxHide
    boxHide = (val) => {
        this.setState({isShowMore: val})
    }

    // 领取砍价神器
    toArtifact = () => {
xuzhenghua committed
87 88 89
        let data = {
            courseId: getParam('id')
        }
zhanghaozhe committed
90
        http.post(`${API.home}/m/bargain/receiveLimit`, data).then((res) => {
xuzhenghua committed
91 92 93 94 95 96 97 98
            if (res.data.code === 200) {
                this.setState({
                    isShowOverlay: true,
                    status: 1,
                })
            } else {
                Toast.info(res.data.msg, 2)
            }
xuzhenghua committed
99 100 101 102 103
        })
    }

    // 使用砍价神器
    useArtifact = () => {
xuzhenghua committed
104
        this.toKanjia(getParam('id'), 2, 0)
xuzhenghua committed
105 106 107 108 109 110 111
    }

    // 加入购物车
    toCart = () => {
        let data = {
            course_id: getParam('id')
        }
zhanghaozhe committed
112
        http.post(`${API.home}/m/cart/add`, data).then((res) => {
xuzhenghua committed
113 114 115 116 117 118 119
            if (res.data.code === 200 || res.data.code === 15001) {
                this.props.history.push('/shopcart')
            } else {
                Toast.info(res.data.msg, 2);
            }
        })
    }
xuzhenghua committed
120 121 122 123 124
    // 继续砍价
    continueBargain = () => {
        const code = this.state.bargainCode
        this.props.history.push(`/bargain-middle-page?id=${getParam('id')}&bargaincode=${code}&is_originator=1`)
    }
xuzhenghua committed
125 126 127

    // 我要砍价
    iWantBargain = () => {
xuzhenghua committed
128
        this.toKanjia(getParam('id'), 1, 0)
xuzhenghua committed
129
    }
xuzhenghua committed
130

xuzhenghua committed
131 132 133 134 135 136 137
    // 砍价接口
    toKanjia = (id, type, uid) => {
        let data = {
            course_id: id,
            type: type,  // 1 用户自己砍价 2 使用砍价神器 3 好友助力砍价 4 好友第二次助力
            parent_uid: uid // 被助力人id 【自己本人操作传0】
        }
FE committed
138
        const { history } = this.props;
zhanghaozhe committed
139
        http.post(`${API.home}/m/bargain/toBargain`, data).then((res) => {
FE committed
140 141
            const { data: { data, code } } = res;
            if (code === 200) {
xuzhenghua committed
142 143 144 145 146
                if (type === 2) {
                    this.setState({
                        isShowOverlay: true,
                        status: 2,
                    })
xuzhenghua committed
147
                } else {
FE committed
148
                    if (data.user_status === 2) {
xuzhenghua committed
149 150 151 152 153
                        this.setState({
                            isShowOverlay: true,
                            status: 3,
                        })
                    } else {
FE committed
154
                        history.push(`/bargain-middle-page?id=${getParam('id')}&bargaincode=${data.bargain_code}&is_originator=1`)
xuzhenghua committed
155
                    }
xuzhenghua committed
156 157 158 159
                }
            } else {
                Toast.info(res.data.msg, 2)
            }
xuzhenghua committed
160 161 162
        })
    }

xuzhenghua committed
163

xuzhenghua committed
164 165 166 167 168 169
    // 关闭弹窗
    close = () => {
        this.setState({
            isShowOverlay: false,
            status: '',
        })
zhanghaozhe committed
170 171
    }

zhanghaozhe committed
172 173
    render() {
        return (
zhanghaozhe committed
174
            <div className={'bargain-func'}>
xuzhenghua committed
175
                {/*bargain_status 砍价状态 0-砍价中,1砍价结束,待支付,2砍价过期(没有砍价记录没有砍价信息),3已购买*/}
zhanghaozhe committed
176
                {
xuzhenghua committed
177 178 179 180
                    this.state.info.bargain_status === 2 &&
                    <BargainIntro limitPeople={this.state.limitPeople} iWantBargain={this.iWantBargain}/>
                }
                {
xuzhenghua committed
181
                    (this.state.info.bargain_status === 0 || this.state.info.bargain_status === 1) &&
xuzhenghua committed
182 183 184 185 186 187
                    <BargainStatus
                        info={this.state.info}
                        outList={this.state.outList}
                        getMore={this.getMore}
                        toArtifact={this.toArtifact}
                        useArtifact={this.useArtifact}
xuzhenghua committed
188
                        continueBargain={this.continueBargain}
xuzhenghua committed
189 190 191 192 193 194
                    />
                }


                {
                    this.state.isShowOverlay &&
zhanghaozhe committed
195
                    <Overlay>
xuzhenghua committed
196 197 198
                        {/*绑定手机号*/}
                        {
                            this.state.status === 3 &&
xuzhenghua committed
199
                            <BargainInfo iWantBargain={this.iWantBargain}/>
xuzhenghua committed
200 201 202 203 204 205
                        }


                        {/*领取砍价神器*/}
                        {
                            this.state.status === 1 &&
xuzhenghua committed
206
                            <Artifact useArtifact={this.useArtifact}/>
xuzhenghua committed
207 208 209 210
                        }
                        {/*使用砍价神器*/}
                        {
                            this.state.status === 2 &&
xuzhenghua committed
211
                            <UseArtifact toCart={this.toCart}/>
xuzhenghua committed
212 213 214 215 216 217
                        }

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

zhanghaozhe committed
219
                    </Overlay>
xuzhenghua committed
220

zhanghaozhe committed
221
                }
xuzhenghua committed
222 223 224 225 226 227


                {/*更多好友砍价*/}
                <Ranking list={this.state.list} icon={this.state.kanjiaIcon} limitPeople={this.state.limitPeople}
                         isShowMore={this.state.isShowMore} boxHide={this.boxHide}></Ranking>

zhanghaozhe committed
228
            </div>
xuzhenghua committed
229
        )
zhanghaozhe committed
230 231 232
    }
}

xuzhenghua committed
233
function BargainIntro(props) {
zhanghaozhe committed
234 235 236 237 238 239
    return (
        <div className="intro-outer">
            <div className="intro-inner">
                <Flex direction={'column'} justify={'center'} className={'intro-wrapper'}>
                    <p>
                        邀请
xuzhenghua committed
240
                        <span className={'indicator'}>{props.limitPeople}</span>
zhanghaozhe committed
241 242 243 244 245
                        好友帮忙砍价可获得
                        <span className={'indicator'}>【砍价神器】</span>
                    </p>
                    <p>一刀绝杀,砍爆底价</p>
                    <Flex.Item>
xuzhenghua committed
246
                        <button onClick={props.iWantBargain}>我要砍价</button>
zhanghaozhe committed
247 248 249 250 251 252 253
                    </Flex.Item>
                </Flex>
            </div>
        </div>
    )
}

xuzhenghua committed
254
function BargainStatus(props) {
xuzhenghua committed
255

xuzhenghua committed
256 257 258 259
    const [day, setDay] = useState(0)
    const [hour, setHour] = useState(0)
    const [min, setMin] = useState(0)
    const [sec, setSec] = useState(0)
xuzhenghua committed
260 261


xuzhenghua committed
262 263 264 265
    let thirdRow, btn
    // is_artifact	 0-再邀请多少人可以使用 1-可以使用未领取 2-已领取 3-已使用
    if (props.info.is_artifact === 0) {
        thirdRow =
xuzhenghua committed
266 267
            <div>再邀请<span className={'indicator'}>{props.info.invit_num}</span>位好友助力即可获得<span
                className={'indicator'}>【砍价神器】</span></div>
xuzhenghua committed
268 269 270 271 272 273 274
    } 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
275 276 277 278 279 280 281 282 283


    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
284
        setDay(differenceInDays(new Date(date), now) % 24)
xuzhenghua committed
285 286 287

    }, 1000)

xuzhenghua committed
288

zhanghaozhe committed
289 290
    return (
        <div className="status-outer">
xuzhenghua committed
291 292 293 294 295 296 297 298 299

            {
                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
300 301 302
                                <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
303
                                <span className='over'>后砍价结束</span>
xuzhenghua committed
304 305
                            </div>
                            <div onClick={props.getMore}>{props.info.assist_num}位好友助力></div>
zhanghaozhe committed
306
                        </div>
xuzhenghua committed
307 308 309 310 311 312 313

                        <div className="sec-row">
                            <div>
                                <img
                                    src={props.outList.avatar_file}
                                    alt=""/>
                                <span className='name'>{props.outList.user_name}</span>
xuzhenghua committed
314
                            </div>
xuzhenghua committed
315 316 317 318 319
                            <div>
                                <div>砍掉<span className={'indicator'}>{props.outList.amount}</span></div>
                                <button onClick={props.continueBargain}>继续砍价</button>
                            </div>
                        </div>
xuzhenghua committed
320 321 322
                        <div className="third-row">
                            {thirdRow}
                            {btn}
zhanghaozhe committed
323
                        </div>
xuzhenghua committed
324 325 326
                    </Flex>
                </div>
            }
zhanghaozhe committed
327

xuzhenghua committed
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
            {
                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
343
                            砍价金额将于
xuzhenghua committed
344 345 346 347 348 349
                            <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
350
                            后清零,请尽快完成支付
xuzhenghua committed
351 352 353 354
                        </div>
                    </Flex>
                </div>
            }
zhanghaozhe committed
355 356 357 358
        </div>
    )
}

zhanghaozhe committed
359

xuzhenghua committed
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
{/*领取砍价神器*/
}

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) {
    return (
        <div className='use-artifact-box'>
xuzhenghua committed
380
            <img className='top-img' src={require('./image/kanjia_cg_icon.png')} alt=""/>
xuzhenghua committed
381 382 383 384 385 386 387 388 389 390 391 392 393 394
            <p className='top-tip'>厉害了,又砍掉了10元!</p>
            <p className='middle-tip'>
                你已经砍了
                <span className={'indicator'}>109</span>
                没见过你这么能砍的人...
            </p>
            <p className='btm-tip'>
                不能再砍了哦~
            </p>
            <button className='tubuy' onClick={props.toCart}>去支付</button>
        </div>
    )
}

zhanghaozhe committed
395

xuzhenghua committed
396 397 398
export default compose(
    withRouter
)(Bargain)