From b810de42baf20ea7f116c876991a8af2abed1d9d Mon Sep 17 00:00:00 2001 From: yujialong <479214531@qq.com> Date: Mon, 9 Aug 2021 14:49:00 +0800 Subject: [PATCH] add comment --- src/components/codemirror.vue | 72 +++++++++++++++++++++-------------- src/utils/api.js | 2 +- 2 files changed, 45 insertions(+), 29 deletions(-) diff --git a/src/components/codemirror.vue b/src/components/codemirror.vue index 5946dc0..3270124 100644 --- a/src/components/codemirror.vue +++ b/src/components/codemirror.vue @@ -150,12 +150,10 @@ export default { theme: "monokai", // 主题 extraKeys: { Ctrl: "autocomplete" } // 可以用于为编辑器指定额外的键绑定,以及keyMap定义的键绑定 }, - inputTextReg: /^((?!#).*?(,|\(|\[|\{|\s)+)?input(?!\w)\(['|"]([\s\S]+?)['|"]\)/mg, - inputReg: /^((?!#).*?(,|\(|\[|\{|\s)+)?(input(?!\w)\(['|"][\s\S]*?['|"]\))/m, - exitTextReg: /exit(?!\w)\(['|"]([\s\S]+?)['|"]\)/m, - exitNotValidReg: /exit(?!\w)\(['|"]([\s\S]+?)['|"]\)/m, + inputTextReg: /^((?!#).*?(,|\(|\[|\{|\s)+)?input(?!\w)\(['|"]([\s\S]+?)['|"]\)/m,// 匹配input() requestList: [], - sourceCode: '' + sourceCode: '', + requestTimer: null }; }, components: { @@ -220,11 +218,9 @@ export default { this.$post(this.api.AddCode, { code: this.exampleData, projectId: this.projectId, - // projectId: 305, judgmentPointsId: this.workbench1, userId: parseInt(this.userId), studentId: this.studentId, - // studentId: 54, submit: this.submit }) .then(res => { @@ -253,28 +249,41 @@ export default { } x.send() }, + // python代码里如果有input函数的话,是做了单独的处理的,原理是先把所有input函数都替换成exit函数,再在exit函数里加上特定标识,再通过接口传给后端去执行 + // 因为exit函数是跟input有类似的效果,就是都会产生阻塞,所以python引擎一旦遇到exit,进程就会被停止,然后返回exit函数里面的值,而这个值,就是上面说的特定标识加上原本这个input函数里的值 + // 然后就可以通过这个返回的值来提示给用户,让用户继续输入 + // 下面这个函数就是递归执行这个input输入过程的函数 confirmInput(msg){ let receiveResult = msg.replace('validing:','') - // console.log('进入函数:',receiveResult) this.modifys += receiveResult this.$prompt(receiveResult, "提示", { confirmButtonText: "确定" }).then(({ value }) => { this.modifys += `${value}
` - console.log('替换前:',this.sourceCode,`exit('validing:${receiveResult.replace(/[\r\n]*/g,'')}')`) + // 把exit函数替换成用户输入的值 this.sourceCode = this.sourceCode.replace(`exit('validing:${receiveResult.replace(/[\r\n]*/g,'')}')`,val => { return `'${value}'` }) this.sourceCode = this.sourceCode.replace(`exit("validing:${receiveResult.replace(/[\r\n]*/g,'')}")`,val => { return `'${value}'` }) - console.log('替换后:',this.sourceCode) + clearTimeout(this.requestTimer) + + // 一秒钟后还没请求成功的话,就直接中断请求,因为一般是陷入死循环了才会需要这么长时间的,中断了后就再次执行AnswerTips(运行按钮的执行函数)函数,再次进入循环 + this.requestTimer = setTimeout(() => { + this.requestList.map(n => n('interrupt')) + },1000) - this.$post(this.api.UpdateCode,{ + axios.post(this.api.UpdateCode,{ code: this.sourceCode, codeId: this.codeId, submit: this.submit - }).then(res => { + },{ + 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 @@ -296,18 +305,16 @@ export default { isError: res.data.isError }) } - }).catch(err => {}) + }).catch(e => { + if(e && e.message == 'interrupt'){ + this.AnswerTips(true) + this.requestList = [] + } + }) + }).catch(err => {}) }, - AnswerTips() { - // let inputTextReg = this.inputTextReg - // let str = `server = int(input('您好!请问您需要什么帮助呢?1 存取款;2 货币兑换;3 咨询\n')` - // str = str.replace(inputTextReg,val => { - // console.log(val.replace(/\n/g,'')) - // return val.replace(/\n/g,'') - // }) - // console.log(str) - // return + AnswerTips(isWhile) {// isWhile为true表示代码里有while循环,右边的运行结果需要拼接展示,而不是直接覆盖 if (this.isSubmit == "") { this.submit = 1; if (this.codeid == "") { @@ -318,19 +325,21 @@ export default { } else { let inputTextReg = this.inputTextReg let inputFuncReg = /input\(['|"]/g + // 该正则是验证代码里是否有input,如果有,就要另外做处理,而不是直接传给后端执行 if (inputTextReg.test(this.exampleData)) { let sourceCode = this.exampleData - // sourceCode = sourceCode.replace(inputTextReg,val => { - // return console.log(val.replace(/\n'/g,"'")) - // }) - // return console.log(11,sourceCode) + sourceCode = sourceCode.replace(inputTextReg,val => { + return val.replace(/\\n/g,"") + }) + this.exampleData = sourceCode + // 把input函数替换成exit函数,加上"validing:"作为特定标识,好方便后面的识别 sourceCode = sourceCode.replace(inputFuncReg,val => { return `exit(${val[val.length - 1]}validing:` }) this.sourceCode = sourceCode - this.modifys = '' + if(!isWhile) this.modifys = '' this.$post(this.api.UpdateCode, { code: sourceCode, codeId: this.codeId, @@ -339,7 +348,12 @@ export default { let result = res.message.result if(result.includes('File ')){ let modify = res.message.result - this.modifys = modify + if(isWhile){ + this.modifys += modify + }else{ + this.modifys = modify + } + this.after = modify.substring( modify.indexOf("line") + 4, modify.length @@ -373,6 +387,8 @@ export default { isError: res.data.isError }) }else{ + // 这段是为要下载图片的项目案例写的,后端会返回图片名称的数组,前端负责循环这个数组,然后下载下来 + // 只有该系统有这段代码,因为其他7个系统没有下载图片的项目,后续如果加了,直接把这段代码复制过去即可 if(!res.message.isError){ let result = '' try { diff --git a/src/utils/api.js b/src/utils/api.js index d55913c..c4acf3c 100644 --- a/src/utils/api.js +++ b/src/utils/api.js @@ -1,6 +1,6 @@ import config from '@/config' // let host = 'http://192.168.31.125:8081/python'//林 -// let host = 'http://192.168.31.151:8081/python'//榕 +// let host = 'http://192.168.31.216:8081/python'//榕 let host = `${config.host}python` export default {