From 2798eecf453753e8fb3ef52c89d38b50a5801c68 Mon Sep 17 00:00:00 2001 From: "jialong.yu" Date: Thu, 30 Dec 2021 19:43:40 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=AF=8F=E4=B8=AA=E5=88=A4?= =?UTF-8?q?=E5=88=86=E8=A7=84=E5=88=99=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/TestPanel.vue | 122 ++++++++++++++++++++++------------- src/views/Home.vue | 15 +++-- 2 files changed, 89 insertions(+), 48 deletions(-) diff --git a/src/components/TestPanel.vue b/src/components/TestPanel.vue index 2abb299..9e2c3bf 100644 --- a/src/components/TestPanel.vue +++ b/src/components/TestPanel.vue @@ -173,8 +173,7 @@ export default { let cache = localStorage.getItem('codeCache') // 获取本地缓存 // 如果有缓存,再调接口取上次运行的代码 if (cache) { - cache = JSON.parse(cache) - this.getCache(cache.projectId, cache.judgmentId) + this.getCache(JSON.parse(cache)) } else { this.getCache() } @@ -278,56 +277,90 @@ export default { }).catch(res => {}) }, // 获取上次缓存记录 - getCache(pId, jId) { - const projectId = pId || this.projectId - const judgmentId = jId || this.judgmentId + getCache(cache) { + const pId = cache ? cache.projectId : '' + const projectId = Number(pId || this.projectId) const cid = this.courseId const assessmentId = this.assessmentId - this.$post(this.api.getLastCache, { - assessmentId: assessmentId ? Number(assessmentId) : '', - bcId: judgmentId, // 如果传进来了判分点id,说明本地有缓存,则直接取本地缓存的判分点id,否则,取第一个项目的第一个判分点id - projectId, // 项目id,同上 - cid // 课程id - }).then(res => { - this.closeLoad() - const code = res.getLastCache - // 如果有缓存代码,再提示用户是否要继续上次的实验 - if (code) { - this.$confirm('是否要继续上次的实验?', '提示', { - confirmButtonText: '是', - cancelButtonText: '否', - type: 'success' - }).then(() => { - localStorage.removeItem('codeCache') // 恢复代码后清除本地缓存 - this.projectId = projectId - this.judgmentId = judgmentId - // 如果是本地缓存里有项目id和判分点id,则要再次获取项目详情,取判分列表再次赋值;本地没有缓存则取只恢复第一个判分点的代码 - if (pId) { - this.getProDetail().then(() => { - const points = this.points - const item = points.find(e => e.judgmentId === judgmentId) - if (item) item.code = code - this.$emit('tell', projectId, points) - this.$emit('recoveryCode', points.findIndex(e => e.judgmentId === judgmentId) + '') // 切换为缓存的判分点,tab索引值要为字符串 - }).catch(res => {}) - } else { - const item = this.points.find(e => e.judgmentId === judgmentId) - if (item) item.code = code - this.$emit('tell', projectId, this.points) - this.$emit('recoveryCode') + let points = [] + if (pId) { + cache.judgmentIdList.map(e => { + points.push({ + judgmentId: e + }) + }) + } else { + // 深拷贝判分列表,下面接口获取到有缓存代码就直接存进去,如果用户选择了恢复代码,再用这个列表替换原来的判分列表 + points = JSON.parse(JSON.stringify(this.points)) + } + let newJudgmentId = '' // 要恢复到第一个有代码的判分规则,因为有代码的规则可能不是第一个,所以要判断如果这个为空,才把判分id赋给该变量 + const promiseList = [] // promise数组 + let hasCache = 0 // 是否有缓存 + points.map((e, i) => { + const judgmentId = e.judgmentId + promiseList.push(new Promise((resolve,reject) => { + this.$post(this.api.getLastCache, { + assessmentId: assessmentId ? Number(assessmentId) : '', + bcId: judgmentId, + projectId, // 项目id,同上 + cid // 课程id + }).then(res => { + this.closeLoad() + const code = res.getLastCache + // 如果有缓存代码 + if (code) { + hasCache = 1 + if (!newJudgmentId) newJudgmentId = i + e.code = code } - }).catch(() => { - localStorage.removeItem('codeCache') - this.$post(`${this.api.delCache}`, { + resolve() + }).catch(res => { + reject() + this.closeLoad() + }) + })) + }) + // 如果有缓存代码,再提示用户是否要继续上次的实验 + Promise.all(promiseList).then(_ => { + hasCache && this.$confirm('是否要继续上次的实验?', '提示', { + confirmButtonText: '是', + cancelButtonText: '否', + type: 'success' + }).then(() => { + localStorage.removeItem('codeCache') // 恢复代码后清除本地缓存 + this.projectId = projectId + // this.judgmentId = newJudgmentId + // 如果是本地缓存里有项目id,则要再次获取项目详情,取判分列表再次赋值;本地没有缓存则取只恢复第一个判分点的代码 + if (pId) { + this.getProDetail().then(() => { + this.points.map(e => { + const item = points.find(n => n.judgmentId === e.judgmentId) + if (item && item.code) this.$set(e, 'code', item.code) + }) + this.$emit('tell', projectId, this.points) + this.$emit('recoveryCode', newJudgmentId + '') // 切换为缓存的判分点,tab索引值要为字符串 + }).catch(res => {}) + } else { + this.points.map(e => { + const item = points.find(n => n.judgmentId === e.judgmentId) + if (item && item.code) this.$set(e, 'code', item.code) + }) + this.$emit('tell', projectId, this.points) + this.$emit('recoveryCode') + } + }).catch(() => { + // 选择了不继续上次的实验,则清除本地缓存,并删除服务器里缓存的代码 + localStorage.removeItem('codeCache') + // 删除该项目下所有判分规则的缓存代码 + points.map(e => { + e.code && this.$post(`${this.api.delCache}`, { assessmentId, - bcId: judgmentId, + bcId: e.judgmentId, projectId, cid }).then(res => {}).catch(() => {}) }) - } - }).catch(res => { - this.closeLoad() + }) }) }, // 定时查询考核状态(只有考核才需要定时查,查到考核如果结束后,直接提交考核) @@ -340,6 +373,7 @@ export default { // 如果考核已结束,则清除查询考核状态的定时器,并且自动提交 if (done) { clearInterval(this.statusTimer) + this.$message.success('考核时间已到,系统已自动交卷') this.submit() } }).catch(res => {}) diff --git a/src/views/Home.vue b/src/views/Home.vue index 72058ca..0b0302a 100644 --- a/src/views/Home.vue +++ b/src/views/Home.vue @@ -13,14 +13,15 @@
- + e.code)){ const cache = { - projectId: this.projectId, - judgmentId: list[Number(this.curTab)].judgmentId + projectId: Number(this.projectId), + judgmentIdList: this.workbench.map(e => e.judgmentId) } localStorage.setItem('codeCache', JSON.stringify(cache)) // 在本地缓存里保存项目id和判分点id,下次进来通过这两个id去查找缓存代码。之所以不在前端缓存代码是预防更换了浏览器或电脑后依然可以找回缓存代码,只不过更换了客户端后本地缓存就消失了,这种情况就做一个向下处理,就切换项目的时候去恢复代码 list.map(e => { @@ -119,6 +120,12 @@ export default { } },1000) }, + // 判分规则切换 + judChange() { + const code = this.$refs['code' + this.curTab][0].$refs.codemirror.codemirror + code.focus() + code.setCursor(code.lineCount(), 0) + }, // 重置编辑器 recoveryCode(curTab = '0'){ this.curTab = curTab