Files
kitchen/docs/api/diagnose.php
Developer 8d27c67d3a api实现
2026-04-09 08:54:36 +08:00

207 lines
7.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* 🍳 数据库诊断脚本
* 访问: http://eat.wktyl.com/api/diagnose.php
*/
require '../zb_system/function/c_system_base.php';
$zbp->Load();
header('Content-Type: text/html; charset=utf-8');
function check($msg, $result, $detail = '') {
$icon = $result ? '✅' : '❌';
$color = $result ? 'green' : 'red';
echo "<div style='margin:5px 0;padding:8px;background:" . ($result ? '#e8f5e9' : '#ffebee') . ";border-radius:4px;'>";
echo "<span style='color:$color;font-size:18px;'>$icon</span> <strong>$msg</strong>";
if ($detail) echo "<br><small style='color:#666;margin-left:28px;'>$detail</small>";
echo "</div>";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>🍳 数据库诊断</title>
<style>
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; max-width: 900px; margin: 40px auto; padding: 20px; background: #f5f5f7; }
.card { background: #fff; border-radius: 12px; padding: 24px; margin-bottom: 20px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); }
h1 { color: #1d1d1f; margin-bottom: 8px; }
h2 { color: #0071e3; border-bottom: 2px solid #0071e3; padding-bottom: 8px; margin-top: 0; }
.code { background: #1d1d1f; color: #f5f5f7; padding: 12px; border-radius: 8px; font-family: monospace; font-size: 13px; overflow-x: auto; margin: 8px 0; }
table { width: 100%; border-collapse: collapse; margin: 12px 0; }
th, td { padding: 10px; text-align: left; border-bottom: 1px solid #e5e5e7; }
th { background: #f5f5f7; font-weight: 600; }
.ok { color: #34c759; }
.err { color: #ff3b30; }
</style>
</head>
<body>
<div class="card">
<h1>🍳 数据库诊断报告</h1>
<p style="color:#666;">生成时间: <?php echo date('Y-m-d H:i:s'); ?></p>
</div>
<div class="card">
<h2>🔌 数据库连接</h2>
<?php
try {
$sql = "SELECT 1";
$result = $zbp->db->Query($sql);
check("数据库连接", true, "类型: " . get_class($zbp->db) . ", 前缀: " . $zbp->db->dbpre);
} catch (Exception $e) {
check("数据库连接", false, $e->getMessage());
}
?>
</div>
<div class="card">
<h2>📋 数据表检查</h2>
<?php
$tables = ['Post', 'Category', 'Tag', 'recipe_ingredient'];
foreach ($tables as $table) {
try {
$fullTable = $zbp->db->dbpre . $table;
$sql = "SELECT COUNT(*) FROM $fullTable";
$count = $zbp->db->Query($sql)[0]['COUNT(*)'] ?? 0;
check("$fullTable 表", true, "$count 条记录");
} catch (Exception $e) {
check("$fullTable 表", false, $e->getMessage());
}
}
?>
</div>
<div class="card">
<h2>🍳 菜谱数据检查</h2>
<?php
// 检查公开菜谱
$tablePost = $zbp->db->dbpre . 'Post';
$sql = "SELECT COUNT(*) as cnt FROM $tablePost WHERE log_Type = 0 AND log_Status = 0";
$publicCount = $zbp->db->Query($sql)[0]['cnt'] ?? 0;
check("公开菜谱 (log_Type=0, log_Status=0)", $publicCount > 0, "数量: $publicCount");
// 检查ID范围
$sql = "SELECT MIN(log_ID) as min_id, MAX(log_ID) as max_id FROM $tablePost WHERE log_Type = 0";
$range = $zbp->db->Query($sql)[0];
check("菜谱ID范围", true, "min={$range['min_id']}, max={$range['max_id']}");
// 检查示例菜谱
$sql = "SELECT log_ID, log_Title, log_CateID FROM $tablePost WHERE log_Type = 0 AND log_Status = 0 LIMIT 3";
$samples = $zbp->db->Query($sql);
?>
<h3 style="margin-top:16px;">示例菜谱数据:</h3>
<table>
<tr><th>ID</th><th>标题</th><th>分类ID</th></tr>
<?php foreach ($samples as $s): ?>
<tr>
<td><?php echo $s['log_ID']; ?></td>
<td><?php echo htmlspecialchars($s['log_Title']); ?></td>
<td><?php echo $s['log_CateID']; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php
// 检查ID=70是否存在
$sql = "SELECT log_ID, log_Title FROM $tablePost WHERE log_ID = 70 AND log_Type = 0";
$id70 = $zbp->db->Query($sql);
if ($id70) {
check("ID=70 菜谱", true, "标题: " . $id70[0]['log_Title']);
} else {
check("ID=70 菜谱", false, "不存在或不是文章类型");
}
?>
</div>
<div class="card">
<h2>🥬 食材数据检查</h2>
<?php
$tableIng = $zbp->db->dbpre . 'recipe_ingredient';
// 检查唯一食材数量
$sql = "SELECT COUNT(DISTINCT name) as cnt FROM $tableIng";
$uniqueCount = $zbp->db->Query($sql)[0]['cnt'] ?? 0;
check("食材种类数", $uniqueCount > 0, "$uniqueCount 种");
// 检查ID范围
$sql = "SELECT MIN(ingredient_id) as min_id, MAX(ingredient_id) as max_id FROM $tableIng";
$range = $zbp->db->Query($sql)[0];
check("ingredient_id范围", true, "min={$range['min_id']}, max={$range['max_id']}");
// 检查示例食材
$sql = "SELECT DISTINCT name, ingredient_id FROM $tableIng LIMIT 5";
$samples = $zbp->db->Query($sql);
?>
<h3 style="margin-top:16px;">示例食材数据:</h3>
<table>
<tr><th>ingredient_id</th><th>名称</th></tr>
<?php foreach ($samples as $s): ?>
<tr>
<td><?php echo $s['ingredient_id']; ?></td>
<td><?php echo htmlspecialchars($s['name']); ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php
// 检查ID=85是否存在
$sql = "SELECT name FROM $tableIng WHERE ingredient_id = 85 LIMIT 1";
$id85 = $zbp->db->Query($sql);
if ($id85) {
check("ID=85 食材", true, "名称: " . $id85[0]['name']);
} else {
check("ID=85 食材", false, "不存在");
}
?>
</div>
<div class="card">
<h2>🧪 API实际查询测试</h2>
<?php
// 模拟list接口查询
$sql = "SELECT log_ID, log_Title FROM $tablePost WHERE log_Type = 0 AND log_Status = 0 ORDER BY log_PostTime DESC LIMIT 3";
$listResult = $zbp->db->Query($sql);
check("list接口查询", count($listResult) > 0, "返回 " . count($listResult) . " 条");
// 模拟ingredients接口查询
$sql = "SELECT DISTINCT name, ingredient_id FROM $tableIng LIMIT 3";
$ingResult = $zbp->db->Query($sql);
check("ingredients接口查询", count($ingResult) > 0, "返回 " . count($ingResult) . " 条");
?>
</div>
<div class="card">
<h2>💡 问题诊断建议</h2>
<?php if ($publicCount == 0): ?>
<div class="code">
⚠️ 公开菜谱数量为0可能原因:
1. log_Status 字段值不是0 (0=公开, 1=草稿, 2=审核中)
2. log_Type 字段值不是0 (0=文章, 1=页面)
修复SQL:
UPDATE zbp_Post SET log_Status = 0 WHERE log_Type = 0;
</div>
<?php endif; ?>
<?php if (!$id70): ?>
<div class="code">
⚠️ ID=70不存在可用ID范围: <?php echo $range['min_id']; ?> ~ <?php echo $range['max_id']; ?>
测试可用的ID:
?act=detail&id=<?php echo $range['min_id']; ?>
</div>
<?php endif; ?>
<?php if (!$id85): ?>
<div class="code">
⚠️ ingredient_id=85不存在可用范围: <?php echo $range['min_id'] ?? 'N/A'; ?> ~ <?php echo $range['max_id'] ?? 'N/A'; ?>
</div>
<?php endif; ?>
</div>
<div class="card" style="text-align:center;color:#86868b;">
<p>🍳 菜谱API诊断工具 | Powered by Z-Blog PHP</p>
</div>
</body>
</html>