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

class Bargain extends Component {
zhanghaozhe committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
  constructor(props) {
    super(props)
    this.state = {
      kanjiaIcon: require("./image/kanjia_icon.png"),
      info: "",
      outList: [],
      list: [],
      status: "",
      time: "",
      barInfo: {},
      country: "86",
      price: 0, // 每次砍价砍掉的价格
      sum: 0, // 累计砍掉的价格,
      bindInfo: {}, // 冲突信息
      formInfo: {},
xuzhenghua committed
35
    }
zhanghaozhe committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
  }

  componentDidMount() {
    this.getBargainInfo()
    this.judgePopupTypeFromCountry()
  }

  // 选择区号后进入
  judgePopupTypeFromCountry = () => {
    const { country, delCountryNum } = this.props
    if (country.from && country.from === "bargain") {
      this.setState({
        country: country.num,
        status: 3,
      })

      // 清除
      delCountryNum()
xuzhenghua committed
54
    }
zhanghaozhe committed
55
  }
xuzhenghua committed
56

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

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

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

  // 自组件传给父组件的boxHide
  boxHide = (val) => {
    this.setState({ isShowMore: val })
  }

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

  // 使用砍价神器
  useArtifact = () => {
    this.toKanjia(getParam("id"), 2, 0)
  }

  // 加入购物车
  toCart = () => {
    let data = {
      course_id: getParam("id"),
xuzhenghua committed
135
    }
zhanghaozhe committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    http.post(`${API.home}/m/cart/add`, data).then((res) => {
      if (res.data.code === 200 || res.data.code === 15001) {
        this.props.history.push("/shopcart")
      } else {
        Toast.info(res.data.msg, 2)
      }
    })
  }

  // 继续砍价
  continueBargain = () => {
    const { history } = this.props
    const { barInfo = {} } = this.state
    const code = barInfo.bargain_code
    history.push(
      `/bargain-middle-page?id=${getParam(
        "id"
      )}&bargaincode=${code}&is_originator=1`
    )
  }

  // 我要砍价
  iWantBargain = () => {
    const { history, user } = this.props
    const uid = user && user.data && user.data.uid
    if (!uid) {
      history.push("/passport/login")
    } else {
      this.toKanjia(getParam("id"), 1, 0)
xuzhenghua committed
165
    }
zhanghaozhe committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
  }

  // 砍价接口
  toKanjia = (id, type, uid) => {
    let data = {
      course_id: id,
      type: type, // 1 用户自己砍价 2 使用砍价神器 3 好友助力砍价 4 好友第二次助力
      parent_uid: uid, // 被助力人id 【自己本人操作传0】
    }
    const { history } = this.props
    http.post(`${API.home}/m/bargain/toBargain`, data).then((res) => {
      const {
        data: { data, code },
      } = res
      if (code === 200) {
        // user_status 用户状态 1-关注公众号,2-绑定手机号 3-再砍一刀 (是发起人没有这个字段)
        if (data.user_status === 2) {
          this.setState({
            status: 3,
          })
xuzhenghua committed
186
        } else {
zhanghaozhe committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
          if (type === 2) {
            this.setState({
              status: 2,
              price: data.amount,
              sum: data.bargain_price,
            })
            this.getBargainInfo(false)
            this.getBargainRankList({
              type: 0,
              bargain_code: data.bargain_code,
            })
          } else {
            history.push(
              `/bargain-middle-page?id=${getParam("id")}&bargaincode=${
                data.bargain_code
              }&is_originator=1`
            )
          }
xuzhenghua committed
205
        }
zhanghaozhe committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
      } else {
        Toast.info(res.data.msg, 2)
      }
    })
  }

  // 关闭弹窗
  handleToHide = () => {
    this.setState({
      status: "",
    })
  }

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

  render() {
    const {
      list,
      outList,
      barInfo,
      country,
      price,
      sum,
      status,
      formInfo,
      bindInfo,
    } = this.state
    const { user } = this.props
    const uid = user && user.data && user.data.uid
    return (
      <div className={"bargain-func"}>
        {/*bargain_status 砍价状态 0-砍价中,1砍价结束,待支付,2砍价过期(没有砍价记录没有砍价信息),3已购买*/}
        {(barInfo.bargain_status === 2 ||
          (getParam("id") === "139" && barInfo.bargain_status === 3) ||
          !uid) && (
          <BargainIntro
            limitPeople={barInfo.limit_people}
            iWantBargain={this.iWantBargain}
          />
        )}
        {(barInfo.bargain_status === 0 || barInfo.bargain_status === 1) &&
          uid && (
            <BargainStatus
              info={barInfo}
              outList={outList}
              getMore={this.getMore}
              toArtifact={this.toArtifact}
              useArtifact={this.useArtifact}
              continueBargain={this.continueBargain}
            />
          )}

        {/* 绑定手机号 */}
        <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>

        {/* 更多好友砍价 */}
        <Ranking
          list={list}
          icon={this.state.kanjiaIcon}
          limitPeople={barInfo.limit_people}
          isShowMore={this.state.isShowMore}
          boxHide={this.boxHide}
        />
      </div>
    )
  }
zhanghaozhe committed
304 305
}

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

xuzhenghua committed
332
function BargainStatus(props) {
zhanghaozhe committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
  const [day, setDay] = useState(0)
  const [hour, setHour] = useState(0)
  const [min, setMin] = useState(0)
  const [sec, setSec] = useState(0)

  let thirdRow, btn
  // is_artifact	 0-再邀请多少人可以使用 1-可以使用未领取 2-已领取 3-已使用
  if (props.info.is_artifact === 0) {
    thirdRow = (
      <div>
        再邀请<span className={"indicator"}>{props.info.invit_num}</span>
        位好友助力即可获得<span className={"indicator"}>【砍价神器】</span>
      </div>
    )
  } 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>
  }

  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)
    setDay(differenceInDays(new Date(date), now) % 24)
  }, 1000)

  return (
    <div className="status-outer">
      {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>
                <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>
                <span className="over">后砍价结束</span>
              </div>
              <div onClick={props.getMore}>
                {props.info.assist_num}位好友助力&gt;
              </div>
            </div>
xuzhenghua committed
399

zhanghaozhe committed
400 401 402 403 404 405 406 407 408
            <div className="sec-row">
              <div>
                <img src={props.outList.avatar_file} alt="" />
                <span className="name">{props.outList.user_name}</span>
              </div>
              <div>
                <div>
                  砍掉
                  <span className={"indicator"}>{props.outList.amount}</span>
xuzhenghua committed
409
                </div>
zhanghaozhe committed
410 411 412 413 414 415 416 417
                <button onClick={props.continueBargain}>继续砍价</button>
              </div>
            </div>
            <div className="third-row">
              {thirdRow}
              {btn}
            </div>
          </Flex>
zhanghaozhe committed
418
        </div>
zhanghaozhe committed
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
      )}

      {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}位好友助力&gt;
              </div>
            </div>
            <div className="time-tobuy">
              砍价金额将于
              <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>
              后清零,请尽快完成支付
            </div>
          </Flex>
        </div>
      )}
    </div>
  )
zhanghaozhe committed
451 452
}

FE committed
453
// 领取砍价神器
xuzhenghua committed
454
function Artifact(props) {
zhanghaozhe committed
455 456 457 458 459 460 461 462 463 464 465 466 467
  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>
  )
xuzhenghua committed
468 469
}

FE committed
470
// 使用砍价神器
xuzhenghua committed
471
function UseArtifact(props) {
zhanghaozhe committed
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
  const { sum, price } = props
  return (
    <div className="use-artifact-box">
      <img
        className="top-img"
        src={require("./image/kanjia_cg_icon.png")}
        alt=""
      />
      <p className="top-tip">厉害了,又砍掉了{price}元!</p>
      <p className="middle-tip">
        你已经砍了
        <span className={"indicator"}>{sum}</span>
        没见过你这么能砍的人...
      </p>
      <p className="btm-tip">不能再砍了哦~</p>
      <button className="tubuy" onClick={props.toCart}>
        去支付
      </button>
    </div>
  )
xuzhenghua committed
492 493
}

xuzhenghua committed
494
export default compose(
zhanghaozhe committed
495 496 497 498 499 500 501
  connect(
    (state) => ({
      user: state.user,
    }),
    null
  ),
  withRouter
xuzhenghua committed
502
)(Bargain)