Skip to content

Commit

Permalink
Merge pull request #917 from pxgo/master
Browse files Browse the repository at this point in the history
优化
  • Loading branch information
pxgo authored Sep 7, 2023
2 parents 7208ba0 + bddc2c6 commit 130832a
Show file tree
Hide file tree
Showing 20 changed files with 596 additions and 374 deletions.
2 changes: 1 addition & 1 deletion languages/zh_cn/response.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"DUPLICATE_QUESTIONS_IN_SELECTED_BANKS": "(题库{v1})与其他所选题库存在重复,导致题库数量不足",
"INVALID_EXAM_PAPER_ID": "无效的PaperID(id={v1})",
"INVALID_ACTIVATION_CODE_ID": "无效的注册码(code={v1})",
"QUESTION_DOES_NOT_EXIST": "试题不存在",
"INVALID_IP": "无效IP",
"EXAM_HAS_ENDED": "考试已结束",
"FORBIDDEN_REGISTER_EXAM_CHECK_RATE_LIMIT": "你访问得太快了,请稍后再试",
"EXAM_CONCLUDED_REGISTER_NOW": "考试已通过请前往注册",
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions pages/exam/takeExam.pug → pages/exam/publicPaper.pug
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extends ../bootstrap_base
block title
title=`开卷考试`
+includeCSS('/exam/takeExam.css')
+includeCSS('/exam/publicPaper.css')
block content
.container-fluid.max-width.box-shadow-panel
div
Expand All @@ -10,4 +10,4 @@ block content
.exam-container(id='take-exam' v-cloak)
Public-Paper(pid=data.pid)
block scripts
+includeJS('/exam/takeExam.js')
+includeJS('/exam/publicPaper.js')
2 changes: 1 addition & 1 deletion pages/experimental/settings/register/register.pug
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ block eContent
.form-group
h5(style="font-weight: bold;") 已选试卷
div(v-if='regSettings.examSource.length === 0')
h5.text-danger 暂未选择任何专业领域
h5.text-danger 暂未选择任何试卷
div(v-else)
table.table.table-bordered
thead
Expand Down
25 changes: 20 additions & 5 deletions pages/lib/vue/LoginCore.vue
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,27 @@ export default {
nationCode: nationCode,
mobile: mobile,
};
const url = t === 'register'? "/sendMessage/register": "/sendMessage/login";
const isRegister = t === 'register';
const url = isRegister? "/sendMessage/register": "/sendMessage/login";
this.$refs.verifications
.open()
.then(function(data) {
body.verifySecret = data.secret;
return Promise.resolve()
.then(() => {
if(isRegister) {
const registerActivationCode = getRegisterActivationCodeFromLocalstorage();
body.registerCode = registerActivationCode;
return nkcAPI(`/api/v1/register/exam?code=${registerActivationCode}`, 'GET')
.then(res => {
if(!res.data.isExamEnabled || !res.data.isValidCode) {
return this.$refs.verifications
.open()
.then((res) => {
body.verifySecret = res.secret;
})
}
})
}
})
.then(() => {
return nkcAPI(url, "POST", body);
})
.then(function() {
Expand Down
67 changes: 55 additions & 12 deletions pages/lib/vue/PublicPaper.vue
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,13 @@
</style>

<script>
import Vue from 'vue';
import { nkcAPI } from '../js/netAPI.js';
import { sweetError ,sweetSuccess} from '../js/sweetAlert.js'
import {getUrl} from "../js/tools";
import {detailedTime} from '../js/time'
import {setRegisterActivationCodeToLocalstorage} from '../js/activationCode.js'
import { renderFormula } from "../js/formula";
import Vue from "vue";
import { nkcAPI } from "../js/netAPI.js";
import { sweetError } from "../js/sweetAlert.js";
import { getUrl } from "../js/tools";
import { detailedTime } from "../js/time";
import { setRegisterActivationCodeToLocalstorage } from "../js/activationCode.js";
import { renderFormula } from "../js/formula";
import { visitUrl } from "../js/pageSwitch";
Vue.component('question-text-content', {
Expand All @@ -273,7 +273,7 @@ Vue.component('question-text-content', {
watch: {
text() {
this.key = (Math.random() * 10000).toString();
}
},
},
template: `<span :key="key">{{text}}</span>`
})
Expand All @@ -298,6 +298,7 @@ export default Vue.extend({
paperName: '',
isCorrect:null,
isShowReminder:false,
url: window.location.href
};
},
props:['pid'],
Expand Down Expand Up @@ -330,6 +331,14 @@ export default Vue.extend({
}
}
},
created() {
window.addEventListener('popstate', this.updateUrl);
window.addEventListener('beforeunload', this.handleBeforeUnload);
},
beforeDestroy() {
window.removeEventListener('popstate', this.updateUrl);
window.removeEventListener('beforeunload', this.handleBeforeUnload);
},
methods: {
getUrl,
detailedTime,
Expand Down Expand Up @@ -363,10 +372,10 @@ export default Vue.extend({
if(type === 'ch4'){
if(this.question.isMultiple){
this.selected = question.answer
.map((item, index) => (item.selected ? index : undefined))
.map((item, index) => (item.correct ? index : undefined))
.filter(index => index !== undefined);
}else {
this.selected = question.answer.findIndex(item => item.selected)
this.selected = question.answer.findIndex(item => item.correct)
}
this.answerDesc = this.question.answer.map((item) => {
const { desc, _id } = item;
Expand Down Expand Up @@ -496,8 +505,14 @@ export default Vue.extend({
this.isShowReminder = false;
if(this.index <= this.questionTotal - 1){
this.index +=1
const currentUrl = window.location.pathname
const newUrl = currentUrl + `#question${this.index}`;
history.pushState({index:this.index}, '', newUrl);
this.getInit('down',this.index);
}
else {
sweetError('index混乱')
}
this.getInit('down',this.index);
},
//上一题
pre(){
Expand All @@ -510,7 +525,17 @@ export default Vue.extend({
this.isCorrect = null;
this.isReselected = false;
this.isShowReminder = false;
this.getInit('up',this.index -1);
if(this.index >=1){
this.index -=1
const currentUrl = window.location.pathname
const newUrl = currentUrl + `#question${this.index}`;
history.pushState({index:this.index}, '', newUrl);
this.getInit('up',this.index);
}
else {
sweetError('index混乱')
}
},
//完成
finish(){
Expand All @@ -530,6 +555,24 @@ export default Vue.extend({
}
});
}
},
updateUrl(e) {
this.answerDesc = [];
this.selected = [];
this.fill = '';
this.isCorrect = null;
this.isReselected = false;
this.isShowReminder = false;
if(!e.state){
this.getInit('up',0);
}else {
this.getInit('down',e.state.index)
}
},
handleBeforeUnload(event) {
const confirmationMessage = '确定要离开页面吗?';
event.returnValue = confirmationMessage;
return confirmationMessage;
}
},
})
Expand Down
6 changes: 4 additions & 2 deletions routes/api/v1/exam/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ router
params: { pid },
db,
query,
state: { uid },
} = ctx;
const ip = ctx.address;
await paperService.checkPaperLegal(pid, ip);
await paperService.checkPaperLegal(pid, ip, uid);
const paper = await db.ExamsPaperModel.findOnly({ _id: pid, ip });
const hasFinish = await paperService.checkIsFinishPaper(pid);
let index = 0;
Expand Down Expand Up @@ -84,10 +85,11 @@ router
body,
params: { pid },
db,
state: { uid },
} = ctx;
const { index, qid, selected, fill } = body;
const ip = ctx.address;
await paperService.checkPaperLegal(pid, ip);
await paperService.checkPaperLegal(pid, ip, uid);
const paper = await db.ExamsPaperModel.findOnly({ _id: pid, ip });
const { ch4, ans } = await db.QuestionModel.getQuestionType();
const { record } = paper;
Expand Down
27 changes: 7 additions & 20 deletions routes/api/v1/register/index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@
const Router = require('koa-router');
const {
activationCodeService,
} = require('../../../../services/activationCode/activationCode.service');
const {
registerExamService,
} = require('../../../../services/register/registerExam.service');
const { getRegisterExamCheckLock } = require('../../../../nkcModules/redLock');

const router = new Router();
router.get('/exam', async (ctx, next) => {
const { query, db, address } = ctx;
const { query, address } = ctx;
const lock = await getRegisterExamCheckLock(address);
try {
const { code: registerActivationCode } = query;
const { registerExamination } = await db.SettingModel.getSettings(
'register',
);
let isExamRequired = false;
if (registerExamination) {
isExamRequired = true;
if (registerActivationCode) {
await registerExamService.accessRateLimit(address);
const valid = await activationCodeService.isActivationCodeValid(
registerActivationCode,
);
if (valid) {
isExamRequired = false;
}
}
const { isExamRequired, isExamEnabled, isValidCode } =
await registerExamService.getRegisterCodeStatus(query.code);
if (isExamEnabled) {
await registerExamService.accessRateLimit(address);
}
ctx.apiData = {
isExamRequired,
isExamEnabled,
isValidCode,
};
await lock.unlock();
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion routes/exam/paper.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ paperRouter
if (type === examCategoryTypes.secret) {
return ctx.redirect(`/exam/paper/${newPaper._id}`);
} else {
return ctx.redirect(`/exam/public/takeExam/${newPaper._id}`);
return ctx.redirect(`/exam/public/public-paper/${newPaper._id}`);
}
})
.get('/:_id', async (ctx, next) => {
Expand Down
7 changes: 4 additions & 3 deletions routes/exam/public.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
const Router = require('koa-router');
const { paperService } = require('../../services/exam/paper.service');
const publicRouter = new Router();
publicRouter.get('/takeExam/:pid', async (ctx, next) => {
publicRouter.get('/public-paper/:pid', async (ctx, next) => {
const {
params: { pid },
data,
state: { uid },
} = ctx;
const ip = ctx.address;
await paperService.checkPaperLegal(pid, ip);
await paperService.checkPaperLegal(pid, ip, uid);
data.pid = pid;
ctx.template = 'exam/takeExam.pug';
ctx.template = 'exam/publicPaper.pug';
await next();
});
module.exports = publicRouter;
77 changes: 46 additions & 31 deletions routes/forum/library.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,62 @@
const router = require("koa-router")();
const router = require('koa-router')();
router
.get("/", async (ctx, next) => {
const {data, db, query} = ctx;
data.type = "library";
const {t, id} = query;
if(t && id) {
if(t === "nav") {
const library = await db.LibraryModel.findOne({_id: id});
if(library) {
data.folderId = library.lid || "";
.get('/', async (ctx, next) => {
const { data, db, query } = ctx;
data.type = 'library';
const { t, id } = query;
if (t && id) {
if (t === 'nav') {
const library = await db.LibraryModel.findOne({ _id: id });
if (library) {
data.folderId = library.lid || '';
data.tLid = library._id;
}
} else if(t === "upload") {
let rid = id.split("-");
const resources = await db.ResourceModel.find({rid: {$in: rid}}, {rid: 1});
data.uploadResourcesId = resources.map(r => r.rid);
} else if (t === 'upload') {
let rid = id.split('-');
const resources = await db.ResourceModel.find(
{ rid: { $in: rid } },
{ rid: 1 },
);
data.uploadResourcesId = resources.map((r) => r.rid);
}
}

if(data.forum.lid) {
const forumLibrary = await db.LibraryModel.findOne({_id: data.forum.lid});
if(forumLibrary && forumLibrary.closed) data.libraryClosed = true;
if (data.forum.lid) {
const forumLibrary = await db.LibraryModel.findOne({
_id: data.forum.lid,
});
if (forumLibrary && forumLibrary.closed) {
data.libraryClosed = true;
}
}
await next();
})
.post("/", async (ctx, next) => {
const {data, db, body} = ctx;
const {forum} = data;
const {type} = body;
if(type === "create") {
.post('/', async (ctx, next) => {
const { data, db, body } = ctx;
const { forum } = data;
const { type } = body;
if (type === 'create') {
data.library = await forum.createLibrary(data.user.uid);
} else{
if(!forum.lid) ctx.throw(400, "专业暂未开设文库");
const library = await db.LibraryModel.findOnly({_id: forum.lid});
if(!library) ctx.throw(400, `未找到文库, lid: ${forum.lid}`);
} else {
if (!forum.lid) {
ctx.throw(400, '专业暂未开设文库');
}
const library = await db.LibraryModel.findOnly({ _id: forum.lid });
if (!library) {
ctx.throw(400, `未找到文库, lid: ${forum.lid}`);
}
data.library = library;
if(type === "open") {
if(!library.closed) ctx.throw(400, "文库未关闭");
await library.updateOne({closed: false});
if (type === 'open') {
if (!library.closed) {
ctx.throw(400, '文库未关闭');
}
await library.updateOne({ closed: false });
data.libraryClosed = false;
} else {
if(library.closed) ctx.throw(400, "文库已关闭");
await library.updateOne({closed: true});
if (library.closed) {
ctx.throw(400, '文库已关闭');
}
await library.updateOne({ closed: true });
data.libraryClosed = true;
}
}
Expand Down
Loading

0 comments on commit 130832a

Please sign in to comment.