|
@@ -983,25 +983,22 @@ class NewsService implements NewsServiceInterface
|
|
|
*/
|
|
|
public function getWebsiteAllArticle(array $data): array
|
|
|
{
|
|
|
+ $startTime = microtime(true); // 记录开始时间
|
|
|
try {
|
|
|
// 解析输入数据
|
|
|
$input = [
|
|
|
'id' => $data['id'] ?? '',
|
|
|
'website_id' => $data['website_id'] ?? 0
|
|
|
];
|
|
|
-
|
|
|
// 验证必要参数
|
|
|
if (empty($input['website_id'])) {
|
|
|
return Result::error("缺少网站ID参数", 0);
|
|
|
}
|
|
|
-
|
|
|
// 将 JSON 字符串转换为 PHP 数组
|
|
|
$items = json_decode($input['id'], true);
|
|
|
-
|
|
|
if (json_last_error() !== JSON_ERROR_NONE || !is_array($items)) {
|
|
|
return Result::error("无效的JSON格式", 0);
|
|
|
}
|
|
|
-
|
|
|
// 提取所有需要查询的分类ID
|
|
|
$parentCatIds = [];
|
|
|
$childCatIds = [];
|
|
@@ -1013,7 +1010,6 @@ class NewsService implements NewsServiceInterface
|
|
|
$parentCatIds[] = $parts[0];
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
if (isset($item['child']) && $item['child'] && $item['child'] !== 'undefined') {
|
|
|
$parts = explode(',', $item['child']);
|
|
|
if (count($parts) === 3) {
|
|
@@ -1021,15 +1017,12 @@ class NewsService implements NewsServiceInterface
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
// 去重分类ID
|
|
|
$parentCatIds = array_unique($parentCatIds);
|
|
|
$childCatIds = array_unique($childCatIds);
|
|
|
-
|
|
|
// 批量查询分类信息
|
|
|
$websiteCondition = ['website_id' => $input['website_id']];
|
|
|
$categories = $this->getCategoriesByIds(array_merge($parentCatIds, $childCatIds), $websiteCondition);
|
|
|
-
|
|
|
// 处理每个项目
|
|
|
$result = [];
|
|
|
foreach ($items as $item) {
|
|
@@ -1041,25 +1034,19 @@ class NewsService implements NewsServiceInterface
|
|
|
'textnum' => [],
|
|
|
'child' => []
|
|
|
];
|
|
|
-
|
|
|
// 处理父级栏目
|
|
|
if (isset($item['parent']) && $item['parent'] && $item['parent'] !== 'undefined') {
|
|
|
$parts = explode(',', $item['parent']);
|
|
|
-
|
|
|
if (count($parts) !== 3) {
|
|
|
return Result::error("父级栏目:缺少必需参数错误", 0);
|
|
|
}
|
|
|
-
|
|
|
list($parentCatId, $parentImgNum, $parentTextNum) = $parts;
|
|
|
-
|
|
|
// 从已查询的分类中获取信息
|
|
|
$category = $categories[$parentCatId] ?? null;
|
|
|
-
|
|
|
if ($category) {
|
|
|
$resultItem['alias'] = $category->alias;
|
|
|
$resultItem['category_id'] = $category->category_id;
|
|
|
$resultItem['pinyin'] = $category->alias_pinyin;
|
|
|
-
|
|
|
// 查询图片新闻
|
|
|
if ($parentImgNum > 0) {
|
|
|
$resultItem['imgnum'] = $this->getArticles(
|
|
@@ -1069,7 +1056,6 @@ class NewsService implements NewsServiceInterface
|
|
|
true
|
|
|
);
|
|
|
}
|
|
|
-
|
|
|
// 查询文字新闻
|
|
|
if ($parentTextNum > 0) {
|
|
|
$resultItem['textnum'] = $this->getArticles(
|
|
@@ -1084,16 +1070,12 @@ class NewsService implements NewsServiceInterface
|
|
|
// 处理子级栏目
|
|
|
if (isset($item['child']) && $item['child'] && $item['child'] !== 'undefined') {
|
|
|
$parts = explode(',', $item['child']);
|
|
|
-
|
|
|
if (count($parts) !== 3) {
|
|
|
return Result::error("子级栏目:缺少必需参数错误", 0);
|
|
|
}
|
|
|
-
|
|
|
list($childCatId, $childImgNum, $childTextNum) = $parts;
|
|
|
-
|
|
|
// 从已查询的分类中获取信息
|
|
|
$childCategory = $categories[$childCatId] ?? null;
|
|
|
-
|
|
|
if ($childCategory) {
|
|
|
$childData = [
|
|
|
'alias' => $childCategory->alias,
|
|
@@ -1103,12 +1085,13 @@ class NewsService implements NewsServiceInterface
|
|
|
'imgnum' => [],
|
|
|
'textnum' => []
|
|
|
];
|
|
|
- // 查询此层级的所有子栏目
|
|
|
- $childData['all_childcat'] = $this->getChildCategories(
|
|
|
- $childCategory->pid,
|
|
|
- $input['website_id']
|
|
|
- );
|
|
|
-
|
|
|
+ if(isset($childCategory->pid) && $childCategory->pid != 0){
|
|
|
+ // 查询此层级的所有子栏目
|
|
|
+ $childData['all_childcat'] = $this->getChildCategories(
|
|
|
+ $childCategory->pid,
|
|
|
+ $input['website_id']
|
|
|
+ );
|
|
|
+ }
|
|
|
// 查询子栏目图片新闻
|
|
|
if ($childImgNum > 0) {
|
|
|
$childData['imgnum'] = $this->getArticles(
|
|
@@ -1127,14 +1110,16 @@ class NewsService implements NewsServiceInterface
|
|
|
false
|
|
|
);
|
|
|
}
|
|
|
-
|
|
|
$resultItem['child'] = $childData;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
$result[] = $resultItem;
|
|
|
+ // 检查是否超时
|
|
|
+ $elapsedTime = microtime(true) - $startTime;
|
|
|
+ if ($elapsedTime > 5) {
|
|
|
+ return Result::error("请求处理时间已超过5秒,已经达到".$elapsedTime, 0);
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
return Result::success($result);
|
|
|
} catch (\Exception $e) {
|
|
|
return Result::error("处理请求时出错: " . $e->getMessage(), 0);
|
|
@@ -1193,7 +1178,6 @@ class NewsService implements NewsServiceInterface
|
|
|
)
|
|
|
->orderBy('updated_at', 'desc')
|
|
|
->limit($limit);
|
|
|
-
|
|
|
if ($isImage) {
|
|
|
$query->where('imgurl', '!=', '');
|
|
|
}
|