You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
<?php
namespace App\Http\Controllers\Api;use App\Common\ProductStatus;use App\Http\Controllers\Controller;use App\Models\AgentProduct;use App\Models\Category;use Illuminate\Http\JsonResponse;
class CategoryController extends Controller{ # 推荐页分类列表
public function index(): JsonResponse { $list = Category::query() ->where('agent_id', $this->agent_id) ->orderBy('sort') ->get(['id', 'name', 'pid']); if ($list->isEmpty()) { //如果是新入驻商户,显示默认数据
if (AgentProduct::where('agent_id', $this->agent_id)->withTrashed()->count() === 0 && request('page', 1) <= 2) { //只获取2页数据
$list = AgentProduct::list($this->agent_id)->orWhere([['status', '=', ProductStatus::ON_SALE], ['price', '>', 500]])->orderBy('id')->simplePaginate(); } } return $this->success($list); }
# 首页频道获取的分类列表(无论分类有几级都只显示两级)
public function index_channel(): JsonResponse { $list = Category::query() ->where('agent_id', $this->agent_id) ->orderBy('sort') ->get(['id', 'name', 'pid'])->toArray();
$list = array_column($list, null, 'id'); foreach ($list as &$v) { if ($v['pid'] == 0) continue;
$parent_pid = $list[$v['pid']]['pid']; while ($parent_pid != 0) { $v['pid'] = $parent_pid; $parent_pid = $list[$parent_pid]['pid']; } } return $this->success(array_values($list)); }}
|