index.js 12.8 KB
Newer Older
zhanghaozhe committed
1 2 3 4 5 6
import React, { Component } from 'react';
import './index.scss'
import { Tabs, Toast } from "antd-mobile";
import { http } from "@/utils"
import storage from 'store2'
import { html } from '@/utils'
zhanghaozhe committed
7
import { compareDesc } from "date-fns";
zhanghaozhe committed
8
import { Link } from "react-router-dom";
zhanghaozhe committed
9
import { connect } from "react-redux";
zhanghaozhe committed
10 11 12 13 14 15


class Scores extends Component {

  store = storage.namespace('aiTestEntry')

zhanghaozhe committed
16

zhanghaozhe committed
17
  state = {
zhanghaozhe committed
18
    selfTabs: [
zhanghaozhe committed
19 20 21 22 23
      {title: '当前成绩'},
      {title: '今日最佳'},
      {title: '本月最佳'},
    ],
    rankList: [],
zhanghaozhe committed
24 25
    rankListTabs: [
      {title: '日榜'},
zhanghaozhe committed
26 27 28 29 30
      {title: '总榜'},
    ],
/*
    rankListTabs: [
      {title: '日榜'},
zhanghaozhe committed
31 32 33
      {title: '周榜'},
      {title: '总榜'},
    ],
zhanghaozhe committed
34
*/
zhanghaozhe committed
35 36 37 38 39 40 41
    isExpandRankList: false,
    icons: [
      require('./rank-1.png'),
      require('./rank-2.png'),
      require('./rank-3.png'),
    ],
    isShowRule: false,
zhanghaozhe committed
42
    isNeverShow: this.store.get('isNeverShow'),
zhanghaozhe committed
43 44 45 46 47 48 49 50 51
    pageState: {},
    availableTestNum: 0,
    userScore: {},
    userAddress: {
      name: '',
      phone: '',
      address: '',
    },
    isShowUserAddress: false,
zhanghaozhe committed
52
    entryButtonInRule: false,
zhanghaozhe committed
53 54 55 56
  }

  componentDidMount() {
    this.getInitialData()
zhanghaozhe committed
57
    this.getRankList(null, 0)
zhanghaozhe committed
58 59 60 61
    if (!this.props.user.hasError) {
      this.getUserScores(0)
      this.getUserAddress()
    }
zhanghaozhe committed
62 63
  }

zhanghaozhe committed
64 65 66 67 68 69 70 71
  componentDidUpdate(prevProps, prevState) {
    if (this.props.user.hasError !== prevProps.user.hasError) {
      this.getUserScores(0)
      this.getUserAddress()
    }
  }


zhanghaozhe committed
72 73 74 75 76
  handleChange = (e) => {
    const isNeverSHow = e.target.checked
    this.setState({
      isNeverSHow,
    })
zhanghaozhe committed
77
    this.store.set('isNeverShow', isNeverSHow)
zhanghaozhe committed
78 79
  }

zhanghaozhe committed
80 81
  startTest = () => {
    this.props.history.push('/ai-test/exam')
zhanghaozhe committed
82 83 84 85 86 87 88 89 90 91
  }

  getInitialData = () => {
    http.get(`${API.home}/sys/activity_data`)
      .then(res => {
        const {code, msg, data} = res.data
        if (code === 200) {
          this.setState({
            pageState: data,
          })
zhanghaozhe committed
92 93
        } else if (code === 3009) {
          this.props.history.push('/')
zhanghaozhe committed
94 95 96 97 98 99
        } else {
          Toast.fail(msg, 2, null, false)
        }
      })
  }

zhanghaozhe committed
100
  getRankList = (tab, type) => {
zhanghaozhe committed
101
    http.get(`${API.home}/sys/at/ranks/${type === 0 ? 0 : 2}`)
zhanghaozhe committed
102 103 104 105 106 107 108 109 110 111 112 113 114
      .then(res => {
        const {code, msg, data} = res.data
        if (code === 200) {
          this.setState({
            rankList: data,
          })
        } else {
          Toast.fail(msg, 2, null, false)
        }
      })
  }

  getUserScores = (type) => {
zhanghaozhe committed
115
    http.get(`${API.home}/sys/at/user_score/${type}/0`)
zhanghaozhe committed
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 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 171 172 173 174 175 176 177
      .then(res => {
        const {code, msg, data} = res.data

        if (code === 200) {
          this.setState({
            userScore: data,
          })
        } else {
          Toast.fail(msg, 2, null, false)
        }
      })
  }

  getUserAddress = () => {
    http.get(`${API.home}/sys/user_address_info`)
      .then(res => {
        const {code, msg, data} = res.data
        if (code === 200) {
          this.setState({
            userAddress: data,
          })
        }
      })
  }

  inputText = e => {
    const key = e.target.name
    const value = e.target.value
    this.setState(state => {
      return {
        userAddress: {
          ...state.userAddress, ...{
            [key]: value,
          },
        },
      }
    })
  }

  submitForm = (e) => {
    e.preventDefault()
    const {userAddress} = this.state
    if (!Object.values(userAddress).every(item => !!item)) {
      Toast.info('请填写完整')
      return
    }
    http.post(`${API.home}/sys/update_address`, userAddress)
      .then(res => {
        const {code, msg, data} = res.data
        if (code === 200) {
          Toast.success('提交成功', 2, null, false)
          this.setState({
            isShowUserAddress: false,
          })
        } else {
          Toast.fail(msg, 2, null, false)
        }
      })
  }

  render() {
    const {
zhanghaozhe committed
178
      selfTabs,
zhanghaozhe committed
179
      rankList,
zhanghaozhe committed
180
      rankListTabs,
zhanghaozhe committed
181 182 183 184 185 186 187 188
      icons,
      isExpandRankList,
      isShowRule,
      isNeverShow,
      pageState,
      userScore,
      isShowUserAddress,
      userAddress,
zhanghaozhe committed
189
      entryButtonInRule,
zhanghaozhe committed
190
    } = this.state
zhanghaozhe committed
191
    const {user, history} = this.props
zhanghaozhe committed
192
    const _rankList = Array.isArray(rankList) ? isExpandRankList ? rankList : rankList.slice(0, 10) : []
zhanghaozhe committed
193 194 195
    return (
      <div className={'scores'}>
        <div className="banner">
zhanghaozhe committed
196
          <img src={pageState.h5_banner} alt=""/>
zhanghaozhe committed
197 198 199 200 201 202 203 204 205 206 207
        </div>
        <div className="info">
          <span>已有{pageState.join_num}人参加测试</span>
          <a href="javascript:void(0);" onClick={() => {
            this.setState({
              isShowRule: true,
            })
          }}>规则</a>
        </div>
        {
          <div className="score-list">
zhanghaozhe committed
208
            <Tabs tabs={selfTabs} tabBarUnderlineStyle={{display: 'none'}} onChange={(tab, i) => {
zhanghaozhe committed
209 210 211
              this.getUserScores(i)
            }}>
              {
zhanghaozhe committed
212
                selfTabs.map((tab, index) => {
zhanghaozhe committed
213 214 215 216 217 218
                  return <div className={'tab-content'} key={index}>
                    <table>
                      <thead>
                      <tr>
                        <th>分数</th>
                        <th>用时</th>
zhanghaozhe committed
219
                        <th>{index === 0 && '最终'}排名</th>
zhanghaozhe committed
220 221 222
                      </tr>
                      </thead>
                      <tbody>
zhanghaozhe committed
223 224 225 226 227 228 229 230 231
                      {
                        user.hasError
                          ? <tr>
                            <td>--</td>
                            <td>--</td>
                            <td>--</td>
                          </tr>
                          : <tr>
                            {
zhanghaozhe committed
232
                              userScore.score === '-' ? <td>'-'</td> :
zhanghaozhe committed
233 234 235 236
                                <td>{userScore.score} <Link to={`/ai-test/analysis/${userScore.r_id}`}>解析</Link></td>
                            }
                            <td>{userScore.cost_time}</td>
                            {
zhanghaozhe committed
237
                              userScore.rank === '-' ? <td>'-'</td> : <td>{userScore.rank}名</td>
zhanghaozhe committed
238 239 240
                            }
                          </tr>
                      }
zhanghaozhe committed
241 242 243 244 245 246 247
                      </tbody>
                    </table>
                  </div>
                })
              }
            </Tabs>
            <div className="share">
zhanghaozhe committed
248
              <Link to={'/ai-test/report'}>分享</Link>
zhanghaozhe committed
249 250 251 252 253 254 255 256 257 258 259
            </div>
          </div>
        }
        <div className="rank-list">
          <div className="head">
            <div>
              测试排行榜
            </div>
            <div>
              <span>仅显示前50</span>
              <a href="javascript:void(0);" onClick={() => {
zhanghaozhe committed
260
                if (user.hasError) {
zhanghaozhe committed
261
                  history.push('/passport')
zhanghaozhe committed
262
                } else {
zhanghaozhe committed
263 264 265 266
                  this.setState({
                    isShowUserAddress: true,
                  })
                }
zhanghaozhe committed
267 268 269 270
              }}>收货地址</a>
            </div>
          </div>
          <div className="list">
zhanghaozhe committed
271
            <Tabs tabs={rankListTabs} tabBarUnderlineStyle={{display: 'none'}} onChange={this.getRankList}>
zhanghaozhe committed
272
              {
zhanghaozhe committed
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
                rankListTabs.map((item, index) => {
                  return <table key={index}>
                    <thead>
                    <tr>
                      <th>名次</th>
                      <th>昵称</th>
                      <th>成绩</th>
                      <th>奖品</th>
                    </tr>
                    </thead>
                    <tbody>
                    {
                      !!_rankList.length && _rankList.map((item, index) => {
                        return <tr key={index}>
                          <td>
                            {
                              index < 3 ? <img src={icons[index]} alt=""/> : index + 1
                            }
                          </td>
                          <td>
                            <img src={item.avatar} className={'avatar'} alt=""/>
                            {item.user_name}
                          </td>
                          <td>
                            <span className={'score'}>{item.score}</span>/<span>{item.cost_time}</span>
                          </td>
                          <td>
zhanghaozhe committed
300 301 302 303 304
                           <div>
                             {
                               item.prize_url ? <a href={`${item.prize_url}#goback`}>{item.prize}</a> : item.prize
                             }
                           </div>
zhanghaozhe committed
305

zhanghaozhe committed
306 307 308 309 310 311
                          </td>
                        </tr>
                      })
                    }
                    </tbody>
                  </table>
zhanghaozhe committed
312 313
                })
              }
zhanghaozhe committed
314
            </Tabs>
zhanghaozhe committed
315
            {
zhanghaozhe committed
316 317 318 319 320 321
              rankList.length > 10 && (!isExpandRankList ?
                  <div className="expand" onClick={() => {
                    this.setState({
                      isExpandRankList: true,
                    })
                  }}>
zhanghaozhe committed
322 323 324 325
                  <span>
                    展开更多
                    <i className={'iconfont iconiconfront-69'}></i>
                  </span>
zhanghaozhe committed
326 327 328 329 330 331 332
                  </div>
                  :
                  <div className="expand" onClick={() => {
                    this.setState({
                      isExpandRankList: false,
                    })
                  }}>
zhanghaozhe committed
333 334 335 336
                  <span>
                    收起
                    <i className={'iconfont iconiconfront-71'}></i>
                  </span>
zhanghaozhe committed
337 338
                  </div>
              )
zhanghaozhe committed
339 340 341
            }
          </div>
        </div>
zhanghaozhe committed
342 343 344 345
        <div className="qrcode">
          <img src="https://julyedu-cdn.oss-cn-beijing.aliyuncs.com/ai-test/qr.png" alt=""/>
          <div>100分,请长按/扫码,免费领课!</div>
        </div>
zhanghaozhe committed
346 347 348 349
        <div className="btn">
          {
            compareDesc(new Date(), pageState.stop_time * 1000) > 0 ?
              pageState.daily_test_num > 0
zhanghaozhe committed
350 351
                ? <button className={'available'} onClick={() => {
                  isNeverShow ? this.startTest() : this.setState({
zhanghaozhe committed
352
                    entryButtonInRule: true,
zhanghaozhe committed
353 354 355
                    isShowRule: true,
                  })
                }}>开始测试<span>(今日可测试{pageState.daily_test_num}次)</span></button>
zhanghaozhe committed
356
                : <Link to={`/ai-test/share?shareCode=${pageState.code}`}>
zhanghaozhe committed
357 358
                  <button className={'get-chance'}>获取测试机会<span>(今日可测试0次)</span></button>
                </Link>
zhanghaozhe committed
359 360 361 362 363
              : <button className={'unavailable'}>活动已结束</button>
          }
        </div>
        {
          isShowRule &&
zhanghaozhe committed
364 365 366 367
          <Rule rule={pageState.rule} startTest={this.startTest} neverShow={this.handleChange} isNeverShow={isNeverShow}
                close={() => {
                  this.setState({
                    isShowRule: false,
zhanghaozhe committed
368
                    entryButtonInRule: false,
zhanghaozhe committed
369
                  })
zhanghaozhe committed
370
                }} entryButtonInRule={entryButtonInRule}/>
zhanghaozhe committed
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
        }
        {
          isShowUserAddress &&
          <div className="user-address-wrapper">
            <div className="user-address">
              <div className="title">收货信息</div>
              <div className="tip">获奖用户(以最终榜单为准)请及时填写收货信息</div>
              <form action="" onSubmit={this.submitForm}>
                <input type="text" placeholder={'收件人'} name={'name'} onChange={this.inputText}
                       value={userAddress.name}/>
                <input type="tel" placeholder={'联系方式'} name={'phone'} onChange={this.inputText}
                       value={userAddress.phone}/>
                <input type="text" placeholder={'收货地址'} name={'address'} onChange={this.inputText}
                       value={userAddress.address}/>
                <button type={'submit'}
                        className={Object.values(userAddress).every(value => !!value) ? 'available' : ''}>提交
                </button>
              </form>
              <i className={'close iconfont iconiconfront-2'} onClick={() => {
                this.setState({
                  isShowUserAddress: false,
                })
              }}/>
            </div>
          </div>
        }
      </div>
    );
  }
}

zhanghaozhe committed
402
function Rule({neverShow, isNeverShow, rule, close, startTest, entryButtonInRule}) {
zhanghaozhe committed
403 404 405 406
  return <div className="rule-mask">
    <div className="rule">
      <div>测试规则</div>
      <div dangerouslySetInnerHTML={html(rule)}></div>
zhanghaozhe committed
407 408 409 410 411 412 413 414 415
      {
        entryButtonInRule && <>
          <div className="option">
            <input id={'never-show'} type="checkbox" onChange={neverShow} checked={isNeverShow}/>
            <label htmlFor="never-show">不再提示</label>
          </div>
          <button onClick={startTest}>进入测试</button>
        </>
      }
zhanghaozhe committed
416 417 418 419 420 421
      <i className={'close iconfont iconiconfront-2'} onClick={close}/>
    </div>
  </div>
}


zhanghaozhe committed
422 423 424 425
export default connect(
  ({user}) => ({user}),
  null,
)(Scores);