index.js 6.23 KB
Newer Older
zhanghaozhe committed
1
import React, { Component } from 'react'
zhanghaozhe committed
2 3
import { HeaderBar } from 'src/common'
import { getParam, http } from 'src/utils'
xuzhenghua committed
4 5 6
import './index.scss'
import QRCode from 'qrcode'
import html2canvas from 'html2canvas'
zhanghaozhe committed
7 8 9
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
10
import { connect } from "react-redux"
xuzhenghua committed
11 12

@connect(state => ({
zhanghaozhe committed
13 14
    user: state.user,
  }),
xuzhenghua committed
15
)
xuzhenghua committed
16 17
class scoreReport extends Component {

zhanghaozhe committed
18 19 20 21 22 23 24 25 26
  constructor(props) {
    super(props)
    this.state = {
      tab: ['当前成绩', '今日最佳', '本月最佳'],
      cutIndex: 0,
      myRankList: '',
      imgUrl: '',
      codeSrc: '',
      avatar_file: '',
xuzhenghua committed
27
    }
zhanghaozhe committed
28
  }
xuzhenghua committed
29

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

xuzhenghua committed
39

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

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

zhanghaozhe committed
53 54 55 56 57
    let _this = this
    var canvas2 = document.createElement("canvas")
    let _canvas = document.getElementsByClassName('score-list')[0]
    if (!_canvas) {
      return
xuzhenghua committed
58
    }
zhanghaozhe committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    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)
    html2canvas(document.getElementsByClassName('score-list')[0], {canvas: canvas2}).then(function (canvas) {
      //document.body.appendChild(canvas);

      let imgUrl = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream")
      _this.setState({
        imgUrl: imgUrl,
      })
    })
  }
xuzhenghua committed
76 77

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

  handleFetchInfo = (index) => {
zhanghaozhe committed
92
    http.get(`${API.home}/sys/at/user_score/${index}/${getParam('uid') ? getParam('uid') : 0}`).then(res => {
zhanghaozhe committed
93 94 95 96
      const {code, data} = res.data
      if (code === 200) {
        this.setState({
          myRankList: data,
xuzhenghua committed
97
        })
xuzhenghua committed
98

xuzhenghua committed
99
        let avatar = data.avatar_file
zhanghaozhe committed
100 101 102 103 104 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 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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
        this.getBase64(avatar)
        setTimeout(() => {
          this.getCanvas()
        }, 500)
      } else if (code === 4030) {
        const {user, history} = this.props
        if (user.hasError) {
          history.push('/passport')
        }
      }
    })
  }


  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()
    image.setAttribute('crossOrigin', 'anonymous')
    image.src = img + '?' + timeStamp
    image.onload = () => {
      base64 = this.getBase64Image(image)
      this.setState({
        avatar_file: base64,
      })
    }
    image.onerror = () => {
      console.log('onerror')
      let timeStamp = +new Date()
      this.getBase64(img + '?' + timeStamp)
    }
  }


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


  render() {
    const {tab, cutIndex, myRankList, codeSrc, imgUrl, avatar_file} = this.state
    return (
      <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>
                )
              })
            }
          </ul>

          <div className="score-list-box">

            <div className={'score-list'} style={{backgroundImage: `url(${scoreReportBg})`}}>
              <div className="title">
                <img src={scoreIconL} alt=""/>
                成绩报告
                <img src={scoreIconR} alt=""/>
              </div>

              <table border="0" cellSpacing="0" cellPadding="0">
                <thead>
                <tr>
                  <td>分数</td>
                  <td>用时</td>
                  <td>{cutIndex === 0 && '最终'}排名</td>
                </tr>
                </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>
                }
xuzhenghua committed
209

zhanghaozhe committed
210 211
                </tbody>
              </table>
xuzhenghua committed
212

zhanghaozhe committed
213 214
              <div className="comment text-overflow-4">
                <img className={'avatar_file'} src={avatar_file} alt=""/>
xuzhenghua committed
215
                <span dangerouslySetInnerHTML={{__html: myRankList.title}}></span>
zhanghaozhe committed
216
              </div>
xuzhenghua committed
217

zhanghaozhe committed
218 219 220 221 222
              <div className="code">
                <img src={codeSrc} alt=""/>
                <p>AI水平测试</p>
              </div>
            </div>
xuzhenghua committed
223 224


zhanghaozhe committed
225 226
            <img className={'imgUrl'} src={imgUrl} alt=""/>
          </div>
xuzhenghua committed
227

zhanghaozhe committed
228
          <div className="tip">长按图片分享给好友,或保存后分享到朋友圈</div>
xuzhenghua committed
229

zhanghaozhe committed
230 231 232 233
        </div>
      </div>
    )
  }
xuzhenghua committed
234 235 236 237
}


export default scoreReport