excer.js 6.8 KB
import {observable, computed, action} from 'mobx';
// export const items = [{
//     id: 1,
//     titleId: 'Q1',
//     title: "以下那个神秘时间不是罗伯特.兰登破解的?(8积分)",
//     optionArr: [
//         {
//             id: 1, value: "A. 1", is_correct: true,
//         },
//         {
//             id: 2, value: "B. 2", is_correct: false,
//         },
//     ],
//     reason: "罗伯特兰登,丹布朗小说《天使与魔鬼》·《达芬奇密码》·《失落的秘符》与《地狱》新作的主要人物"
// }, {
//     id: 2,
//     titleId: 'Q2',
//     title: "以下那个神秘时间不是罗伯特.兰登破解的?(8积分)2",
//     optionArr: [
//         {
//             id: 1, value: "A. 1", is_correct: true,
//         },
//         {
//             id: 2, value: "B. 2", is_correct: false,
//         },
//         {
//             id: 3, value: "C. 3", is_correct: false,
//         },
//         {
//             id: 4, value: "D. 4", is_correct: false
//         }
//     ],
//     reason: "罗伯特兰登,丹布朗小说《天使与魔鬼》·《达芬奇密码》·《失落的秘符》与《地狱》新作的主要人物"
// }, {
//     id: 3,
//     titleId: 'Q2',
//     title: "以下那个神秘时间不是罗伯特.兰登破解的?(8积分)3",
//     optionArr: [
//         {
//             id: 1, value: "A. 1", is_correct: true,
//         },
//         {
//             id: 2, value: "B. 2", is_correct: false,
//         },
//         {
//             id: 3, value: "C. 3", is_correct: false,
//         },
//         {
//             id: 4, value: "D. 4", is_correct: false
//         }
//     ],
//     reason: "罗伯特兰登,丹布朗小说《天使与魔鬼》·《达芬奇密码》·《失落的秘符》与《地狱》新作的主要人物"
// }]

export const items = {
    "class": "com.xiniunet.site.testing.response.TestingQuestionFindResponse",
    "errors": [],
    "firstErrorMessage": "",
    "result": [{
        "class": "com.xiniunet.site.testing.domain.TestingQuestion",
        "id": "873081455239368704",
        "objectId": "873078041528897536",
        "objectType": "COURSE_LESSON",
        "point": 5,
        "questionOptions": [{
            "class": "com.xiniunet.site.testing.domain.TestingQuestionOption",
            "id": "873081455243563008",
            "isAnswer": false,
            "label": "a",
            "value":"我是选项",
            "questionId": "873081455239368704",
            "rowVersion": "0",
            "tenantId": "512824102474878976"
        }, {
            "class": "com.xiniunet.site.testing.domain.TestingQuestionOption",
            "id": "873081455260340224",
            "isAnswer": false,
            "label": "b",
            "value":"我是选项",
            "questionId": "873081455239368704",
            "rowVersion": "0",
            "tenantId": "512824102474878976"
        }, {
            "class": "com.xiniunet.site.testing.domain.TestingQuestionOption",
            "id": "873081455268728832",
            "isAnswer": false,
            "label": "c",
            "value":"我是选项",
            "questionId": "873081455239368704",
            "rowVersion": "0",
            "tenantId": "512824102474878976"
        }, {
            "class": "com.xiniunet.site.testing.domain.TestingQuestionOption",
            "id": "873081455277117440",
            "isAnswer": true,
            "label": "d",
            "value":"我是选项",
            "questionId": "873081455239368704",
            "rowVersion": "0",
            "tenantId": "512824102474878976"
        }],
        "rowVersion": "0",
        "tenantId": "512824102474878976",
        "title": "作文1",
        "type": "MULTIPLE_CHOICE"
    }],
    "totalCount": "1"
}

export const subList = observable.box([]);//存的是Sub数组


class Sub {
    id;         //第几项
    titleId;//没什么卵用
    title;      //问题的标题
    @observable
    optionArr = [];//选项数组
    reason;       //讲解
    @observable
    selectId;//这就是 那个 确定  的 答案
    //是否是选对,用于确认做对的数量
    value_correct;
    //这个 是 当时  改的  答案 可以 修改
    @observable
    sub_select;
    questionId;//习题Id
    point;//赠送积分

    from(obj, index) {
        this.id = index + 1;
        this.titleId = "Q"+(index+1);
        this.title = obj.title;
        this.reason = " ";
        this.questionId = obj.id;
        this.point = obj.point;

        for (let i = 0; i < obj.questionOptions.length; i++) {
            this.optionArr.push(new optionItem().from(obj.questionOptions[i], i))
        }

        this.selectId = 0;
        this.value_correct = false;
        this.sub_select = 0;
        return this;
    }

    @computed
    get isCorrect() {
        let flag = false;
        this.optionArr.map((v, i) => {
            if (this.selectId === v.id && v.is_correct === true) {
                flag = true;
            }
        })
        return flag;
    }

    @action
    setValue = (x) => {
        this.sub_select = x;
        this.optionArr.map((v, i) => {
            if (v.id === x) {
                v.setSelect(true);
            } else {
                v.setSelect(false);
            }
        })
    }
    //设置 最后  的答案  在 下一题 这个 按钮 这边
    @action
    setSub = () => {
        this.selectId = this.sub_select;
        this.value_correct = this.isCorrect;
    }
    @action
    setLoading = () => {
        this.selectId = this.sub_select;
        this.value_correct = this.isCorrect;
    }
}
class optionItem {
    id;//第几个选项
    value;//选项的值
    is_correct;//是否是正确答案
    @observable
    select;//是否选中
    optionId;//选项Id

    from(obj, index) {
        if(obj.label == null || obj.label == undefined){
            obj.label = "";
        }
        if(obj.value == null || obj.value == undefined){
            obj.value = "";
        }

        this.id = index + 1;
        this.value = obj.label + ". " + obj.value;
        this.is_correct = obj.isAnswer;
        this.select = false;
        this.optionId = obj.id;
        return this;
    }

    @action
    setSelect = (x)=> {
        this.select = x;
    }
}

//传入questions
export async function loadData(questions, onUpdated) {
    subList.replace(questions.map((v, i) => {
        return new Sub().from(v, i);
    }))

    onUpdated();
}

export function getDataById(id = 1) {
    const item = subList.find((v, i) => v.id == id);
    return item;
}

export function getCorrect() {
    let num = 0;
    subList.slice(0).map((v, i) => {
        if (v.value_correct) {
            num++;
        }
    })
    return num;
}

export function getScore(){
    let score = 0;
    subList.slice(0).map((v, i) => {
        if (v.value_correct) {
            score = score + v.point;
        }
    })
    return score;
}