|
@@ -43,7 +43,7 @@ use App\Model\Sector;
|
|
|
use PhpParser\Node\Stmt\Return_;
|
|
|
use function PHPUnit\Framework\isNull;
|
|
|
use Overtrue\Pinyin\Pinyin;
|
|
|
-
|
|
|
+use App\Tools\buildTree;
|
|
|
#[RpcService(name: "WebsiteService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
|
|
|
class WebsiteService implements WebsiteServiceInterface
|
|
|
{
|
|
@@ -622,11 +622,11 @@ class WebsiteService implements WebsiteServiceInterface
|
|
|
// 以下注释掉的代码是之前的逻辑,用于获取非顶级分类的 pid
|
|
|
$pidQuery = WebsiteCategory::where($website_id)
|
|
|
->where('pid', '!=', 0)
|
|
|
+ ->whereRaw('JSON_CONTAINS(category_arr_id,?)', [$data['pid']])
|
|
|
->orderBy('sort')
|
|
|
- ->select('pid', 'category_id', 'alias');
|
|
|
+ ->select('*');
|
|
|
$pid = $pidQuery->pluck('pid');
|
|
|
$pid = array_values(array_unique($pid->toArray()));
|
|
|
-
|
|
|
// 构建查询语句
|
|
|
$query = WebsiteCategory::where($website_id)
|
|
|
->where('pid', $data['pid'])
|
|
@@ -641,21 +641,32 @@ class WebsiteService implements WebsiteServiceInterface
|
|
|
// 如果 $pid 数组为空,不添加 CASE WHEN 条件,添加字段 children_count 并赋值为 0
|
|
|
$query->select('website_category.*', DB::raw('0 as children_count'));
|
|
|
}
|
|
|
-
|
|
|
- // 执行查询
|
|
|
- $placeid = $data['placeid'] - 1;
|
|
|
+ // $child = Result::buildMenuTree($child);
|
|
|
+ $placeid = $data['placeid']-1;
|
|
|
$result = $query->offset($placeid)->limit($data['num'])->get();
|
|
|
-
|
|
|
if (!empty($result)) {
|
|
|
$pidResults = $pidQuery->get();
|
|
|
- $pidMap = $pidResults->keyBy('pid');
|
|
|
- $result->each(function ($record) use ($pidMap) {
|
|
|
- if ($pidMap->has($record->category_id)) {
|
|
|
- $pidResult = $pidMap->get($record->category_id);
|
|
|
- $record->chilid_id = $pidResult->category_id;
|
|
|
- $record->chilid_alias = $pidResult->alias;
|
|
|
- }
|
|
|
- });
|
|
|
+ if(!isset($data['type']) || empty($data['type'])){
|
|
|
+ $pidMap = $pidResults->keyBy('pid');
|
|
|
+ $result->each(function ($record) use ($pidMap) {
|
|
|
+ if ($pidMap->has($record->category_id)) {
|
|
|
+ $pidResult = $pidMap->get($record->category_id);
|
|
|
+ $record->chilid_id = $pidResult->category_id;
|
|
|
+ $record->chilid_alias = $pidResult->alias;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }else{
|
|
|
+ $result = $result->map(function ($item) use ($pidResults) {
|
|
|
+ $children = $pidResults->where('pid', $item->category_id)->map(function ($child) {
|
|
|
+ if(!empty($child)){
|
|
|
+ return $child;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ // 重置索引,使 key 值从 0 开始
|
|
|
+ $item->children = $children->values();
|
|
|
+ return $item;
|
|
|
+ });
|
|
|
+ }
|
|
|
return Result::success($result);
|
|
|
} else {
|
|
|
return Result::error("本网站暂无栏目", 0);
|
|
@@ -2274,11 +2285,19 @@ class WebsiteService implements WebsiteServiceInterface
|
|
|
return Result::error("暂无该网站", 0);
|
|
|
}
|
|
|
// return Result::success($data);
|
|
|
- if (isset($data['pinyin']) && !empty($data['pinyin'])) {
|
|
|
- $result = WebsiteCategory::where('website_id', $data['website_id'])->where('aLIas_pinyin', $data['pinyin'])->first(['category_id', 'alias']);
|
|
|
- }
|
|
|
- if (isset($data['foot_pinyin']) && !empty($data['foot_pinyin'])) {
|
|
|
- $result = FooterCategory::where('website_id', $data['website_id'])->where('name_pinyin', $data['foot_pinyin'])->first(['id']);
|
|
|
+ if (isset($data['pinyin']) &&!empty($data['pinyin'])) {
|
|
|
+ $result = WebsiteCategory::where('website_id', $data['website_id'])
|
|
|
+ ->where('aLIas_pinyin', $data['pinyin'])
|
|
|
+ ->selectRaw('website_category.*,
|
|
|
+ CASE
|
|
|
+ WHEN EXISTS (SELECT 1 FROM website_category as wc WHERE wc.pid = website_category.category_id)
|
|
|
+ THEN 1
|
|
|
+ ELSE 0
|
|
|
+ END as childrencount')
|
|
|
+ ->first(['category_id', 'alias', 'children_count']);
|
|
|
+ }
|
|
|
+ if(isset($data['foot_pinyin']) &&!empty($data['foot_pinyin'])){
|
|
|
+ $result = FooterCategory::where('website_id',$data['website_id'])->where('name_pinyin',$data['foot_pinyin'])->first(['id']);
|
|
|
}
|
|
|
if (!isset($result) || empty($result)) {
|
|
|
return Result::error("暂无该导航", 0);
|