const batchInsert = { template: `

1、题目的识别基于标题前的题型说明,放在标题的前面,格式为“[题型]标题内容((描述))“
2、题干与选项,及各选项之间需回车换行

清空文本
{{errorMsg}}
{{index+1}}、{{issue.iname}}
{{wor.wtxt}}
{{wor.wtxt}}
`, data() { return { visible: false, txt: null, errorMsg: null } }, computed: { issues() { if (!this.txt) { return [] } let errorMsg = '' // 使用正则表达式分割文本为单独的问题 // const questionTexts = this.txt.split('\n').map(line => line.trim()).join(); // 每个问题之间用换行符分隔 const questionTexts = this.txt.split('\n').map(v => v.trim()).join('\n').split("\n\n") // 初始化问题数组 const questions = []; // 遍历每个问题文本 questionTexts.forEach((questionText, index) => { debugger // 提取问题编号和题干 const match = questionText.trim().split('\n').slice(0, 1).join('').match(/^(?:\d+\.|\d+、)?\s*(.*?)\s*\[(.*题)\]\s*(?:\((\d+)分\)|\((\d+)分\))?$/); if (match) { const questionContent = match[1].trim(); const questionType = match[2]; const scoreChinese = match[3]; const scoreEnglish = match[4]; const score = scoreChinese || scoreEnglish; // 题型合法性校验 const validTypes = ['多选题', '单选题']; if (!validTypes.includes(questionType)) { console.warn('问题' + (index + 1) + '的题型不合法: ' + questionType); errorMsg += '\n问题' + (index + 1) + '的题型不合法: ' + questionType; return; } // 分数格式校验 if (score) { const scoreNumber = parseInt(score, 10); if (isNaN(scoreNumber)) { console.warn('问题' + (index + 1) + '的分数格式无效: ' + score); errorMsg += '\n问题' + (index + 1) + '的分数格式无效: ' + score; return; } } // 提取选项 const optionTexts = questionText.replaceAll(match.input, "").split('\n').slice(1).filter(line => line.trim()); // 假设选项从第二行开始 const options = []; optionTexts.forEach(optionText => { // 使用正则表达式提取选项名称和是否正确 const optionMatch = optionText.match(/^([A-Z])\.?(.+?)\((正确答案|错误答案)\)/); if (optionMatch) { const optionName = optionMatch[1] + optionMatch[2].trim(); const isCorrect = optionMatch[3] === '正确答案'; options.push({wtxt: optionName, correct: isCorrect}); } else { options.push({wtxt: optionText.trim(), correct: false}); } }); // 构建问题对象 const question = { //是否必答 iisbd: true, //单选还是多选 iisdx: questionType === '单选题', //是否随机选项??? iisrandom: false, //题干 iname: questionContent, //排列方式 没用 iplfs: 1, //提示 its: null, //题型 (1 选择题 2 填空题 3 判断题) itx: (questionType === '单选题' || questionType === '多选题') ? 1 : 3, //分数 fraction: score, //选项 wors: options }; // 添加到问题数组中 questions.push(question); } }); this.errorMsg = errorMsg console.log(questions) return questions } }, methods: { onOpen() { debugger this.txt = null this.visible = true }, async confirm() { if (this.errorMsg) { this.$message.warning('存在不合法的题目,请调整后再提交') return } this.$emit('confirm', this.issues) this.visible = false }, } }