diff --git a/src/components/TestPanel.vue b/src/components/TestPanel.vue index e36aef3..73ce352 100644 --- a/src/components/TestPanel.vue +++ b/src/components/TestPanel.vue @@ -6,7 +6,7 @@

实训项目

重新开始 - 提交 + 提交
@@ -96,7 +96,7 @@ - +
@@ -105,13 +105,13 @@ -
+
- +
@@ -139,15 +139,23 @@ import util from '@/util' export default { data() { return { + userId: util.getCookie("userId"), + studentId: util.getCookie("studentId"), + courseId: util.getCookie("courseId"), + projectId: util.getCookie("projectId"), + assessmentId: util.getCookie("assessmentId"), + projectPermissions: 0, // 项目权限(0、练习 1、考核 2、竞赛) + isSubmit: false, + entryTime: util.formatDate('yyyy-MM-dd hh:mm:ss'), + startTime: util.getCookie("startTime") ? new Date(util.getCookie("startTime")).getTime() : '', + stopTime: util.getCookie("stopTime") ? new Date(util.getCookie("stopTime")).getTime() : '', test: [], ishow: true, grade: "00", exampleData: "", codeid: "", - codeIds: [], - judgmentPointsIds: [], text: "", - isStart: false, + isStart: false, // 是否开启倒计时的标识 globalTimer: null, //获取setInterval对象值 millisecond: 0, countVal: this.defaultVal, //获取初始值 @@ -156,10 +164,9 @@ export default { seconds: 0, minutes: 0, hour: 0, - createTime: "", //开始时间 - actEndTime: "", //倒计时结束时间 - value1: "", //选中后绑定的对象 - value: [], + createTime: '', // 开始时间 + actEndTime: '', // 倒计时结束时间 + projectList: [], experimentTarget: "", //实验目标 experimentDescription: "", //案例描述 experimentHint: "", //实验提示 @@ -167,21 +174,9 @@ export default { points: [], activeNames: [], tableData: [], - activeName: "first", - studentId: "", - courseId: "", - projectId: "", - assessmentId: "", - projectPermissions: 0, //项目权限(0、练习 1、考核 2、竞赛) - isSubmit: false, - entryTime: util.formatDate('yyyy-MM-dd hh:mm:ss'), - startTime: '', - stopTime: '', - isSelected: false, - userId: '', - workbenchCus: this.workbench, - isRecovery: false, - isstartexperimentSuggests: 1 + pannelTab: "first", + isSelected: false, // 是否选择过项目的标识,选择了会置为true + hintOpen: 1 // 是否显示实验提示 }; }, watch: { @@ -240,51 +235,23 @@ export default { } }, mounted() { - this.assessmentId = util.getCookie("assessmentId"); - this.userId = util.getCookie("userId"); - this.studentId = util.getCookie("studentId"); - this.projectId = util.getCookie("projectId"); - this.courseId = util.getCookie("courseId"); - this.startTime = util.getCookie("startTime") ? new Date(util.getCookie("startTime")).getTime() : '' - this.stopTime = util.getCookie("stopTime") ? new Date(util.getCookie("stopTime")).getTime() : '' - - this.projectPermissions = this.projectId ? 1 : 0 - if(this.projectId){ - this.getQueryProject(); - }else{ - console.log(33) - this.getProjects(); - let codeCache = localStorage.getItem('codeCache') - if(codeCache){ - codeCache = JSON.parse(codeCache) - // if(codeCache.systemId && codeCache.systemId == this.$config.systemId){ - // this.$confirm('是否要继续上次的实验?', '提示', { - // confirmButtonText: '是', - // cancelButtonText: '否', - // type: 'success' - // }).then(() => { - // this.value1 = codeCache.projectId - // this.workbenchCus = codeCache.workbench - // this.isRecovery = true - // this.selectProject() - // let codeIds = [] - // codeCache.workbench.map(n => codeIds.push(n.code.codeId)) - // codeIds = codeIds.filter(n => n) - // this.codeIds = codeIds - // this.$emit('recoveryCode',codeCache.workbench1) - // }).catch(() => { - // localStorage.removeItem('codeCache') - // }) - // } - } + this.projectPermissions = this.projectId ? 1 : 0 // 考核/练习 + if(this.projectId){ // 考核(考核才会从外面带进来projectId,练习是默认显示第一个项目) + this.getProDetail() + }else{ // 练习 + // 获取项目列表 + this.getList().then(() => { + let cache = localStorage.getItem('codeCache') + if(cache){ + cache = JSON.parse(cache) + this.getCache(cache.projectId, cache.judgmentId) + } + }).catch(res => {}) } - this.assessmentId && this.projectId && this.checkVer() - this.codeId && this.codeIds.push(this.codeId) //兄弟组件传值 newmain.$on("codeid", val => { this.codeid = val; let codeId = this.codeid; - this.codeIds.push(codeId); }); // 更改tableData newmain.$on("updateJud", item => { @@ -296,32 +263,44 @@ export default { }); }, methods: { - checkVer() { - let data = { - userId: this.userId, - id: this.assessmentId - } - this.$get(this.api.qualifications,data).then(res => { - if(res.message == 'false'){ - this.isSubmit = true; - newmain.$emit("isSubmit", this.isSubmit); + // 获取上次缓存记录 + getCache(projectId, judgmentId) { + this.$post(this.api.getLastCache, { + bcId: judgmentId ? judgmentId : this.judgmentId, + cid: this.courseId, + projectId: projectId ? projectId : this.projectId + }).then(res => { + const code = res.getLastCache + if (code) { + this.$confirm('是否要继续上次的实验?', '提示', { + confirmButtonText: '是', + cancelButtonText: '否', + type: 'success' + }).then(() => { + localStorage.removeItem('codeCache') + this.projectId = projectId + this.judgmentId = judgmentId + this.points[0].code = code + this.$emit("tell", this.projectId, this.projectPermissions, this.points) + // this.$emit('recoveryCode', cache.curTab) + }).catch(() => { + localStorage.removeItem('codeCache') + }) } - }); + }).catch(res => {}) }, //重新开始 reload() { - this.$post(this.api.DeleteCodes, this.codeIds).then(res => { + this.$post(this.api.DeleteCodes).then(res => { this.getClearTime() this.grade = '00' localStorage.removeItem('codeCache') - this.codeIds = [] this.isSubmit = false newmain.$emit("isSubmit", this.isSubmit) let workbench = this.workbench workbench.map(n => { n.code = { - code: '', - codeId: '' + code: '' } }) this.$emit('recoveryCode',workbench) @@ -334,29 +313,24 @@ export default { this.startCountFn() }); }, + // 提交 Submit() { - if(!this.codeIds.length) return this.$message.error('请先完成实验') + // if(!this.codeIds.length) return this.$message.error('请先完成实验') this.$confirm("此操作将视为结束考试, 是否继续?", "提示", { confirmButtonText: "确定", cancelButtonText: "取消", type: "warning", center: true - }) - .then(() => { + }).then(() => { this.actEndTime = new Date().getTime(); this.getClearTime(); //提交代码与判分点进行判断 this.$post(this.api.Submit, { - // projectPermissions: 0, createTime: this.startTime, endTime: this.stopTime, projectId: this.projectId, projectPermissions: this.projectPermissions, - // assessmentId: 1, assessmentId: this.assessmentId ? this.assessmentId : '', - codeIds: this.codeIds, - judgmentPointsIds: this.judgmentPointsIds, - // studentId: 54, studentId: this.studentId, record: { courseId: 1, @@ -394,20 +368,17 @@ export default { }); }) .catch(err => {}); - }) - .catch(() => { + }).catch(() => { this.$message({ type: "info", message: "已取消提交" }); }); }, - // 倒计时 timeFormat(param) { return param < 10 ? "0" + param : param; }, - //控制面板 step() { $(".m_step").toggleClass("n_step"); @@ -428,45 +399,26 @@ export default { // this.text = '所用' // } }, - - handleData(project,type){ - if(!this.isSelected){ - this.value = project; - this.value1 = project[0].projectId; - } - this.projectId = type == 2 ? project[0].projectId : this.projectId - if(type == 2) this.getQueryProject(0,1) - this.projectPermissions = type == 1 ? project[0].projectPermissions : this.projectPermissions - this.experimentTarget = project[0].experimentTarget; - this.experimentDescription = project[0].experimentDescription; - this.experimentHint = project[0].experimentHint; - this.actEndTime = project[0].endTime; - this.isRecovery = false - }, - getQueryProject(projectId,type) { - //项目名称+实验目标+案例描述+实验提示+实验要求 - this.$get(this.api.QueryProject, { - projectId: projectId ? projectId : this.projectId - }) - .then(res => { - if(!type){ - this.handleData(res.projectManage,1) - this.projectId = res.projectManage[0].projectId - } - // debugger - this.points = res.projectJudgmentVos; - this.$emit("tell", this.value1, this.projectPermissions, this.points); - // 切换实训项目-标签页回到第一个位置 - if (this.points.length) { - this.$parent.workbench1 = '0'; - } - - this.tableData = res.message.projectJudgmentVos; - let arr1 = this.tableData; - let result1 = arr1.map(e => e.judgmentPointsId); - this.judgmentPointsIds = this.judgmentPointsIds.concat(result1) - this.isstartexperimentSuggests = !res.message.project[0].isstartexperimentSuggests //0显示,1不显示 - + // 获取项目详情 + getProDetail() { + const projectId = this.projectId + return new Promise((resolve, reject) => { + this.$get(this.api.QueryProject, { + projectId + }).then(res => { + const points = res.projectJudgmentVos + const project = res.projectManage + points.map(e => { + e.code = '' + }) + this.points = points + this.judgmentId = points[0].judgmentId + this.experimentTarget = project.experimentTarget + this.experimentDescription = project.experimentDescription + this.experimentHint = project.experimentHint + // this.actEndTime = project[0].endTime + this.hintOpen = !res.projectManage.hintOpen // 0显示,1不显示 + this.$emit("tell", projectId, this.projectPermissions, this.points) if (this.projectPermissions == "1") { this.text = "剩余"; var interval = setInterval(() => { @@ -511,25 +463,41 @@ export default { this.startCountFn(); } } + resolve() + }).catch(err => { + reject() }) - .catch(err => {}); + }) }, - getProjects(){ + // 获取项目列表 + getList(){ let data = { - systemId: 1, - cId: this.courseId, - permissions: this.projectPermissions + systemId: 1, + cId: this.courseId, // 课程id + permissions: this.projectPermissions } - this.$get(`${this.api.queryTestProject}`,data).then(res => { - this.handleData(res.projects,2) - }).catch(res => {}); + return new Promise((resolve, reject) => { + this.$get(`${this.api.queryTestProject}`,data).then(res => { + const list = res.projects + this.projectList = list + this.projectId = list ? list[0].projectId : 0 + this.getProDetail().then(() => { + resolve() + }).catch(res => { + reject() + }) + }).catch(res => { + reject() + }) + }) }, + // 项目选择回调 selectProject(){ this.isSelected = true this.assessmentId = '' - this.judgmentPointsIds = [] - this.codeIds = [] - this.getQueryProject(this.value1) + // 切换实训项目-标签页回到第一个位置 + this.$parent.workbench1 = '0' + this.getProDetail() this.isSubmit = false this.countVal = 0 this.grade = 0 @@ -552,9 +520,9 @@ export default { this.minutes = minutes; this.seconds = seconds; }, - + // 倒计时 startCountFn() { - if (!this.isStart) { + if (!this.isStart) { // 未开始倒计时 this.countVal = this.countVal ? this.countVal : 0; let timer = setInterval(() => { this.counterFn(this.countVal++); diff --git a/src/components/codemirror.vue b/src/components/codemirror.vue index 72c4d11..59e2f75 100644 --- a/src/components/codemirror.vue +++ b/src/components/codemirror.vue @@ -2,24 +2,22 @@
运行
-

- +

@@ -27,17 +25,14 @@ 下载图片 下载图片
-
+
运行成功
-
+
- 第{{num}}行出现错误 - - 提示 - + 第{{errLine}}行出现错误 + 提示 +
@@ -90,37 +85,27 @@ import "codemirror/addon/fold/xml-fold.js"; // 编辑的主题文件 import "codemirror/theme/monokai.css"; import "codemirror/theme/base16-light.css"; -import * as $ from "jquery"; import { Loading } from 'element-ui'; import axios from 'axios'; import util from '@/util' -const CANCEL_TOKEN = axios.CancelToken +const CANCEL_TOKEN = axios.CancelToken // 用于input中中断请求 export default { - props: ["workbench1", "code", "codeid", "projectId"], + props: ["workbench1", "code", "projectId"], data() { return { - courseId: util.getCookie("courseId"), - datas: [], - ShowAssessmentId: false, - answer: "", - exampleData: this.code, - codeId: this.codeid, - codes: [], - submit: "", - modifys: "", //运行后返回的 - isError: 2, - timer: "", - after: "", - num: "", - userId: '', - studentId: "", - isSubmit: "", //提交按钮 - isAnswerTips: false, - error: false, - picVisible: false, - picSrc: '', - loadIns: null, - submiting: false, + assessmentId: util.getCookie("assessmentId"), // 考核id + courseId: util.getCookie("courseId"), // 课程id + showTips: false, // 显示隐藏提示按钮 + answer: '', // 正确答案 + codeVal: this.code, + runResult: '', // 运行结果 + isError: false, // 运行正确与否 + errLine: '', // 错误代码的位置 + isSubmit: false, // 是否提交了 + runEnable: false, // 是否禁用运行按钮 + tipsVisible: false, // 答案提示弹框显示标识 + picSrc: '', // 代码运行出来的图片url + loadIns: null, // loading实例 cmOption: { scrollbarStyle: "native", tabSize: 2, // tab @@ -149,41 +134,34 @@ export default { theme: "monokai", // 主题 extraKeys: { Ctrl: "autocomplete" } // 可以用于为编辑器指定额外的键绑定,以及keyMap定义的键绑定 }, - inputTextReg: /^((?!#).*?(,|\(|\[|\{|\s)+)?input(?!\w)\(['|"]([\s\S]+?)['|"]\)/m,// 匹配input() - requestList: [], - sourceCode: '', - requestTimer: null, - assessmentId: util.getCookie("assessmentId"), + inputTextReg: /^((?!#).*?(,|\(|\[|\{|\s)+)?input(?!\w)\(['|"]([\s\S]+?)['|"]\)/m, // 匹配input() + requestList: [], // 有input的情况下,保存每个axios的对象,用于中断请求 + sourceCode: '', // 把input替换成exit函数后的代码 + requestTimer: null // 用于中断请求的定时器 }; }, components: { codemirror }, watch: { - list() { - this.timer(); - }, - exampleData(val) { - this.$emit("update:code", val); - }, - codeId(val) { - this.$emit("update:codeid", val); + codeVal(val) { + this.$emit("update:code", val) } }, mounted() { - if (!this.assessmentId) { - this.ShowAssessmentId = true; - } + if (!this.assessmentId) this.showTips = true //兄弟组件传值 newmain.$on("isSubmit", isSubmit => { - this.isSubmit = isSubmit; - }); + this.isSubmit = isSubmit + }) }, methods: { - onCmReady3() { + // 页面加载完后重置编辑框大小 + ready() { this.$refs.myCmGenerate.codemirror.setSize("auto", "calc(100vh - 149px)"); }, + // 下载文件 downloadFile(fileName,url) { var x = new XMLHttpRequest() x.open("GET", url, true) @@ -203,11 +181,11 @@ export default { // 下面这个函数就是递归执行这个input输入过程的函数 confirmInput(msg){ let receiveResult = msg.replace('validing:','') - this.modifys += receiveResult + this.runResult += receiveResult this.$prompt(receiveResult, "提示", { confirmButtonText: "确定" }).then(({ value }) => { - this.modifys += `${value}
` + this.runResult += `${value}
` // 把exit函数替换成用户输入的值 this.sourceCode = this.sourceCode.replace(`exit('validing:${receiveResult.replace(/[\r\n]*/g,'')}')`,val => { return `'${value}'` @@ -222,8 +200,8 @@ export default { this.requestList.map(n => n('interrupt')) },1000) - axios.post(this.api.UpdateCode,{ - code: this.exampleData, + axios.post(this.api.runPythonCode,{ + code: this.codeVal, bcId: this.workbench1, cid: this.courseId, projectId: this.projectId @@ -236,19 +214,15 @@ export default { let result = res.message.result if(result.includes('File ')){ let modify = res.message.result - this.modifys = modify - this.after = modify.substring( - modify.indexOf("line") + 4, - modify.length - ); - this.num = parseInt(this.after) + 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 this.confirmInput(result) }else if(!res.message.isError){ this.isError = 0 - this.modifys += result + this.runResult += result newmain.$emit("updateJud",{ id: res.data.judgmentPointsId, isError: res.data.isError @@ -256,31 +230,30 @@ export default { } }).catch(e => { if(e && e.message == 'interrupt'){ - this.AnswerTips(true) + this.runCode(true) this.requestList = [] } }) }).catch(err => {}) }, - AnswerTips(isWhile) {// isWhile为true表示代码里有while循环,右边的运行结果需要拼接展示,而不是直接覆盖 - if (this.isSubmit == "") { - this.submit = 1; - if (this.codeid == "") { + runCode(isWhile) { // isWhile为true表示代码里有while循环,右边的运行结果需要拼接展示,而不是直接覆盖 + if (!this.isSubmit) { + if (!this.codeVal) { this.$message({ message: "警告哦,内容为空不可运行", type: "warning" - }); + }) } else { let inputTextReg = this.inputTextReg let inputFuncReg = /input\(['|"]/g // 该正则是验证代码里是否有input,如果有,就要另外做处理,而不是直接传给后端执行 - if (inputTextReg.test(this.exampleData)) { - let sourceCode = this.exampleData + if (inputTextReg.test(this.codeVal)) { + let sourceCode = this.codeVal sourceCode = sourceCode.replace(inputTextReg,val => { return val.replace(/\\n/g,"") }) - this.exampleData = sourceCode + this.codeVal = sourceCode // 把input函数替换成exit函数,加上"validing:"作为特定标识,好方便后面的识别 sourceCode = sourceCode.replace(inputFuncReg,val => { @@ -288,9 +261,9 @@ export default { }) this.sourceCode = sourceCode - if(!isWhile) this.modifys = '' - this.$post(this.api.UpdateCode, { - code: this.exampleData, + if(!isWhile) this.runResult = '' + this.$post(this.api.runPythonCode, { + code: this.codeVal, bcId: this.workbench1, cid: this.courseId, projectId: this.projectId @@ -299,16 +272,12 @@ export default { if(result.includes('File ')){ let modify = res.message.result if(isWhile){ - this.modifys += modify + this.runResult += modify }else{ - this.modifys = modify + this.runResult = modify } - this.after = modify.substring( - modify.indexOf("line") + 4, - modify.length - ); - this.num = parseInt(this.after) + this.errLine = parseInt(modify.substring(modify.indexOf("line") + 4, modify.length)) this.isError = res.message.isError }else if(result.includes('validing:')){ this.isError = 0 @@ -319,93 +288,65 @@ export default { this.loadIns = Loading.service({ background: 'transparent' }) - //实时更新编辑器代码(修改代码) - this.$post(this.api.UpdateCode, { - code: this.exampleData, + // 把代码传给后端,在后端运行Python代码 + this.$post(this.api.runPythonCode, { + code: this.codeVal, bcId: this.workbench1, cid: this.courseId, projectId: this.projectId - }) - .then(res => { - this.loadIns.close() - this.picSrc = '' - if(typeof res.message == 'string'){ - this.isError = res.message.isError; - this.modifys = '' - this.picSrc = `${res.message}?id=${new Date().getTime()}` - newmain.$emit("updateJud",{ - id: res.data.judgmentPointsId, - isError: res.data.isError - }) - }else{ - // 这段是为要下载图片的项目案例写的,后端会返回图片名称的数组,前端负责循环这个数组,然后下载下来 - // 只有该系统有这段代码,因为其他7个系统没有下载图片的项目,后续如果加了,直接把这段代码复制过去即可 - if(!res.message.isError){ - let result = '' - try { - result = eval(res.message.result) - } catch (error) {} - if(result instanceof Array && result.length && (result[0].includes('.jpg') || result[0].includes('.png') || result[0].includes('.gif'))){ - result.map((n,i) => { - this.downloadFile(`${i+1}.jpg`,n) - }) - this.isError = 0 - this.modifys = '下载完成' - }else{ - this.isError = res.message.isError; - newmain.$emit("updateJud",{ - id: res.data.judgmentPointsId, - isError: res.data.isError - }) - var modify = res.message.result; - this.modifys = modify; - this.after = modify.substring( - modify.indexOf("line") + 4, - modify.length - ); - this.num = parseInt(this.after); - } - }else{ - this.isError = res.message.isError; - newmain.$emit("updateJud",{ - id: res.data.judgmentPointsId, - isError: res.data.isError + }).then(res => { + const data = res.code + const photo = data.photoUrl + const result = data.runResult + this.loadIns.close() + this.picSrc = '' + if(photo){ + this.isError = 0 + this.runResult = '' + this.picSrc = `${photo}?id=${new Date().getTime()}` + }else{ + // 这段是为要下载图片的项目案例写的,后端会返回图片名称的数组,前端负责循环这个数组,然后下载下来 + // 只有该系统有这段代码,因为其他7个系统没有下载图片的项目,后续如果加了,直接把这段代码复制过去即可 + if(0){ + if(result instanceof Array && result.length && (result[0].includes('.jpg') || result[0].includes('.png') || result[0].includes('.gif'))){ + result.map((n,i) => { + this.downloadFile(`${i+1}.jpg`,n) }) - var modify = res.message.result; - this.modifys = modify; - this.after = modify.substring( - modify.indexOf("line") + 4, - modify.length - ); - this.num = parseInt(this.after); + this.isError = 0 + this.runResult = '下载完成' + }else{ + this.isError = 0 + this.runResult = data.runResult + this.errLine = parseInt(result.substring(modify.indexOf("line") + 4, modify.length)) } + } else { + this.isError = data.retResult + this.runResult = result + this.errLine = parseInt(result.substring(result.indexOf("line") + 4, result.length)) } - }) - .catch(res => { - res.status == 500 && this.$message.error('检测到代码里有非法代码,请检查是否有调用系统命令。') - this.loadIns.close() - }); - clearTimeout(this.timer); + } + }).catch(res => { + res.status == 500 && this.$message.error('检测到代码里有非法代码,请检查是否有调用系统命令。') + this.loadIns.close() + }) } } } else { - this.isAnswerTips = true; + this.runEnable = true this.$message({ message: "警告哦,已提交不可再运行", type: "warning" - }); + }) } }, - getQueryAnswer() { - this.error = true; + getTips() { + this.tipsVisible = true //提示答案 this.$get(this.api.QueryAnswer, { judgmentPointsId: this.workbench1 - }) - .then(res => { - this.answer = res.message; - }) - .catch(err => {}); + }).then(res => { + this.answer = res.message + }).catch(err => {}) } } }; diff --git a/src/utils/api.js b/src/utils/api.js index 6095ba7..27acd03 100644 --- a/src/utils/api.js +++ b/src/utils/api.js @@ -2,15 +2,12 @@ import config from '@/config' let host = `${config.host}` export default { - QueryProject: `${host}occupationlab/projectManage/getProjectDetail`, //项目选择下拉框+项目信息+判分点信息展示 - ProjectId: `${host}/python/projectId`, //判分点下拉框信息展示 - Submit: `${host}/python/submit`, //提交代码与判分点进行判断 - UpdateCode: `${host}python/python/runPythonCode`, //实时更新编辑器代码(修改代码) - QueryAnswer: `${host}/python/queryAnswer`, //提示答案 - DeleteCodes: `${host}/python/deleteCodes`, //重新开始 - QueryAllProject: `${host}/python/queryAllProject`, //项目选择下拉框 - ChangeCode: `${host}/python/changeCode`, //input交互 - queryTestProject: `${host}occupationlab/projectManage/getProjectBySystemId`, //input交互 - qualifications: `${host}/python/qualifications`, //input交互 - saveCache: `${host}python/python/saveCache` //中途退出实验并保存实验数据(虚拟仿真实验) + QueryProject: `${host}occupationlab/projectManage/getProjectDetail`, + Submit: `${host}/python/submit`, + runPythonCode: `${host}python/python/runPythonCode`, + QueryAnswer: `${host}/python/queryAnswer`, + DeleteCodes: `${host}/python/deleteCodes`, + queryTestProject: `${host}occupationlab/projectManage/getProjectBySystemId`, + saveCache: `${host}python/python/saveCache`, + getLastCache: `${host}python/python/getLastCache` } \ No newline at end of file diff --git a/src/views/Home.vue b/src/views/Home.vue index 85d6cdb..03dd055 100644 --- a/src/views/Home.vue +++ b/src/views/Home.vue @@ -11,11 +11,11 @@

{{$config.title}}

编程语言

- +
- + { + this.leavePage() } }, - destroyed() { - window.removeEventListener("popstate", this.goBack, false); - }, methods: { + // 页面离开的时候保存未提交的代码(只有练习和未提交状态下才会调用该接口) leavePage(){ - if(!this.$refs.mainindex.isSubmit && !this.assessmentId && this.workbench.length){ - let data = { - code: '', - bcId: this.workbench1, - cid: this.courseId, - projectId: this.projectId + const list = this.workbench + if(!this.$refs.mainindex.isSubmit && !this.assessmentId && list.length){ + if(list.some(e => e.code)){ + const cache = { + projectId: this.projectId, + judgmentId: list[this.curTab].judgmentId + } + localStorage.setItem('codeCache', JSON.stringify(cache)) + list.map(e => { + if (e.code) { + let data = { + code: e.code, + bcId: e.judgmentId, + cid: this.courseId, + projectId: this.projectId + } + this.$post(this.api.saveCache,data).then(res => {}).catch(e => {}) + } + }) } - this.$post(this.api.saveCache,data).then(res => {}).catch(e => {}) } }, + // 自动退出 autoLogout(){ let lastTime = new Date().getTime() let logout = false + // 页面点击后赋值当前时间 document.onmousedown = () => { lastTime = new Date().getTime() } @@ -138,23 +128,19 @@ export default { } },1000) }, - goBack() { - this.leavePage() - history.back() - }, getDataFromChild(data) { sessionStorage.setItem("timer", parseInt(data)); }, recoveryCode(workbench){ this.workbench = workbench this.codeKey++ - this.workbench1 = '0' + this.curTab = '0' }, reload(){ this.$refs.mainindex.reload() }, + // 退出实验 back() { - this.leavePage() if(this.projectPermissions){ // 返回到考核列表 if (this.$config.isBeta) { // 判断是否是职站测试服 @@ -173,9 +159,11 @@ export default { //工作台展示 getQueryIndex(value1, projectPermissions,workBench) { //父子组件传值 - this.projectId = value1; - this.projectPermissions = projectPermissions; + console.log(5555,workBench) + this.projectId = value1 + this.projectPermissions = projectPermissions this.workbench = workBench + this.codeKey++ }, } };