123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- import React from 'react';
- import Fullscreen from 'react-fullscreen-crossbrowser';
- import './index.less';
- import Page from '@src/containers/Page';
- import { randomList, sortListWithOrder, resortListWithOrder } from '@src/services/Tools';
- import { Question } from '../../../stores/question';
- import Base from './base';
- import Sentence from './sentence';
- import { ExaminationOrder } from '../../../../Constant';
- import { Main } from '../../../stores/main';
- import { My } from '../../../stores/my';
- export default class extends Page {
- init() {
- this.singleTime = null;
- this.singleInterval = null;
- this.totalTime = null;
- this.totalInterval = null;
- this.stages = {};
- this.stage = '';
- this.stageInterval = null;
- this.stageTime = 0;
- this.stageProcess = { number: 0, time: 0 };
- this.relaxProcess = { time: 0 };
- this.startInterval = null;
- this.startTime = 0;
- this.baseRef = null;
- this.sentenceRef = null;
- this.accTime = 0;
- }
- outPage() {
- if (this.stageInterval) {
- clearInterval(this.stageInterval);
- this.stageInterval = null;
- }
- if (this.totalInterval) {
- clearInterval(this.totalInterval);
- this.totalInterval = null;
- }
- if (this.singleInterval) {
- clearInterval(this.singleInterval);
- this.singleInterval = null;
- }
- }
- initState() {
- return {
- showTime: true,
- showNo: true,
- setting: {},
- report: {},
- question: {},
- userQuestion: {},
- paper: {},
- scene: null,
- };
- }
- switchTime() {
- this.setState({ showTime: !this.state.showTime });
- }
- switchNo() {
- this.setState({ showNo: !this.state.showNo });
- }
- initData() {
- const { type, id } = this.params;
- // type 是获取基础paper的表信息
- // 等同于PaperOrigin
- Question.getPaper(type, id).then(paper => {
- let totalNumber = paper.questionNumber;
- let handler = null;
- if (paper.paperModule === 'examination') {
- // 模考获取配置信息
- handler = Main.getExaminationNumber().then(result => {
- totalNumber = 0;
- Object.keys(result).forEach(key => {
- result[key].time = Number(result[key].time);
- result[key].number = Number(result[key].number);
- totalNumber += result[key].number;
- });
- this.stages = result;
- this.relaxProcess = { time: 8 * 60 };
- });
- } else {
- handler = Promise.resolve();
- }
- this.setState({ paper, totalNumber });
- const { r } = this.state.search;
- if (r) {
- handler.then(() => {
- this.continue(r);
- });
- } else if (paper.paperModule === 'sentence') {
- // 长难句没有设置,直接开始
- handler.then(() => {
- this.start({});
- });
- } else {
- this.setState({ scene: 'start' });
- // 模考cat1分钟自动开始
- if (paper.paperModule === 'examination') {
- this.startWaitTime();
- }
- }
- handler.catch(() => {
- goBack();
- });
- });
- }
- setSetting(newSetting) {
- const { setting } = this.state;
- this.setState({ setting: Object.assign(setting, newSetting) });
- }
- start(setting) {
- const { type, id } = this.params;
- return Question.start(type, id, setting).then(report => {
- if (report.isFinish) {
- return this.finish();
- }
- this.changeQuery({ r: report.id });
- this.setState({ report, scene: 'question' });
- // 开始统计做题进度
- if (report.paperModule === 'examination') {
- const { order } = setting;
- this.initStage(order[0], 0, 0);
- }
- this.totalQuestionTime();
- return this.next();
- });
- }
- continue(reportId) {
- return Question.continue(reportId)
- .then(report => {
- if (report.isFinish) {
- throw new Error('做题结束,请先重置');
- }
- this.setState({ report, scene: 'questionn' });
- // 更新当前做题进度
- if (report.paperModule === 'examination') {
- const { stage, time, number } = report.setting;
- this.initStage(stage, time[stage], number[stage]);
- }
- this.totalQuestionTime(report.userTime || 0);
- return this.next();
- })
- .catch(() => {
- Question.reportLink({ report: { id: reportId } }, false);
- });
- }
- next() {
- const { report } = this.state;
- const { setting = {} } = report;
- return Question.next(report.id)
- .then(userQuestion => {
- const questionSetting = {};
- if (setting.disorder && userQuestion.questionType !== 'ds') {
- const { questions } = userQuestion.question.content;
- if (questions) {
- // 乱序显示选项
- questionSetting.questions = [];
- questions.forEach(q => {
- const order = randomList(q.select.length);
- q.select = sortListWithOrder(q.select, order);
- questionSetting.questions.push(order);
- });
- }
- }
- this.setState({
- userQuestion,
- question: userQuestion.question,
- questionSetting,
- scene: 'question',
- });
- this.singleQuestionTime();
- return true;
- })
- .catch(err => {
- if (err.message === 'finish') {
- // 考试结束
- return this.finish();
- }
- return false;
- });
- }
- submit(answer) {
- const { report, userQuestion, questionSetting, singleTime } = this.state;
- const { setting = {} } = report;
- if (setting.disorder && userQuestion.questionType !== 'ds') {
- const { questions } = answer;
- if (questions) {
- // 还原乱序选项
- questions.forEach((q, index) => {
- const order = questionSetting.questions[index];
- Object.keys(q).forEach(k => {
- if (q[k]) q[k] = resortListWithOrder(q[k], order);
- });
- });
- }
- }
- return Question.submit(userQuestion.id, answer, singleTime, questionSetting).then(() => {
- this.singleQuestionTime(true);
- if (report.paperModule === 'examination') {
- // 更新模考做题进度
- this.stageNumber = userQuestion.stageNo;
- if (this.stageNumber >= this.stageProcess.number) {
- const { order } = report.setting;
- // 进入休息
- if (order.indexOf(this.stage) < order.length - 1) {
- this.stageToRelax();
- return Promise.reject();
- }
- }
- }
- return Promise.resolve();
- });
- }
- // 主动进入下一阶段
- nextStage() {
- const { report } = this.state;
- return Question.stage(report.id)
- .then(() => {
- this.baseRef.timeOut(() => {
- this.stageToRelax();
- });
- })
- .catch(err => {
- if (err.message === 'finish') {
- // 考试结束
- return this.finish();
- }
- return false;
- });
- }
- relaxToNextStage() {
- this.stageQuestionTime(0, true);
- // 进入下一阶段
- this.initNextStage();
- this.next();
- }
- stageToRelax() {
- if (this.accTime > 55 * 60) {
- this.relaxStage();
- this.accTime = 0;
- } else {
- this.relaxToNextStage();
- }
- }
- finish() {
- const { report } = this.state;
- return Question.finish(report.id)
- .then(() => {
- // 跳转到报告页
- Question.reportLink({ report }, false);
- // this.setState({ scene: 'finish' });
- })
- .catch(() => {
- Question.reportLink({ report }, false);
- });
- }
- singleQuestionTime(stop) {
- if (this.singleInterval) {
- clearInterval(this.singleInterval);
- this.singleInterval = null;
- }
- if (!stop) {
- this.singleTime = 0;
- this.singleInterval = setInterval(() => {
- this.singleTime += 1;
- this.setState({ singleTime: this.singleTime });
- }, 1000);
- }
- }
- totalQuestionTime(initTime, stop) {
- if (initTime) {
- this.totalTime = initTime;
- }
- if (this.totalInterval) {
- clearInterval(this.totalInterval);
- this.totalInterval = null;
- }
- if (!stop) {
- this.totalInterval = setInterval(() => {
- this.totalTime += 1;
- this.setState({ totalTime: this.totalTime });
- }, 1000);
- }
- }
- stageQuestionTime(initTime, stop) {
- if (this.stageInterval) {
- clearInterval(this.stageInterval);
- this.stageInterval = null;
- this.stageTime = initTime;
- }
- if (!stop) {
- this.stageInterval = setInterval(() => {
- this.stageTime += 1;
- if (this.stageTime >= this.stageProcess.time) {
- clearInterval(this.stageInterval);
- const { scene } = this.state;
- if (scene === 'relax') {
- // 进入下一阶段,获取下一题
- this.relaxToNextStage();
- } else {
- // 提交当前阶段
- this.nextStage();
- }
- return;
- }
- this.setState({ stageTime: this.stageProcess.time - this.stageTime });
- }, 1000);
- this.setState({ stageTime: this.stageProcess.time - this.stageTime });
- }
- }
- startWaitTime() {
- if (this.startInterval) {
- clearInterval(this.startInterval);
- this.startInterval = null;
- }
- this.startInterval = setInterval(() => {
- this.startTime += 1;
- // 1分钟等待: 自动提交第一选择
- const { scene } = this.state;
- const { paper, setting } = this.state;
- const { disorder = true, order = [] } = setting;
- if (scene !== 'start') {
- clearInterval(this.startInterval);
- this.startInterval = null;
- } else if (this.startTime >= 60) {
- clearInterval(this.startInterval);
- this.startInterval = null;
- this.start({
- disorder: paper.finishTimes > 0 ? disorder : false,
- order: order.length > 0 ? order.filter(row => row) : ExaminationOrder[0].value,
- });
- }
- this.setState({ startTime: 60 - this.startTime });
- }, 1000);
- }
- initNextStage() {
- const { report } = this.state;
- // 进入下一阶段
- const { order } = report.setting;
- this.stage = order[order.indexOf(this.stage) + 1];
- this.stageProcess = this.stages[this.stage];
- this.stageQuestionTime(0);
- this.setState({ totalNumber: this.stageProcess.number });
- this.accTime += this.stageProcess.time;
- }
- relaxStage() {
- this.stageProcess = this.relaxProcess;
- this.stageQuestionTime(0);
- this.setState({
- scene: 'relax',
- });
- return true;
- }
- initStage(stage, time) {
- this.stage = stage;
- this.stageProcess = this.stages[stage];
- this.stageTime = time;
- this.stageQuestionTime(time);
- this.setState({ totalNumber: this.stageProcess.number });
- this.accTime += this.stageProcess.time;
- }
- toggleFullscreen() {
- const { isFullscreenEnabled } = this.state;
- this.setState({ isFullscreenEnabled: !isFullscreenEnabled });
- }
- toggleCollect() {
- const { userQuestion = {} } = this.state;
- if (!userQuestion.collect) {
- My.addQuestionCollect(userQuestion.questionNoId).then(() => {
- userQuestion.collect = true;
- this.setState({ userQuestion });
- });
- } else {
- My.delQuestionCollect(userQuestion.questionNoId).then(() => {
- userQuestion.collect = false;
- this.setState({ userQuestion });
- });
- }
- }
- renderView() {
- return (
- <Fullscreen
- enabled={this.state.isFullscreenEnabled}
- onChange={isFullscreenEnabled => this.setState({ isFullscreenEnabled })}
- >
- {this.renderDetail()}
- </Fullscreen>
- );
- }
- renderDetail() {
- const { scene, paper, userQuestion } = this.state;
- if (!paper.id || !scene) return null;
- switch (paper.paperModule) {
- case 'sentence':
- return <Sentence ref={(ref) => { this.sentenceRef = ref; }} key={userQuestion.id} {...this.state} flow={this} mode='process' />;
- default:
- return <Base ref={(ref) => { this.baseRef = ref; }} key={userQuestion.id} {...this.state} flow={this} mode='process' />;
- }
- }
- }
|