- login.php: token直进/密码/邮箱三路登录前加已阅协议勾选门 * rules目录无.md → 整段隐藏(不要求勾选/签章) * 已签署 → 无感登录直接跳转(原逻辑), 仅未签才停留要求签署 * 未签 → 须读完协议(滚到底部)才可勾选, 勾选时同步写签章 - api/sync.php: 新增 rules_status / rules_mark / rules_content 三action * rules_status: 返回 skipped/need_agreement/already_signed/fingerprint(协议版本指纹) * rules_mark: 写rules_ack+生成 用户协议/签收回执/<玩家>.md 签章(与Java口径对齐) * rules_content: 后台查看协议原文+已签玩家清单 - core.php: 建 rules_ack 表(player_name主键, 玩家单独记忆勾选/签署) - db_schema_mysql.sql: 同步 rules_ack 快照(utf8mb4_bin, 长文本列不写DEFAULT)
735 lines
36 KiB
PHP
735 lines
36 KiB
PHP
<?php
|
||
require_once __DIR__ . '/security.php'; // 第四层:全局限流(2026-09-30)
|
||
// ★ 人机验证码(2026-10-02):按 config.php 的 CAPTCHA_PROVIDER 选择 CF/VA,密钥在 captcha_keys.php
|
||
require_once __DIR__ . '/captcha_guard.php';
|
||
?>
|
||
<!DOCTYPE html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>SDF1 - Web登录</title>
|
||
<style>
|
||
:root {
|
||
--bg: #0d1117; --card: #161b22; --border: #30363d;
|
||
--text: #e6edf3; --dim: #8b949e; --accent: #58a6ff;
|
||
--green: #3fb950; --red: #f85149; --yellow: #d29922;
|
||
}
|
||
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||
body {
|
||
background: var(--bg); color: var(--text);
|
||
font-family: 'Segoe UI', system-ui, sans-serif;
|
||
min-height: 100vh; display: flex; justify-content: center; align-items: center;
|
||
}
|
||
.login-card {
|
||
background: var(--card); border: 1px solid var(--border);
|
||
border-radius: 12px; padding: 40px; width: 400px; max-width: 90%;
|
||
text-align: center;
|
||
}
|
||
.login-card h1 { color: var(--accent); margin-bottom: 8px; font-size: 24px; }
|
||
.login-card .subtitle { color: var(--dim); margin-bottom: 32px; font-size: 14px; }
|
||
.status-box {
|
||
padding: 20px; border-radius: 8px; margin-bottom: 24px;
|
||
font-size: 14px; line-height: 1.8;
|
||
}
|
||
.status-box.loading { background: rgba(88,166,255,0.1); border: 1px solid var(--accent); }
|
||
.status-box.success { background: rgba(63,185,80,0.1); border: 1px solid var(--green); }
|
||
.status-box.error { background: rgba(248,81,73,0.1); border: 1px solid var(--red); }
|
||
.spinner {
|
||
display: inline-block; width: 20px; height: 20px;
|
||
border: 2px solid var(--dim); border-top-color: var(--accent);
|
||
border-radius: 50%; animation: spin 0.8s linear infinite;
|
||
vertical-align: middle; margin-right: 8px;
|
||
}
|
||
@keyframes spin { to { transform: rotate(360deg); } }
|
||
.btn {
|
||
padding: 12px 24px; border: none; border-radius: 6px;
|
||
cursor: pointer; font-size: 14px; font-weight: 600;
|
||
transition: all 0.2s; text-decoration: none; display: inline-block;
|
||
}
|
||
.btn-primary { background: var(--accent); color: #fff; }
|
||
.btn-primary:hover { background: #79c0ff; }
|
||
.btn-dim { background: var(--border); color: var(--dim); }
|
||
.btn-dim:hover { background: #484f58; color: var(--text); }
|
||
.actions { display: flex; gap: 8px; justify-content: center; }
|
||
.player-name { color: var(--green); font-weight: 600; font-size: 16px; }
|
||
|
||
/* ===== 用户协议勾选门控 ===== */
|
||
.agreement-box {
|
||
margin: 16px 0 4px; padding: 12px 14px;
|
||
background: rgba(88,166,255,0.06); border: 1px solid var(--border);
|
||
border-radius: 8px; text-align: left;
|
||
}
|
||
.agreement-box .ag-row { display: flex; align-items: flex-start; gap: 10px; }
|
||
.agreement-box input[type=checkbox] {
|
||
margin-top: 3px; width: 16px; height: 16px; flex-shrink: 0;
|
||
accent-color: var(--accent); cursor: pointer;
|
||
}
|
||
.agreement-box .ag-text { font-size: 13px; color: var(--dim); line-height: 1.6; }
|
||
.agreement-box .ag-text b { color: var(--text); }
|
||
.agreement-box .ag-link {
|
||
display: inline-block; margin-top: 6px; font-size: 12px; color: var(--accent);
|
||
cursor: pointer; text-decoration: underline;
|
||
}
|
||
.agreement-box .ag-tip { font-size: 11px; color: var(--yellow); margin-top: 6px; }
|
||
/* 阅读弹窗:滚动到底部才解锁勾选 */
|
||
.ag-modal-mask {
|
||
position: fixed; inset: 0; background: rgba(0,0,0,0.6);
|
||
display: none; align-items: center; justify-content: center; z-index: 999;
|
||
}
|
||
.ag-modal-mask.show { display: flex; }
|
||
.ag-modal {
|
||
background: var(--card); border: 1px solid var(--border); border-radius: 12px;
|
||
width: 520px; max-width: 92%; max-height: 80vh; display: flex; flex-direction: column;
|
||
overflow: hidden;
|
||
}
|
||
.ag-modal .ag-head {
|
||
padding: 14px 18px; border-bottom: 1px solid var(--border);
|
||
display: flex; justify-content: space-between; align-items: center;
|
||
}
|
||
.ag-modal .ag-head h3 { margin: 0; font-size: 15px; color: var(--accent); }
|
||
.ag-modal .ag-close { cursor: pointer; color: var(--dim); font-size: 20px; line-height: 1; }
|
||
.ag-body {
|
||
padding: 14px 18px; overflow-y: auto; flex: 1;
|
||
font-size: 13px; line-height: 1.75; color: var(--text); white-space: pre-wrap;
|
||
word-break: break-word;
|
||
}
|
||
.ag-foot {
|
||
padding: 12px 18px; border-top: 1px solid var(--border);
|
||
display: flex; justify-content: space-between; align-items: center; gap: 10px;
|
||
}
|
||
.ag-foot .ag-progress { font-size: 12px; color: var(--dim); }
|
||
.ag-foot .ag-btn {
|
||
padding: 8px 18px; border-radius: 6px; border: none; cursor: pointer; font-size: 13px; font-weight: 600;
|
||
background: var(--accent); color: #fff;
|
||
}
|
||
.ag-foot .ag-btn:disabled { background: var(--border); color: var(--dim); cursor: not-allowed; }
|
||
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div class="login-card">
|
||
<h1>SDF1 Web登录</h1>
|
||
<p class="subtitle" id="subtitle">Token验证中...</p>
|
||
|
||
<!-- Token验证状态 -->
|
||
<div id="tokenSection">
|
||
<div id="statusBox" class="status-box loading">
|
||
<span class="spinner"></span> 正在验证登录Token...
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 密码验证(Token通过后显示) -->
|
||
<div id="passwordSection" style="display:none">
|
||
<div id="pwdStatusBox" class="status-box" style="display:none"></div>
|
||
<div style="text-align:left;margin-bottom:16px">
|
||
<label style="color:var(--dim);font-size:13px;display:block;margin-bottom:4px">玩家名</label>
|
||
<input type="text" id="playerName" placeholder="玩家名" readonly
|
||
style="width:100%;padding:10px 14px;background:var(--bg);border:1px solid var(--border);border-radius:6px;color:var(--text);font-size:14px;outline:none;box-sizing:border-box;opacity:0.7">
|
||
</div>
|
||
<!-- ★ 切换标签:密码/邮箱 -->
|
||
<div style="display:flex;gap:8px;margin-bottom:16px">
|
||
<button class="btn btn-primary" id="tabPassword" onclick="switchLoginTab('password')" style="flex:1">游戏密码</button>
|
||
<button class="btn btn-dim" id="tabEmail" onclick="switchLoginTab('email')" style="flex:1">邮箱验证码</button>
|
||
</div>
|
||
<!-- 密码登录 -->
|
||
<div id="loginPasswordTab">
|
||
<div style="text-align:left;margin-bottom:20px">
|
||
<label style="color:var(--dim);font-size:13px;display:block;margin-bottom:4px">游戏内密码</label>
|
||
<input type="password" id="password" placeholder="输入游戏内密码" maxlength="32"
|
||
style="width:100%;padding:10px 14px;background:var(--bg);border:1px solid var(--border);border-radius:6px;color:var(--text);font-size:14px;outline:none;box-sizing:border-box">
|
||
<p style="color:var(--dim);font-size:12px;margin-top:8px;text-align:left">密码由游戏服务器验证,Web端不存储密码</p>
|
||
</div>
|
||
<?php echo cgWidgetHtml(); ?>
|
||
<button class="btn btn-primary" style="width:100%" onclick="doPasswordLogin()">登录到Web端</button>
|
||
</div>
|
||
<!-- 邮箱验证码登录 -->
|
||
<div id="loginEmailTab" style="display:none">
|
||
<div style="text-align:left;margin-bottom:12px">
|
||
<label style="color:var(--dim);font-size:13px;display:block;margin-bottom:4px">绑定邮箱</label>
|
||
<input type="text" id="maskedEmail" readonly placeholder="加载中..."
|
||
style="width:100%;padding:10px 14px;background:var(--bg);border:1px solid var(--border);border-radius:6px;color:var(--text);font-size:14px;outline:none;box-sizing:border-box;opacity:0.7">
|
||
</div>
|
||
<div style="display:flex;gap:8px;margin-bottom:20px">
|
||
<input type="text" id="emailCode" placeholder="6位验证码" maxlength="6"
|
||
style="flex:1;padding:10px 14px;background:var(--bg);border:1px solid var(--border);border-radius:6px;color:var(--text);font-size:14px;outline:none;box-sizing:border-box">
|
||
<button class="btn btn-primary" id="sendCodeBtn" onclick="sendEmailCode()" style="white-space:nowrap">发送验证码</button>
|
||
</div>
|
||
<button class="btn btn-primary" style="width:100%" onclick="doEmailLogin()">登录到Web端</button>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- ★ 用户协议送达:勾选门控(rules 目录无协议时整段隐藏) -->
|
||
<div id="agreementSection" class="agreement-box" style="display:none">
|
||
<div class="ag-row">
|
||
<input type="checkbox" id="agreementCb">
|
||
<div class="ag-text" id="agText">
|
||
我已阅读并同意 <b id="agTitle">用户协议</b> 的全部条款。
|
||
<br><span class="ag-link" onclick="openAgreement()">📄 查看协议原文(须读完)</span>
|
||
<div class="ag-tip" id="agTip" style="display:none"></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<!-- 登录按钮区 -->
|
||
<div class="actions" id="actions" style="display:none"></div>
|
||
|
||
<!-- ★ 协议阅读弹窗:滚到底部才能勾选 -->
|
||
<div class="ag-modal-mask" id="agModalMask">
|
||
<div class="ag-modal">
|
||
<div class="ag-head">
|
||
<h3 id="agModalTitle">服务器用户协议</h3>
|
||
<span class="ag-close" onclick="closeAgreement()">×</span>
|
||
</div>
|
||
<div class="ag-body" id="agModalBody"></div>
|
||
<div class="ag-foot">
|
||
<span class="ag-progress" id="agProgress">尚未阅读</span>
|
||
<button class="ag-btn" id="agModalBtn" disabled onclick="confirmAgreement()">我已完成阅读</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<script>
|
||
const TOKEN = new URLSearchParams(location.search).get('token') || '';
|
||
const statusBox = document.getElementById('statusBox');
|
||
const actionsDiv = document.getElementById('actions');
|
||
const tokenSection = document.getElementById('tokenSection');
|
||
const passwordSection = document.getElementById('passwordSection');
|
||
const subtitle = document.getElementById('subtitle');
|
||
const pwdStatusBox = document.getElementById('pwdStatusBox');
|
||
|
||
// ===== 安全拦截:无Token则拒绝访问 =====
|
||
if (!TOKEN) {
|
||
document.body.innerHTML = `
|
||
<div style="text-align:center;padding:60px 20px;">
|
||
<h1 style="color:#f85149;margin-bottom:20px">🚫 访问被拒绝</h1>
|
||
<p style="color:var(--dim);font-size:16px;line-height:1.8">
|
||
Web登录必须通过游戏内Token验证。<br>
|
||
请在游戏中执行 <b style="color:var(--accent)"> <b>/web</b><br>
|
||
然后点击上方链接登录Web端。
|
||
</p>
|
||
</div>
|
||
`;
|
||
console.error('[登录页面] 无Token访问被拒绝');
|
||
throw new Error('NoToken');
|
||
}
|
||
|
||
// ===== 用户协议送达门控 =====
|
||
// 语义:rules 目录无 .md → 整段隐藏(无需勾选、不要求签章);
|
||
// 有协议 → 必须勾选"已阅"且读过弹窗(滚到底部)才能登录;已签过 → 默认勾选。
|
||
let AG = { loaded: false, skipped: true, need: false, fingerprint: '', title: '服务器用户协议', already: false };
|
||
let AG_READ = false; // 本次会话是否完整阅读过协议
|
||
|
||
// 加载协议状态(登录页打开即调)
|
||
async function loadAgreementStatus(player) {
|
||
try {
|
||
const url = new URL('./api/sync.php', location.href);
|
||
url.searchParams.set('action', 'rules_status');
|
||
if (player) url.searchParams.set('player', player);
|
||
const res = await fetch(url, { method: 'POST', headers: {'Content-Type': 'application/json'} });
|
||
const data = await res.json();
|
||
if (data.success && data.data) {
|
||
AG.skipped = !!data.data.skipped;
|
||
AG.need = !AG.skipped;
|
||
AG.fingerprint = data.data.fingerprint || '';
|
||
AG.title = data.data.title || AG.title;
|
||
AG.already = !!data.data.already_signed;
|
||
} else {
|
||
AG.skipped = true; // 接口异常时降级为跳过(不阻塞登录,避免 PHP 故障锁死用户)
|
||
AG.need = false;
|
||
}
|
||
} catch (e) {
|
||
AG.skipped = true; AG.need = false; // 网络异常降级跳过,不锁死登录
|
||
}
|
||
AG.loaded = true;
|
||
applyAgreementUI();
|
||
}
|
||
|
||
function applyAgreementUI() {
|
||
const sec = document.getElementById('agreementSection');
|
||
const cb = document.getElementById('agreementCb');
|
||
if (!AG.loaded) return;
|
||
if (AG.skipped) {
|
||
sec.style.display = 'none';
|
||
setAgreementGate(false);
|
||
return;
|
||
}
|
||
sec.style.display = 'block';
|
||
document.getElementById('agTitle').textContent = AG.title;
|
||
if (AG.already) {
|
||
cb.checked = true;
|
||
AG_READ = true; // 已签过:本次会话视为已读,直接放行
|
||
} else {
|
||
cb.checked = false;
|
||
AG_READ = false;
|
||
}
|
||
setAgreementGate(true);
|
||
}
|
||
|
||
// 勾选框只在「已勾选 且 已阅读」时可勾(已签玩家自动满足)
|
||
function setAgreementGate(on) {
|
||
const cb = document.getElementById('agreementCb');
|
||
const tip = document.getElementById('agTip');
|
||
if (!on) { cb.disabled = false; tip.style.display = 'none'; syncLoginButtons(); return; }
|
||
// 需要协议:未读且未签 → 锁勾选框,提示先阅读
|
||
if (AG.already) { cb.disabled = false; tip.style.display = 'none'; syncLoginButtons(); return; }
|
||
if (AG_READ) { cb.disabled = false; tip.style.display = 'none'; syncLoginButtons(); return; }
|
||
cb.disabled = true;
|
||
cb.checked = false;
|
||
tip.style.display = 'block';
|
||
tip.textContent = '请先点击上方「查看协议原文」完整阅读(滚动至底部)后,方可勾选。';
|
||
syncLoginButtons();
|
||
}
|
||
|
||
document.addEventListener('DOMContentLoaded', () => {
|
||
// 勾选联动
|
||
document.getElementById('agreementCb').addEventListener('change', (e) => {
|
||
if (e.target.checked) {
|
||
// 勾选成功 = 触发签章(PHP 同步写 rules_ack + 生成回执)
|
||
fireAgreementMark();
|
||
document.getElementById('agTip').style.display = 'none';
|
||
}
|
||
syncLoginButtons();
|
||
});
|
||
});
|
||
|
||
// 打开阅读弹窗
|
||
function openAgreement() {
|
||
if (!AG.need) return;
|
||
loadAgreementContent();
|
||
const mask = document.getElementById('agModalMask');
|
||
mask.classList.add('show');
|
||
const body = document.getElementById('agModalBody');
|
||
body.scrollTop = 0;
|
||
updateAgProgress();
|
||
}
|
||
function closeAgreement() {
|
||
document.getElementById('agModalMask').classList.remove('show');
|
||
}
|
||
|
||
// 拉协议原文填充弹窗
|
||
async function loadAgreementContent() {
|
||
const body = document.getElementById('agModalBody');
|
||
body.textContent = '加载中...';
|
||
try {
|
||
const url = new URL('./api/sync.php', location.href);
|
||
url.searchParams.set('action', 'rules_content');
|
||
const res = await fetch(url, { method: 'POST', headers: {'Content-Type': 'application/json'} });
|
||
const data = await res.json();
|
||
if (data.success && data.data && data.data.has_agreement) {
|
||
body.textContent = data.data.content;
|
||
document.getElementById('agModalTitle').textContent = data.data.title || AG.title;
|
||
} else {
|
||
body.textContent = '(服务器未配置用户协议)';
|
||
}
|
||
// 滚到顶部,让用户从头读
|
||
setTimeout(() => { body.scrollTop = 0; updateAgProgress(); }, 30);
|
||
} catch (e) {
|
||
body.textContent = '协议加载失败:' + e.message;
|
||
}
|
||
}
|
||
|
||
// 阅读进度:滚到底部(容差 8px)→ 解锁勾选
|
||
document.addEventListener('DOMContentLoaded', () => {
|
||
document.getElementById('agModalBody').addEventListener('scroll', updateAgProgress);
|
||
});
|
||
function updateAgProgress() {
|
||
const body = document.getElementById('agModalBody');
|
||
const prog = document.getElementById('agProgress');
|
||
const btn = document.getElementById('agModalBtn');
|
||
const remain = body.scrollHeight - body.clientHeight - body.scrollTop;
|
||
if (remain <= 8) {
|
||
AG_READ = true;
|
||
prog.textContent = '已读至底部 ✓';
|
||
prog.style.color = 'var(--green)';
|
||
btn.disabled = false;
|
||
setAgreementGate(AG.need); // 已读 → 解锁勾选框
|
||
} else {
|
||
prog.textContent = '请向下滚动至协议末尾';
|
||
prog.style.color = 'var(--dim)';
|
||
if (!AG.already) btn.disabled = true; // 已签玩家本就不需要再读
|
||
}
|
||
}
|
||
|
||
// 点「我已完成阅读」→ 关闭弹窗并自动勾选
|
||
function confirmAgreement() {
|
||
closeAgreement();
|
||
const cb = document.getElementById('agreementCb');
|
||
if (!cb.checked) {
|
||
cb.checked = true;
|
||
fireAgreementMark();
|
||
document.getElementById('agTip').style.display = 'none';
|
||
}
|
||
syncLoginButtons();
|
||
}
|
||
|
||
// 勾选时同步写签章(PHP rules_ack + 回执文件)
|
||
let AG_MARKED = false;
|
||
async function fireAgreementMark() {
|
||
if (AG_MARKED) return;
|
||
const player = document.getElementById('playerName')?.value?.trim() || '';
|
||
if (!player) return;
|
||
AG_MARKED = true;
|
||
try {
|
||
const url = new URL('./api/sync.php', location.href);
|
||
url.searchParams.set('action', 'rules_mark');
|
||
const res = await fetch(url, {
|
||
method: 'POST',
|
||
headers: {'Content-Type': 'application/json'},
|
||
body: JSON.stringify({player: player, fingerprint: AG.fingerprint})
|
||
});
|
||
const data = await res.json();
|
||
if (!data.success) console.warn('[agreement] 签章回执:', data.message);
|
||
} catch (e) { console.warn('[agreement] 签章失败(不阻塞登录):', e.message); }
|
||
}
|
||
|
||
// 三条登录路按钮的可用性 = 协议门是否放行
|
||
function agreementGatePass() {
|
||
if (!AG.loaded || AG.skipped || !AG.need) return true;
|
||
const cb = document.getElementById('agreementCb');
|
||
return !!(cb && cb.checked);
|
||
}
|
||
function syncLoginButtons() {
|
||
const pass = agreementGatePass();
|
||
// 密码/邮箱登录按钮
|
||
document.querySelectorAll('#loginPasswordTab .btn-primary, #loginEmailTab .btn-primary').forEach(b => {
|
||
b.disabled = !pass;
|
||
});
|
||
const hint = document.getElementById('agTip');
|
||
}
|
||
|
||
// 拦截:未过协议门 → 禁止发起登录
|
||
function requireAgreement() {
|
||
if (agreementGatePass()) return true;
|
||
const tip = document.getElementById('agTip');
|
||
if (tip) { tip.style.display = 'block'; tip.textContent = '请先勾选「已阅用户协议」并完成阅读。'; }
|
||
alert('请先阅读并勾选「已阅用户协议」后再登录');
|
||
return false;
|
||
}
|
||
|
||
// ===== 有Token → 验证Token =====
|
||
tokenSection.style.display = 'block';
|
||
// ★ 加载协议门控(player.php 已签过则自动勾选)
|
||
loadAgreementStatus('');
|
||
doTokenLogin();
|
||
|
||
// ===== 未签玩家:勾选门放行后点「进入 Web 端」完成跳转 =====
|
||
function enterWeb(player) {
|
||
// 二次校验协议门(防止用户没真正勾选就点按钮)
|
||
if (AG.need && !AG.already) {
|
||
if (!agreementGatePass()) {
|
||
alert('请先完整阅读并勾选「已阅用户协议」');
|
||
return;
|
||
}
|
||
}
|
||
fireAgreementMark();
|
||
statusBox.className = 'status-box success';
|
||
statusBox.innerHTML = '✅ 已签署用户协议!<br><br>欢迎, <span class="player-name">' + player + '</span><br><small style="color:var(--dim)">正在跳转到玩家商城...</small>';
|
||
fetch('./api/sync.php?action=web_access_check', {
|
||
method: 'POST',
|
||
headers: {'Content-Type': 'application/json'},
|
||
body: JSON.stringify({web_token: TOKEN, access_action: 'view'})
|
||
}).catch(() => {});
|
||
setTimeout(() => {
|
||
window.location.href = 'player.php?login=' + encodeURIComponent(player) + '&token=' + encodeURIComponent(TOKEN);
|
||
}, 500);
|
||
}
|
||
|
||
// ===== Token验证 =====
|
||
async function doTokenLogin() {
|
||
try {
|
||
const apiUrl = './api/sync.php?action=validate_weblogin_token';
|
||
const res = await fetch(apiUrl, {
|
||
method: 'POST',
|
||
headers: {'Content-Type': 'application/json'},
|
||
body: JSON.stringify({web_token: TOKEN})
|
||
});
|
||
const text = await res.text();
|
||
|
||
let data;
|
||
try {
|
||
data = JSON.parse(text);
|
||
} catch (parseErr) {
|
||
statusBox.className = 'status-box error';
|
||
statusBox.innerHTML = '❌ 服务器响应异常<br><br><code style="font-size:11px;word-break:break-all;color:var(--dim)">' + text.substring(0, 300) + '</code>';
|
||
actionsDiv.style.display = 'flex';
|
||
actionsDiv.innerHTML = '<span style="color:var(--dim);font-size:13px">请在游戏中重新执行 <b>/sdf1_login weblogin</b></span>';
|
||
return;
|
||
}
|
||
|
||
if (data.success && data.data) {
|
||
const player = data.data.player;
|
||
localStorage.setItem('sdf1_player', player);
|
||
localStorage.setItem('sdf1_token', TOKEN);
|
||
localStorage.setItem('sdf1_login_time', Date.now());
|
||
|
||
// ★ 协议门控:先加载该玩家签署状态。
|
||
// 语义(用户明令):已签署 → 无感登录,按原逻辑直接跳转,不拦截;
|
||
// 仅「有协议且未签」才停留要求签署。无协议(skipped) → 同样无感放行。
|
||
await loadAgreementStatus(player);
|
||
const needSign = (AG.need && !AG.already);
|
||
if (needSign) {
|
||
// 仅未签玩家:停留 + 展示勾选门 + 「进入 Web 端」按钮
|
||
statusBox.className = 'status-box success';
|
||
statusBox.innerHTML = '✅ Token验证成功!<br><br>欢迎, <span class="player-name">' + player + '</span><br><small style="color:var(--yellow)">首次使用请勾选「已阅用户协议」完成签署后再进入。</small>';
|
||
applyAgreementUI();
|
||
document.getElementById('agreementSection').style.display = AG.need ? 'block' : 'none';
|
||
actionsDiv.style.display = 'flex';
|
||
actionsDiv.innerHTML = '<button class="btn btn-primary" style="width:100%" onclick="enterWeb(' + JSON.stringify(player) + ')">进入 Web 端</button>';
|
||
return;
|
||
}
|
||
// ★ 已签 / 无协议 → 无感登录:直接跳转(fireAgreementMark 幂等,未签才补写)
|
||
fireAgreementMark();
|
||
|
||
// ★ Token验证成功 → 直接跳转到player.php(跳过密码,token本身即身份证明)
|
||
statusBox.className = 'status-box success';
|
||
statusBox.innerHTML = '✅ Token验证成功!<br><br>欢迎, <span class="player-name">' + player + '</span><br><small style="color:var(--dim)">正在跳转到玩家商城...</small>';
|
||
|
||
// ★ 同时写入web_session_log(让validateWebAccess能识别在线状态)
|
||
fetch('./api/sync.php?action=web_access_check', {
|
||
method: 'POST',
|
||
headers: {'Content-Type': 'application/json'},
|
||
body: JSON.stringify({web_token: TOKEN, access_action: 'view'})
|
||
}).catch(() => {});
|
||
|
||
setTimeout(() => {
|
||
// ★ 跳转时带上token,确保player.php一定能读到
|
||
window.location.href = 'player.php?login=' + encodeURIComponent(player) + '&token=' + encodeURIComponent(TOKEN);
|
||
}, 800);
|
||
} else {
|
||
statusBox.className = 'status-box error';
|
||
statusBox.innerHTML = '❌ 登录失败<br><br>' + (data.message || 'Token无效或已过期');
|
||
actionsDiv.style.display = 'flex';
|
||
actionsDiv.innerHTML = '<span style="color:var(--dim);font-size:13px">请在游戏中重新执行 <b>/sdf1_login weblogin</b></span>';
|
||
}
|
||
} catch (e) {
|
||
statusBox.className = 'status-box error';
|
||
statusBox.innerHTML = '❌ 连接失败<br><br>' + e.message;
|
||
}
|
||
}
|
||
|
||
// ===== 密码登录 =====
|
||
async function doPasswordLogin() {
|
||
const player = document.getElementById('playerName').value.trim();
|
||
const password = document.getElementById('password').value;
|
||
|
||
// ★ 协议门:未签先要求签署(已签/无协议自动放行,requireAgreement 幂等)
|
||
if (!requireAgreement()) return;
|
||
fireAgreementMark(); // 放行后补写/确认签章(幂等)
|
||
if (!password) { showPwdError('请输入密码'); return; }
|
||
|
||
// ★ 人机验证(2026-10-02):CF 渲染完即有 token(基本无感);VA 没做会在这里拉起验证
|
||
let cap = {};
|
||
if (window.CaptchaGuard) {
|
||
try { cap = await CaptchaGuard.payload(); }
|
||
catch (e) { showPwdError(e.message || '请先完成人机验证'); return; }
|
||
}
|
||
|
||
pwdStatusBox.className = 'status-box loading';
|
||
pwdStatusBox.style.display = 'block';
|
||
pwdStatusBox.innerHTML = '<span class="spinner"></span> 正在验证密码...';
|
||
|
||
try {
|
||
// 1. 提交登录请求
|
||
const apiUrl = './api/sync.php';
|
||
const reqRes = await fetch(apiUrl + '?action=web_login_request', {
|
||
method: 'POST',
|
||
headers: {'Content-Type': 'application/json'},
|
||
body: JSON.stringify(Object.assign({player: player, password: password}, cap))
|
||
});
|
||
const reqData = await reqRes.json();
|
||
|
||
if (!reqData.success) {
|
||
showPwdError(reqData.message || '提交失败');
|
||
return;
|
||
}
|
||
|
||
const requestId = reqData.data.request_id;
|
||
|
||
// 2. 轮询结果
|
||
pwdStatusBox.innerHTML = '<span class="spinner"></span> 等待游戏服务器验证密码...';
|
||
let attempts = 0;
|
||
const maxAttempts = 60; // 最多等60秒
|
||
|
||
const pollInterval = setInterval(async () => {
|
||
attempts++;
|
||
if (attempts > maxAttempts) {
|
||
clearInterval(pollInterval);
|
||
showPwdError('验证超时,请稍后重试');
|
||
return;
|
||
}
|
||
|
||
try {
|
||
const pollRes = await fetch(apiUrl + '?action=check_web_login_result&player=' + encodeURIComponent(player) + '&request_id=' + requestId);
|
||
const pollText = await pollRes.text();
|
||
const pollData = JSON.parse(pollText);
|
||
|
||
console.log('[poll] status:', pollData.status || pollData.data?.status, 'data:', JSON.stringify(pollData.data).substring(0, 100));
|
||
|
||
if (pollData.success && pollData.data) {
|
||
const result = pollData.data;
|
||
|
||
if (result.status === 'success') {
|
||
clearInterval(pollInterval);
|
||
// 登录成功 → 进入玩家商城
|
||
pwdStatusBox.className = 'status-box success';
|
||
pwdStatusBox.innerHTML = '✅ 登录成功!<br><br>欢迎, <span class="player-name">' + player + '</span><br><small style="color:var(--dim)">正在跳转...</small>';
|
||
|
||
// ★ token已存入localStorage,跳转player.php带token参数
|
||
setTimeout(() => {
|
||
window.location.href = 'player.php?login=' + encodeURIComponent(player) + '&token=' + encodeURIComponent(TOKEN);
|
||
}, 500);
|
||
} else if (result.status === 'failed') {
|
||
clearInterval(pollInterval);
|
||
showPwdError(result.message || '密码错误');
|
||
}
|
||
// status === 'pending' → 继续轮询
|
||
} else if (pollData.data && pollData.data.status === 'pending') {
|
||
// 仍在pending状态,可能是请求过期,继续轮询
|
||
console.log('[poll] 请求pending或过期,继续等待...');
|
||
} else {
|
||
console.warn('[poll] 轮询响应异常:', JSON.stringify(pollData).substring(0, 200));
|
||
}
|
||
} catch (pollErr) {
|
||
console.error('[poll] 轮询异常:', pollErr);
|
||
}
|
||
}, 1000);
|
||
|
||
} catch (e) {
|
||
showPwdError('连接失败: ' + e.message);
|
||
}
|
||
}
|
||
|
||
function showPwdError(msg) {
|
||
pwdStatusBox.className = 'status-box error';
|
||
pwdStatusBox.style.display = 'block';
|
||
pwdStatusBox.innerHTML = '❌ ' + msg;
|
||
}
|
||
|
||
// 回车提交
|
||
document.getElementById('password').addEventListener('keydown', function(e) {
|
||
if (e.key === 'Enter') doPasswordLogin();
|
||
});
|
||
document.getElementById('emailCode').addEventListener('keydown', function(e) {
|
||
if (e.key === 'Enter') doEmailLogin();
|
||
});
|
||
|
||
// ===== 标签切换 =====
|
||
let currentLoginTab = 'password';
|
||
function switchLoginTab(tab) {
|
||
currentLoginTab = tab;
|
||
document.getElementById('loginPasswordTab').style.display = tab === 'password' ? 'block' : 'none';
|
||
document.getElementById('loginEmailTab').style.display = tab === 'email' ? 'block' : 'none';
|
||
document.getElementById('tabPassword').className = tab === 'password' ? 'btn btn-primary' : 'btn btn-dim';
|
||
document.getElementById('tabEmail').className = tab === 'email' ? 'btn btn-primary' : 'btn btn-dim';
|
||
document.getElementById('pwdStatusBox').style.display = 'none';
|
||
// 切到邮箱时加载脱敏邮箱
|
||
if (tab === 'email') loadMaskedEmail();
|
||
}
|
||
|
||
// ===== 加载脱敏邮箱 =====
|
||
async function loadMaskedEmail() {
|
||
const player = document.getElementById('playerName').value;
|
||
try {
|
||
const url = new URL('./api/sync.php', location.href);
|
||
url.searchParams.set('action', 'get_player_email');
|
||
url.searchParams.set('player', player);
|
||
const res = await fetch(url, { method: 'POST', headers: {'Content-Type': 'application/json'} });
|
||
const data = await res.json();
|
||
if (data.success && data.data) {
|
||
document.getElementById('maskedEmail').value = data.data.masked_email || '未绑定邮箱';
|
||
} else {
|
||
document.getElementById('maskedEmail').value = '未绑定邮箱';
|
||
}
|
||
} catch (e) {
|
||
document.getElementById('maskedEmail').value = '加载失败';
|
||
}
|
||
}
|
||
|
||
// ===== 发送邮箱验证码 =====
|
||
async function sendEmailCode() {
|
||
const player = document.getElementById('playerName').value;
|
||
const btn = document.getElementById('sendCodeBtn');
|
||
btn.disabled = true;
|
||
btn.textContent = '发送中...';
|
||
try {
|
||
const url = new URL('./api/sync.php', location.href);
|
||
url.searchParams.set('action', 'send_email_code');
|
||
const res = await fetch(url, {
|
||
method: 'POST',
|
||
headers: {'Content-Type': 'application/json'},
|
||
body: JSON.stringify({player: player, web_token: TOKEN})
|
||
});
|
||
const data = await res.json();
|
||
if (data.success) {
|
||
showPwdSuccess('验证码已发送至 ' + (data.data.masked_email || '**'));
|
||
let countdown = 60;
|
||
const timer = setInterval(() => {
|
||
btn.textContent = countdown + '秒后重发';
|
||
countdown--;
|
||
if (countdown <= 0) {
|
||
clearInterval(timer);
|
||
btn.disabled = false;
|
||
btn.textContent = '发送验证码';
|
||
}
|
||
}, 1000);
|
||
} else {
|
||
showPwdError(data.message || '发送失败');
|
||
btn.disabled = false;
|
||
btn.textContent = '发送验证码';
|
||
}
|
||
} catch (e) {
|
||
showPwdError('连接失败: ' + e.message);
|
||
btn.disabled = false;
|
||
btn.textContent = '发送验证码';
|
||
}
|
||
}
|
||
|
||
// ===== 邮箱验证码登录 =====
|
||
async function doEmailLogin() {
|
||
const player = document.getElementById('playerName').value;
|
||
const code = document.getElementById('emailCode').value.trim();
|
||
// ★ 协议门:未签先要求签署(已签/无协议自动放行)
|
||
if (!requireAgreement()) return;
|
||
fireAgreementMark(); // 放行后补写/确认签章(幂等)
|
||
if (!code) { showPwdError('请输入验证码'); return; }
|
||
if (code.length !== 6) { showPwdError('验证码为6位数字'); return; }
|
||
|
||
pwdStatusBox.className = 'status-box loading';
|
||
pwdStatusBox.style.display = 'block';
|
||
pwdStatusBox.innerHTML = '<span class="spinner"></span> 正在验证邮箱验证码...';
|
||
|
||
try {
|
||
const url = new URL('./api/sync.php', location.href);
|
||
url.searchParams.set('action', 'verify_email_code');
|
||
const res = await fetch(url, {
|
||
method: 'POST',
|
||
headers: {'Content-Type': 'application/json'},
|
||
body: JSON.stringify({player: player, code: code, web_token: TOKEN})
|
||
});
|
||
const data = await res.json();
|
||
if (data.success) {
|
||
localStorage.setItem('sdf1_player', player);
|
||
localStorage.setItem('sdf1_token', TOKEN);
|
||
pwdStatusBox.className = 'status-box success';
|
||
pwdStatusBox.innerHTML = '✅ 邮箱验证成功!<br><br>欢迎, <span class="player-name">' + player + '</span><br><small style="color:var(--dim)">正在跳转...</small>';
|
||
setTimeout(() => {
|
||
window.location.href = 'player.php?login=' + encodeURIComponent(player);
|
||
}, 500);
|
||
} else {
|
||
showPwdError(data.message || '验证失败');
|
||
}
|
||
} catch (e) {
|
||
showPwdError('连接失败: ' + e.message);
|
||
}
|
||
}
|
||
|
||
function showPwdSuccess(msg) {
|
||
pwdStatusBox.className = 'status-box success';
|
||
pwdStatusBox.style.display = 'block';
|
||
pwdStatusBox.innerHTML = '✅ ' + msg;
|
||
}
|
||
</script>
|
||
</body>
|
||
</html>
|