answer-card.js 5.2 KB

1
  1. import store from"../../store/index";import Event from"../../common/chat/eventTypes";import AnswerCard from"../../common/answerCard/index";import LANGUAGE from"../../common/answerCard/Language";Component({properties:{answerCardSize:{type:Object,observer(t){const{width:e,height:s}=this.answerCardSize=t;e&&s&&this.resize({width:e,height:s})}}},data:{title:LANGUAGE.TITLE,submit:LANGUAGE.SUBMIT,submitDone:LANGUAGE.SUBMIT_DONE,cutOff:LANGUAGE.CUT_OFF,average:LANGUAGE.AVERAGE,hasChoice:!1,topTextClass:"",STATUS:{OPTION:"option",RESULT:"result",SCORE_RESULT:"score_result",SUBMIT:"submit",CUT_OFF:"cut_off",NONE:"none",SUBMIT_CLOSE:LANGUAGE.IKNOW},status:"",isShow:!1,scroeTips:"",averageNum:0,duration:0,useTime:0,isVote:!1,singleResult:[],answerTotal:0,voteResult:[],showResultTip:!0,answerResult:{}},lifetimes:{created(){this.chatEvent=this.getChatEvent(),this.chat=null,this.answerRetryTime=3,this.answerCard=new AnswerCard},attached(){this.unsub=store.get({"main.chat":t=>{this.chat=t,t&&this.bindEvent(this.chatEvent)}})},ready(){this.answerModal=this.selectComponent("#answerModal")}},methods:{bindEvent(t){const e=this.chat;e&&Object.keys(t).forEach((s=>{e.on(s,t[s])}))},getChatEvent(){const t=this,{STATUS:e}=this.data;return{[Event.GET_TEST_QUESTION_CONTENT](s,a){console.log("GET_TEST_QUESTION_CONTENT:",a),t.clearAnswerTimeoutClock(),t.answerCard.getQuestionContent(a,((s,a,i)=>{t.setData(s),t.setData({title:a,topTextClass:i,status:e.OPTION,isShow:!0,isVote:!1})})),t.resizeScroll(),t.toShow(!0),t.getChatEvent.startTime=Date.now(),t.getDuration(a.duration)},[Event.GET_TEST_QUESTION_RESULT](s,a){console.log("GET_TEST_QUESTION_RESULT",a),t.clearAnswerTimeoutClock();if(t.answerCard.getIsScore())return t.answerCard.getQuestionScoreResult(a,(({name:s,integer:a,decimalPoint:i,average:o})=>{t.setData({scoreContent:s,integer:a,isShow:!0,averageNum:Number(o).toFixed(1),decimalPoint:i,status:e.SCORE_RESULT,title:"答题结果",submit:e.SUBMIT_CLOSE})})),t.resizeScroll(),void t.toShow(!0);t.answerCard.getQuestionResult(a,(({answersOption:s,topTextClass:a,singleResult:i,type:o,isRight:r})=>{const n=i.reduce(((t,e)=>parseInt(t)+parseInt(e))),h=i.map((t=>Math.ceil(t/n*100)));let l=r?"answer-right":"answer-wrong",T=r?"恭喜你答对了":"抱歉您答错了",c="答题结果";t.answerCard.getIsSubmitted()||(l="tip",T="未作答",c=T),t.setData({answersOption:s,title:c,topTextClass:a,status:e.RESULT,isVote:"V"===o,singleResult:i,voteResult:h,answerTotal:n,answerResult:{icon:l,tip:T},showResultTip:!0,isShow:!0}),t.toShow(!0)})),t.resizeScroll()},[Event.STOP_TEST_QUESTION](s,a){console.log("STOP_TEST_QUESTION",a),t.clearAnswerTimeoutClock(),t.answerCard.getLatestQusetionId(),a.questionId,t.answerCard.getIsSubmitted()||(t.setData({status:e.CUT_OFF,modalData:t.getModalData(0)}),t.selectComponent("#answerModal").show())}}},getDuration(t){let e=parseInt(t);if(isNaN(e))return;clearInterval(this.getDuration.interval);const s=()=>new Promise((t=>this.chat.getTestQuestionDuration(t)));this.setData({duration:e}),this.getDuration.interval=setInterval((async()=>{if(0===e)return clearInterval(this.getDuration.interval),void this.submitTap();const t=await s();e=t.data.time,this.setData({duration:e})}),1e3)},choiceTap(t){const{status:e,STATUS:s}=this.data,{index:a,letter:i}=t.currentTarget.dataset;e===s.OPTION&&this.answerCard.choiceTap(a,i,((t,e,s)=>{this.setData({hasChoice:t,answersOption:e,scoreTips:s})}))},submitTap(){const{STATUS:t}=this.data;if("score_result"===this.data.status)return this.toShow(!1),void this.setData({submit:t.SUBMIT});this.answerCard.getHasChoice()&&(this.setData({isShow:!1}),this.clearAnswerTimeoutClock(),this.answerRetryTime=3,this.submitHandle())},submitHandle(){this.answerCard.toSendAnswer(this.chat,(t=>{let e;this.clearAnswerTimeoutClock();try{e=JSON.parse(t).code}catch(t){e=400}const s={200:LANGUAGE.SUBMIT_DONE,310:LANGUAGE.IGNORE,302:LANGUAGE.ANSWER_END,303:LANGUAGE.ANSWER_SUBMITED,400:LANGUAGE.SUBMIT_FAILED}[e]||LANGUAGE.SUBMIT_FAILED;this.setData({status:this.data.STATUS.SUBMIT,modalData:this.getModalData(200===e?1:e,s)}),this.selectComponent("#answerModal").show()}));const t=Date.now()-this.getChatEvent.startTime;this.setData({useTime:(t/1e3).toFixed(1)}),this.answerTimeout=setTimeout((()=>{this.answerRetryTime-=1,this.toShow(!1),this.answerRetryTime<0?(this.setData({status:"submit",modalData:this.getModalData(400,LANGUAGE.SUBMIT_FAILED)}),this.selectComponent("#answerModal").show()):this.submitHandle()}),5e3)},clearAnswerTimeoutClock(){this.answerTimeout&&clearTimeout(this.answerTimeout)},closePanel(){this.toShow(!1)},resize({width:t,height:e}){const{status:s,STATUS:a}=this.data;let i=0;i=s===a.RESULT?e-96:e-96-140,this.setData({questionScrollH:`height:${i}rpx`})},resizeScroll(){const{width:t,height:e}=this.answerCardSize;t&&e&&this.resize({width:t,height:e})},onModalHide(){this.toShow(!1)},getModalData(t,e=""){const{submitDone:s,cutOff:a}=this.data;let i={};return i=1===t?{imgSrc:"../../assets/images/interact-submit-success.png",modalText:s}:{imgSrc:"../../assets/images/interact-cut-off.png",modalText:0===t?a:e},i},toShow(t){this.triggerEvent("onAnswerCardShow",{isShow:t})},checkResult(){this.setData({showResultTip:!1})},handleClickBack(){this.setData({showResultTip:!0})}},options:{addGlobalClass:!0}});