forked from kggzs/Driftbottle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththrow.html
More file actions
795 lines (706 loc) · 41.4 KB
/
throw.html
File metadata and controls
795 lines (706 loc) · 41.4 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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
<!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 rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="assets/css/bootstrap/bootstrap.min.css">
<link rel="stylesheet" href="assets/css/fontawesome/all.min.css">
<link rel="stylesheet" href="assets/css/style.css">
<script src="assets/js/bootstrap/bootstrap.bundle.min.js"></script>
<script src="assets/js/utils.js"></script>
</head>
<body>
<!-- 导航栏 -->
<nav class="navbar navbar-expand">
<div class="container">
<a href="index.html" class="navbar-brand">漂流瓶</a>
<div class="navbar-collapse">
<ul class="navbar-nav">
<!-- 动态生成导航项 -->
</ul>
</div>
</div>
</nav>
<!-- 主内容 -->
<div class="container">
<div class="alert-container"></div>
<div class="content">
<div class="row">
<div class="col-md-8 offset-md-2">
<!-- 用户限制和积分信息 -->
<div class="card mb-4">
<div class="card-body">
<div class="row align-items-center">
<div class="col-md-8">
<div class="user-limits">
<p class="mb-1"><i class="fas fa-gift text-success"></i> 免费次数剩余: <span id="freeThrowsRemaining">--</span>/<span id="totalFreeThrows">--</span> 次</p>
<p class="mb-1" id="vipThrowBonus" style="display: none;"><i class="fas fa-crown text-warning"></i> <span class="text-warning">VIP特权:每天额外免费次数(0点刷新)</span></p>
<p class="mb-1" id="vipFreeThrows"><i class="fas fa-star text-warning"></i> 会员次数剩余: <span id="vipFreeThrowsRemaining">--</span>/<span id="totalVipFreeThrows">--</span> 次</p>
</div>
</div>
<div class="col-md-4 text-end">
<p class="mb-0"><i class="fas fa-coins text-warning"></i> 我的积分: <span id="userPoints">--</span></p>
<small class="text-muted">每瓶消耗<span id="pointsPerBottle">1</span>积分 (免费次数内不消耗)</small>
</div>
</div>
</div>
</div>
<div class="card">
<div class="card-header">
<h2 class="text-center">扔一个漂流瓶</h2>
</div>
<div class="card-body">
<form id="throwBottleForm">
<!-- 漂流瓶类型选择 -->
<div class="form-group mb-4">
<label class="form-label">选择漂流瓶类型</label>
<div class="btn-group w-100" role="group">
<input type="radio" class="btn-check" name="bottleType" id="bottleTypeText" value="text" checked>
<label class="btn btn-outline-primary" for="bottleTypeText">
<i class="fas fa-pen"></i> 文字漂流瓶
</label>
<input type="radio" class="btn-check" name="bottleType" id="bottleTypeVoice" value="voice">
<label class="btn btn-outline-primary" for="bottleTypeVoice">
<i class="fas fa-microphone"></i> 语音漂流瓶
</label>
</div>
</div>
<!-- 文字内容区域 -->
<div class="form-group mb-4" id="textContentArea">
<label for="bottleContent" class="form-label">写下你想说的话</label>
<textarea class="form-control" id="bottleContent" name="bottleContent" rows="6" placeholder="在这里写下你的心情、秘密或想法..."></textarea>
</div>
<!-- 语音录制区域 -->
<div class="form-group mb-4" id="voiceContentArea" style="display: none;">
<label class="form-label">录制你的语音</label>
<div class="voice-recorder border rounded p-4 text-center">
<div id="recordingStatus" class="mb-3">
<i class="fas fa-microphone fa-3x text-muted"></i>
<p class="mt-2 text-muted">点击下方按钮开始录音</p>
</div>
<div id="recordingControls" class="mb-3">
<button type="button" class="btn btn-danger btn-lg" id="startRecordBtn">
<i class="fas fa-circle"></i> 开始录音
</button>
<button type="button" class="btn btn-warning btn-lg" id="stopRecordBtn" style="display: none;">
<i class="fas fa-stop"></i> 停止录音
</button>
<button type="button" class="btn btn-secondary btn-lg" id="playRecordBtn" style="display: none;">
<i class="fas fa-play"></i> 播放
</button>
<button type="button" class="btn btn-secondary btn-lg" id="deleteRecordBtn" style="display: none;">
<i class="fas fa-trash"></i> 重新录制
</button>
</div>
<div id="recordingTimer" class="mb-2" style="display: none;">
<span class="badge bg-danger fs-6">录音时长: <span id="timerDisplay">00:00</span></span>
</div>
<audio id="audioPreview" controls style="display: none; width: 100%;" class="mt-3"></audio>
<input type="hidden" id="audioBlob" name="audioBlob">
</div>
</div>
<div class="form-group mb-4">
<label for="mood" class="form-label">选择你的心情</label>
<select class="form-control" id="mood" name="mood">
<option value="开心">开心 😊</option>
<option value="难过">难过 😢</option>
<option value="平静">平静 😌</option>
<option value="愤怒">愤怒 😡</option>
<option value="期待">期待 🤩</option>
<option value="忧郁">忧郁 😔</option>
<option value="其他">其他 😶</option>
</select>
</div>
<div class="form-check mb-4">
<input class="form-check-input" type="checkbox" id="isAnonymous" name="isAnonymous">
<label class="form-check-label" for="isAnonymous">
匿名发送(隐藏你的用户名)
</label>
</div>
<div class="form-check mb-4" id="locationDiv">
<input class="form-check-input" type="checkbox" id="locationCheckbox" name="includeLocation" checked>
<label class="form-check-label" for="locationCheckbox">
<i class="fas fa-map-marker-alt"></i> 显示地理位置信息
<span class="text-muted small" id="locationHint"></span>
</label>
<div class="mt-2 small text-muted location-preview" id="locationPreview"></div>
</div>
<div class="text-center">
<button type="submit" class="btn btn-primary px-5" id="throwButton">
<i class="fas fa-paper-plane"></i> 扔向大海
</button>
</div>
</form>
</div>
<div class="card-footer">
<div class="text-center text-muted">
<p><small>你的漂流瓶会被随机的用户捡起。提示:不要在漂流瓶中包含你的个人隐私信息。</small></p>
<p><small>每天有10次免费扔瓶机会,VIP用户额外获得5次免费机会。所有次数在每天0点自动刷新!超过免费次数后,每次扔瓶消耗1积分。</small></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- 页脚 -->
<footer class="bg-dark text-light py-4 mt-5">
<div class="container text-center">
<p class="mb-2">漂流瓶 © 2023 版权所有</p>
</div>
</footer>
<script>
// 标记页面已有自己的初始化逻辑,避免 app.js 重复初始化
window.pageHasCustomInit = true;
</script>
<script src="assets/js/app.js"></script>
<script>
// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', async function() {
// 防止表单重复提交的标记
let isSubmitting = false;
// 默认配置常量(后续会从API获取)
window.POINTS_PER_BOTTLE = 1;
window.DAILY_BOTTLE_LIMIT = 10;
window.VIP_DAILY_BOTTLE_LIMIT = 15;
// 检查登录状态
try {
const authResponse = await fetch('api.php?action=check_auth');
const authData = await authResponse.json();
if (!authData.success || !authData.loggedIn) {
window.location.href = 'login.html';
return;
}
// 保存用户信息
if (!window.userInfo) window.userInfo = {};
window.userInfo.id = authData.user_id;
window.userInfo.username = authData.username;
window.userInfo.points = authData.points || 0;
// 显示每瓶消耗的积分(使用默认值,稍后会被更新)
document.getElementById('pointsPerBottle').textContent = window.POINTS_PER_BOTTLE;
document.getElementById('userPoints').textContent = window.userInfo.points;
// 并行加载关键数据(地理位置延迟加载)
const [configRes, limitsRes, vipRes] = await Promise.allSettled([
fetch('api.php?action=get_system_config'),
fetch('api.php?action=get_daily_limits'),
fetch('api.php?action=check_vip_status')
]);
// 地理位置信息延迟加载(非关键信息)
const locationResPromise = fetch('api.php?action=get_location_preview').catch(err => {
Logger.error('地理位置查询失败:', err);
return { ok: false };
});
// 处理系统配置
if (configRes.status === 'fulfilled') {
try {
const config = await configRes.value.json();
if (config.success) {
window.POINTS_PER_BOTTLE = config.POINTS_PER_BOTTLE || window.POINTS_PER_BOTTLE;
window.DAILY_BOTTLE_LIMIT = config.DAILY_BOTTLE_LIMIT || window.DAILY_BOTTLE_LIMIT;
window.VIP_DAILY_BOTTLE_LIMIT = config.VIP_DAILY_BOTTLE_LIMIT || window.VIP_DAILY_BOTTLE_LIMIT;
document.getElementById('pointsPerBottle').textContent = window.POINTS_PER_BOTTLE;
}
} catch (e) {
Logger.error('解析系统配置失败:', e);
}
}
// 处理每日限制和用户信息
if (limitsRes.status === 'fulfilled') {
try {
const limitsData = await limitsRes.value.json();
if (limitsData.success) {
updateDailyLimitsDisplay(limitsData);
}
} catch (e) {
Logger.error('解析每日限制失败:', e);
}
}
// 处理VIP状态
if (vipRes.status === 'fulfilled') {
try {
const vipData = await vipRes.value.json();
if (vipData.success) {
updateVipStatusDisplay(vipData);
}
} catch (e) {
Logger.error('解析VIP状态失败:', e);
}
}
// 处理地理位置预览(延迟加载)
locationResPromise.then(async (locationRes) => {
if (locationRes.ok) {
try {
const locationData = await locationRes.json();
if (locationData.success) {
updateLocationPreview(locationData);
}
} catch (e) {
Logger.error('解析地理位置失败:', e);
}
}
}).catch(err => {
Logger.error('地理位置查询失败:', err);
});
// 表单提交事件 - 使用onclick替代form submit
const submitBtn = document.getElementById('throwButton');
if (submitBtn) {
submitBtn.onclick = async function(e) {
e.preventDefault();
// 防止重复提交
if (isSubmitting) {
Logger.log('防止重复提交');
return false;
}
try {
const bottleType = document.querySelector('input[name="bottleType"]:checked').value;
const mood = document.getElementById('mood').value;
const isAnonymous = document.getElementById('isAnonymous').checked ? 1 : 0;
const includeLocation = document.getElementById('locationCheckbox').checked ? 1 : 0;
// 验证内容
if (bottleType === 'text') {
const content = document.getElementById('bottleContent').value.trim();
if (content === '') {
showAlert('请输入漂流瓶内容', 'danger');
return false;
}
} else if (bottleType === 'voice') {
const audioBlob = document.getElementById('audioBlob').value;
if (!audioBlob) {
showAlert('请先录制语音', 'danger');
return false;
}
}
// 设置防重复提交标记
isSubmitting = true;
submitBtn.disabled = true;
submitBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 正在扔出...';
// 准备提交数据
let submitData = {
bottle_type: bottleType,
mood: mood,
is_anonymous: isAnonymous,
include_location: includeLocation
};
if (bottleType === 'text') {
submitData.content = document.getElementById('bottleContent').value.trim();
} else if (bottleType === 'voice') {
// 语音漂流瓶需要先上传音频文件
const audioBlobData = document.getElementById('audioBlob').value;
if (!audioBlobData) {
showAlert('请先录制语音', 'danger');
submitBtn.disabled = false;
submitBtn.innerHTML = '<i class="fas fa-paper-plane"></i> 扔向大海';
isSubmitting = false;
return false;
}
// 先上传音频文件
try {
const audioBlob = dataURLtoBlob(audioBlobData);
const formData = new FormData();
formData.append('audio', audioBlob, 'voice_' + Date.now() + '.webm');
const uploadResponse = await fetch('api.php?action=upload_audio', {
method: 'POST',
body: formData
});
const uploadResult = await uploadResponse.json();
if (!uploadResult.success) {
throw new Error(uploadResult.message || '音频上传失败');
}
submitData.audio_file = uploadResult.audio_file;
submitData.audio_duration = uploadResult.audio_duration || 0;
} catch (uploadError) {
Logger.error('音频上传错误:', uploadError);
showAlert('音频上传失败:' + uploadError.message, 'danger');
submitBtn.disabled = false;
submitBtn.innerHTML = '<i class="fas fa-paper-plane"></i> 扔向大海';
isSubmitting = false;
return false;
}
}
// 提交扔瓶请求
const response = await fetch('api.php?action=create_bottle', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(submitData)
});
// 检查响应状态
if (!response.ok) {
throw new Error(`HTTP错误: ${response.status} ${response.statusText}`);
}
// 检查响应内容类型
const contentType = response.headers.get('content-type');
if (!contentType || !contentType.includes('application/json')) {
const text = await response.text();
Logger.error('非JSON响应:', text);
throw new Error('服务器返回了非JSON格式的响应');
}
// 尝试解析JSON
const text = await response.text();
let data;
try {
data = JSON.parse(text);
} catch (parseError) {
Logger.error('JSON解析错误:', parseError);
Logger.error('原始响应:', text);
throw new Error('服务器返回的数据格式不正确: ' + parseError.message);
}
if (data.success) {
showAlert('漂流瓶已成功扔出!', 'success');
document.getElementById('bottleContent').value = '';
document.getElementById('isAnonymous').checked = false;
// 重置语音录制
if (typeof resetVoiceRecorder === 'function') {
resetVoiceRecorder();
}
// 更新积分和限制信息
updateUserPointsDisplay(data.is_free, data.points_deducted);
getDailyLimits();
} else {
showAlert(data.message || '扔漂流瓶失败,请稍后再试', 'danger');
}
} catch (e) {
Logger.error('Exception:', e);
showAlert('发生异常,请稍后再试', 'danger');
} finally {
// 无论成功失败,都重置提交状态
submitBtn.disabled = false;
submitBtn.innerHTML = '<i class="fas fa-paper-plane"></i> 扔向大海';
isSubmitting = false;
}
return false; // 防止表单默认提交
};
}
// 阻止表单默认提交
const form = document.getElementById('throwBottleForm');
if (form) {
form.onsubmit = function(e) {
e.preventDefault();
return false;
};
}
// 初始化语音录制相关功能
initVoiceRecorderControls();
initBottleTypeSwitcher();
// 手动更新导航栏,避免 app.js 重复调用 check_auth
updateNavBarManually();
} catch (error) {
Logger.error('页面初始化错误:', error);
showAlert('页面加载失败,请刷新重试', 'danger');
}
});
// 手动更新导航栏(复用已有的用户信息)
function updateNavBarManually() {
const navbarContainer = document.querySelector('.navbar-nav');
if (!navbarContainer) return;
if (window.userInfo && window.userInfo.id) {
// 已登录状态
navbarContainer.innerHTML = `
<li class="nav-item"><a href="index.html" class="nav-link">首页</a></li>
<li class="nav-item"><a href="throw.html" class="nav-link">扔漂流瓶</a></li>
<li class="nav-item"><a href="pick.html" class="nav-link">捡漂流瓶</a></li>
<li class="nav-item"><a href="profile.html" class="nav-link">个人中心</a></li>
<li class="nav-item"><a href="#" class="nav-link logout-btn">退出登录</a></li>
`;
// 绑定退出登录事件
const logoutBtn = document.querySelector('.logout-btn');
if (logoutBtn) {
logoutBtn.addEventListener('click', async (e) => {
e.preventDefault();
try {
const response = await fetch('api.php?action=logout');
const data = await response.json();
if (data.success) {
window.location.href = 'login.html';
}
} catch (error) {
Logger.error('退出登录失败:', error);
}
});
}
} else {
// 未登录状态
navbarContainer.innerHTML = `
<li class="nav-item"><a href="index.html" class="nav-link">首页</a></li>
<li class="nav-item"><a href="login.html" class="nav-link">登录</a></li>
<li class="nav-item"><a href="register.html" class="nav-link">注册</a></li>
`;
}
}
// 初始化语音录制控制
function initVoiceRecorderControls() {
const startBtn = document.getElementById('startRecordBtn');
const stopBtn = document.getElementById('stopRecordBtn');
const playBtn = document.getElementById('playRecordBtn');
const deleteBtn = document.getElementById('deleteRecordBtn');
if (startBtn) {
startBtn.addEventListener('click', async function() {
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
mediaRecorder = new MediaRecorder(stream);
audioChunks = [];
mediaRecorder.ondataavailable = function(event) {
audioChunks.push(event.data);
};
mediaRecorder.onstop = function() {
audioBlob = new Blob(audioChunks, { type: 'audio/webm' });
const audioUrl = URL.createObjectURL(audioBlob);
const audioPreview = document.getElementById('audioPreview');
audioPreview.src = audioUrl;
audioPreview.style.display = 'block';
const reader = new FileReader();
reader.onloadend = function() {
document.getElementById('audioBlob').value = reader.result;
};
reader.readAsDataURL(audioBlob);
stream.getTracks().forEach(track => track.stop());
};
mediaRecorder.start();
recordingStartTime = Date.now();
document.getElementById('startRecordBtn').style.display = 'none';
document.getElementById('stopRecordBtn').style.display = 'inline-block';
document.getElementById('recordingStatus').innerHTML = '<i class="fas fa-microphone fa-3x text-danger"></i><p class="mt-2 text-danger">正在录音中...</p>';
document.getElementById('recordingTimer').style.display = 'block';
recordingTimer = setInterval(updateTimer, 1000);
} catch (error) {
Logger.error('录音失败:', error);
showAlert('无法访问麦克风,请检查浏览器权限设置', 'danger');
}
});
}
if (stopBtn) {
stopBtn.addEventListener('click', function() {
if (mediaRecorder && mediaRecorder.state !== 'inactive') {
mediaRecorder.stop();
clearInterval(recordingTimer);
document.getElementById('startRecordBtn').style.display = 'inline-block';
document.getElementById('stopRecordBtn').style.display = 'none';
document.getElementById('playRecordBtn').style.display = 'inline-block';
document.getElementById('deleteRecordBtn').style.display = 'inline-block';
document.getElementById('recordingStatus').innerHTML = '<i class="fas fa-check-circle fa-3x text-success"></i><p class="mt-2 text-success">录音完成</p>';
}
});
}
if (playBtn) {
playBtn.addEventListener('click', function() {
const audioPreview = document.getElementById('audioPreview');
if (audioPreview.src) {
audioPreview.play();
}
});
}
if (deleteBtn) {
deleteBtn.addEventListener('click', function() {
resetVoiceRecorder();
});
}
}
// 初始化漂流瓶类型切换
function initBottleTypeSwitcher() {
const textRadio = document.getElementById('bottleTypeText');
const voiceRadio = document.getElementById('bottleTypeVoice');
const textArea = document.getElementById('textContentArea');
const voiceArea = document.getElementById('voiceContentArea');
if (textRadio && voiceRadio) {
textRadio.addEventListener('change', function() {
if (this.checked) {
textArea.style.display = 'block';
voiceArea.style.display = 'none';
resetVoiceRecorder();
}
});
voiceRadio.addEventListener('change', function() {
if (this.checked) {
textArea.style.display = 'none';
voiceArea.style.display = 'block';
initVoiceRecorder();
}
});
}
}
// 更新每日限制显示
function updateDailyLimitsDisplay(data) {
const freeThrowsRemaining = Math.max(0, data.free_throws_remaining || 0);
document.getElementById('freeThrowsRemaining').textContent = freeThrowsRemaining;
document.getElementById('totalFreeThrows').textContent = window.DAILY_BOTTLE_LIMIT;
// 更新积分显示(如果返回了积分信息)
if (data.points !== undefined && window.userInfo) {
window.userInfo.points = data.points;
document.getElementById('userPoints').textContent = data.points;
}
// 更新VIP相关信息
if (data.is_vip) {
const vipThrowBonus = document.getElementById('vipThrowBonus');
if (vipThrowBonus) {
vipThrowBonus.style.display = 'block';
}
const vipFreeThrows = document.getElementById('vipFreeThrows');
if (vipFreeThrows) {
vipFreeThrows.style.display = 'block';
const vipFreeThrowsRemaining = Math.max(0, data.vip_free_throws_remaining || 0);
document.getElementById('vipFreeThrowsRemaining').textContent = vipFreeThrowsRemaining;
const vipExtraLimit = window.VIP_DAILY_BOTTLE_LIMIT - window.DAILY_BOTTLE_LIMIT;
document.getElementById('totalVipFreeThrows').textContent = vipExtraLimit;
}
const limitInfo = document.querySelector('.card-footer p:last-child small');
if (limitInfo) {
limitInfo.textContent = `VIP特权:每天有${window.VIP_DAILY_BOTTLE_LIMIT}次免费扔瓶机会,包括${window.VIP_DAILY_BOTTLE_LIMIT - window.DAILY_BOTTLE_LIMIT}次专属会员次数。`;
}
} else {
const vipThrowBonus = document.getElementById('vipThrowBonus');
if (vipThrowBonus) vipThrowBonus.style.display = 'none';
const vipFreeThrows = document.getElementById('vipFreeThrows');
if (vipFreeThrows) vipFreeThrows.style.display = 'none';
}
// 当免费次数用完时,提醒用户将消耗积分
const pointsWarning = document.querySelector('.col-md-4 small.text-muted');
if (pointsWarning) {
if (freeThrowsRemaining <= 0 && (!data.is_vip || data.vip_free_throws_remaining <= 0)) {
pointsWarning.innerHTML = '<span class="text-danger"><i class="fas fa-exclamation-circle"></i> 免费次数已用完,将消耗积分</span>';
} else {
pointsWarning.textContent = '每瓶消耗' + window.POINTS_PER_BOTTLE + '积分 (免费次数内不消耗)';
}
}
}
// 更新VIP状态显示
function updateVipStatusDisplay(data) {
if (!window.userInfo) window.userInfo = {};
window.userInfo.is_vip = data.is_vip;
window.userInfo.vip_level = data.vip_level;
window.userInfo.vip_expire_date = data.vip_expire_date;
const locationCheckbox = document.getElementById('locationCheckbox');
const locationHint = document.getElementById('locationHint');
if (data.is_vip) {
const vipThrowBonus = document.getElementById('vipThrowBonus');
if (vipThrowBonus) vipThrowBonus.style.display = 'block';
const vipFreeThrows = document.getElementById('vipFreeThrows');
if (vipFreeThrows) vipFreeThrows.style.display = 'block';
if (locationCheckbox) {
locationCheckbox.disabled = false;
locationCheckbox.checked = true;
}
if (locationHint) {
locationHint.textContent = '(VIP可取消)';
}
} else {
if (locationCheckbox) {
locationCheckbox.disabled = true;
locationCheckbox.checked = true;
}
if (locationHint) {
locationHint.textContent = '(必选)';
}
}
}
// 更新地理位置预览
function updateLocationPreview(data) {
const locationPreview = document.getElementById('locationPreview');
if (!locationPreview) return;
let previewText = `<i class="fas fa-map-marker-alt text-danger"></i> ${data.location}`;
if (data.ip_address) {
const isVip = window.userInfo && window.userInfo.is_vip;
if (isVip) {
previewText += `<br><i class="fas fa-globe text-primary"></i> IP: ${data.ip_address}`;
} else {
const maskedIp = data.ip_address.replace(/(\d+)\.(\d+)\.(\d+)\.(\d+)/, '$1.$2.**.***');
previewText += `<br><i class="fas fa-globe text-primary"></i> IP: ${maskedIp}`;
}
}
locationPreview.innerHTML = previewText;
}
// 语音录制相关变量
let mediaRecorder = null;
let audioChunks = [];
let audioBlob = null;
let recordingTimer = null;
let recordingStartTime = null;
// 初始化语音录制器
function initVoiceRecorder() {
// 检查浏览器支持
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
showAlert('您的浏览器不支持录音功能,请使用Chrome、Firefox或Edge浏览器', 'warning');
return;
}
}
// 更新计时器
function updateTimer() {
if (recordingStartTime) {
const elapsed = Math.floor((Date.now() - recordingStartTime) / 1000);
const minutes = Math.floor(elapsed / 60);
const seconds = elapsed % 60;
document.getElementById('timerDisplay').textContent =
String(minutes).padStart(2, '0') + ':' + String(seconds).padStart(2, '0');
}
}
// 重置录音器
function resetVoiceRecorder() {
if (mediaRecorder && mediaRecorder.state !== 'inactive') {
mediaRecorder.stop();
}
if (recordingTimer) {
clearInterval(recordingTimer);
recordingTimer = null;
}
audioChunks = [];
audioBlob = null;
recordingStartTime = null;
document.getElementById('startRecordBtn').style.display = 'inline-block';
document.getElementById('stopRecordBtn').style.display = 'none';
document.getElementById('playRecordBtn').style.display = 'none';
document.getElementById('deleteRecordBtn').style.display = 'none';
document.getElementById('recordingStatus').innerHTML = '<i class="fas fa-microphone fa-3x text-muted"></i><p class="mt-2 text-muted">点击下方按钮开始录音</p>';
document.getElementById('recordingTimer').style.display = 'none';
document.getElementById('audioPreview').style.display = 'none';
document.getElementById('audioPreview').src = '';
document.getElementById('audioBlob').value = '';
document.getElementById('timerDisplay').textContent = '00:00';
}
// 将dataURL转换为Blob
function dataURLtoBlob(dataurl) {
const arr = dataurl.split(',');
const mime = arr[0].match(/:(.*?);/)[1];
const bstr = atob(arr[1]);
let n = bstr.length;
const u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], { type: mime });
}
// 获取用户每日限制(用于刷新)
async function getDailyLimits() {
try {
const response = await fetch('api.php?action=get_daily_limits');
const data = await response.json();
if (data.success) {
updateDailyLimitsDisplay(data);
}
} catch (error) {
Logger.error('获取每日限制失败:', error);
}
}
// 更新积分显示
function updateUserPointsDisplay(isFree, pointsDeducted) {
if (window.userInfo) {
if (!isFree) {
window.userInfo.points -= pointsDeducted;
document.getElementById('userPoints').textContent = window.userInfo.points;
}
}
}
// 更新扔瓶按钮状态
function updateThrowButton(disable) {
const throwButton = document.getElementById('throwButton');
if (disable) {
throwButton.disabled = true;
throwButton.innerHTML = '<i class="fas fa-ban"></i> 今日已达上限';
} else {
throwButton.disabled = false;
throwButton.innerHTML = '<i class="fas fa-paper-plane"></i> 扔向大海';
}
}
</script>
</body>
</html>