Files
2026-08-14 10:55:06 +08:00

77 lines
2.9 KiB
PHP

<?php
// 邮箱验证 / 重发验证邮件
require __DIR__ . '/includes/init.php';
$pageTitle = '邮箱验证';
$msg = '';
$ok = false;
// 重发验证邮件(需登录)
if (isset($_GET['resend'])) {
requireLogin('verify.php?resend=1');
$user = currentUser();
if (!empty($user['verified'])) {
$msg = '你的邮箱已完成验证,无需重复操作。';
$ok = true;
} else {
$res = sendVerifyMail($user['id']);
$msg = $res['ok']
? '验证邮件已重新发送至 ' . h($user['email']) . ',请查收并点击其中的链接。'
: '发送失败:' . h($res['error']);
$ok = $res['ok'];
}
} elseif (isset($_GET['token'])) {
// 点击邮件链接完成验证
$token = trim($_GET['token']);
if ($token === '') {
$msg = '验证链接无效。';
} else {
$stmt = db()->prepare('SELECT id FROM ' . tn('users') . ' WHERE email_token = ?');
$stmt->execute([$token]);
$row = $stmt->fetch();
if (!$row) {
// 可能是重复点击:若当前登录用户已完成验证,则提示成功而非报错
$uid = $_SESSION['user_id'] ?? 0;
if ($uid > 0) {
$chk = db()->prepare('SELECT verified FROM ' . tn('users') . ' WHERE id = ?');
$chk->execute([$uid]);
$verified = (int) $chk->fetchColumn();
if ($verified === 1) {
$msg = '你的邮箱已经完成验证,无需重复操作。';
$ok = true;
} else {
$msg = '验证链接无效或已使用,请重新获取。';
}
} else {
$msg = '验证链接无效或已使用,请重新获取。';
}
} else {
db()->prepare('UPDATE ' . tn('users') . ' SET verified = 1, email_token = ? WHERE id = ?')
->execute(['', $row['id']]);
$msg = '邮箱验证成功!你也可以在「用户设置」中完善昵称与资料。';
$ok = true;
}
}
} else {
$msg = '链接不完整,请使用邮件中的完整验证链接。';
}
require 'includes/header.php';
?>
<section class="section auth-section">
<div class="auth-card verify-card">
<div class="verify-icon <?= $ok ? 'ok' : 'bad' ?>">
<i class="fas <?= $ok ? 'fa-check-circle' : 'fa-exclamation-triangle' ?>"></i>
</div>
<h2 class="verify-title"><?= $ok ? '验证成功' : '验证提示' ?></h2>
<p class="verify-msg"><?= h($msg) ?></p>
<div class="verify-actions">
<a href="index.php" class="btn btn-primary">返回商城首页</a>
<?php if ($ok && !empty($_SESSION['user_id'])): ?>
<a href="settings.php" class="btn btn-ghost">前往用户设置</a>
<?php endif; ?>
</div>
</div>
</section>
<?php require 'includes/footer.php'; ?>