From 82d352f1c38aa4359df10139fe5f2add041d982d Mon Sep 17 00:00:00 2001 From: ypsdf1 Date: Wed, 28 Oct 2026 18:40:30 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC58=E6=89=B9=E7=BB=AD=EF=BC=9A=E4=BF=A1?= =?UTF-8?q?=E8=AA=89=E7=A7=AF=E5=88=86API=E6=96=B0=E5=A2=9E=E7=A7=AF?= =?UTF-8?q?=E5=88=86=E7=AE=A1=E7=90=86+Java=E5=8F=8C=E5=90=91=E5=90=8C?= =?UTF-8?q?=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit api/credit.php: - 新增 admin_player_score:管理员查询玩家当前积分 - 新增 admin_adjust_score:管理员手动加减积分(直接调整玩家积分) source='admin' 标记来源,积分范围限制0~1000 - 新增 sync_push:接收Java端推送的积分数据(覆盖PHP副本) 批量更新玩家积分+积分记录去重插入 - 新增 sync_pull:返回PHP端所有积分数据供Java拉回 密钥校验用 SECRET_KEY(与现有Java-PHP同步一致) admin.php: - loadCredit 新增积分管理板块(手动加减积分界面) 查询玩家积分 → 显示当前分值 → 输入变动值+理由 → 加分/扣分按钮 加分绿色按钮,扣分红色按钮,扣分后不能为负数 - 新增 creditQueryScore/doCreditAdjustScore JS函数 --- admin.php | 84 ++++++++++++++++++++++++++ api/credit.php | 157 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 241 insertions(+) diff --git a/admin.php b/admin.php index 5d4c6d2..809868a 100644 --- a/admin.php +++ b/admin.php @@ -3450,6 +3450,19 @@ async function loadCredit(el) { } html += ''; + // 积分管理 — 管理员手动加减积分(PHP 端副本,Java 定时拉回) + html += '
'; + html += '

⚖️ 积分管理(手动加减)

'; + html += '
'; + html += '
'; + html += ''; + html += ''; + html += '
'; + html += ''; + html += '
'; + html += '
'; + html += '
'; + // 积分规则 — 使用 .card class html += '
'; html += '

📋 积分规则

'; @@ -3563,6 +3576,77 @@ async function creditToggleRule(ruleId, enabled) { } } +async function creditQueryScore() { + const target = document.getElementById('creditAdjustTarget')?.value?.trim() || ''; + if (!target) { glassAlert('请输入玩家名'); return; } + const resultEl = document.getElementById('creditAdjustResult'); + resultEl.innerHTML = '
查询中...
'; + try { + const r = await fetch('api/credit.php?action=admin_player_score&target=' + encodeURIComponent(target), { credentials: 'same-origin' }).then(r => r.json()); + if (!r.success) { resultEl.innerHTML = '
查询失败: ' + escAdmHtml(r.message || '未知错误') + '
'; return; } + const score = r.data.credit_score; + resultEl.innerHTML = ` +
+
+ ${escAdmHtml(r.data.player)} + ${score} +
+
+
+ + +
+
+ + +
+
+
+ + +
+
+
+ `; + } catch(e) { + resultEl.innerHTML = '
查询异常: ' + escAdmHtml(e.message) + '
'; + } +} + +async function doCreditAdjustScore(target, currentScore, isDeduct) { + const amountStr = document.getElementById('creditAdjustAmount')?.value?.trim() || ''; + if (!amountStr) { glassAlert('请输入积分变动值'); return; } + const amount = parseInt(amountStr, 10); + if (isNaN(amount) || amount === 0) { glassAlert('请输入有效的非零整数'); return; } + // 扣分模式强制取负 + const finalAmount = isDeduct ? Math.abs(amount) : Math.abs(amount); + const sign = isDeduct ? -1 : 1; + const finalValue = sign * finalAmount; + // 检查是否超出范围 + const newScore = currentScore + finalValue; + if (newScore < 0) { glassAlert('扣分后积分不能为负数(当前 ' + currentScore + ',最多扣 ' + currentScore + ')'); return; } + const reason = document.getElementById('creditAdjustReason')?.value?.trim() || (isDeduct ? '管理员扣分' : '管理员加分'); + + const statusEl = document.getElementById('creditAdjustStatus'); + statusEl.innerHTML = '提交中...'; + try { + const r = await fetch('api/credit.php?action=admin_adjust_score&target=' + encodeURIComponent(target) + '&amount=' + finalValue + '&reason=' + encodeURIComponent(reason), { credentials: 'same-origin' }).then(r => r.json()); + if (!r.success) { statusEl.innerHTML = '操作失败: ' + escAdmHtml(r.message || '未知错误') + ''; return; } + const newScore = r.data.new_score; + statusEl.innerHTML = '' + escAdmHtml(r.message) + ''; + // 更新显示的积分 + const scoreSpan = statusEl.parentElement.querySelector('span:nth-child(2)'); + if (scoreSpan) { + scoreSpan.textContent = newScore; + scoreSpan.style.color = newScore >= 60 ? 'var(--green)' : newScore >= 30 ? 'var(--yellow)' : 'var(--red)'; + } + document.getElementById('creditAdjustAmount').value = ''; + document.getElementById('creditAdjustReason').value = ''; + } catch(e) { + statusEl.innerHTML = '操作异常: ' + escAdmHtml(e.message) + ''; + } +} + function showHomeConfig() { const homeCfg = window._homeCfgData || '5'; const html = `
diff --git a/api/credit.php b/api/credit.php index e46be5d..21b8b3a 100644 --- a/api/credit.php +++ b/api/credit.php @@ -235,6 +235,163 @@ switch ($action) { success(null, '规则已更新'); break; + // ====== 管理员:查询玩家当前积分 ====== + case 'admin_player_score': + creditRequireAdmin(); + $target = getParam('target', ''); + if (!$target) error('缺少玩家名'); + $stmt = $db->prepare("SELECT credit_score FROM users WHERE player_name = ?"); + $stmt->bindValue(1, $target, SQLITE3_TEXT); + $res = $stmt->execute(); + $score = 100; + if ($row = $res->fetchArray(SQLITE3_ASSOC)) $score = (int)$row['credit_score']; + success(['player' => $target, 'credit_score' => $score]); + break; + + // ====== 管理员:手动加减积分(直接调整玩家积分) ====== + case 'admin_adjust_score': + creditRequireAdmin(); + $target = getParam('target', ''); + $amount = (int)getParam('amount', 0); + $reason = getParam('reason', '管理员手动调整'); + if (!$target) error('缺少玩家名'); + if ($amount == 0) error('积分变动值不能为0'); + + // 查当前积分 + $stmt = $db->prepare("SELECT credit_score FROM users WHERE player_name = ?"); + $stmt->bindValue(1, $target, SQLITE3_TEXT); + $res = $stmt->execute(); + $oldScore = 100; + if ($row = $res->fetchArray(SQLITE3_ASSOC)) $oldScore = (int)$row['credit_score']; + + // 更新积分 + $newScore = $oldScore + $amount; + if ($newScore < 0) $newScore = 0; + if ($newScore > 1000) $newScore = 1000; // 上限防溢出 + $stmt = $db->prepare("UPDATE users SET credit_score = ? WHERE player_name = ?"); + $stmt->bindValue(1, $newScore, SQLITE3_INTEGER); + $stmt->bindValue(2, $target, SQLITE3_TEXT); + $stmt->execute(); + + // 记录一条 credit_record(source='admin' 标记管理员手动调整) + $stmt = $db->prepare("INSERT INTO credit_records (target_player, reporter_player, type, content, status, score_change, created_at, approved_at, approve_reason, source) VALUES (?, ?, ?, ?, 'approved', ?, ?, ?, ?, 'admin')"); + $type = $amount > 0 ? 'good' : 'bad'; + $stmt->bindValue(1, $target, SQLITE3_TEXT); + $stmt->bindValue(2, $_SESSION['admin_user'] ?? 'admin', SQLITE3_TEXT); + $stmt->bindValue(3, $type, SQLITE3_TEXT); + $stmt->bindValue(4, $reason, SQLITE3_TEXT); + $stmt->bindValue(5, $amount, SQLITE3_INTEGER); + $stmt->bindValue(6, time(), SQLITE3_INTEGER); + $stmt->bindValue(7, time(), SQLITE3_INTEGER); + $stmt->bindValue(8, $reason, SQLITE3_TEXT); + $stmt->execute(); + + success(['target' => $target, 'old_score' => $oldScore, 'new_score' => $newScore, 'amount' => $amount], + '积分已调整: ' . $target . ' ' . ($amount > 0 ? '+' : '') . $amount . ' (当前: ' . $newScore . ')'); + break; + + // ====== Java 推送同步:接收 Java 端推送的积分数据(覆盖 PHP 副本) ====== + case 'sync_push': + // 接收 Java 端 SECRET_KEY 验证 + $syncSecret = getParam('secret', ''); + if (!$syncSecret || $syncSecret !== SECRET_KEY) error('同步密钥无效', 401); + + $playersJson = getParam('players', ''); + $recordsJson = getParam('records', ''); + + // 批量更新玩家积分 + $playerData = json_decode($playersJson, true); + if (is_array($playerData) && !empty($playerData)) { + $db->beginTransaction(); + foreach ($playerData as $pname => $score) { + // 确保玩家存在 + $stmt = $db->prepare("SELECT 1 FROM users WHERE player_name = ?"); + $stmt->bindValue(1, $pname, SQLITE3_TEXT); + $res = $stmt->execute(); + if ($row = $res->fetchArray()) { + $stmt = $db->prepare("UPDATE users SET credit_score = ? WHERE player_name = ?"); + $stmt->bindValue(1, (int)$score, SQLITE3_INTEGER); + $stmt->bindValue(2, $pname, SQLITE3_TEXT); + $stmt->execute(); + } else { + // 玩家不存在则插入 + $stmt = $db->prepare("INSERT INTO users (player_name, credit_score) VALUES (?, ?)"); + $stmt->bindValue(1, $pname, SQLITE3_TEXT); + $stmt->bindValue(2, (int)$score, SQLITE3_INTEGER); + $stmt->execute(); + } + } + $db->commit(); + } + + // 批量插入/更新积分记录(去重:同 target+source+created_at 的记录跳过) + $recordData = json_decode($recordsJson, true); + if (is_array($recordData) && !empty($recordData)) { + foreach ($recordData as $rec) { + $target = $rec['target_player'] ?? ''; + $reporter = $rec['reporter_player'] ?? ''; + $type = $rec['type'] ?? 'good'; + $content = $rec['content'] ?? ''; + $status = $rec['status'] ?? 'approved'; + $scoreChange = (int)($rec['score_change'] ?? 0); + $createdAt = (int)($rec['created_at'] ?? 0); + $approvedAt = (int)($rec['approved_at'] ?? 0); + $reason = $rec['approve_reason'] ?? ''; + $source = $rec['source'] ?? 'game'; + + if (!$target || !$createdAt) continue; + + // 去重检查 + $stmt = $db->prepare("SELECT 1 FROM credit_records WHERE target_player = ? AND source = ? AND created_at = ? AND type = ?"); + $stmt->bindValue(1, $target, SQLITE3_TEXT); + $stmt->bindValue(2, $source, SQLITE3_TEXT); + $stmt->bindValue(3, $createdAt, SQLITE3_INTEGER); + $stmt->bindValue(4, $type, SQLITE3_TEXT); + $res = $stmt->execute(); + if ($row = $res->fetchArray()) continue; // 已存在,跳过 + + $stmt = $db->prepare("INSERT INTO credit_records (target_player, reporter_player, type, content, status, score_change, created_at, approved_at, approve_reason, source) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); + $stmt->bindValue(1, $target, SQLITE3_TEXT); + $stmt->bindValue(2, $reporter, SQLITE3_TEXT); + $stmt->bindValue(3, $type, SQLITE3_TEXT); + $stmt->bindValue(4, $content, SQLITE3_TEXT); + $stmt->bindValue(5, $status, SQLITE3_TEXT); + $stmt->bindValue(6, $scoreChange, SQLITE3_INTEGER); + $stmt->bindValue(7, $createdAt, SQLITE3_INTEGER); + $stmt->bindValue(8, $approvedAt, SQLITE3_INTEGER); + $stmt->bindValue(9, $reason, SQLITE3_TEXT); + $stmt->bindValue(10, $source, SQLITE3_TEXT); + $stmt->execute(); + } + } + + success(['synced' => count($playerData ?? []) . ' players, ' . count($recordData ?? []) . ' records'], 'Java同步推送成功'); + break; + + // ====== Java 拉回同步:返回 PHP 端所有积分数据(管理员手动调整的部分) ====== + case 'sync_pull': + $syncSecret = getParam('secret', ''); + if (!$syncSecret || $syncSecret !== SECRET_KEY) error('同步密钥无效', 401); + + // 返回所有玩家积分 + $stmt = $db->prepare("SELECT player_name, credit_score FROM users WHERE credit_score IS NOT NULL ORDER BY player_name"); + $res = $stmt->execute(); + $players = []; + while ($row = $res->fetchArray(SQLITE3_ASSOC)) { + $players[$row['player_name']] = (int)$row['credit_score']; + } + + // 返回所有积分记录(标记 source='admin' 的是管理员手动调整的) + $stmt = $db->prepare("SELECT * FROM credit_records ORDER BY created_at DESC LIMIT 500"); + $res = $stmt->execute(); + $records = []; + while ($row = $res->fetchArray(SQLITE3_ASSOC)) { + $records[] = $row; + } + + success(['players' => $players, 'records' => $records], 'Java同步拉回成功'); + break; + default: error('未知操作: ' . $action); }