Files
kitchen/docs/api/api_check_duplicate.php
2026-04-11 07:07:13 +08:00

307 lines
7.7 KiB
PHP

<?php
/**
* 📋 查重API接口
*
* 用于用户投稿时查询重复率
* 支持菜品、食材、营养成分、菜品内容、食材内容查重
*
* 访问方式: /api/api_check_duplicate.php?act=xxx
*/
$startTime = microtime(true);
require '../zb_system/function/c_system_base.php';
$zbp->Load();
require_once 'response.php';
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Content-Type: application/json; charset=utf-8');
$act = strtolower(trim($_GET['act'] ?? ''));
$format = ApiResponse::getFormat();
$allowedActs = array(
'recipe_title',
'ingredient_name',
'nutrition_name',
'recipe_content',
'ingredient_content'
);
if (!in_array($act, $allowedActs)) {
$result = array(
'code' => 400,
'message' => '❌ 无效的查重类型',
'data' => null,
'_query_time' => round((microtime(true) - $startTime) * 1000, 2) . 'ms'
);
ApiResponse::output($result, $format);
exit;
}
$result = call_user_func('check_' . $act);
$result['_query_time'] = round((microtime(true) - $startTime) * 1000, 2) . 'ms';
ApiResponse::output($result, $format);
exit;
// ==================== 查重函数 ====================
/**
* 菜品标题查重
* 参数: title - 菜品标题
* 返回: 重复率百分比
*/
function check_recipe_title() {
global $zbp;
$title = trim($_GET['title'] ?? $_POST['title'] ?? '');
if (empty($title)) {
return array(
'code' => 400,
'message' => '❌ 缺少菜品标题参数',
'data' => null
);
}
$tablePost = $zbp->db->dbpre . 'post';
$sql = "SELECT log_Title FROM {$tablePost} WHERE log_Type = 0";
$rows = $zbp->db->Query($sql);
$maxSimilarity = 0;
$titleLimited = mb_substr($title, 0, 100);
foreach ($rows as $row) {
$existingTitle = mb_substr($row['log_Title'], 0, 100);
similar_text($titleLimited, $existingTitle, $similarity);
if ($similarity > $maxSimilarity) {
$maxSimilarity = $similarity;
}
if ($maxSimilarity >= 100) {
break;
}
}
return array(
'code' => 200,
'message' => 'success',
'data' => array(
'duplicate_rate' => round($maxSimilarity, 2)
)
);
}
/**
* 食材名称查重
* 参数: name - 食材名称
* 返回: 重复率百分比
*/
function check_ingredient_name() {
global $zbp;
$name = trim($_GET['name'] ?? $_POST['name'] ?? '');
if (empty($name)) {
return array(
'code' => 400,
'message' => '❌ 缺少食材名称参数',
'data' => null
);
}
$tableIngredient = $zbp->db->dbpre . 'ingredient_detail';
$sql = "SELECT name FROM {$tableIngredient}";
$rows = $zbp->db->Query($sql);
$maxSimilarity = 0;
$nameLimited = mb_substr($name, 0, 100);
foreach ($rows as $row) {
$existingName = mb_substr($row['name'], 0, 100);
similar_text($nameLimited, $existingName, $similarity);
if ($similarity > $maxSimilarity) {
$maxSimilarity = $similarity;
}
if ($maxSimilarity >= 100) {
break;
}
}
return array(
'code' => 200,
'message' => 'success',
'data' => array(
'duplicate_rate' => round($maxSimilarity, 2)
)
);
}
/**
* 营养成分查重
* 参数: name - 营养成分名称
* 返回: 重复率百分比
*/
function check_nutrition_name() {
global $zbp;
$name = trim($_GET['name'] ?? $_POST['name'] ?? '');
if (empty($name)) {
return array(
'code' => 400,
'message' => '❌ 缺少营养成分名称参数',
'data' => null
);
}
$tableNutrition = $zbp->db->dbpre . 'recipe_nutrition';
$sql = "SELECT DISTINCT name FROM {$tableNutrition}";
$rows = $zbp->db->Query($sql);
$maxSimilarity = 0;
$nameLimited = mb_substr($name, 0, 100);
foreach ($rows as $row) {
$existingName = mb_substr($row['name'], 0, 100);
similar_text($nameLimited, $existingName, $similarity);
if ($similarity > $maxSimilarity) {
$maxSimilarity = $similarity;
}
if ($maxSimilarity >= 100) {
break;
}
}
return array(
'code' => 200,
'message' => 'success',
'data' => array(
'duplicate_rate' => round($maxSimilarity, 2)
)
);
}
/**
* 菜品内容查重
* 参数: content - 菜品内容(制作步骤等)
* 返回: 重复率百分比
*/
function check_recipe_content() {
global $zbp;
$content = trim($_GET['content'] ?? $_POST['content'] ?? '');
if (empty($content)) {
return array(
'code' => 400,
'message' => '❌ 缺少菜品内容参数',
'data' => null
);
}
$tablePost = $zbp->db->dbpre . 'post';
$sql = "SELECT log_Content FROM {$tablePost} WHERE log_Type = 0 LIMIT 1000";
$rows = $zbp->db->Query($sql);
$maxSimilarity = 0;
$contentLimited = mb_substr($content, 0, 100);
$contentLength = mb_strlen($contentLimited);
foreach ($rows as $row) {
$existingContent = mb_substr(strip_tags($row['log_Content']), 0, 100);
$existingLength = mb_strlen($existingContent);
if ($existingLength > 0 && $contentLength > 0) {
similar_text($contentLimited, $existingContent, $similarity);
if ($similarity > $maxSimilarity) {
$maxSimilarity = $similarity;
}
if ($maxSimilarity >= 100) {
break;
}
}
}
return array(
'code' => 200,
'message' => 'success',
'data' => array(
'duplicate_rate' => round($maxSimilarity, 2)
)
);
}
/**
* 食材内容查重
* 参数: content - 食材内容(功效、营养、使用提示等)
* 返回: 重复率百分比
*/
function check_ingredient_content() {
global $zbp;
$content = trim($_GET['content'] ?? $_POST['content'] ?? '');
if (empty($content)) {
return array(
'code' => 400,
'message' => '❌ 缺少食材内容参数',
'data' => null
);
}
$tableIngredientDetail = $zbp->db->dbpre . 'ingredient_detail';
$sql = "SELECT effect, nutrition, usage_tip FROM {$tableIngredientDetail} LIMIT 1000";
$rows = $zbp->db->Query($sql);
$maxSimilarity = 0;
$contentLimited = mb_substr($content, 0, 100);
$contentLength = mb_strlen($contentLimited);
foreach ($rows as $row) {
$existingContent = '';
if (!empty($row['effect'])) {
$existingContent .= $row['effect'] . ' ';
}
if (!empty($row['nutrition'])) {
$existingContent .= $row['nutrition'] . ' ';
}
if (!empty($row['usage_tip'])) {
$existingContent .= $row['usage_tip'];
}
$existingContent = mb_substr(trim($existingContent), 0, 100);
$existingLength = mb_strlen($existingContent);
if ($existingLength > 0 && $contentLength > 0) {
similar_text($contentLimited, $existingContent, $similarity);
if ($similarity > $maxSimilarity) {
$maxSimilarity = $similarity;
}
if ($maxSimilarity >= 100) {
break;
}
}
}
return array(
'code' => 200,
'message' => 'success',
'data' => array(
'duplicate_rate' => round($maxSimilarity, 2)
)
);
}