-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path专家抽取.js
More file actions
427 lines (364 loc) · 15.1 KB
/
专家抽取.js
File metadata and controls
427 lines (364 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const html = `
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>专家抽取系统</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/animate.css@4.1.1/animate.min.css" rel="stylesheet">
<style>
body {
font-family: 'Microsoft YaHei', sans-serif;
color: #fff;
overflow-x: hidden;
min-height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
}
.background-animation {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: linear-gradient(-45deg, #ee7752, #e73c7e, #23a6d5, #23d5ab);
background-size: 400% 400%;
animation: gradientBG 15s ease infinite;
z-index: -1;
}
@keyframes gradientBG {
0% { background-position: 0% 50%; }
50% { background-position: 100% 50%; }
100% { background-position: 0% 50%; }
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
z-index: 1;
}
h1 {
font-size: 3rem;
font-weight: bold;
text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
margin-top: 20px;
}
.lottery-display {
background-color: rgba(255, 255, 255, 0.15);
backdrop-filter: blur(10px);
border-radius: 15px;
padding: 40px;
margin: 30px 0;
min-height: 300px;
display: flex;
align-items: center;
justify-content: center;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
border: 1px solid rgba(255, 255, 255, 0.2);
}
.display-text {
font-size: 4rem;
font-weight: bold;
text-align: center;
transition: all 0.3s ease;
text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
}
.control-panel {
background-color: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(5px);
border-radius: 15px;
padding: 30px;
box-shadow: 0 5px 20px rgba(0, 0, 0, 0.15);
border: 1px solid rgba(255, 255, 255, 0.1);
}
.btn {
font-weight: bold;
padding: 10px 20px;
border-radius: 50px;
transition: all 0.3s ease;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
border: none;
}
.btn:hover {
transform: translateY(-3px);
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
}
.btn-primary {
background-color: #4e73df;
}
.btn-success {
background-color: #1cc88a;
}
.btn-danger {
background-color: #e74a3b;
}
.btn-warning {
background-color: #f6c23e;
}
.form-control {
background-color: rgba(255, 255, 255, 0.2);
border: none;
color: #fff;
border-radius: 50px;
padding: 12px 20px;
}
.form-control:focus {
background-color: rgba(255, 255, 255, 0.3);
color: #fff;
box-shadow: 0 0 0 0.25rem rgba(255, 255, 255, 0.25);
}
.form-control::placeholder {
color: rgba(255, 255, 255, 0.6);
}
label {
font-weight: bold;
margin-bottom: 5px;
display: block;
}
.winner {
color: #f8d800;
animation: pulse 0.5s infinite alternate;
font-size: 2.5rem;
}
.winner-name {
font-size: 2rem;
line-height: 1.5;
margin: 10px 0;
}
@keyframes pulse {
from { transform: scale(1); }
to { transform: scale(1.05); }
}
@media (max-width: 768px) {
h1 {
font-size: 2rem;
}
.display-text {
font-size: 2.5rem;
}
.lottery-display {
min-height: 200px;
padding: 20px;
}
.winner {
font-size: 1.8rem;
}
.winner-name {
font-size: 1.5rem;
}
}
</style>
</head>
<body>
<div class="background-animation"></div>
<div class="container">
<h1 class="text-center mb-4 animate__animated animate__fadeInDown">专家抽取系统</h1>
<div class="lottery-display animate__animated animate__fadeIn">
<div id="nameDisplay" class="display-text">等待抽取开始...</div>
</div>
<div class="control-panel animate__animated animate__fadeInUp">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="input-group mb-3">
<input type="text" id="nameInput" class="form-control" placeholder="输入姓名">
<button id="addNameBtn" class="btn btn-primary">添加</button>
</div>
<div class="row g-3">
<div class="col-md-4">
<label for="durationInput">抽取时间(秒)</label>
<input type="number" id="durationInput" class="form-control" value="5" min="1">
</div>
<div class="col-md-4">
<label for="winnerCount">抽中人数</label>
<input type="number" id="winnerCount" class="form-control" value="1" min="1">
</div>
<div class="col-md-4">
<label for="fileInput">导入Excel</label>
<input type="file" id="fileInput" class="form-control" accept=".xlsx, .xls">
</div>
</div>
<div class="d-flex justify-content-center mt-4">
<button id="startBtn" class="btn btn-success me-3">开始抽取</button>
<button id="resetBtn" class="btn btn-danger">重置</button>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xlsx@0.18.5/dist/xlsx.full.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
let names = [];
let isRunning = false;
let timer = null;
let slowdownTimer = null;
let winners = [];
let startTime;
let duration = 5;
const nameInput = document.getElementById('nameInput');
const addNameBtn = document.getElementById('addNameBtn');
const startBtn = document.getElementById('startBtn');
const resetBtn = document.getElementById('resetBtn');
const nameDisplay = document.getElementById('nameDisplay');
const durationInput = document.getElementById('durationInput');
const winnerCountInput = document.getElementById('winnerCount');
const fileInput = document.getElementById('fileInput');
addNameBtn.addEventListener('click', function() {
const name = nameInput.value.trim();
if (name) {
names.push(name);
nameInput.value = '';
updateDisplay('已添加: ' + name);
setTimeout(function() { updateDisplay('等待抽取开始...'); }, 1500);
}
});
startBtn.addEventListener('click', function() {
if (isRunning) {
stopLottery();
return;
}
if (names.length === 0) {
updateDisplay('请先添加参与者');
return;
}
const winnerCount = Math.min(
parseInt(winnerCountInput.value) || 1,
names.length
);
startLottery(winnerCount);
});
resetBtn.addEventListener('click', function() {
stopAllTimers();
names = [];
winners = [];
fileInput.value = '';
updateDisplay('已重置,等待抽取开始...');
});
function stopAllTimers() {
if (timer) cancelAnimationFrame(timer);
if (slowdownTimer) clearTimeout(slowdownTimer);
isRunning = false;
startBtn.textContent = '开始抽取';
startBtn.classList.remove('btn-warning');
startBtn.classList.add('btn-success');
}
fileInput.addEventListener('change', function(e) {
const file = e.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = function(e) {
try {
const data = new Uint8Array(e.target.result);
const workbook = XLSX.read(data, { type: 'array' });
const firstSheet = workbook.Sheets[workbook.SheetNames[0]];
const jsonData = XLSX.utils.sheet_to_json(firstSheet);
names = jsonData.map(function(row) {
const key = Object.keys(row).find(function(k) {
return row[k] && row[k].toString().trim();
});
return key ? row[key].toString().trim() : null;
}).filter(function(name) { return name; });
updateDisplay('已导入 ' + names.length + ' 个姓名');
setTimeout(function() { updateDisplay('等待抽取开始...'); }, 2000);
} catch (error) {
updateDisplay('文件解析失败');
console.error(error);
}
};
reader.readAsArrayBuffer(file);
});
function startLottery(winnerCount) {
stopAllTimers();
isRunning = true;
winners = [];
startTime = Date.now();
duration = parseInt(durationInput.value) || 5;
startBtn.textContent = '停止抽取';
startBtn.classList.remove('btn-success');
startBtn.classList.add('btn-warning');
let candidates = [...names];
let lastDisplayTime = 0;
function runLottery() {
const now = Date.now();
const elapsed = now - startTime;
const remaining = duration * 1000 - elapsed;
if (remaining <= 0) {
stopAllTimers();
selectWinners(winnerCount, candidates);
return;
}
const progress = elapsed / (duration * 1000);
let speed;
if (progress < 0.7) {
speed = 100 - (progress * 80);
} else if (progress < 0.9) {
speed = 100 + (progress * 100);
} else {
speed = 300;
}
speed = Math.max(speed, 30);
if (now - lastDisplayTime >= speed) {
const randomIndex = Math.floor(Math.random() * candidates.length);
nameDisplay.textContent = candidates[randomIndex];
nameDisplay.classList.add('animate__animated', 'animate__pulse');
lastDisplayTime = now;
}
if (progress >= 0.9 && !slowdownTimer) {
slowdownTimer = setTimeout(function() {
stopAllTimers();
selectWinners(winnerCount, candidates);
}, remaining);
}
if (isRunning) {
timer = requestAnimationFrame(runLottery);
}
};
timer = requestAnimationFrame(runLottery);
}
function selectWinners(count, candidates) {
winners = [...candidates]
.sort(function() { return Math.random() - 0.5; })
.slice(0, count);
displayWinners();
}
function displayWinners() {
nameDisplay.innerHTML = '';
nameDisplay.classList.add('winner');
if (winners.length === 1) {
nameDisplay.innerHTML = '<div>抽中专家</div><div class="winner-name">' + winners[0] + '</div>';
} else {
const winnerList = winners.map(function(winner, index) {
return '<div class="winner-name">' + (index + 1) + '. ' + winner + '</div>';
}).join('');
nameDisplay.innerHTML = '<div>专家名单</div>' + winnerList;
}
}
function updateDisplay(text) {
nameDisplay.textContent = text;
nameDisplay.classList.remove('winner', 'animate__animated', 'animate__pulse');
}
document.addEventListener('keydown', function(e) {
if (e.key === 'Enter' && nameInput === document.activeElement) {
addNameBtn.click();
nameInput.focus();
}
});
});
</script>
</body>
</html>
`;
return new Response(html, {
headers: {
'content-type': 'text/html;charset=UTF-8',
'cache-control': 'no-cache, no-store, must-revalidate'
}
});
}