CyberExam2-front/pages/js/common.js
2024-12-03 01:03:00 +08:00

83 lines
2.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

var current_user=null;
//获取用户登录状态
var xhr1 = new XMLHttpRequest();
xhr1.open('GET', '/login/current-user', true);
xhr1.responseType = 'json';
xhr1.onload = function() {
if (this.status === 200) {
if (this.response.msg!='ok'){
console.error('Error:', this.response.msg);
}else{
current_user = this.response.user;
}
} else {
console.error('Error:', this.statusText);
}
};
xhr1.send();
//登录后则跳转,没有登录则跳转到登录的函数。
function jumpOrNot(href){
if (current_user === null){
alert('在部署环境将会跳转到login文件打开将跳转错误');
window.location.href='/login.html';
}else if(current_user.status==='ok'){
window.location.href=href;
}
}
//拉取上一题的函数
function last_question(){
get_question('last')
}
//从服务端获取题目函数
function get_question(opt){
//将会从服务端拉取题目这里演示直接向显示函数传入固定的json。
alert('从服务器拉取上一题,这里演示直接向显示函数传入固定的json。');
const test_json = {
'question': '这里是测试题目内容',
'choice_a': '这个是测试选项',
'choice_b': '这个是b测试选项',
'choice_c': '这个是测试选项测试选项测试选项测试选项测试选项',
'choice_d': '你好awinx'
}
set_question(test_json);
var xhr = new XMLHttpRequest();
xhr.open('GET', '/get-question?user='+current_user.username+'&&'+opt, true);
xhr.responseType = 'json';
xhr.onload = function() {
if (this.status === 200) {
if (this.response.msg!='ok'){
console.error('Error:', this.response.msg);
}else{
set_question(this.response);
}
} else {
console.error('Error:', this.statusText);
}
};
xhr.send();
}
//将获取到的题目显示出来的函数
function set_question(content){
console.log('设置题目内容');
const question = document.querySelector('.question-text');
const choice_a = document.getElementById('A-text');
const choice_b = document.getElementById('B-text');
const choice_c = document.getElementById('C-text');
const choice_d = document.getElementById('D-text');
question.textContent = content.question;
choice_a.textContent = content.choice_a;
choice_b.textContent = content.choice_b;
choice_c.textContent = content.choice_c;
choice_d.textContent = content.choice_d;
}
function submit(){
alert('尚未完成后端还未适配该模型请访问http://exam.awin-x.top \n(仅ipv6地址)。');
}