From b367a5cb93730a524f6dadfb15d8e7a81a9f6896 Mon Sep 17 00:00:00 2001 From: "jialong.yu" Date: Tue, 21 Dec 2021 21:10:45 +0800 Subject: [PATCH] =?UTF-8?q?input=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/index.js | 2 +- src/components/TestPanel.vue | 2 +- src/components/codemirror.vue | 93 +++++++++++++++++++---------------- src/config/index.js | 2 +- 4 files changed, 53 insertions(+), 46 deletions(-) diff --git a/src/api/index.js b/src/api/index.js index f4b2414..ecf87fe 100644 --- a/src/api/index.js +++ b/src/api/index.js @@ -1,5 +1,5 @@ export default { - QueryProject: 'occupationlab/projectManage/getProjectDetail', + getProjectDetail: 'occupationlab/projectManage/getProjectDetail', submit: 'python/python/submit', runPythonCode: 'python/python/runPythonCode', queryBcJudgmentByBcId: 'judgment/bcJudgmentPoint/queryBcJudgmentByBcId', diff --git a/src/components/TestPanel.vue b/src/components/TestPanel.vue index f7fe41d..8de7e21 100644 --- a/src/components/TestPanel.vue +++ b/src/components/TestPanel.vue @@ -219,7 +219,7 @@ export default { getProDetail() { const projectId = this.projectId return new Promise((resolve, reject) => { - this.$get(this.api.QueryProject, { + this.$get(this.api.getProjectDetail, { projectId }).then(res => { const points = res.projectJudgmentVos diff --git a/src/components/codemirror.vue b/src/components/codemirror.vue index 2470a67..bb4f4d8 100644 --- a/src/components/codemirror.vue +++ b/src/components/codemirror.vue @@ -11,7 +11,7 @@
运行 @@ -88,12 +88,14 @@ import "codemirror/theme/base16-light.css"; import { Loading } from 'element-ui'; import axios from 'axios'; import util from '@/util' +import config from '@/config' const CANCEL_TOKEN = axios.CancelToken // 用于input中中断请求 export default { props: ['workbench1', 'code', 'codeId', 'projectId'], data() { return { - assessmentId: util.getCookie(';assessmentId'), // 考核id + token: util.getCookie('admin-token'), + assessmentId: util.getCookie('assessmentId'), // 考核id courseId: util.getCookie('courseId'), // 课程id showTips: false, // 显示隐藏提示按钮 answer: '', // 正确答案 @@ -168,17 +170,17 @@ export default { * 下面这个函数就是递归执行这个input输入过程的函数 */ confirmInput(msg){ - let receiveResult = msg.replace('validing:','') + const receiveResult = msg.replace('validing:', '') this.runResult += receiveResult - this.$prompt(receiveResult, "提示", { + this.$prompt(receiveResult, '提示', { confirmButtonText: "确定" }).then(({ value }) => { this.runResult += `${value}
` // 把exit函数替换成用户输入的值 - this.sourceCode = this.sourceCode.replace(`exit('validing:${receiveResult.replace(/[\r\n]*/g,'')}')`,val => { + this.sourceCode = this.sourceCode.replace(`exit('validing:${receiveResult.replace(/[\r\n]*/g,'')}')`, _ => { return `'${value}'` }) - this.sourceCode = this.sourceCode.replace(`exit("validing:${receiveResult.replace(/[\r\n]*/g,'')}")`,val => { + this.sourceCode = this.sourceCode.replace(`exit("validing:${receiveResult.replace(/[\r\n]*/g,'')}")`, _ => { return `'${value}'` }) clearTimeout(this.requestTimer) @@ -186,30 +188,33 @@ export default { // 一秒钟后还没请求成功的话,就直接中断请求,因为一般是陷入死循环了才会需要这么长时间的,中断了后就再次执行AnswerTips(运行按钮的执行函数)函数,再次进入循环 this.requestTimer = setTimeout(() => { this.requestList.map(n => n('interrupt')) - },1000) + }, 1000) - axios.post(this.api.runPythonCode,{ - code: this.codeVal, + axios.post(config.host + this.api.runPythonCode, { + code: this.sourceCode, bcId: this.workbench1, cid: this.courseId, projectId: this.projectId - },{ + }, { + headers: { + token: this.token + }, cancelToken: new CANCEL_TOKEN(c => { //强行中断请求要用到的,记录请求信息 this.requestList.push(c) }) }).then(response => { let res = response.data - let result = res.message.result - if(result.includes('File ')){ - let modify = res.message.result - this.runResult = modify - this.errLine = parseInt(modify.substring(modify.indexOf("line") + 4, modify.length)) - this.isError = res.message.isError - }else if(result.includes('validing:')){ - this.isError = 0 + const data = res.code + const result = data.runResult + if (result.includes('File ')) { + this.runResult = result + this.errLine = parseInt(result.substring(result.indexOf('line') + 4, result.length)) + this.isError = data.retResult + } else if (result.includes('validing:')) { + this.isError = 1 this.confirmInput(result) - }else if(!res.message.isError){ - this.isError = 0 + } else if(data.retResult) { + this.isError = 1 this.runResult += result } }).catch(e => { @@ -231,41 +236,43 @@ export default { type: "warning" }) } else { - let inputTextReg = this.inputTextReg - let inputFuncReg = /input\(['|"]/g + const inputTextReg = this.inputTextReg + const inputFuncReg = /input\(['|"]/g + const bcId = this.workbench1 + const cid = this.courseId + const projectId = this.projectId // 该正则是验证代码里是否有input,如果有,就要另外做处理,而不是直接传给后端执行 if (inputTextReg.test(code)) { - let sourceCode = this.codeVal - sourceCode = sourceCode.replace(inputTextReg,val => { + code = code.replace(inputTextReg, val => { return val.replace(/\\n/g,"") }) - this.codeVal = sourceCode + this.codeVal = code // 把input函数替换成exit函数,加上"validing:"作为特定标识,好方便后面的识别 - sourceCode = sourceCode.replace(inputFuncReg,val => { + code = code.replace(inputFuncReg,val => { return `exit(${val[val.length - 1]}validing:` }) - this.sourceCode = sourceCode + this.sourceCode = code if(!isWhile) this.runResult = '' this.$post(this.api.runPythonCode, { - code: this.codeVal, - bcId: this.workbench1, - cid: this.courseId, - projectId: this.projectId + code, + bcId, + cid, + projectId }).then(res => { - let result = res.message.result + const data = res.code + const result = data.runResult if(result.includes('File ')){ - let modify = res.message.result - if(isWhile){ - this.runResult += modify - }else{ - this.runResult = modify + if (isWhile) { + this.runResult += result + } else { + this.runResult = result } - this.errLine = parseInt(modify.substring(modify.indexOf("line") + 4, modify.length)) - this.isError = res.message.isError + this.errLine = parseInt(result.substring(result.indexOf('line') + 4, result.length)) + this.isError = data.retResult }else if(result.includes('validing:')){ - this.isError = 0 + this.isError = 1 this.confirmInput(result) } }).catch(err => {}) @@ -280,9 +287,9 @@ export default { // 把代码传给后端,在后端运行Python代码 this.$post(this.api.runPythonCode, { code, - bcId: this.workbench1, - cid: this.courseId, - projectId: this.projectId + bcId, + cid, + projectId }).then(res => { const data = res.code const photo = data.photoUrl diff --git a/src/config/index.js b/src/config/index.js index c11f8f4..7ce86c6 100644 --- a/src/config/index.js +++ b/src/config/index.js @@ -3,7 +3,7 @@ const isBeta = process.env.NODE_ENV === 'development' || location.host.includes( let host = '' if(isBeta) { - host = 'http://39.108.250.202:9000' + host = 'http://39.108.250.202:9000/' // host = 'http://192.168.31.151:9000' } else { host = location.origin + '/'