index.js 14.7 KB
Newer Older
zhanghaozhe committed
1
/* eslint-disable eqeqeq */
zhanghaozhe committed
2
import React, { Component } from "react"
zhanghaozhe committed
3
import { http } from "src/utils"
zhanghaozhe committed
4 5 6 7 8 9
import { Toast, Tabs } from "antd-mobile"
import "./index.scss"
import "swiper/dist/css/swiper.min.css"
import { Link, withRouter } from "react-router-dom"
import classnames from "classnames"
import Swiper from "swiper"
zhanghaozhe committed
10
import { Popup } from "src/common/index"
zhanghaozhe committed
11 12
import QRCode from "qrcode"
import { connect } from "react-redux"
xuzhenghua committed
13

zhanghaozhe committed
14
@connect((state) => ({ user: state.user }))
xuzhenghua committed
15 16 17 18 19 20 21 22 23
class FormalDraw extends Component {
  swiper = null
  popupInstance = null
  subscribePopupInstance = null

  state = {
    prizes: [],
    currentDisplayPrizes: [],
    tabs: [],
zhanghaozhe committed
24
    today: "",
xuzhenghua committed
25 26 27 28 29 30 31
    initialPageIndex: 0,
    list: [],
    activeTimeRangeIndex: 0,
    userValue: {},
  }

  componentDidMount() {
xuzhenghua committed
32 33 34 35
    this.getPrizeData()
  }

  shouldComponentUpdate(nextProps, nextState, nextContext) {
xuzhenghua committed
36
    if (this.props.isApp !== nextProps.isApp) {
xuzhenghua committed
37
      this.getPrizeData()
xuzhenghua committed
38
      return false
xuzhenghua committed
39
    }
xuzhenghua committed
40
    return true
xuzhenghua committed
41 42 43
  }

  getPrizeData = () => {
zhanghaozhe committed
44 45 46 47 48 49 50 51 52 53 54
    http.get(`${API.home}/sys/activity/prize_data`).then((res) => {
      const { data, code, msg } = res.data
      if (code == 200) {
        let oneDay =
          data.list.find((item) => item.date === data.value.today) ||
          data.list[0]
        const today = oneDay.date
        const activeIndex = oneDay["son"].findIndex((item) => item.status == 3)
        this.setState(
          {
            tabs: data.list.map((item) => ({ title: item.date })),
xuzhenghua committed
55
            today,
zhanghaozhe committed
56 57
            initialPageIndex:
              data.list.findIndex((item) => item.date == today) || 0,
xuzhenghua committed
58 59
            list: data.list,
            userValue: data.value,
zhanghaozhe committed
60 61 62
            activeTimeRangeIndex: activeIndex < 0 ? 0 : activeIndex,
          },
          () => {
xuzhenghua committed
63
            this.initializeSwiper()
zhanghaozhe committed
64 65 66 67 68 69
          }
        )
      } else {
        Toast.info(msg, 2, null, false)
      }
    })
xuzhenghua committed
70 71 72 73
  }

  initializeSwiper = () => {
    new Swiper(this.swiper, {
zhanghaozhe committed
74
      slidesPerView: "auto",
xuzhenghua committed
75
      scrollbar: {
zhanghaozhe committed
76 77 78
        el: ".swiper-scrollbar",
        draggable: true,
      },
xuzhenghua committed
79 80 81 82
    })
  }

  changeTab = (tab) => {
zhanghaozhe committed
83 84 85 86 87 88 89 90 91 92 93 94
    const { list } = this.state
    const data = list.filter((item) => item.date === tab.title)
    let index = data[0]["son"].findIndex((item) => item.status == 3)
    this.setState(
      {
        today: tab.title,
        activeTimeRangeIndex: index < 0 ? 0 : index,
      },
      () => {
        this.initializeSwiper()
      }
    )
xuzhenghua committed
95 96
  }

zhanghaozhe committed
97 98
  draw = (id) => {
    let { surplus, is_prize, hot_value } = this.state.userValue
xuzhenghua committed
99
    if (hot_value < 50) {
zhanghaozhe committed
100 101 102 103 104 105
      Toast.info(
        "你的福气值未达到参与抽奖所需福气值分数,快去积攒福气值吧",
        2,
        null,
        false
      )
xuzhenghua committed
106
    } else if (is_prize == 1) {
zhanghaozhe committed
107 108 109 110 111 112 113 114
      Toast.info(
        "你已参与当前时段抽奖",
        2,
        () => {
          this.requestDraw(id, true)
        },
        false
      )
xuzhenghua committed
115
    } else if (surplus < 1) {
zhanghaozhe committed
116 117 118 119 120 121
      Toast.info(
        "你的抽奖次数已用光,快去积攒福气值可获得更多抽奖机会",
        2,
        null,
        false
      )
xuzhenghua committed
122 123 124 125 126 127
    } else {
      this.requestDraw(id)
    }
  }

  requestDraw = (id, isDrawn) => {
zhanghaozhe committed
128 129 130 131 132 133 134 135 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 165 166 167 168 169 170
    let { surplus } = this.state.userValue
    http
      .post(`${API.home}/sys/activity/prize`, {
        id,
      })
      .then((res) => {
        const { code, msg, data } = res.data
        if (code == 200) {
          var _czc = _czc || []
          _czc.push(["_trackEvent", "点击抽奖", "m端双十一正式活动-点击抽奖"])
          QRCode.toDataURL(data.url, (err, url) => {
            this.popupInstance = Popup({
              title: "你已成功参与本时段抽奖",
              className: "join-lottery",
              content: (
                <>
                  <div className="text">
                    <div className="code">抽奖码为:{data.code}</div>
                    <div className="time">
                      本时段的中奖结果将在{data.date}公布
                    </div>
                    <div className="hint">
                      你可关注‘七月在线’服务号第一时间获得中奖信息。
                    </div>
                    <img src={url} className="qr-code" alt="" />
                  </div>
                  <button
                    onClick={() => {
                      this.popupInstance.close()
                    }}
                  >
                    知道了
                  </button>
                </>
              ),
            })
            this.setState({
              userValue: {
                ...this.state.userValue,
                ...{ surplus: isDrawn ? surplus : --surplus },
                ...{ is_prize: 1 },
              },
            })
xuzhenghua committed
171
          })
zhanghaozhe committed
172 173 174 175
        } else {
          Toast.info(msg, 2, null, false)
        }
      })
xuzhenghua committed
176 177 178
  }

  lotteryFunc = (status, id) => {
zhanghaozhe committed
179
    const { hasError } = this.props.user
xuzhenghua committed
180 181 182 183 184 185 186 187 188
    if (hasError && status != 4) {
      this.props.toLogin()
      return
    }
    if (status == 3) {
      this.draw(id)
    } else if (status == 2) {
      this.subscribe(id)
    } else if (status == 4) {
zhanghaozhe committed
189
      window.location.href = `/prize-winner-list?tid=${id}`
xuzhenghua committed
190 191 192 193
    }
  }

  subscribe = (id) => {
zhanghaozhe committed
194
    var _czc = window._czc || []
zhanghaozhe committed
195
    _czc.push(["_trackEvent", "预约抽奖", "m端双十一正式页-立即预约"])
xuzhenghua committed
196 197 198
    if (this.subscribePopupInstance) {
      return
    }
zhanghaozhe committed
199 200 201 202 203 204 205
    if (!this.state.userValue["is_pre"]) {
      http
        .get(`${API["base-api"]}/sys/activity/create_pre_qrcode`, {
          params: { id },
        })
        .then((res) => {
          const { code, data, msg } = res.data
xuzhenghua committed
206
          if (code == 200) {
zhanghaozhe committed
207 208 209 210 211 212 213 214 215 216 217 218
            this.subscribePopupInstance = QRCode.toDataURL(
              data.url,
              (err, url) => {
                Popup({
                  title: "微信扫码“七月在线”服务号即可预约",
                  className: "subscribe",
                  content: <img src={url} alt="" className="qr-code" />,
                  close: () =>
                    new Promise((resolve) => {
                      this.subscribePopupInstance = null
                      resolve()
                    }),
xuzhenghua committed
219
                })
zhanghaozhe committed
220 221
              }
            )
xuzhenghua committed
222 223 224 225 226
          } else {
            Toast.info(msg, 2, null, false)
          }
        })
    } else {
zhanghaozhe committed
227 228 229 230
      http
        .get(`${API["base-api"]}/activity_pre/${id}`)
        .then((res) => {
          const { code } = res.data
xuzhenghua committed
231
          if (code == 200) {
zhanghaozhe committed
232
            Toast.info("预约成功", 2, null, false)
xuzhenghua committed
233 234
          }
        })
zhanghaozhe committed
235
        .catch((err) => {
xuzhenghua committed
236 237 238
          console.log(err)
        })
    }
xuzhenghua committed
239 240 241
  }

  render() {
zhanghaozhe committed
242 243 244 245 246 247 248 249 250
    const {
      tabs,
      today,
      initialPageIndex,
      list,
      activeTimeRangeIndex,
      userValue,
    } = this.state
    const { hasError } = this.props.user
xuzhenghua committed
251 252
    const isLogin = !hasError
    const schedule = userValue.schedule <= 2 ? 2 : userValue.schedule
zhanghaozhe committed
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
    return list.length > 0 ? (
      <div id={"formal-draw"}>
        {list.length && (
          <Tabs
            tabs={tabs}
            initialPage={initialPageIndex}
            tabBarBackgroundColor={"transparent"}
            tabBarActiveTextColor={"#5600DF"}
            tabBarInactiveTextColor={"#FFF604"}
            tabBarUnderlineStyle={{ display: "none" }}
            onTabClick={this.changeTab}
            swipeable={false}
          >
            {list.map((listItem, index) => {
              return (
                <div
                  className={`content ${
                    isLogin && today === userValue.today ? "current" : ""
                  }`}
                  key={index}
                >
                  {isLogin && today === userValue.today ? (
                    <div className="prize_number_con">
                      当日剩余抽奖次数:
                      <span className={"prize__number"}>
                        {userValue.surplus}
                      </span>
                    </div>
                  ) : (
                    ""
                  )}
                  <div className="title">
                    <img
                      src="https://julyedu-cdn.oss-cn-beijing.aliyuncs.com/active19_1111/title-decorate-left.png"
                      alt=""
                    />
                    {isLogin ? (
                      <div className="hot-value">
                        中奖概率值:
                        <span className={"grade"}>
                          {userValue["hot_value"]}
                        </span>
xuzhenghua committed
295
                      </div>
zhanghaozhe committed
296 297 298 299 300 301 302 303 304 305
                    ) : (
                      <div className="hot-value">
                        中奖概率值:<Link to={"/passport"}>登录</Link>
                      </div>
                    )}
                    <img
                      src="https://julyedu-cdn.oss-cn-beijing.aliyuncs.com/active19_1111/title-decorate-right.png"
                      alt=""
                    />
                  </div>
xuzhenghua committed
306

zhanghaozhe committed
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
                  {isLogin ? (
                    <div className="progress-bar">
                      <img
                        className={"locator"}
                        src="https://julyedu-cdn.oss-cn-beijing.aliyuncs.com/active19_1111/locator.png"
                        alt=""
                        style={{
                          left: `${schedule - (11 / 330 - 6 / 330) * 100}%`,
                        }}
                      />
                      <div
                        className="progress"
                        style={{
                          width: `${schedule}%`,
                        }}
                      ></div>
                    </div>
                  ) : null}
                  <ul className="time-ranges">
                    {listItem.son &&
                      listItem.son.map((item, i) => {
                        let statusContent
                        switch (item.status) {
                          case 5:
                            statusContent = "已预约"
                            break
                          case 4:
                            statusContent = (
                              <>
                                <span>已结束</span>
                                <Link to={`/prize-winner-list?tid=${item.id}`}>
                                  查看中奖名单
                                </Link>
                              </>
                            )
                            break
                          case 3:
                            statusContent = (
                              <>
                                <div>立即抽奖</div>
                                <div>{item.num}人正在参与</div>
                              </>
xuzhenghua committed
349
                            )
zhanghaozhe committed
350 351 352 353 354 355
                            break
                          case 2:
                            statusContent = "立即预约"
                            break
                          default:
                            statusContent = "即将开启"
xuzhenghua committed
356
                        }
zhanghaozhe committed
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 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
                        return (
                          <li
                            key={i}
                            className={classnames(`status-${item.status}`, {
                              active: activeTimeRangeIndex === i,
                            })}
                          >
                            <div
                              className={"status-bar"}
                              onClick={() => {
                                this.setState(
                                  { activeTimeRangeIndex: i },
                                  () => {
                                    this.initializeSwiper()
                                  }
                                )
                              }}
                            >
                              <img
                                className={"icon-clock"}
                                src={
                                  item.status == 3
                                    ? "https://julyedu-cdn.oss-cn-beijing.aliyuncs.com/active19_1111/m/m-icon-clock-purple.png"
                                    : "https://julyedu-cdn.oss-cn-beijing.aliyuncs.com/active19_1111/m/m-icon-clock.png"
                                }
                                alt=""
                              />
                              <div className="time">
                                <span>{item.time}</span>
                              </div>
                              <div
                                className="status"
                                onClick={this.lotteryFunc.bind(
                                  this,
                                  item.status,
                                  item.id
                                )}
                              >
                                {statusContent}
                              </div>
                            </div>
                            {activeTimeRangeIndex === i &&
                              listItem.date === today && (
                                <div
                                  className="swiper-container"
                                  ref={(el) => (this.swiper = el)}
                                >
                                  <ul className={"prizes swiper-wrapper"}>
                                    {item.data.map((prize, index) => {
                                      return (
                                        <li
                                          key={index}
                                          className={"swiper-slide"}
                                        >
                                          {prize.level === 1 &&
                                            item.status === 3 && (
                                              <div className="tip">
                                                10000人释放
                                              </div>
                                            )}
                                          <img src={prize.img} alt="" />
                                          <div className="prize-name">
                                            <div>{prize.name}</div>
                                            <div>*{prize.num}</div>
                                          </div>
                                        </li>
                                      )
                                    })}
                                    <li className="swiper-scrollbar"></li>
                                  </ul>
                                </div>
                              )}
                          </li>
                        )
                      })}
                  </ul>
                </div>
              )
            })}
          </Tabs>
        )}
      </div>
    ) : null
xuzhenghua committed
440 441 442 443
  }
}

export default withRouter(FormalDraw)