index.js 6.51 KB
Newer Older
zhanghaozhe committed
1
/* eslint-disable eqeqeq */
zhanghaozhe committed
2 3 4 5 6 7 8 9 10
import React, { Component } from "react"
import { HeaderBar } from "src/common"
import { getParam, http } from "src/utils"
import "./index.scss"
import QRCode from "qrcode"
import html2canvas from "html2canvas"
import scoreIconL from "src/assets/image/score_icon-l.png"
import scoreIconR from "src/assets/image/score_icon-r.png"
import scoreReportBg from "src/assets/image/scoreReport_bg.png"
zhanghaozhe committed
11
import { connect } from "react-redux"
xuzhenghua committed
12

zhanghaozhe committed
13 14 15
@connect((state) => ({
  user: state.user,
}))
xuzhenghua committed
16
class scoreReport extends Component {
zhanghaozhe committed
17 18 19
  constructor(props) {
    super(props)
    this.state = {
zhanghaozhe committed
20
      tab: ["当前成绩", "今日最佳", "本月最佳"],
zhanghaozhe committed
21
      cutIndex: 0,
zhanghaozhe committed
22 23 24 25
      myRankList: "",
      imgUrl: "",
      codeSrc: "",
      avatar_file: "",
xuzhenghua committed
26
    }
zhanghaozhe committed
27
  }
xuzhenghua committed
28

zhanghaozhe committed
29 30 31 32
  componentDidMount() {
    window.onbeforeunload = () => {
      document.documentElement.scrollTop = 0
      document.body.scrollTop = 0
xuzhenghua committed
33
    }
zhanghaozhe committed
34 35 36
    this.getCodeWe()
    this.handleFetchInfo(0)
  }
xuzhenghua committed
37

xuzhenghua committed
38 39 40 41 42 43 44 45
  // shouldComponentUpdate(nextProps, nextState, nextContext) {
  //   if (this.props.user.data.avatar !== nextProps.user.data.avatar) {
  //     this.getCanvas()
  //     return false
  //   }
  //   return true
  // }
  //
xuzhenghua committed
46

zhanghaozhe committed
47 48 49
  getCanvas() {
    document.documentElement.scrollTop = 0
    document.body.scrollTop = 0
xuzhenghua committed
50

zhanghaozhe committed
51 52
    let _this = this
    var canvas2 = document.createElement("canvas")
zhanghaozhe committed
53
    let _canvas = document.getElementsByClassName("score-list")[0]
zhanghaozhe committed
54 55
    if (!_canvas) {
      return
xuzhenghua committed
56
    }
zhanghaozhe committed
57 58 59 60 61 62 63 64
    var w = parseInt(window.getComputedStyle(_canvas).width)
    var h = parseInt(window.getComputedStyle(_canvas).height)
    var scale = window.devicePixelRatio
    canvas2.width = w * scale
    canvas2.height = h * scale

    var context = canvas2.getContext("2d")
    context.scale(1, 1)
zhanghaozhe committed
65 66 67
    html2canvas(document.getElementsByClassName("score-list")[0], {
      canvas: canvas2,
    }).then(function (canvas) {
zhanghaozhe committed
68 69
      //document.body.appendChild(canvas);

zhanghaozhe committed
70 71 72
      let imgUrl = canvas
        .toDataURL("image/png")
        .replace("image/png", "image/octet-stream")
zhanghaozhe committed
73 74 75 76 77
      _this.setState({
        imgUrl: imgUrl,
      })
    })
  }
xuzhenghua committed
78

zhanghaozhe committed
79
  // 获取二维码
zhanghaozhe committed
80 81 82
  getCodeWe() {
    let _this = this
    let qrCodeLink = `${API.m}/ai-test`
zhanghaozhe committed
83
    return new Promise((resolve) => {
zhanghaozhe committed
84 85 86
      QRCode.toDataURL(qrCodeLink, {}, function (err, url) {
        _this.setState({
          codeSrc: url,
xuzhenghua committed
87
        })
zhanghaozhe committed
88 89 90 91 92 93
      })
      resolve()
    })
  }

  handleFetchInfo = (index) => {
zhanghaozhe committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    http
      .get(
        `${API.home}/sys/at/user_score/${index}/${
          getParam("uid") ? getParam("uid") : 0
        }`
      )
      .then((res) => {
        const { code, data } = res.data
        if (code === 200) {
          this.setState({
            myRankList: data,
          })

          let avatar = data.avatar_file
          this.getBase64(avatar)
          setTimeout(() => {
            this.getCanvas()
          }, 500)
        } else if (code === 4030) {
          const { user, history } = this.props
          if (user.hasError) {
            history.push("/passport")
          }
zhanghaozhe committed
117
        }
zhanghaozhe committed
118
      })
zhanghaozhe committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
  }

  getBase64Image = (img) => {
    var canvas = document.createElement("canvas")
    canvas.width = img.width
    canvas.height = img.height

    var ctx = canvas.getContext("2d")
    ctx.drawImage(img, 0, 0, img.width, img.height)
    var ext = img.src.substring(img.src.lastIndexOf(".") + 1).toLowerCase()
    var dataURL = canvas.toDataURL("image/" + ext)
    return dataURL
  }

  getBase64 = (img) => {
    let base64 = null
    let image = new Image()
    let timeStamp = +new Date()
zhanghaozhe committed
137 138
    image.setAttribute("crossOrigin", "anonymous")
    image.src = img + "?" + timeStamp
zhanghaozhe committed
139 140 141 142 143 144 145
    image.onload = () => {
      base64 = this.getBase64Image(image)
      this.setState({
        avatar_file: base64,
      })
    }
    image.onerror = () => {
zhanghaozhe committed
146
      console.log("onerror")
zhanghaozhe committed
147
      let timeStamp = +new Date()
zhanghaozhe committed
148
      this.getBase64(img + "?" + timeStamp)
zhanghaozhe committed
149 150 151 152 153 154 155 156 157 158 159
    }
  }

  change = (index) => {
    this.setState({
      cutIndex: index,
    })
    this.handleFetchInfo(index)
  }

  render() {
zhanghaozhe committed
160 161 162 163 164 165 166 167
    const {
      tab,
      cutIndex,
      myRankList,
      codeSrc,
      imgUrl,
      avatar_file,
    } = this.state
zhanghaozhe committed
168
    return (
zhanghaozhe committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
      <div className={"score-content"}>
        <HeaderBar title="成绩报告" arrow={true} home={false} />
        <div className={"score-report"}>
          <ul className={"tab-list"}>
            {tab.map((item, index) => {
              return (
                <li
                  key={index}
                  className={cutIndex === index ? "active" : ""}
                  onClick={() => this.change(index)}
                >
                  {item}
                </li>
              )
            })}
zhanghaozhe committed
184 185 186
          </ul>

          <div className="score-list-box">
zhanghaozhe committed
187 188 189 190
            <div
              className={"score-list"}
              style={{ backgroundImage: `url(${scoreReportBg})` }}
            >
zhanghaozhe committed
191
              <div className="title">
zhanghaozhe committed
192
                <img src={scoreIconL} alt="" />
zhanghaozhe committed
193
                成绩报告
zhanghaozhe committed
194
                <img src={scoreIconR} alt="" />
zhanghaozhe committed
195 196 197 198 199
              </div>

              <table border="0" cellSpacing="0" cellPadding="0">
                <thead>
                  <tr>
zhanghaozhe committed
200 201 202
                    <td>分数</td>
                    <td>用时</td>
                    <td>{cutIndex === 0 && "最终"}排名</td>
zhanghaozhe committed
203
                  </tr>
zhanghaozhe committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
                </thead>
                <tbody>
                  {myRankList.r_id !== 0 && (
                    <tr className={"has"}>
                      <td>{myRankList.score}</td>
                      <td>{myRankList.cost_time}</td>
                      <td className={"rank"}>{myRankList.rank}</td>
                    </tr>
                  )}
                  {myRankList.r_id == 0 && (
                    <tr>
                      <td>-</td>
                      <td>-</td>
                      <td>-</td>
                    </tr>
                  )}
zhanghaozhe committed
220 221
                </tbody>
              </table>
xuzhenghua committed
222

zhanghaozhe committed
223
              <div className="comment text-overflow-4">
zhanghaozhe committed
224 225 226 227
                <img className={"avatar_file"} src={avatar_file} alt="" />
                <span
                  dangerouslySetInnerHTML={{ __html: myRankList.title }}
                ></span>
zhanghaozhe committed
228
              </div>
xuzhenghua committed
229

zhanghaozhe committed
230
              <div className="code">
zhanghaozhe committed
231
                <img src={codeSrc} alt="" />
zhanghaozhe committed
232 233 234
                <p>AI水平测试</p>
              </div>
            </div>
xuzhenghua committed
235

zhanghaozhe committed
236
            <img className={"imgUrl"} src={imgUrl} alt="" />
zhanghaozhe committed
237
          </div>
xuzhenghua committed
238

zhanghaozhe committed
239 240 241 242 243
          <div className="tip">长按图片分享给好友,或保存后分享到朋友圈</div>
        </div>
      </div>
    )
  }
xuzhenghua committed
244 245
}

zhanghaozhe committed
246
export default scoreReport