79 lines
3.6 KiB
PHP
79 lines
3.6 KiB
PHP
<?php
|
|
// 每日签到:登录用户领取积分
|
|
require __DIR__ . '/includes/init.php';
|
|
requireLogin('sign.php');
|
|
$pageTitle = '每日签到';
|
|
$user = currentUser();
|
|
|
|
$enabled = getSetting('points_enabled', '1') === '1';
|
|
$signPoints = max(0, (int) getSetting('points_sign', '5'));
|
|
$points = getUserPoints($user['id']);
|
|
$signed = hasSignedToday($user['id']);
|
|
$msg = '';
|
|
$err = '';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['dosign'])) {
|
|
verifyCsrf();
|
|
if (!$enabled) {
|
|
$err = '积分系统已关闭';
|
|
} elseif ($signed) {
|
|
$err = '今天已经签到过了,明天再来吧~';
|
|
} else {
|
|
$pdo = db();
|
|
try {
|
|
$pdo->beginTransaction();
|
|
// 写入签到记录(uk_user_date 唯一,重复签到会抛异常)
|
|
$pdo->prepare('INSERT INTO ' . tn('sign_logs') . ' (user_id, sign_date, points, created_at) VALUES (?, CURDATE(), ?, NOW())')
|
|
->execute([$user['id'], $signPoints]);
|
|
// 同事务内加积分并写流水(避免嵌套事务)
|
|
$pdo->prepare('UPDATE ' . tn('users') . ' SET points = points + ? WHERE id = ?')
|
|
->execute([$signPoints, $user['id']]);
|
|
$bal = getUserPoints($user['id']);
|
|
$pdo->prepare('INSERT INTO ' . tn('points_log') . ' (user_id, type, amount, balance, remark, created_at) VALUES (?, ?, ?, ?, ?, NOW())')
|
|
->execute([$user['id'], 'sign', $signPoints, $bal, '每日签到']);
|
|
$pdo->commit();
|
|
$points = getUserPoints($user['id']);
|
|
$signed = true;
|
|
$msg = '签到成功,获得 ' . $signPoints . ' 积分!';
|
|
} catch (Throwable $e) {
|
|
if ($pdo->inTransaction()) $pdo->rollBack();
|
|
$err = '签到失败:' . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
|
|
require 'includes/header.php';
|
|
?>
|
|
<section class="section">
|
|
<h2 class="sec-title"><i class="fas fa-calendar-check"></i> 每日签到</h2>
|
|
|
|
<div class="sign-card">
|
|
<div class="sign-points">
|
|
<div class="sp-num"><?= (int) $points ?></div>
|
|
<div class="sp-label">我的积分</div>
|
|
</div>
|
|
<div class="sign-tip">
|
|
<?php if (!$enabled): ?>
|
|
<p class="muted">积分系统当前已关闭,暂不可签到。</p>
|
|
<?php elseif ($signed): ?>
|
|
<p class="banner-ok"><i class="fas fa-check-circle"></i> 今天已签到,明日可再得 <?= $signPoints ?> 积分。</p>
|
|
<?php else: ?>
|
|
<p>每日签到可领取 <strong><?= $signPoints ?></strong> 积分,积分可用于兑换设置了「积分价」的商品。</p>
|
|
<?php endif; ?>
|
|
</div>
|
|
<?php if ($err): ?><p class="form-err"><?= h($err) ?></p><?php endif; ?>
|
|
<?php if ($msg): ?><p class="banner-ok"><i class="fas fa-check-circle"></i> <?= h($msg) ?></p><?php endif; ?>
|
|
<?php if ($enabled && !$signed): ?>
|
|
<form method="post" action="" class="sign-form">
|
|
<?= csrfField() ?>
|
|
<button type="submit" name="dosign" class="btn btn-primary"><i class="fas fa-calendar-check"></i> 立即签到(+<?= $signPoints ?> 积分)</button>
|
|
</form>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<p class="muted" style="margin-top:16px;">
|
|
<i class="fas fa-gift"></i> 想赚更多积分?<a href="invite.php">邀请好友</a> 注册成功可得 <?= (int) getSetting('points_invite', '20') ?> 积分。
|
|
</p>
|
|
</section>
|
|
<?php require 'includes/footer.php'; ?>
|