index.js 13.5 KB
Newer Older
xuzhenghua committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
import React, { Component } from 'react'
import { http } from "@/utils"
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'
import { Popup } from "@common/index"
import QRCode from 'qrcode'
import { connect } from 'react-redux'


@connect(state => ({user: state.user}))
class FormalDraw extends Component {

  swiper = null
  popupInstance = null
  subscribePopupInstance = null

  state = {
    prizes: [],
    currentDisplayPrizes: [],
    tabs: [],
    today: '',
    initialPageIndex: 0,
    list: [],
    activeTimeRangeIndex: 0,
    userValue: {},
  }

  componentDidMount() {
xuzhenghua committed
33 34 35 36 37 38 39 40 41 42 43 44
    this.getPrizeData()
  }

  shouldComponentUpdate(nextProps, nextState, nextContext) {
    if (this.props.isApp !== nextProps.isApp) {
      this.getPrizeData()
      return false
    }
    return true
  }

  getPrizeData = () => {
xuzhenghua committed
45 46 47 48
    http.get(`${API.home}/sys/activity/prize_data`)
      .then(res => {
        const {data, code, msg} = res.data
        if (code == 200) {
xuzhenghua committed
49 50 51
          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)
xuzhenghua committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
          this.setState({
            tabs: data.list.map(item => ({title: item.date})),
            today,
            initialPageIndex: data.list.findIndex(item => item.date == today) || 0,
            list: data.list,
            userValue: data.value,
            activeTimeRangeIndex: activeIndex < 0 ? 0 : activeIndex
          }, () => {
            this.initializeSwiper()
          })
        } else {
          Toast.info(msg, 2, null, false)
        }
      })
  }

  initializeSwiper = () => {
    new Swiper(this.swiper, {
      slidesPerView: 'auto',
      scrollbar: {
        el: '.swiper-scrollbar',
        draggable: true
      }
    })
  }

  changeTab = (tab) => {
    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()
    })
  }

  draw = id => {
    let {surplus, is_prize, hot_value} = this.state.userValue
xuzhenghua committed
92
    if (hot_value < 50) {
xuzhenghua committed
93
      Toast.info('你的福气值未达到参与抽奖所需福气值分数,快去积攒福气值吧', 2, null, false)
xuzhenghua committed
94 95 96 97 98
    } else if (is_prize == 1) {
      Toast.info('你已参与当前时段抽奖', 2, () => {
        this.requestDraw(id, true)
      }, false)
    } else if (surplus < 1) {
xuzhenghua committed
99
      Toast.info('你的抽奖次数已用光,快去积攒福气值可获得更多抽奖机会', 2, null, false)
xuzhenghua committed
100
    } else {
xuzhenghua committed
101
      this.requestDraw(id)
xuzhenghua committed
102
    }
xuzhenghua committed
103 104
  }

xuzhenghua committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  requestDraw = (id, isDrawn) => {
    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}}
          })
        })
      } else {
        Toast.info(msg, 2, null, false)
      }
    })
  }

xuzhenghua committed
143 144
  lotteryFunc = (status, id) => {
    const {hasError} = this.props.user
xuzhenghua committed
145
    if (hasError && status != 4) {
xuzhenghua committed
146 147 148 149 150 151
      this.props.toLogin()
      return
    }
    if (status == 3) {
      this.draw(id)
    } else if (status == 2) {
xuzhenghua committed
152 153 154
      this.subscribe(id)
    } else if (status == 4) {
      location.href = `/prize-winner-list?tid=${id}`
xuzhenghua committed
155 156 157
    }
  }

xuzhenghua committed
158
  subscribe = (id) => {
xuzhenghua committed
159 160 161 162 163
    var _czc = _czc || []
    _czc.push(["_trackEvent", '预约抽奖', 'm端双十一正式页-立即预约'])
    if (this.subscribePopupInstance) {
      return
    }
xuzhenghua committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177
    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
          if (code == 200) {
            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
178 179
              })
            })
xuzhenghua committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
          } else {
            Toast.info(msg, 2, null, false)
          }
        })
    } else {
      http.get(`${API["base-api"]}/activity_pre/${id}`)
        .then(res => {
          const {code} = res.data
          if (code == 200) {
            Toast.info('预约成功', 2, null, false)
          }
        })
        .catch(err => {
          console.log(err)
        })
    }


xuzhenghua committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
  }

  render() {
    const {tabs, today, initialPageIndex, list, activeTimeRangeIndex, userValue} = this.state
    const {hasError} = this.props.user
    const isLogin = !hasError
    const schedule = userValue.schedule <= 2 ? 2 : userValue.schedule
    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 (
xuzhenghua committed
223 224 225 226 227 228 229 230 231 232
                    <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>
                          : ""
                      }
xuzhenghua committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246
                      <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>
                            </div>
                            : <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
247

xuzhenghua committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
                      {
                        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) {
xuzhenghua committed
269 270 271
                              case 5:
                                statusContent = '已预约'
                                break
xuzhenghua committed
272 273 274 275
                              case 4:
                                statusContent = (
                                  <>
                                    <span>已结束</span>
xuzhenghua committed
276
                                    <Link to={`/prize-winner-list?tid=${item.id}`}>查看中奖名单</Link>
xuzhenghua committed
277 278 279 280 281 282
                                  </>
                                )
                                break
                              case 3:
                                statusContent = (
                                  <>
xuzhenghua committed
283
                                    <div>立即抽奖</div>
xuzhenghua committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
                                    <div>{item.num}人正在参与</div>
                                  </>
                                )
                                break
                              case 2:
                                statusContent = '立即预约'
                                break
                              default:
                                statusContent = '即将开启'
                            }
                            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 &&
xuzhenghua committed
326
                                                item.status === 3 &&
xuzhenghua committed
327
                                                <div className="tip">
xuzhenghua committed
328
                                                  10000人释放
xuzhenghua committed
329 330 331 332 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
                                                </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
    )
  }
}

export default withRouter(FormalDraw)