index.js 14.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"
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";
xuzhenghua committed
12

zhanghaozhe committed
13 14

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

    componentDidMount() {
xuzhenghua committed
32
        this.getBargainInfo();
xuzhenghua committed
33 34 35 36 37 38 39 40
    }

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


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

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

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

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

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

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

    // 我要砍价
    iWantBargain = () => {
xuzhenghua committed
132 133 134 135 136 137 138
        const {user} = this.props
        const uid = user && user.data && user.data.uid
        if(!uid){
            this.props.history.push('/passport/login')
        } else {
            this.toKanjia(getParam('id'), 1, 0)
        }
xuzhenghua committed
139
    }
xuzhenghua committed
140

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

xuzhenghua committed
173

xuzhenghua committed
174 175 176 177 178 179
    // 关闭弹窗
    close = () => {
        this.setState({
            isShowOverlay: false,
            status: '',
        })
zhanghaozhe committed
180 181
    }

zhanghaozhe committed
182
    render() {
xuzhenghua committed
183
        const { list, outList, barInfo } = this.state;
xuzhenghua committed
184 185
        const {user} = this.props
        const uid = user && user.data && user.data.uid
zhanghaozhe committed
186
        return (
zhanghaozhe committed
187
            <div className={'bargain-func'}>
xuzhenghua committed
188

xuzhenghua committed
189
                {/*bargain_status 砍价状态 0-砍价中,1砍价结束,待支付,2砍价过期(没有砍价记录没有砍价信息),3已购买*/}
zhanghaozhe committed
190
                {
xuzhenghua committed
191
                    (barInfo.bargain_status === 2 || !uid) &&
xuzhenghua committed
192
                    <BargainIntro limitPeople={barInfo.limit_people} iWantBargain={this.iWantBargain}/>
xuzhenghua committed
193 194
                }
                {
xuzhenghua committed
195
                    (barInfo.bargain_status === 0 || barInfo.bargain_status === 1) && uid &&
xuzhenghua committed
196
                    <BargainStatus
xuzhenghua committed
197 198
                        info={barInfo}
                        outList={outList}
xuzhenghua committed
199 200 201
                        getMore={this.getMore}
                        toArtifact={this.toArtifact}
                        useArtifact={this.useArtifact}
xuzhenghua committed
202
                        continueBargain={this.continueBargain}
xuzhenghua committed
203 204 205 206 207 208
                    />
                }


                {
                    this.state.isShowOverlay &&
zhanghaozhe committed
209
                    <Overlay>
xuzhenghua committed
210 211 212
                        {/*绑定手机号*/}
                        {
                            this.state.status === 3 &&
xuzhenghua committed
213
                            <BargainInfo iWantBargain={this.iWantBargain}/>
xuzhenghua committed
214 215 216 217 218 219
                        }


                        {/*领取砍价神器*/}
                        {
                            this.state.status === 1 &&
xuzhenghua committed
220
                            <Artifact useArtifact={this.useArtifact}/>
xuzhenghua committed
221 222 223 224
                        }
                        {/*使用砍价神器*/}
                        {
                            this.state.status === 2 &&
xuzhenghua committed
225
                            <UseArtifact toCart={this.toCart}/>
xuzhenghua committed
226 227 228 229 230 231
                        }

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

zhanghaozhe committed
233
                    </Overlay>
xuzhenghua committed
234

zhanghaozhe committed
235
                }
xuzhenghua committed
236 237 238


                {/*更多好友砍价*/}
xuzhenghua committed
239 240 241
                <Ranking
                    list={list}
                    icon={this.state.kanjiaIcon}
xuzhenghua committed
242
                    limitPeople={barInfo.limit_people}
xuzhenghua committed
243
                    isShowMore={this.state.isShowMore}
xuzhenghua committed
244 245
                    boxHide={this.boxHide}
                />
xuzhenghua committed
246

zhanghaozhe committed
247
            </div>
xuzhenghua committed
248
        )
zhanghaozhe committed
249 250 251
    }
}

xuzhenghua committed
252
function BargainIntro(props) {
zhanghaozhe committed
253 254 255 256 257 258
    return (
        <div className="intro-outer">
            <div className="intro-inner">
                <Flex direction={'column'} justify={'center'} className={'intro-wrapper'}>
                    <p>
                        邀请
xuzhenghua committed
259
                        <span className={'indicator'}>{props.limitPeople}</span>
zhanghaozhe committed
260 261 262 263 264
                        好友帮忙砍价可获得
                        <span className={'indicator'}>【砍价神器】</span>
                    </p>
                    <p>一刀绝杀,砍爆底价</p>
                    <Flex.Item>
xuzhenghua committed
265
                        <button onClick={props.iWantBargain}>我要砍价</button>
zhanghaozhe committed
266 267 268 269 270 271 272
                    </Flex.Item>
                </Flex>
            </div>
        </div>
    )
}

xuzhenghua committed
273
function BargainStatus(props) {
xuzhenghua committed
274

xuzhenghua committed
275 276 277 278
    const [day, setDay] = useState(0)
    const [hour, setHour] = useState(0)
    const [min, setMin] = useState(0)
    const [sec, setSec] = useState(0)
xuzhenghua committed
279 280


xuzhenghua committed
281 282 283 284
    let thirdRow, btn
    // is_artifact	 0-再邀请多少人可以使用 1-可以使用未领取 2-已领取 3-已使用
    if (props.info.is_artifact === 0) {
        thirdRow =
xuzhenghua committed
285 286
            <div>再邀请<span className={'indicator'}>{props.info.invit_num}</span>位好友助力即可获得<span
                className={'indicator'}>【砍价神器】</span></div>
xuzhenghua committed
287 288 289 290 291 292 293
    } 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
294 295 296 297 298 299 300 301 302


    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
303
        setDay(differenceInDays(new Date(date), now) % 24)
xuzhenghua committed
304 305 306

    }, 1000)

xuzhenghua committed
307

zhanghaozhe committed
308 309
    return (
        <div className="status-outer">
xuzhenghua committed
310 311 312 313 314 315 316 317 318

            {
                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
319 320 321
                                <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
322
                                <span className='over'>后砍价结束</span>
xuzhenghua committed
323 324
                            </div>
                            <div onClick={props.getMore}>{props.info.assist_num}位好友助力></div>
zhanghaozhe committed
325
                        </div>
xuzhenghua committed
326 327 328 329 330 331 332

                        <div className="sec-row">
                            <div>
                                <img
                                    src={props.outList.avatar_file}
                                    alt=""/>
                                <span className='name'>{props.outList.user_name}</span>
xuzhenghua committed
333
                            </div>
xuzhenghua committed
334 335 336 337 338
                            <div>
                                <div>砍掉<span className={'indicator'}>{props.outList.amount}</span></div>
                                <button onClick={props.continueBargain}>继续砍价</button>
                            </div>
                        </div>
xuzhenghua committed
339 340 341
                        <div className="third-row">
                            {thirdRow}
                            {btn}
zhanghaozhe committed
342
                        </div>
xuzhenghua committed
343 344 345
                    </Flex>
                </div>
            }
zhanghaozhe committed
346

xuzhenghua committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
            {
                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
362
                            砍价金额将于
xuzhenghua committed
363 364 365 366 367 368
                            <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
369
                            后清零,请尽快完成支付
xuzhenghua committed
370 371 372 373
                        </div>
                    </Flex>
                </div>
            }
zhanghaozhe committed
374 375 376 377
        </div>
    )
}

zhanghaozhe committed
378

xuzhenghua committed
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
{/*领取砍价神器*/
}

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
399
            <img className='top-img' src={require('./image/kanjia_cg_icon.png')} alt=""/>
xuzhenghua committed
400 401 402 403 404 405 406 407 408 409 410 411 412 413
            <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
414

xuzhenghua committed
415
export default compose(
xuzhenghua committed
416 417 418 419 420 421
    connect(
        state => ({
            user: state.user
        }),
        null
    ),
xuzhenghua committed
422 423
    withRouter
)(Bargain)