parent
d9960ef64c
commit
2b465511d1
@ -1,135 +1,200 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>抽奖页面</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: 'Arial', sans-serif;
|
||||
height: 100vh;
|
||||
margin: 0;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
background: linear-gradient(135deg, #6e45e2, #88d3ce);
|
||||
color: white;
|
||||
transition: background-color 0.5s ease;
|
||||
}
|
||||
.container {
|
||||
text-align: center;
|
||||
background-color: rgba(255, 255, 255, 0.1);
|
||||
padding: 30px;
|
||||
border-radius: 15px;
|
||||
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.2);
|
||||
opacity: 1;
|
||||
transition: opacity 0.5s ease;
|
||||
}
|
||||
#pointsDisplay {
|
||||
font-size: 1.5em;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
button {
|
||||
padding: 12px 30px;
|
||||
font-size: 1.1em;
|
||||
border: none;
|
||||
border-radius: 50px;
|
||||
background-color: #ff5722;
|
||||
color: white;
|
||||
cursor: pointer;
|
||||
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
|
||||
transition: background-color 0.3s ease, box-shadow 0.3s ease;
|
||||
margin: 10px;
|
||||
}
|
||||
button:hover {
|
||||
background-color: #ff3d00;
|
||||
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
|
||||
}
|
||||
#loading {
|
||||
display: none;
|
||||
font-size: 1.5em;
|
||||
margin-top: 20px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div class="container">
|
||||
<div id="pointsDisplay">当前积分: </div>
|
||||
<button id="startLotteryButton">开始抽奖</button>
|
||||
<button class="back-button" id="backButton">返回前一页面</button>
|
||||
<div id="result"></div>
|
||||
<div id="loading">正在加载,请稍候...</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
let name = urlParams.get('name');
|
||||
|
||||
// 如果 URL 中没有 name 参数,尝试从 localStorage 获取
|
||||
if (!name) {
|
||||
name = localStorage.getItem('currentUserName');
|
||||
}
|
||||
|
||||
const pointsDisplay = document.querySelector('#pointsDisplay');
|
||||
const startLotteryButton = document.querySelector('#startLotteryButton');
|
||||
const resultDisplay = document.querySelector('#result');
|
||||
const backButton = document.getElementById('backButton');
|
||||
const loadingIndicator = document.getElementById('loading');
|
||||
|
||||
// 睡眠函数,让页面跳转更自然
|
||||
function sleep(d) {
|
||||
return new Promise(resolve => setTimeout(resolve, d));
|
||||
}
|
||||
|
||||
// 获取该用户的积分
|
||||
let userPoints = parseInt(localStorage.getItem(name)) || 0;
|
||||
pointsDisplay.textContent = `${name} 的当前积分: ${userPoints}`;
|
||||
|
||||
// 设置返回按钮的 URL
|
||||
backButton.onclick = () => {
|
||||
loadingIndicator.style.display = 'block'; // 显示加载提示
|
||||
window.location.href = `choice.html?name=${encodeURIComponent(name)}`;
|
||||
};
|
||||
|
||||
// 抽奖逻辑
|
||||
startLotteryButton.addEventListener('click', async () => {
|
||||
if (userPoints >= 20) {
|
||||
userPoints -= 20; // 每次抽奖消耗 20 分
|
||||
localStorage.setItem(name, userPoints); // 保存更新后的积分
|
||||
pointsDisplay.textContent = `${name} 的当前积分: ${userPoints}`;
|
||||
|
||||
// 随机选择奖品
|
||||
const prizes = ["跳过权", "再来一次", "什么也没有发生", "加30积分且跳过"];
|
||||
const randomPrize = prizes[Math.floor(Math.random() * prizes.length)];
|
||||
resultDisplay.textContent = `你赢得了: ${randomPrize}`;
|
||||
|
||||
// 等待 2 秒以展示结果
|
||||
await sleep(2000);
|
||||
loadingIndicator.style.display = 'block'; // 显示加载提示
|
||||
|
||||
if (randomPrize === '跳过权') {
|
||||
await sleep(1000);
|
||||
window.location.href = `select.html?name=${encodeURIComponent(name)}`;
|
||||
} else if (randomPrize === '再来一次') {
|
||||
userPoints += 20;
|
||||
localStorage.setItem(name, userPoints);
|
||||
} else if (randomPrize === '什么也没有发生') {
|
||||
await sleep(1000);
|
||||
window.location.href = `answer.html?name=${encodeURIComponent(name)}`;
|
||||
} else if (randomPrize === '加30积分且跳过') {
|
||||
userPoints += 30;
|
||||
localStorage.setItem(name, userPoints);
|
||||
await sleep(1000);
|
||||
window.location.href = `select.html?name=${encodeURIComponent(name)}`;
|
||||
}
|
||||
} else {
|
||||
resultDisplay.textContent = "积分不足,无法抽奖!";
|
||||
loadingIndicator.style.display = 'block'; // 显示加载提示
|
||||
await sleep(1000);
|
||||
window.location.href = `answer.html?name=${encodeURIComponent(name)}`;
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>抽奖页面</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: 'Arial', sans-serif;
|
||||
height: 100vh;
|
||||
margin: 0;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
background: linear-gradient(135deg, #6e45e2, #88d3ce);
|
||||
color: white;
|
||||
transition: background-color 0.5s ease;
|
||||
}
|
||||
.container {
|
||||
text-align: center;
|
||||
background-color: rgba(255, 255, 255, 0.1);
|
||||
padding: 30px;
|
||||
border-radius: 15px;
|
||||
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.2);
|
||||
opacity: 1;
|
||||
transition: opacity 0.5s ease;
|
||||
}
|
||||
#pointsDisplay {
|
||||
font-size: 1.5em;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
button {
|
||||
padding: 12px 30px;
|
||||
font-size: 1.1em;
|
||||
border: none;
|
||||
border-radius: 50px;
|
||||
background-color: #ff5722;
|
||||
color: white;
|
||||
cursor: pointer;
|
||||
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
|
||||
transition: background-color 0.3s ease, box-shadow 0.3s ease;
|
||||
margin: 10px;
|
||||
}
|
||||
button:hover {
|
||||
background-color: #ff3d00;
|
||||
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
|
||||
}
|
||||
#loading {
|
||||
display: none;
|
||||
font-size: 1.5em;
|
||||
margin-top: 20px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div class="container">
|
||||
<div id="pointsDisplay">当前积分: </div>
|
||||
<button id="startLotteryButton">开始抽奖</button>
|
||||
<button class="back-button" id="backButton">返回前一页面</button>
|
||||
<div id="result"></div>
|
||||
<div id="loading">正在加载,请稍候...</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
let student_name = urlParams.get('student_name');
|
||||
let student_id = urlParams.get('student_id');
|
||||
|
||||
const pointsDisplay = document.querySelector('#pointsDisplay');
|
||||
const startLotteryButton = document.querySelector('#startLotteryButton');
|
||||
const resultDisplay = document.querySelector('#result');
|
||||
const backButton = document.getElementById('backButton');
|
||||
const loadingIndicator = document.getElementById('loading');
|
||||
|
||||
// 睡眠函数,让页面跳转更自然
|
||||
function sleep(d) {
|
||||
return new Promise(resolve => setTimeout(resolve, d));
|
||||
}
|
||||
|
||||
// 全局变量,用于存储用户积分
|
||||
let userPoints = null;
|
||||
|
||||
// 获取该用户的分数
|
||||
const fetchStudentScore = async (student_id) => {
|
||||
try {
|
||||
const response = await fetch('http://localhost:3000/api/get-score', {
|
||||
method: 'POST', // 使用 POST 方法
|
||||
headers: {
|
||||
'Content-Type': 'application/json', // 设置请求体的内容类型
|
||||
},
|
||||
body: JSON.stringify({ student_id }), // 将 student_id 放入请求体
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
alert('分数获取失败');
|
||||
return null; // 返回 null 表示获取失败
|
||||
}
|
||||
|
||||
// 直接返回响应体中的数字
|
||||
const data = await response.json(); // 直接解析为 JSON
|
||||
const score = data.score;
|
||||
return score; // 返回数字
|
||||
} catch (error) {
|
||||
alert('网络请求失败');
|
||||
return null; // 返回 null 表示获取失败
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// 更新用户分数
|
||||
async function updateScore(student_id, points) {
|
||||
try {
|
||||
const response = await fetch('http://localhost:3000/api/update-score', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ student_id, points }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('网络响应不正常');
|
||||
}
|
||||
|
||||
// 使用 text() 解析响应,得到字符串形式的分数
|
||||
const data = await response.text();
|
||||
let score = Number(data); // 转换为数字
|
||||
return score; // 返回数字
|
||||
} catch (error) {
|
||||
console.error('分数更新失败:', error);
|
||||
return null; // 返回 null 表示更新失败
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 初始化函数,用于设置用户积分
|
||||
async function init() {
|
||||
userPoints = await fetchStudentScore(student_id); // 获取分数并存储到全局变量
|
||||
pointsDisplay.textContent = `${student_name} 的当前积分: ${userPoints}`;
|
||||
}
|
||||
|
||||
// 执行初始化函数
|
||||
init();
|
||||
|
||||
// 设置返回按钮的 URL
|
||||
backButton.onclick = () => {
|
||||
loadingIndicator.style.display = 'block'; // 显示加载提示
|
||||
window.location.href = `choice.html?student_name=${encodeURIComponent(student_name)}&student_id=${encodeURIComponent(student_id)}`;
|
||||
};
|
||||
|
||||
// 开始抽奖
|
||||
startLotteryButton.addEventListener('click', async () => {
|
||||
if (userPoints >= 2) {
|
||||
userPoints = await updateScore(student_id, -2); // 积分-2
|
||||
|
||||
pointsDisplay.textContent = `${student_name} 的当前积分: ${userPoints}`;
|
||||
|
||||
// 随机选择奖品
|
||||
const prizes = ["跳过权", "再来一次", "什么也没有发生", "加3积分且跳过"];
|
||||
const randomPrize = prizes[Math.floor(Math.random() * prizes.length)];
|
||||
resultDisplay.textContent = `你赢得了: ${randomPrize}`;
|
||||
|
||||
// 等待 2 秒以展示结果
|
||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||
loadingIndicator.style.display = 'block'; // 显示加载提示
|
||||
|
||||
if (randomPrize === '跳过权') {
|
||||
await sleep(2000);
|
||||
window.location.href = `select.html?student_name=${encodeURIComponent(student_name)}&student_id=${encodeURIComponent(student_id)}`;
|
||||
}
|
||||
|
||||
else if (randomPrize === '再来一次') {
|
||||
userPoints = await updateScore(student_id, 2); // 积分返回2
|
||||
await sleep(2000);
|
||||
window.location.href = `lottery.html?student_name=${encodeURIComponent(student_name)}&student_id=${encodeURIComponent(student_id)}`;
|
||||
}
|
||||
|
||||
else if (randomPrize === '什么也没有发生') {
|
||||
await sleep(2000);
|
||||
window.location.href = `answer.html?student_name=${encodeURIComponent(student_name)}&student_id=${encodeURIComponent(student_id)}`;
|
||||
}
|
||||
|
||||
else if (randomPrize === '加3积分且跳过') {
|
||||
userPoints = await updateScore(student_id, 3); // 积分+3
|
||||
await sleep(2000);
|
||||
window.location.href = `select.html?student_name=${encodeURIComponent(student_name)}&student_id=${encodeURIComponent(student_id)}`;
|
||||
}
|
||||
|
||||
} else {
|
||||
resultDisplay.textContent = "积分不足,无法抽奖!返回回答问题";
|
||||
loadingIndicator.style.display = 'block'; // 显示加载提示
|
||||
await sleep(2000);
|
||||
window.location.href = `answer.html?student_name=${encodeURIComponent(student_name)}&student_id=${encodeURIComponent(student_id)}`;
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
Loading…
Reference in new issue