理论题目导入调整

This commit is contained in:
dong 2022-10-19 18:15:17 +08:00
parent daa05eb67b
commit 0d6f6dda1c
2 changed files with 95 additions and 47 deletions

View File

@ -135,7 +135,7 @@ export default {
{ text: '添 加', handler: this.doCreate }, { text: '添 加', handler: this.doCreate },
{ text: '导 入', fileType: 'file', handler: this.importQuestionBank }, { text: '导 入', fileType: 'file', handler: this.importQuestionBank },
// { text: '', handler: this.questionsRuleManage}, // { text: '', handler: this.questionsRuleManage},
// { text: '', handler: this.exportTemplate} // { text: '', handler: this.exportTemplate},
{ text: '导出模板', handler: this.downloadTemplate} { text: '导出模板', handler: this.downloadTemplate}
] ]
} }
@ -249,11 +249,12 @@ export default {
}; };
if (file) { if (file) {
try { try {
setTimeout(() => { // setTimeout(() => {
const that = this; const that = this;
const reader = new FileReader(); const reader = new FileReader();
if (reader) { if (reader) {
reader.onload = function (e) { reader.onload = function (e) {
try {
let wb; let wb;
const data = e.target.result; const data = e.target.result;
if (that.rABS) { if (that.rABS) {
@ -269,46 +270,46 @@ export default {
if (wb && wb.Sheets) { if (wb && wb.Sheets) {
for (const index in wb.Sheets) { for (const index in wb.Sheets) {
const dataList = convertSheetToList(wb.Sheets[index], true); const dataList = convertSheetToList(wb.Sheets[index], true);
let questionTypeIndex; const objHeader = {
let topicIndex; questionTypeIndex: '题型(必填)',
let tagsIndex; topicIndex: '题干(必填)',
let option1Index; tagsIndex: '标签',
let option2Index; option1Index: '选项A必填',
let option3Index; option2Index: '选项B必填',
let option4Index; option3Index: '选项C',
let option5Index; option4Index: '选项D',
let option6Index; option5Index: '选项E',
let answerIndex; option6Index: '选项F',
answerIndex: '正确答案(必填)'
};
const indexHeader = JSON.parse(JSON.stringify(objHeader));
dataList.forEach((item, ii) => { dataList.forEach((item, ii) => {
if (!item[0] && !item[1] && item[2] === '题型(必填)') { Object.keys(objHeader).forEach(key => {
questionTypeIndex = ii; if (!item[0] && !item[1] && item[2] === objHeader[key]) {
} else if (!item[0] && !item[1] && item[2] === '题干(必填)') { indexHeader[key] = ii;
topicIndex = ii;
} else if (!item[0] && !item[1] && item[2] === '标签') {
tagsIndex = ii;
} else if (!item[0] && !item[1] && item[2] === '选项A必填') {
option1Index = ii;
} else if (!item[0] && !item[1] && item[2] === '选项B必填') {
option2Index = ii;
} else if (!item[0] && !item[1] && item[2] === '选项C') {
option3Index = ii;
} else if (!item[0] && !item[1] && item[2] === '选项D') {
option4Index = ii;
} else if (!item[0] && !item[1] && item[2] === '选项E') {
option5Index = ii;
} else if (!item[0] && !item[1] && item[2] === '选项F') {
option6Index = ii;
} else if (!item[0] && !item[1] && item[2] === '正确答案(必填)') {
answerIndex = ii;
} }
}); });
});
const errHeader = [];
Object.keys(objHeader).forEach(key => {
if (typeof indexHeader[key] != 'number') {
errHeader.push(objHeader[key]);
}
});
let errInfo = errHeader.join('');
if (errInfo) {
const error = `以下列:${errInfo},无法找到`;
throw error;
}
const errRow = [];
const {questionTypeIndex, topicIndex, tagsIndex, option1Index, option2Index, option3Index, option4Index, option5Index, option6Index, answerIndex } = indexHeader;
if (questionTypeIndex || questionTypeIndex === 0) { if (questionTypeIndex || questionTypeIndex === 0) {
dataList[questionTypeIndex].forEach((item, index) => { dataList[questionTypeIndex].forEach((item, index) => {
if (item && item !== '题型(必填)') { if (item && item !== '题型(必填)' && index > 2) {
const param = { const param = {
type: questionTypeMap[item], type: questionTypeMap[item],
topic: dataList[topicIndex][index], topic: dataList[topicIndex][index],
tags: dataList[tagsIndex][index], tags: dataList[tagsIndex][index] ? dataList[tagsIndex][index].replace(/\s+/g, ',') : '',
optionList: [] optionList: []
}; };
if (param.type === 'fill') { if (param.type === 'fill') {
@ -335,11 +336,51 @@ export default {
param.optionList.push({content:dataList[option6Index][index], correct: dataList[answerIndex][index].includes('F')}); param.optionList.push({content:dataList[option6Index][index], correct: dataList[answerIndex][index].includes('F')});
} }
} }
param.id = questionList.length; // param.id = questionList.length;
questionList.push(param); questionList.push(param);
if (!param.type || !param.topic || !dataList[option1Index][index] || !dataList[option2Index][index] || !dataList[answerIndex][index]) {
errRow.push(index + 1);
}
if (param.type === 'judge') {
const arr = ['√', '正确', '×', '错误'];
const asJudge = ['A', 'B'];
if (!arr.includes(dataList[option1Index][index]) || !arr.includes(dataList[option2Index][index]) || !asJudge.includes(dataList[answerIndex][index])) {
if (!errRow.includes(index + 1)) {
errRow.push(index + 1);
}
}
}
const asList = ['A', 'B', 'C', 'D', 'E', 'F'];
if (param.type === 'select') {
if (!asList.includes(dataList[answerIndex][index])) {
if (!errRow.includes(index + 1)) {
errRow.push(index + 1);
}
}
}
if (param.type === 'multi') {
if (dataList[answerIndex][index]) {
const list = dataList[answerIndex][index].split('');
const sortNew = list.sort(function(a, b) {
return a.localeCompare(b);
});
const fArr = sortNew.filter((item, ii) => {
return !asList.includes(item) || item == sortNew[ii + 1];
});
if (!errRow.includes(index + 1) && fArr.length) {
errRow.push(index + 1);
}
}
}
} }
}); });
} }
errInfo = errRow.join('');
if (errInfo) {
const error = `${errInfo}行,数据错误,请检查!`;
throw error;
}
} }
that.$store.dispatch('race/setPreTheoryData', questionList).then(() => { that.$store.dispatch('race/setPreTheoryData', questionList).then(() => {
that.$router.push({ path: `/teaching/preTheoryImport`}); that.$router.push({ path: `/teaching/preTheoryImport`});
@ -347,6 +388,13 @@ export default {
that.$message.error('导入题库失败!'); that.$message.error('导入题库失败!');
}); });
} }
} catch (e) {
that.$message.warning('导入错误!' + e);
throw e;
}
};
reader.onerror = function (e) {
throw e;
}; };
if (that.rABS) { if (that.rABS) {
reader.readAsArrayBuffer(file); reader.readAsArrayBuffer(file);
@ -354,7 +402,7 @@ export default {
reader.readAsBinaryString(file); reader.readAsBinaryString(file);
} }
} }
}, 200); // }, 200);
} catch (e) { } catch (e) {
this.$message.error('请根据下载模板导入题目!'); this.$message.error('请根据下载模板导入题目!');
} }
@ -386,7 +434,7 @@ export default {
exportTemplate() { exportTemplate() {
const wb = XLSX.utils.book_new(); const wb = XLSX.utils.book_new();
const data1 = [{A: '理论试题导入模板', B: '', C:'', D:'', E:'', F: '', G: '', H: '', I: '', J: '', K: ''}]; const data1 = [{A: '理论试题导入模板', B: '', C:'', D:'', E:'', F: '', G: '', H: '', I: '', J: '', K: ''}];
const data2 = [{A: '说明1、本表表头第一行到第三行内容不能修改删除;\n 2、支持批量导入的题型单选题多选题判断题题型不能自定义只能按照表格提供的进行录入;\n 3、【判断题】在选项A中可填写√ 或正确选项B中填写× 或错误答案填写A或B\n 4、【分类】多个分类项使用空格隔开', B: '', C:'', D:'', E:'', F: '', G: '', H: '', I: '', J: '', K: ''}]; const data2 = [{A: '说明1、本表表头第一行到第三行内容不能修改删除;\n 2、支持批量导入的题型单选题多选题判断题题型不能自定义只能按照表格提供的进行录入;\n 3、【判断题】在选项A中可填写√ 或正确选项B中填写× 或错误答案填写A或B\n 4、【标签】多个标签项使用空格隔开', B: '', C:'', D:'', E:'', F: '', G: '', H: '', I: '', J: '', K: ''}];
const data3 = [{A: '序号', B: '题干(必填)', C:'题型(必填)', D:'选项A必填', E:'选项B必填', F: '选项C', G: '选项D', H: '选项E', I: '选项F', J: '正确答案(必填)', K: '标签'}]; const data3 = [{A: '序号', B: '题干(必填)', C:'题型(必填)', D:'选项A必填', E:'选项B必填', F: '选项C', G: '选项D', H: '选项E', I: '选项F', J: '正确答案(必填)', K: '标签'}];
const data = [...data1, ...data2, ...data3]; const data = [...data1, ...data2, ...data3];
const mapType = { const mapType = {
@ -421,7 +469,7 @@ export default {
H: item.optionList[4] ? item.optionList[4].content : '', H: item.optionList[4] ? item.optionList[4].content : '',
I: item.optionList[5] ? item.optionList[5].content : '', I: item.optionList[5] ? item.optionList[5].content : '',
J: as, J: as,
K: item.tags K: item.tags ? item.tags.replace(',', ' ') : ''
}; };
data.push(obj); data.push(obj);
}); });