index.js 14.6 KB
Newer Older
zhanghaozhe committed
1
/* eslint-disable jsx-a11y/anchor-is-valid, no-script-url */
zhanghaozhe committed
2 3 4
import React, { Component } from "react"
import "./index.scss"
import { Tabs, Toast } from "antd-mobile"
zhanghaozhe committed
5
import { http } from "src/utils"
zhanghaozhe committed
6 7 8 9 10
import storage from "store2"
import { html } from "src/utils"
import { compareDesc } from "date-fns"
import { Link } from "react-router-dom"
import { connect } from "react-redux"
zhanghaozhe committed
11 12

class Scores extends Component {
zhanghaozhe committed
13
  store = storage.namespace("aiTestEntry")
zhanghaozhe committed
14

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

  componentDidMount() {
    this.getInitialData()
zhanghaozhe committed
45
    this.getRankList(null, 0)
zhanghaozhe committed
46 47 48 49
    if (!this.props.user.hasError) {
      this.getUserScores(0)
      this.getUserAddress()
    }
zhanghaozhe committed
50 51
  }

zhanghaozhe committed
52 53 54 55 56 57 58
  componentDidUpdate(prevProps, prevState) {
    if (this.props.user.hasError !== prevProps.user.hasError) {
      this.getUserScores(0)
      this.getUserAddress()
    }
  }

zhanghaozhe committed
59 60 61 62 63
  handleChange = (e) => {
    const isNeverSHow = e.target.checked
    this.setState({
      isNeverSHow,
    })
zhanghaozhe committed
64
    this.store.set("isNeverShow", isNeverSHow)
zhanghaozhe committed
65 66
  }

zhanghaozhe committed
67
  startTest = () => {
zhanghaozhe committed
68
    this.props.history.push("/ai-test/exam")
zhanghaozhe committed
69 70 71
  }

  getInitialData = () => {
zhanghaozhe committed
72 73 74 75 76 77 78 79 80 81 82 83
    http.get(`${API.home}/sys/activity_data`).then((res) => {
      const { code, msg, data } = res.data
      if (code === 200) {
        this.setState({
          pageState: data,
        })
      } else if (code === 3009) {
        this.props.history.push("/")
      } else {
        Toast.fail(msg, 2, null, false)
      }
    })
zhanghaozhe committed
84 85
  }

zhanghaozhe committed
86
  getRankList = (tab, type) => {
zhanghaozhe committed
87 88 89 90 91 92 93 94 95 96
    http.get(`${API.home}/sys/at/ranks/${type === 0 ? 0 : 2}`).then((res) => {
      const { code, msg, data } = res.data
      if (code === 200) {
        this.setState({
          rankList: data,
        })
      } else {
        Toast.fail(msg, 2, null, false)
      }
    })
zhanghaozhe committed
97 98 99
  }

  getUserScores = (type) => {
zhanghaozhe committed
100 101
    http.get(`${API.home}/sys/at/user_score/${type}/0`).then((res) => {
      const { code, msg, data } = res.data
zhanghaozhe committed
102

zhanghaozhe committed
103 104 105 106 107 108 109 110
      if (code === 200) {
        this.setState({
          userScore: data,
        })
      } else {
        Toast.fail(msg, 2, null, false)
      }
    })
zhanghaozhe committed
111 112 113
  }

  getUserAddress = () => {
zhanghaozhe committed
114
    http.get(`${API.home}/sys/user_address_info`).then((res) => {
zhanghaozhe committed
115
      const { code, data } = res.data
zhanghaozhe committed
116 117 118 119 120 121
      if (code === 200) {
        this.setState({
          userAddress: data,
        })
      }
    })
zhanghaozhe committed
122 123
  }

zhanghaozhe committed
124
  inputText = (e) => {
zhanghaozhe committed
125 126
    const key = e.target.name
    const value = e.target.value
zhanghaozhe committed
127
    this.setState((state) => {
zhanghaozhe committed
128 129
      return {
        userAddress: {
zhanghaozhe committed
130 131
          ...state.userAddress,
          ...{
zhanghaozhe committed
132 133 134 135 136 137 138 139 140
            [key]: value,
          },
        },
      }
    })
  }

  submitForm = (e) => {
    e.preventDefault()
zhanghaozhe committed
141 142 143
    const { userAddress } = this.state
    if (!Object.values(userAddress).every((item) => !!item)) {
      Toast.info("请填写完整")
zhanghaozhe committed
144 145
      return
    }
zhanghaozhe committed
146
    http.post(`${API.home}/sys/update_address`, userAddress).then((res) => {
zhanghaozhe committed
147
      const { code, msg } = res.data
zhanghaozhe committed
148 149 150 151 152 153 154 155 156
      if (code === 200) {
        Toast.success("提交成功", 2, null, false)
        this.setState({
          isShowUserAddress: false,
        })
      } else {
        Toast.fail(msg, 2, null, false)
      }
    })
zhanghaozhe committed
157 158 159 160
  }

  render() {
    const {
zhanghaozhe committed
161
      selfTabs,
zhanghaozhe committed
162
      rankList,
zhanghaozhe committed
163
      rankListTabs,
zhanghaozhe committed
164 165 166 167 168 169 170 171
      icons,
      isExpandRankList,
      isShowRule,
      isNeverShow,
      pageState,
      userScore,
      isShowUserAddress,
      userAddress,
zhanghaozhe committed
172
      entryButtonInRule,
zhanghaozhe committed
173
    } = this.state
zhanghaozhe committed
174 175 176 177 178 179
    const { user, history } = this.props
    const _rankList = Array.isArray(rankList)
      ? isExpandRankList
        ? rankList
        : rankList.slice(0, 10)
      : []
zhanghaozhe committed
180
    return (
zhanghaozhe committed
181
      <div className={"scores"}>
zhanghaozhe committed
182
        <div className="banner">
zhanghaozhe committed
183
          <img src={pageState.h5_banner} alt="" />
zhanghaozhe committed
184 185 186
        </div>
        <div className="info">
          <span>已有{pageState.join_num}人参加测试</span>
zhanghaozhe committed
187 188 189 190 191 192 193 194 195 196
          <a
            href="javascript:void(0);"
            onClick={() => {
              this.setState({
                isShowRule: true,
              })
            }}
          >
            规则
          </a>
zhanghaozhe committed
197 198 199
        </div>
        {
          <div className="score-list">
zhanghaozhe committed
200 201 202 203 204 205 206 207 208 209
            <Tabs
              tabs={selfTabs}
              tabBarUnderlineStyle={{ display: "none" }}
              onChange={(tab, i) => {
                this.getUserScores(i)
              }}
            >
              {selfTabs.map((tab, index) => {
                return (
                  <div className={"tab-content"} key={index}>
zhanghaozhe committed
210 211
                    <table>
                      <thead>
zhanghaozhe committed
212 213 214 215 216
                        <tr>
                          <th>分数</th>
                          <th>用时</th>
                          <th>{index === 0 && "最终"}排名</th>
                        </tr>
zhanghaozhe committed
217 218
                      </thead>
                      <tbody>
zhanghaozhe committed
219 220
                        {user.hasError ? (
                          <tr>
zhanghaozhe committed
221 222 223 224
                            <td>--</td>
                            <td>--</td>
                            <td>--</td>
                          </tr>
zhanghaozhe committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238
                        ) : (
                          <tr>
                            {userScore.score === "-" ? (
                              <td>'-'</td>
                            ) : (
                              <td>
                                {userScore.score}{" "}
                                <Link
                                  to={`/ai-test/analysis/${userScore.r_id}`}
                                >
                                  解析
                                </Link>
                              </td>
                            )}
zhanghaozhe committed
239
                            <td>{userScore.cost_time}</td>
zhanghaozhe committed
240 241 242 243 244
                            {userScore.rank === "-" ? (
                              <td>'-'</td>
                            ) : (
                              <td>{userScore.rank}</td>
                            )}
zhanghaozhe committed
245
                          </tr>
zhanghaozhe committed
246
                        )}
zhanghaozhe committed
247 248 249
                      </tbody>
                    </table>
                  </div>
zhanghaozhe committed
250 251
                )
              })}
zhanghaozhe committed
252 253
            </Tabs>
            <div className="share">
zhanghaozhe committed
254
              <Link to={"/ai-test/report"}>分享</Link>
zhanghaozhe committed
255 256 257 258 259
            </div>
          </div>
        }
        <div className="rank-list">
          <div className="head">
zhanghaozhe committed
260
            <div>测试排行榜</div>
zhanghaozhe committed
261 262
            <div>
              <span>仅显示前50</span>
zhanghaozhe committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276
              <a
                href="javascript:void(0);"
                onClick={() => {
                  if (user.hasError) {
                    history.push("/passport")
                  } else {
                    this.setState({
                      isShowUserAddress: true,
                    })
                  }
                }}
              >
                收货地址
              </a>
zhanghaozhe committed
277 278 279
            </div>
          </div>
          <div className="list">
zhanghaozhe committed
280 281 282 283 284 285 286 287
            <Tabs
              tabs={rankListTabs}
              tabBarUnderlineStyle={{ display: "none" }}
              onChange={this.getRankList}
            >
              {rankListTabs.map((item, index) => {
                return (
                  <table key={index}>
zhanghaozhe committed
288
                    <thead>
zhanghaozhe committed
289 290 291 292 293 294
                      <tr>
                        <th>名次</th>
                        <th>昵称</th>
                        <th>成绩</th>
                        <th>奖品</th>
                      </tr>
zhanghaozhe committed
295 296
                    </thead>
                    <tbody>
zhanghaozhe committed
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 326 327 328 329 330 331 332 333
                      {!!_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>
                                <div>
                                  {item.prize_url ? (
                                    <a href={`${item.prize_url}#goback`}>
                                      {item.prize}
                                    </a>
                                  ) : (
                                    item.prize
                                  )}
                                </div>
                              </td>
                            </tr>
                          )
                        })}
zhanghaozhe committed
334 335
                    </tbody>
                  </table>
zhanghaozhe committed
336 337
                )
              })}
zhanghaozhe committed
338
            </Tabs>
zhanghaozhe committed
339 340 341 342 343
            {rankList.length > 10 &&
              (!isExpandRankList ? (
                <div
                  className="expand"
                  onClick={() => {
zhanghaozhe committed
344 345 346
                    this.setState({
                      isExpandRankList: true,
                    })
zhanghaozhe committed
347 348
                  }}
                >
zhanghaozhe committed
349 350
                  <span>
                    展开更多
zhanghaozhe committed
351
                    <i className={"iconfont iconiconfront-69"}></i>
zhanghaozhe committed
352
                  </span>
zhanghaozhe committed
353 354 355 356 357
                </div>
              ) : (
                <div
                  className="expand"
                  onClick={() => {
zhanghaozhe committed
358 359 360
                    this.setState({
                      isExpandRankList: false,
                    })
zhanghaozhe committed
361 362
                  }}
                >
zhanghaozhe committed
363 364
                  <span>
                    收起
zhanghaozhe committed
365
                    <i className={"iconfont iconiconfront-71"}></i>
zhanghaozhe committed
366
                  </span>
zhanghaozhe committed
367 368
                </div>
              ))}
zhanghaozhe committed
369 370
          </div>
        </div>
zhanghaozhe committed
371
        <div className="qrcode">
372
          <img src="https://cdn.julyedu.com/ai-test/qr.png" alt="" />
zhanghaozhe committed
373 374
          <div>100分,请长按/扫码,免费领课!</div>
        </div>
zhanghaozhe committed
375
        <div className="btn">
zhanghaozhe committed
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
          {compareDesc(new Date(), pageState.stop_time * 1000) > 0 ? (
            pageState.daily_test_num > 0 ? (
              <button
                className={"available"}
                onClick={() => {
                  isNeverShow
                    ? this.startTest()
                    : this.setState({
                        entryButtonInRule: true,
                        isShowRule: true,
                      })
                }}
              >
                开始测试<span>(今日可测试{pageState.daily_test_num}次)</span>
              </button>
            ) : (
              <Link to={`/ai-test/share?shareCode=${pageState.code}`}>
                <button className={"get-chance"}>
                  获取测试机会<span>(今日可测试0次)</span>
                </button>
              </Link>
            )
          ) : (
            <button className={"unavailable"}>活动已结束</button>
          )}
zhanghaozhe committed
401
        </div>
zhanghaozhe committed
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
        {isShowRule && (
          <Rule
            rule={pageState.rule}
            startTest={this.startTest}
            neverShow={this.handleChange}
            isNeverShow={isNeverShow}
            close={() => {
              this.setState({
                isShowRule: false,
                entryButtonInRule: false,
              })
            }}
            entryButtonInRule={entryButtonInRule}
          />
        )}
        {isShowUserAddress && (
zhanghaozhe committed
418 419 420
          <div className="user-address-wrapper">
            <div className="user-address">
              <div className="title">收货信息</div>
zhanghaozhe committed
421 422 423
              <div className="tip">
                获奖用户(以最终榜单为准)请及时填写收货信息
              </div>
zhanghaozhe committed
424
              <form action="" onSubmit={this.submitForm}>
zhanghaozhe committed
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 451 452 453 454
                <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"
                      : ""
                  }
                >
                  提交
zhanghaozhe committed
455 456
                </button>
              </form>
zhanghaozhe committed
457 458 459 460 461 462 463 464
              <i
                className={"close iconfont iconiconfront-2"}
                onClick={() => {
                  this.setState({
                    isShowUserAddress: false,
                  })
                }}
              />
zhanghaozhe committed
465 466
            </div>
          </div>
zhanghaozhe committed
467
        )}
zhanghaozhe committed
468
      </div>
zhanghaozhe committed
469
    )
zhanghaozhe committed
470 471 472
  }
}

zhanghaozhe committed
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
function Rule({
  neverShow,
  isNeverShow,
  rule,
  close,
  startTest,
  entryButtonInRule,
}) {
  return (
    <div className="rule-mask">
      <div className="rule">
        <div>测试规则</div>
        <div dangerouslySetInnerHTML={html(rule)}></div>
        {entryButtonInRule && (
          <>
            <div className="option">
              <input
                id={"never-show"}
                type="checkbox"
                onChange={neverShow}
                checked={isNeverShow}
              />
              <label htmlFor="never-show">不再提示</label>
            </div>
            <button onClick={startTest}>进入测试</button>
          </>
        )}
        <i className={"close iconfont iconiconfront-2"} onClick={close} />
      </div>
zhanghaozhe committed
502
    </div>
zhanghaozhe committed
503
  )
zhanghaozhe committed
504 505
}

zhanghaozhe committed
506
export default connect(({ user }) => ({ user }), null)(Scores)