api 升级v2.0
This commit is contained in:
@@ -1,364 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* 热门统计接口
|
||||
* 今日热门、本月热门、累计热门
|
||||
* 浏览量、点赞、推荐排行
|
||||
*
|
||||
* 性能优化:支持文件缓存,减少数据库查询
|
||||
*/
|
||||
|
||||
$startTime = microtime(true);
|
||||
|
||||
define('ZBP_HOOKERROR', false);
|
||||
require '../zb_system/function/c_system_base.php';
|
||||
$zbp->Load();
|
||||
|
||||
require_once 'cache.php';
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
|
||||
header('Cache-Control: public, max-age=300');
|
||||
|
||||
$act = strtolower(trim($_GET['act'] ?? 'hot'));
|
||||
|
||||
$forceRefresh = isset($_GET['_refresh']) && $_GET['_refresh'] === '1';
|
||||
$staleMode = isset($_GET['_stale']) && $_GET['_stale'] === '1';
|
||||
|
||||
$cacheKey = 'hot_' . $act;
|
||||
$cacheParams = array('act' => $act);
|
||||
|
||||
if (!$forceRefresh) {
|
||||
$cachedResult = ApiCache::get('hot', $cacheParams);
|
||||
if ($cachedResult !== null) {
|
||||
header('X-Cache: HIT');
|
||||
$cachedResult['_cached'] = true;
|
||||
$cachedResult['_cache_age'] = ApiCache::getCacheAge('hot', $cacheParams);
|
||||
$cachedResult['_query_time'] = round((microtime(true) - $startTime) * 1000, 2) . 'ms';
|
||||
echo json_encode($cachedResult, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($staleMode || isset($_SERVER['HTTP_X_STALE_CACHE'])) {
|
||||
$staleResult = ApiCache::getStale('hot', $cacheParams);
|
||||
if ($staleResult !== null) {
|
||||
header('X-Cache: STALE');
|
||||
$staleResult['_cached'] = true;
|
||||
$staleResult['_stale'] = true;
|
||||
$staleResult['_cache_age'] = ApiCache::getCacheAge('hot', $cacheParams);
|
||||
$staleResult['_query_time'] = round((microtime(true) - $startTime) * 1000, 2) . 'ms';
|
||||
echo json_encode($staleResult, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
header('X-Cache: MISS');
|
||||
|
||||
$result = array();
|
||||
|
||||
switch ($act) {
|
||||
case 'hot':
|
||||
case 'index':
|
||||
$result = get_hot_stats();
|
||||
break;
|
||||
case 'today':
|
||||
$result = get_today_hot();
|
||||
break;
|
||||
case 'month':
|
||||
$result = get_month_hot();
|
||||
break;
|
||||
case 'total':
|
||||
$result = get_total_hot();
|
||||
break;
|
||||
default:
|
||||
$result = get_hot_stats();
|
||||
break;
|
||||
}
|
||||
|
||||
ApiCache::set('hot', $cacheParams, $result);
|
||||
|
||||
$result['_query_time'] = round((microtime(true) - $startTime) * 1000, 2) . 'ms';
|
||||
|
||||
echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
exit;
|
||||
|
||||
/**
|
||||
* 获取全部热门统计
|
||||
*/
|
||||
function get_hot_stats() {
|
||||
$todayData = get_hot_data('today');
|
||||
$monthData = get_hot_data('month');
|
||||
$totalData = get_hot_data('total');
|
||||
|
||||
return array(
|
||||
'code' => 200,
|
||||
'message' => 'success',
|
||||
'data' => array(
|
||||
'today' => $todayData,
|
||||
'month' => $monthData,
|
||||
'total' => $totalData
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取今日热门
|
||||
*/
|
||||
function get_today_hot() {
|
||||
return array(
|
||||
'code' => 200,
|
||||
'message' => 'success',
|
||||
'data' => get_hot_data('today')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本月热门
|
||||
*/
|
||||
function get_month_hot() {
|
||||
return array(
|
||||
'code' => 200,
|
||||
'message' => 'success',
|
||||
'data' => get_hot_data('month')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取累计热门
|
||||
*/
|
||||
function get_total_hot() {
|
||||
return array(
|
||||
'code' => 200,
|
||||
'message' => 'success',
|
||||
'data' => get_hot_data('total')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取热门数据
|
||||
* @param string $period 时间范围: today, month, total
|
||||
*/
|
||||
function get_hot_data($period) {
|
||||
global $zbp;
|
||||
|
||||
$data = array();
|
||||
|
||||
$data['recipe_view'] = get_top_by_field('recipe', 'view', 20, $period);
|
||||
$data['recipe_like'] = get_top_by_field('recipe', 'like', 20, $period);
|
||||
$data['recipe_recommend'] = get_top_by_field('recipe', 'recommend', 20, $period);
|
||||
|
||||
$data['ingredient_view'] = get_top_by_field('ingredient', 'view', 10, $period);
|
||||
$data['ingredient_like'] = get_top_by_field('ingredient', 'like', 10, $period);
|
||||
$data['ingredient_recommend'] = get_top_by_field('ingredient', 'recommend', 10, $period);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按字段获取排行
|
||||
* @param string $type 类型: recipe, ingredient
|
||||
* @param string $field 字段: view, like, recommend
|
||||
* @param int $limit 限制数量
|
||||
* @param string $period 时间范围: today, month, total
|
||||
*/
|
||||
function get_top_by_field($type, $field, $limit = 20, $period = 'total') {
|
||||
global $zbp;
|
||||
|
||||
$list = array();
|
||||
|
||||
if ($period === 'total') {
|
||||
$list = get_total_top($type, $field, $limit);
|
||||
} elseif ($period === 'today') {
|
||||
$list = get_today_top($type, $field, $limit);
|
||||
} elseif ($period === 'month') {
|
||||
$list = get_month_top($type, $field, $limit);
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取累计排行
|
||||
*/
|
||||
function get_total_top($type, $field, $limit) {
|
||||
global $zbp;
|
||||
|
||||
$list = array();
|
||||
|
||||
if ($type === 'recipe') {
|
||||
$table = $zbp->db->dbpre . 'post';
|
||||
$statTable = $zbp->db->dbpre . 'post_stat';
|
||||
|
||||
if ($field === 'view') {
|
||||
$sql = "SELECT p.log_ID as id, p.log_Title as name, p.log_ViewNums as count
|
||||
FROM $table p
|
||||
WHERE p.log_Type = 0 AND p.log_Status = 0
|
||||
ORDER BY p.log_ViewNums DESC
|
||||
LIMIT $limit";
|
||||
} elseif ($field === 'like') {
|
||||
$sql = "SELECT p.log_ID as id, p.log_Title as name, COALESCE(s.like_nums, 0) as count
|
||||
FROM $table p
|
||||
LEFT JOIN $statTable s ON p.log_ID = s.log_id
|
||||
WHERE p.log_Type = 0 AND p.log_Status = 0
|
||||
ORDER BY count DESC
|
||||
LIMIT $limit";
|
||||
} elseif ($field === 'recommend') {
|
||||
$sql = "SELECT p.log_ID as id, p.log_Title as name, COALESCE(s.recommend_nums, 0) as count
|
||||
FROM $table p
|
||||
LEFT JOIN $statTable s ON p.log_ID = s.log_id
|
||||
WHERE p.log_Type = 0 AND p.log_Status = 0
|
||||
ORDER BY count DESC
|
||||
LIMIT $limit";
|
||||
}
|
||||
} else {
|
||||
$table = $zbp->db->dbpre . 'ingredient_detail';
|
||||
$statTable = $zbp->db->dbpre . 'ingredient_stat';
|
||||
|
||||
if ($field === 'view') {
|
||||
$sql = "SELECT i.ingredient_id as id, i.name, i.view_count as count
|
||||
FROM $table i
|
||||
ORDER BY i.view_count DESC
|
||||
LIMIT $limit";
|
||||
} elseif ($field === 'like') {
|
||||
$sql = "SELECT i.ingredient_id as id, i.name, COALESCE(s.like_nums, 0) as count
|
||||
FROM $table i
|
||||
LEFT JOIN $statTable s ON i.ingredient_id = s.ingredient_id
|
||||
ORDER BY count DESC
|
||||
LIMIT $limit";
|
||||
} elseif ($field === 'recommend') {
|
||||
$sql = "SELECT i.ingredient_id as id, i.name, COALESCE(s.recommend_nums, 0) as count
|
||||
FROM $table i
|
||||
LEFT JOIN $statTable s ON i.ingredient_id = s.ingredient_id
|
||||
ORDER BY count DESC
|
||||
LIMIT $limit";
|
||||
}
|
||||
}
|
||||
|
||||
$results = $zbp->db->Query($sql);
|
||||
|
||||
foreach ($results as $row) {
|
||||
$list[] = array(
|
||||
'id' => (int) $row['id'],
|
||||
'name' => $row['name'],
|
||||
'count' => (int) ($row['count'] ?? 0)
|
||||
);
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取今日排行
|
||||
*/
|
||||
function get_today_top($type, $field, $limit) {
|
||||
global $zbp;
|
||||
|
||||
$list = array();
|
||||
$today = date('Y-m-d');
|
||||
|
||||
if ($type === 'recipe') {
|
||||
$logTable = $zbp->db->dbpre . 'recipe_stat_log';
|
||||
$postTable = $zbp->db->dbpre . 'post';
|
||||
|
||||
$fieldMap = array(
|
||||
'view' => 'view_count',
|
||||
'like' => 'like_count',
|
||||
'recommend' => 'recommend_count'
|
||||
);
|
||||
|
||||
$sql = "SELECT l.log_id as id, p.log_Title as name, l.{$fieldMap[$field]} as count
|
||||
FROM $logTable l
|
||||
LEFT JOIN $postTable p ON l.log_id = p.log_ID
|
||||
WHERE l.stat_date = '$today' AND p.log_Type = 0 AND p.log_Status = 0
|
||||
ORDER BY l.{$fieldMap[$field]} DESC
|
||||
LIMIT $limit";
|
||||
} else {
|
||||
$logTable = $zbp->db->dbpre . 'ingredient_stat_log';
|
||||
$ingredientTable = $zbp->db->dbpre . 'ingredient_detail';
|
||||
|
||||
$fieldMap = array(
|
||||
'view' => 'view_count',
|
||||
'like' => 'like_count',
|
||||
'recommend' => 'recommend_count'
|
||||
);
|
||||
|
||||
$sql = "SELECT l.ingredient_id as id, i.name, l.{$fieldMap[$field]} as count
|
||||
FROM $logTable l
|
||||
LEFT JOIN $ingredientTable i ON l.ingredient_id = i.ingredient_id
|
||||
WHERE l.stat_date = '$today'
|
||||
ORDER BY l.{$fieldMap[$field]} DESC
|
||||
LIMIT $limit";
|
||||
}
|
||||
|
||||
$results = $zbp->db->Query($sql);
|
||||
|
||||
foreach ($results as $row) {
|
||||
$list[] = array(
|
||||
'id' => (int) $row['id'],
|
||||
'name' => $row['name'] ?? '未知',
|
||||
'count' => (int) ($row['count'] ?? 0)
|
||||
);
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本月排行
|
||||
*/
|
||||
function get_month_top($type, $field, $limit) {
|
||||
global $zbp;
|
||||
|
||||
$list = array();
|
||||
$monthStart = date('Y-m-01');
|
||||
$monthEnd = date('Y-m-t');
|
||||
|
||||
if ($type === 'recipe') {
|
||||
$logTable = $zbp->db->dbpre . 'recipe_stat_log';
|
||||
$postTable = $zbp->db->dbpre . 'post';
|
||||
|
||||
$fieldMap = array(
|
||||
'view' => 'view_count',
|
||||
'like' => 'like_count',
|
||||
'recommend' => 'recommend_count'
|
||||
);
|
||||
|
||||
$sql = "SELECT l.log_id as id, p.log_Title as name, SUM(l.{$fieldMap[$field]}) as count
|
||||
FROM $logTable l
|
||||
LEFT JOIN $postTable p ON l.log_id = p.log_ID
|
||||
WHERE l.stat_date >= '$monthStart' AND l.stat_date <= '$monthEnd' AND p.log_Type = 0 AND p.log_Status = 0
|
||||
GROUP BY l.log_id
|
||||
ORDER BY count DESC
|
||||
LIMIT $limit";
|
||||
} else {
|
||||
$logTable = $zbp->db->dbpre . 'ingredient_stat_log';
|
||||
$ingredientTable = $zbp->db->dbpre . 'ingredient_detail';
|
||||
|
||||
$fieldMap = array(
|
||||
'view' => 'view_count',
|
||||
'like' => 'like_count',
|
||||
'recommend' => 'recommend_count'
|
||||
);
|
||||
|
||||
$sql = "SELECT l.ingredient_id as id, i.name, SUM(l.{$fieldMap[$field]}) as count
|
||||
FROM $logTable l
|
||||
LEFT JOIN $ingredientTable i ON l.ingredient_id = i.ingredient_id
|
||||
WHERE l.stat_date >= '$monthStart' AND l.stat_date <= '$monthEnd'
|
||||
GROUP BY l.ingredient_id
|
||||
ORDER BY count DESC
|
||||
LIMIT $limit";
|
||||
}
|
||||
|
||||
$results = $zbp->db->Query($sql);
|
||||
|
||||
foreach ($results as $row) {
|
||||
$list[] = array(
|
||||
'id' => (int) $row['id'],
|
||||
'name' => $row['name'] ?? '未知',
|
||||
'count' => (int) ($row['count'] ?? 0)
|
||||
);
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
Reference in New Issue
Block a user