Files
Fuxsto-V3/user/get_products.php
2025-10-18 14:46:52 +08:00

60 lines
1.7 KiB
PHP
Raw Permalink 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
function getProductsData($directory) {
// 检查目录是否存在
if (!is_dir($directory)) {
return null;
}
$files = glob($directory . '/*.json');
// 如果没有找到任何JSON文件返回null
if (empty($files)) {
return null;
}
$productsData = [];
$allHidden = true;
foreach ($files as $file) {
$content = file_get_contents($file);
$data = json_decode($content, true);
// 跳过hidden为1的文件
if (isset($data['hidden']) && $data['hidden'] == 1) {
continue;
} else {
$allHidden = false;
}
// 获取文件名(不包括后缀)
$id = basename($file, '.json');
// 只获取指定键的值如果不存在则设为null
$productInfo = [
'name' => isset($data['name']) ? $data['name'] : null,
'describe' => isset($data['describe']) ? $data['describe'] : null,
'fid' => isset($data['fid']) ? $data['fid'] : null,
'sid' => isset($data['sid']) ? $data['sid'] : null,
'nid' => isset($data['sid']) ? $data['nid'] : null,
'day' => isset($data['day']) ? $data['day'] : null,
'have' => isset($data['have']) ? $data['have'] : null,
'star' => isset($data['star']) ? $data['star'] : null,
'bi' => isset($data['bi']) ? $data['bi'] : null
];
$productsData[$id] = $productInfo;
}
// 如果所有内容都是被隐藏的返回null
if ($allHidden) {
return null;
}
return json_encode($productsData, JSON_UNESCAPED_UNICODE);
}
$directory = './products';
echo getProductsData($directory);
?>