index.js 5.22 KB
Newer Older
zhanghaozhe committed
1 2
import React, { Component } from 'react';
import './index.scss'
zhanghaozhe committed
3 4 5
import { HeaderBar } from "src/common/index"
import Question from "src/components/ai-test/common/question"
import { html, http, browser } from "src/utils"
zhanghaozhe committed
6
import { Toast } from "antd-mobile";
zhanghaozhe committed
7
import { Link } from "react-router-dom";
zhanghaozhe committed
8
import { connect } from "react-redux";
zhanghaozhe committed
9
import Recommends from "src/components/ai-test/common/recommends"
zhanghaozhe committed
10
import classnames from 'classnames'
zhanghaozhe committed
11
import storage from 'store2'
zhanghaozhe committed
12 13 14


class Assist extends Component {
zhanghaozhe committed
15 16 17

  store = storage.namespace('aiTestAssist')

zhanghaozhe committed
18
  state = {
zhanghaozhe committed
19 20
    question: this.store.session.get('question'),
    answer: this.store.session.get('answer'),
zhanghaozhe committed
21 22 23 24 25 26
    result: null,
    rightAnswer: '',
    userAnswer: '',
  }

  componentDidMount() {
zhanghaozhe committed
27 28 29
    if (!this.store.session.get('question')) {
      this.getData()
    }
zhanghaozhe committed
30 31 32 33 34 35 36 37
  }

  getData = () => {
    http.get(`${API.home}/sys/aitest/assist`)
      .then(res => {
        const {code, msg, data} = res.data
        if (code === 200) {
          const answer = {
zhanghaozhe committed
38
            [data.id]: 0,
zhanghaozhe committed
39
          }
zhanghaozhe committed
40 41
          this.store.session.set('question', data)
          this.store.session.set('answer', answer)
zhanghaozhe committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
          this.setState({
            question: data,
            answer,
          });
        } else {
          Toast.fail(msg, 2, null, false)
        }
      })
  }

  selectAnswer = (question, optionId) => {
    this.setState({
      answer: {
        [question.id]: optionId,
      },
    });
  }

  submit = () => {
zhanghaozhe committed
61 62
    const {history, user} = this.props
    if (user.hasError) {
zhanghaozhe committed
63 64 65 66 67
      if (browser.isWeixin) {
        window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx23dac6775ac82877&redirect_uri=${encodeURIComponent(`${window.location.origin}${location.pathname}?aa=bb`)}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
      } else {
        history.push('/passport')
      }
zhanghaozhe committed
68 69
      return
    }
zhanghaozhe committed
70 71 72 73 74 75 76 77 78 79 80
    const {question, answer} = this.state
    if (!answer[question.id]) {
      Toast.info('请选择后进提交', 2, null, false)
      return
    }
    http.post(`${API.home}/sys/aitest/assistSubmit`, {
      code: this.props.match.params.assistCode,
      question_id: question.id,
      answer_id: answer[question.id],
    }).then(res => {
      const {code, msg, data} = res.data
zhanghaozhe committed
81 82 83 84 85
      if (answer[question.id] === data.correct_answer) {
        Toast.info('回答正确', 2, null, false)
      } else {
        Toast.info('回答错误', 2, null, false)
      }
zhanghaozhe committed
86 87 88 89 90
      this.setAnswer(data.correct_answer)
      if (code === 200) {
        this.setState({
          result: data,
        });
zhanghaozhe committed
91
        this.store.session.clearAll()
zhanghaozhe committed
92 93 94 95 96 97 98 99 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
      } else {
        Toast.fail(msg, 2, null, false)
      }
    })
  }

  setAnswer = (rightAnswerId) => {
    this.setState(state => {
      let rightAnswer = '', userAnswer = ''
      const question = {
        ...state.question, ...{
          options: state.question.options.map((item, index) => {
            if (item.id === rightAnswerId) {
              item.is_ans = 1
              rightAnswer = String.fromCharCode(65 + index)
            }
            if (item.id === state.answer[state.question.id]) {
              userAnswer = String.fromCharCode(65 + index)
              item.user_select = 1
            }
            return item
          }),
        },
      }
      return {
        question,
        rightAnswer,
        userAnswer,
      }
    });
  }

  render() {
    const {question, answer, result, rightAnswer, userAnswer} = this.state
    return (
      <div className={'assist'}>
        <HeaderBar title={'AI水平测试'} arrow={true}/>
        {
          question &&
          <Question question={question} category={'机器学习'} answer={answer} selectAnswer={!result && this.selectAnswer}/>
        }
        <div style={{height: '8px', backgroundColor: '#f5f5f5'}}></div>
        {
          result && <div className="content">
zhanghaozhe committed
136 137
            <div
              className={'info'}>您的选择是{userAnswer},正确答案是{rightAnswer} 回答{userAnswer === rightAnswer ? '正确' : '错误'}</div>
zhanghaozhe committed
138 139 140 141 142 143 144 145
            <div className="head">
              <i className="icon"></i>
              <span>解析</span>
            </div>
            <div className="analysis-content" dangerouslySetInnerHTML={html(result.analysis)}></div>
          </div>
        }
        {
zhanghaozhe committed
146 147
          result && question.commend_course && question.commend_course.length && <>
            <Recommends recommends={question.commend_course}/>
zhanghaozhe committed
148 149 150 151 152 153
          </>
        }

        <div className="btns">
          {
            result && <div className={classnames(['status', {
zhanghaozhe committed
154 155 156 157 158 159
              end: result.status === 6,
              success: result.status === 1,
              error: result.status === 2 || result.status === 3 || result.status === 4 || result.status === 5,
            }])}>
              {result.desc}
            </div>
zhanghaozhe committed
160
          }
zhanghaozhe committed
161 162 163 164 165
          {
            !result && <button className={'submit'} onClick={this.submit}>提交</button>
          }
          {
            result && (result.status === 6
zhanghaozhe committed
166 167
              ? <Link to={'/'} className={'home'}>返回首页</Link>
              : <Link to={'/ai-test/scores'} className={'test'}>我也要测试</Link>)
zhanghaozhe committed
168 169 170 171 172 173 174
          }
        </div>
      </div>
    );
  }
}

zhanghaozhe committed
175
export default connect(
zhanghaozhe committed
176
  ({user}) => ({user}),
zhanghaozhe committed
177 178
  null,
)(Assist)