|
@@ -21,6 +21,7 @@ use Ramsey\Uuid\Uuid;
|
|
use Hyperf\Utils\Random;
|
|
use Hyperf\Utils\Random;
|
|
use Fukuball\Jieba\Jieba;
|
|
use Fukuball\Jieba\Jieba;
|
|
use Fukuball\Jieba\Finalseg;
|
|
use Fukuball\Jieba\Finalseg;
|
|
|
|
+use Hyperf\Utils\Collection;
|
|
|
|
|
|
#[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
|
|
#[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
|
|
class NewsService implements NewsServiceInterface
|
|
class NewsService implements NewsServiceInterface
|
|
@@ -33,12 +34,75 @@ class NewsService implements NewsServiceInterface
|
|
*/
|
|
*/
|
|
public function getCategoryList(array $data): array
|
|
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 Result::success($rep);
|
|
|
|
|
|
+ $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 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
|
|
public function myCategoryList(array $data): array
|
|
{
|
|
{
|
|
$sszq = $data['sszq'] ?? '';
|
|
$sszq = $data['sszq'] ?? '';
|