99 lines
3.1 KiB
PHP
99 lines
3.1 KiB
PHP
|
|
<?php
|
||
|
|
require_once '../config.php';
|
||
|
|
|
||
|
|
header('Content-Type: application/json');
|
||
|
|
|
||
|
|
function validateApiKey($api_key) {
|
||
|
|
global $pdo;
|
||
|
|
|
||
|
|
if (empty($api_key)) return false;
|
||
|
|
|
||
|
|
$stmt = $pdo->prepare("SELECT id, username FROM users WHERE api_key = ?");
|
||
|
|
$stmt->execute([$api_key]);
|
||
|
|
return $stmt->fetch(PDO::FETCH_ASSOC);
|
||
|
|
}
|
||
|
|
|
||
|
|
$api_key = $_GET['api_key'] ?? '';
|
||
|
|
$user = validateApiKey($api_key);
|
||
|
|
|
||
|
|
if (!$user) {
|
||
|
|
http_response_code(401);
|
||
|
|
echo json_encode([
|
||
|
|
'success' => false,
|
||
|
|
'error' => '无效的API密钥'
|
||
|
|
]);
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
|
||
|
|
$page = max(1, intval($_GET['page'] ?? 1));
|
||
|
|
$limit = min(50, max(1, intval($_GET['limit'] ?? 20)));
|
||
|
|
$offset = ($page - 1) * $limit;
|
||
|
|
|
||
|
|
try {
|
||
|
|
$stmt = $pdo->prepare("
|
||
|
|
SELECT i.*,
|
||
|
|
GROUP_CONCAT(DISTINCT t.name) as tag_names,
|
||
|
|
(SELECT COUNT(*) FROM image_feedbacks WHERE image_id = i.id AND type = 'like') as like_count,
|
||
|
|
(SELECT COUNT(*) FROM image_feedbacks WHERE image_id = i.id AND type = 'report') as report_count
|
||
|
|
FROM images i
|
||
|
|
LEFT JOIN image_tags it ON i.id = it.image_id
|
||
|
|
LEFT JOIN tags t ON it.tag_id = t.id
|
||
|
|
WHERE i.user_id = ?
|
||
|
|
GROUP BY i.id
|
||
|
|
ORDER BY i.uploaded_at DESC
|
||
|
|
LIMIT ? OFFSET ?
|
||
|
|
");
|
||
|
|
$stmt->bindValue(1, $user['id'], PDO::PARAM_INT);
|
||
|
|
$stmt->bindValue(2, $limit, PDO::PARAM_INT);
|
||
|
|
$stmt->bindValue(3, $offset, PDO::PARAM_INT);
|
||
|
|
$stmt->execute();
|
||
|
|
|
||
|
|
$images = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||
|
|
|
||
|
|
$stmt = $pdo->prepare("SELECT COUNT(*) as total FROM images WHERE user_id = ?");
|
||
|
|
$stmt->execute([$user['id']]);
|
||
|
|
$total = $stmt->fetch(PDO::FETCH_ASSOC)['total'];
|
||
|
|
|
||
|
|
$processedImages = [];
|
||
|
|
foreach ($images as $image) {
|
||
|
|
$tagNames = [];
|
||
|
|
if ($image['tag_names']) {
|
||
|
|
$tagNames = explode(',', $image['tag_names']);
|
||
|
|
}
|
||
|
|
|
||
|
|
$processedImages[] = [
|
||
|
|
'id' => $image['id'],
|
||
|
|
'title' => $image['title'],
|
||
|
|
'filename' => $image['filename'],
|
||
|
|
'url' => SITE_URL . '/view-image.php?id=' . $image['id'],
|
||
|
|
'direct_url' => SITE_URL . '/uploads/' . $image['filename'],
|
||
|
|
'is_public' => (bool)$image['is_public'],
|
||
|
|
'file_size' => $image['file_size'],
|
||
|
|
'file_size_formatted' => formatFileSize($image['file_size']),
|
||
|
|
'views' => $image['views'],
|
||
|
|
'like_count' => $image['like_count'],
|
||
|
|
'report_count' => $image['report_count'],
|
||
|
|
'tags' => $tagNames,
|
||
|
|
'uploaded_at' => $image['uploaded_at'],
|
||
|
|
'uploaded_at_formatted' => date('Y-m-d H:i', strtotime($image['uploaded_at']))
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
echo json_encode([
|
||
|
|
'success' => true,
|
||
|
|
'data' => $processedImages,
|
||
|
|
'pagination' => [
|
||
|
|
'page' => $page,
|
||
|
|
'limit' => $limit,
|
||
|
|
'total' => $total,
|
||
|
|
'pages' => ceil($total / $limit),
|
||
|
|
'has_next' => $page < ceil($total / $limit),
|
||
|
|
'has_prev' => $page > 1
|
||
|
|
]
|
||
|
|
]);
|
||
|
|
|
||
|
|
} catch(PDOException $e) {
|
||
|
|
http_response_code(500);
|
||
|
|
echo json_encode(['success' => false, 'error' => '数据库错误: ' . $e->getMessage()]);
|
||
|
|
}
|
||
|
|
?>
|