|
@@ -22,6 +22,7 @@ use Hyperf\Utils\Random;
|
|
|
use Fukuball\Jieba\Jieba;
|
|
|
use Fukuball\Jieba\Finalseg;
|
|
|
use App\Model\ChatRecords;
|
|
|
+use Hyperf\Utils\Collection;
|
|
|
|
|
|
#[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
|
|
|
class NewsService implements NewsServiceInterface
|
|
@@ -34,12 +35,75 @@ class NewsService implements NewsServiceInterface
|
|
|
*/
|
|
|
public function getCategoryList(array $data): array
|
|
|
{
|
|
|
- $rep = Category::select("category.*")->orderBy('category.updated_at', "desc")->get();
|
|
|
- if (empty($rep)) {
|
|
|
- return Result::error("没有导航池数据");
|
|
|
+ $page = (int) $data['page'] ?? 1;
|
|
|
+ $perPage = (int) $data['pageSize'] ?? 10;
|
|
|
+ $search = $data['name'] ?? '';
|
|
|
+ // 查找所有匹配的分类
|
|
|
+ $matchedCategories = $this->findMatchedCategories($search);
|
|
|
+ // 查找所有匹配分类的父级分类
|
|
|
+ $allRequiredCategories = $this->findAllParents($matchedCategories);
|
|
|
+ // 提取一级分类
|
|
|
+ $topLevelCategories = [];
|
|
|
+ foreach ($allRequiredCategories as $category) {
|
|
|
+ if ($category->pid === 0) {
|
|
|
+ $topLevelCategories[] = $category;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 分页处理
|
|
|
+ $offset = ($page - 1) * $perPage;
|
|
|
+ $paginatedTopLevelCategories = array_slice($topLevelCategories, $offset, $perPage);
|
|
|
+ $categoryTree = [];
|
|
|
+ foreach ($paginatedTopLevelCategories as $category) {
|
|
|
+ $categoryTree[] = $this->buildCategoryTree($category, $allRequiredCategories);
|
|
|
+ }
|
|
|
+ $return = [
|
|
|
+ 'rows' => $categoryTree,
|
|
|
+ 'total' => count($topLevelCategories),
|
|
|
+ ];
|
|
|
+ return Result::success($return);
|
|
|
+ }
|
|
|
+ private function findMatchedCategories($search)
|
|
|
+ {
|
|
|
+ if ($search) {
|
|
|
+ return Category::where('name', 'like', "%{$search}%")->get();
|
|
|
+ }
|
|
|
+ return Category::all();
|
|
|
+ }
|
|
|
+
|
|
|
+ private function findAllParents($categories)
|
|
|
+ {
|
|
|
+ $allCategories = [];
|
|
|
+ foreach ($categories as $category) {
|
|
|
+ $parentId = $category->pid;
|
|
|
+ while ($parentId > 0) {
|
|
|
+ $parent = Category::find($parentId);
|
|
|
+ if ($parent) {
|
|
|
+ $allCategories[$parent->id] = $parent;
|
|
|
+ $parentId = $parent->pid;
|
|
|
+ } else {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $allCategories[$category->id] = $category;
|
|
|
}
|
|
|
- return Result::success($rep);
|
|
|
+ return array_values($allCategories);
|
|
|
}
|
|
|
+
|
|
|
+ private function buildCategoryTree($category, $allRequiredCategories)
|
|
|
+ {
|
|
|
+ $categoryData = $category->toArray();
|
|
|
+ $children = [];
|
|
|
+ foreach ($allRequiredCategories as $c) {
|
|
|
+ if ($c->pid === $category->id) {
|
|
|
+ $children[] = $this->buildCategoryTree($c, $allRequiredCategories);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!empty($children)) {
|
|
|
+ $categoryData['children'] = $children;
|
|
|
+ }
|
|
|
+ return $categoryData;
|
|
|
+ }
|
|
|
+
|
|
|
public function myCategoryList(array $data): array
|
|
|
{
|
|
|
$sszq = $data['sszq'] ?? '';
|
|
@@ -1033,7 +1097,7 @@ class NewsService implements NewsServiceInterface
|
|
|
// $total = 0;
|
|
|
if (!empty($other)) {
|
|
|
$total = $total + $other['results'];
|
|
|
- $other['choice_name'] = $other['choice_name'] . '(其他)';
|
|
|
+ $other['choice_name'] = '(其他)';
|
|
|
if (!empty($others)) {
|
|
|
$other['hasChildren'] = true;
|
|
|
// array_push($other,['hasChildren','=',true]);
|
|
@@ -1113,6 +1177,7 @@ class NewsService implements NewsServiceInterface
|
|
|
*/
|
|
|
public function getWebsiteCatidArticle(array $data): array
|
|
|
{
|
|
|
+ // return Result::success($data);
|
|
|
$where = [
|
|
|
// 'category.status' => 1,
|
|
|
'website_category.category_id' => $data['catid'],
|
|
@@ -1120,7 +1185,51 @@ class NewsService implements NewsServiceInterface
|
|
|
// 'article.status' => 1,
|
|
|
];
|
|
|
// $category = WebsiteCategory::where($where);
|
|
|
- if (!empty($data['img_num'])) {
|
|
|
+ if (isset($data['child_catnum']) && !empty($data['child_catnum'])) {
|
|
|
+ $child_catnum = $data['child_catnum'] ?? 1;
|
|
|
+ $category['child'] = WebsiteCategory::where('pid', $data['catid'])->where('website_id', $data['website_id'])->select('category_id', 'alias')->limit($child_catnum)->get()->toArray();
|
|
|
+ $childCategoryIds = array_column($category['child'], 'category_id');
|
|
|
+ if (empty($childCategoryIds)) {
|
|
|
+ return Result::error("暂无子栏目", 0);
|
|
|
+ }
|
|
|
+ $imgArticles = [];
|
|
|
+ $textArticles = [];
|
|
|
+ // return Result::success($childCategoryIds);
|
|
|
+ if (isset($data['child_imgnum']) && !empty($data['child_imgnum']) && $data['child_imgnum'] != 0) {
|
|
|
+ // 初始化子分类图片新闻和文字新闻数组
|
|
|
+
|
|
|
+ // 查询所有子级栏目的图文新闻
|
|
|
+ $imgArticles = Article::where('catid', $childCategoryIds[0])
|
|
|
+ ->where('status', 1)
|
|
|
+ ->where('imgurl', '!=', '')
|
|
|
+ ->select('*')
|
|
|
+ ->orderBy('updated_at', 'desc')
|
|
|
+ ->limit($data['child_imgnum'])
|
|
|
+ ->get();
|
|
|
+ }
|
|
|
+ if (isset($data['child_textnum']) && !empty($data['child_textnum']) && $data['child_textnum'] != 0) {
|
|
|
+ // 查询所有子级栏目的文字新闻
|
|
|
+ $textArticles = Article::where('catid', $childCategoryIds[0])
|
|
|
+ ->where('status', 1)
|
|
|
+ // ->where(function ($query) {
|
|
|
+ // $query->whereNull('imgurl')
|
|
|
+ // ->orWhere('imgurl', '');
|
|
|
+ // })
|
|
|
+ ->select('*')
|
|
|
+ ->orderBy('updated_at', 'desc')
|
|
|
+ ->limit($data['child_textnum'])
|
|
|
+ ->get();
|
|
|
+ }
|
|
|
+ // 遍历子分类,将图文新闻和文字新闻分别添加到子分类中
|
|
|
+ $category['child'] = array_map(function ($child) use ($imgArticles, $textArticles) {
|
|
|
+ $child['img'] = $imgArticles ? $imgArticles->where('catid', $child['category_id']) : [];
|
|
|
+ $child['text'] = $textArticles ? $textArticles->where('catid', $child['category_id']) : [];
|
|
|
+ return $child;
|
|
|
+ }, $category['child']);
|
|
|
+
|
|
|
+ }
|
|
|
+ // }
|
|
|
+ if (isset($data['img_num']) && !empty($data['img_num'])) {
|
|
|
$category['img'] = WebsiteCategory::where($where)
|
|
|
->leftJoin('article', 'article.catid', 'website_category.category_id')
|
|
|
->where('article.status', 1)
|
|
@@ -1131,14 +1240,15 @@ class NewsService implements NewsServiceInterface
|
|
|
->get();
|
|
|
}
|
|
|
|
|
|
- if (!empty($data['text_num'])) {
|
|
|
+ if (isset($data['text_num']) && !empty($data['text_num'])) {
|
|
|
$category['text'] = WebsiteCategory::where($where)
|
|
|
->leftJoin('article', 'article.catid', 'website_category.category_id')
|
|
|
->where('article.status', 1)
|
|
|
- ->where(function ($query) {
|
|
|
- $query->whereNull('article.imgurl')
|
|
|
- ->orWhere('article.imgurl', '');
|
|
|
- })
|
|
|
+
|
|
|
+ // ->where(function ($query) {
|
|
|
+ // $query->whereNull('article.imgurl')
|
|
|
+ // ->orWhere('article.imgurl', '');
|
|
|
+ // })
|
|
|
->select('article.*', 'website_category.category_id', 'website_category.alias')
|
|
|
->orderBy('article.updated_at', 'desc')
|
|
|
->limit($data['text_num'])
|