瀑布流
This commit is contained in:
241
docs/api/api_hot.php
Normal file
241
docs/api/api_hot.php
Normal file
@@ -0,0 +1,241 @@
|
||||
<?php
|
||||
/**
|
||||
* 🔥 热门排行接口
|
||||
*
|
||||
* @file api_hot.php
|
||||
* @author AI Assistant
|
||||
* @date 2026-04-12
|
||||
* @version 1.1.0
|
||||
* @desc 获取热门菜谱、热门食材排行
|
||||
* @lastUpdate 2026-04-12 添加评分显示功能,更新字段名 rate_nums/rate_score
|
||||
*
|
||||
* 访问地址: /api/api_hot.php
|
||||
*
|
||||
* 参数说明:
|
||||
* - type: recipe(菜谱) / ingredient(食材),默认recipe
|
||||
* - sort: view(浏览量) / like(点赞数) / rate(评分),默认view
|
||||
* - limit: 返回数量,默认20,最大100
|
||||
* - cate_id: 分类ID筛选(可选)
|
||||
*/
|
||||
|
||||
$startTime = microtime(true);
|
||||
|
||||
require '../zb_system/function/c_system_base.php';
|
||||
$zbp->Load();
|
||||
|
||||
require_once 'response.php';
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
||||
http_response_code(200);
|
||||
exit;
|
||||
}
|
||||
|
||||
$type = strtolower(trim($_GET['type'] ?? 'recipe'));
|
||||
$sort = strtolower(trim($_GET['sort'] ?? 'view'));
|
||||
$limit = min((int) ($_GET['limit'] ?? 20), 100);
|
||||
$limit = max(1, $limit);
|
||||
$cateId = isset($_GET['cate_id']) ? (int) $_GET['cate_id'] : 0;
|
||||
|
||||
$result = array();
|
||||
|
||||
switch ($type) {
|
||||
case 'recipe':
|
||||
$result = get_recipe_hot($sort, $limit, $cateId);
|
||||
break;
|
||||
case 'ingredient':
|
||||
$result = get_ingredient_hot($sort, $limit);
|
||||
break;
|
||||
case 'all':
|
||||
$result = array(
|
||||
'code' => 200,
|
||||
'message' => 'success',
|
||||
'data' => array(
|
||||
'recipe_view' => get_recipe_list('view', $limit, $cateId),
|
||||
'recipe_like' => get_recipe_list('like', $limit, $cateId),
|
||||
'ingredient_view' => get_ingredient_list('view', $limit)
|
||||
)
|
||||
);
|
||||
break;
|
||||
default:
|
||||
$result = array(
|
||||
'code' => 400,
|
||||
'message' => '无效的type参数,可选: recipe/ingredient/all',
|
||||
'data' => null
|
||||
);
|
||||
}
|
||||
|
||||
$result['_query_time'] = round((microtime(true) - $startTime) * 1000, 2) . 'ms';
|
||||
|
||||
$format = ApiResponse::getFormat();
|
||||
ApiResponse::output($result, $format);
|
||||
exit;
|
||||
|
||||
/**
|
||||
* 获取热门菜谱
|
||||
*/
|
||||
function get_recipe_hot($sort, $limit, $cateId) {
|
||||
global $zbp;
|
||||
|
||||
$tablePost = $zbp->db->dbpre . 'post';
|
||||
$tablePostStat = $zbp->db->dbpre . 'post_stat';
|
||||
$tableCategory = $zbp->db->dbpre . 'category';
|
||||
|
||||
$whereClauses = array("p.log_Type = 0", "p.log_Status = 0");
|
||||
|
||||
if ($cateId > 0) {
|
||||
$whereClauses[] = "p.log_CateID = $cateId";
|
||||
}
|
||||
|
||||
$whereSql = implode(' AND ', $whereClauses);
|
||||
|
||||
switch ($sort) {
|
||||
case 'like':
|
||||
$orderBy = 'COALESCE(s.like_nums, 0) DESC';
|
||||
break;
|
||||
case 'rate':
|
||||
$orderBy = 'COALESCE(s.rate_score, 0) DESC';
|
||||
break;
|
||||
default:
|
||||
$orderBy = 'p.log_ViewNums DESC';
|
||||
}
|
||||
|
||||
$sql = "SELECT
|
||||
p.log_ID as id,
|
||||
p.log_Title as title,
|
||||
p.log_Intro as intro,
|
||||
p.log_CateID as category_id,
|
||||
c.cate_Name as category_name,
|
||||
p.log_ViewNums as view_count,
|
||||
COALESCE(s.like_nums, 0) as like_count,
|
||||
COALESCE(s.rate_nums, 0) as rate_count,
|
||||
COALESCE(s.rate_score, 0) as rate_score
|
||||
FROM $tablePost p
|
||||
LEFT JOIN $tablePostStat s ON p.log_ID = s.log_id
|
||||
LEFT JOIN $tableCategory c ON p.log_CateID = c.cate_ID
|
||||
WHERE $whereSql
|
||||
ORDER BY $orderBy
|
||||
LIMIT $limit";
|
||||
|
||||
$rows = $zbp->db->Query($sql);
|
||||
|
||||
$list = array();
|
||||
foreach ($rows as $row) {
|
||||
$list[] = array(
|
||||
'id' => (int) $row['id'],
|
||||
'title' => $row['title'],
|
||||
'intro' => mb_substr(strip_tags($row['intro']), 0, 100),
|
||||
'category_id' => (int) $row['category_id'],
|
||||
'category_name' => $row['category_name'],
|
||||
'view_count' => (int) $row['view_count'],
|
||||
'like_count' => (int) $row['like_count'],
|
||||
'rate_count' => (int) $row['rate_count'],
|
||||
'rate_score' => (float) $row['rate_score'],
|
||||
'rating' => ApiResponse::getRatingSummary(
|
||||
(float) $row['rate_score'],
|
||||
(int) $row['rate_count']
|
||||
),
|
||||
'url' => "?act=detail&id=" . $row['id']
|
||||
);
|
||||
}
|
||||
|
||||
return array(
|
||||
'code' => 200,
|
||||
'message' => 'success',
|
||||
'data' => array(
|
||||
'type' => 'recipe',
|
||||
'sort' => $sort,
|
||||
'category_id' => $cateId,
|
||||
'list' => $list,
|
||||
'total' => count($list)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取热门食材
|
||||
*/
|
||||
function get_ingredient_hot($sort, $limit) {
|
||||
global $zbp;
|
||||
|
||||
$tableIngredient = $zbp->db->dbpre . 'ingredient_detail';
|
||||
$tableIngredientStat = $zbp->db->dbpre . 'ingredient_stat';
|
||||
$tableCategory = $zbp->db->dbpre . 'category';
|
||||
|
||||
switch ($sort) {
|
||||
case 'like':
|
||||
$orderBy = 'COALESCE(s.like_nums, 0) DESC';
|
||||
break;
|
||||
case 'rate':
|
||||
$orderBy = 'COALESCE(s.rate_score, 0) DESC';
|
||||
break;
|
||||
default:
|
||||
$orderBy = 'i.view_count DESC';
|
||||
}
|
||||
|
||||
$sql = "SELECT
|
||||
i.ingredient_id as id,
|
||||
i.name,
|
||||
i.cate_ID as category_id,
|
||||
c.cate_Name as category_name,
|
||||
i.view_count,
|
||||
COALESCE(s.like_nums, 0) as like_count,
|
||||
COALESCE(s.rate_nums, 0) as rate_count,
|
||||
COALESCE(s.rate_score, 0) as rate_score
|
||||
FROM $tableIngredient i
|
||||
LEFT JOIN $tableIngredientStat s ON i.ingredient_id = s.ingredient_id
|
||||
LEFT JOIN $tableCategory c ON i.cate_ID = c.cate_ID
|
||||
ORDER BY $orderBy
|
||||
LIMIT $limit";
|
||||
|
||||
$rows = $zbp->db->Query($sql);
|
||||
|
||||
$list = array();
|
||||
foreach ($rows as $row) {
|
||||
$list[] = array(
|
||||
'id' => (int) $row['id'],
|
||||
'name' => $row['name'],
|
||||
'category_id' => (int) $row['category_id'],
|
||||
'category_name' => $row['category_name'],
|
||||
'view_count' => (int) $row['view_count'],
|
||||
'like_count' => (int) $row['like_count'],
|
||||
'rate_count' => (int) $row['rate_count'],
|
||||
'rate_score' => (float) $row['rate_score'],
|
||||
'rating' => ApiResponse::getRatingSummary(
|
||||
(float) $row['rate_score'],
|
||||
(int) $row['rate_count']
|
||||
),
|
||||
'url' => "?act=ingredient_detail&id=" . $row['id']
|
||||
);
|
||||
}
|
||||
|
||||
return array(
|
||||
'code' => 200,
|
||||
'message' => 'success',
|
||||
'data' => array(
|
||||
'type' => 'ingredient',
|
||||
'sort' => $sort,
|
||||
'list' => $list,
|
||||
'total' => count($list)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取菜谱列表(用于all类型)
|
||||
*/
|
||||
function get_recipe_list($sort, $limit, $cateId) {
|
||||
$result = get_recipe_hot($sort, $limit, $cateId);
|
||||
return $result['data']['list'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取食材列表(用于all类型)
|
||||
*/
|
||||
function get_ingredient_list($sort, $limit) {
|
||||
$result = get_ingredient_hot($sort, $limit);
|
||||
return $result['data']['list'];
|
||||
}
|
||||
Reference in New Issue
Block a user