海南旅游SAAS
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.

47 lines
1.4 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Common\ProductStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\AgentProduct;
  6. use App\Models\Category;
  7. use Illuminate\Http\JsonResponse;
  8. class CategoryController extends Controller
  9. {
  10. # 推荐页分类列表
  11. public function index(): JsonResponse
  12. {
  13. $list = Category::query()
  14. ->where('agent_id', $this->agent_id)
  15. ->orderBy('sort')
  16. ->get(['id', 'name', 'pid']);
  17. if ($list->isEmpty()) { //如果是新入驻商户,显示默认数据
  18. if (AgentProduct::where('agent_id', $this->agent_id)->withTrashed()->count() === 0 && request('page', 1) <= 2) { //只获取2页数据
  19. $list = AgentProduct::list($this->agent_id)->orWhere([['status', '=', ProductStatus::ON_SALE], ['price', '>', 500]])->orderBy('id')->simplePaginate();
  20. }
  21. }
  22. return $this->success($list);
  23. }
  24. # 首页频道获取的分类列表(无论分类有几级都只显示两级)
  25. public function index_channel(): JsonResponse
  26. {
  27. $list = Category::query()
  28. ->where('agent_id', $this->agent_id)
  29. ->orderBy('sort')
  30. ->get(['id', 'name', 'pid'])->toArray();
  31. $list = array_column($list, null, 'id');
  32. foreach ($list as &$v) {
  33. if ($v['pid'] == 0) continue;
  34. $parent_pid = $list[$v['pid']]['pid'];
  35. while ($parent_pid != 0) {
  36. $v['pid'] = $parent_pid;
  37. $parent_pid = $list[$parent_pid]['pid'];
  38. }
  39. }
  40. return $this->success(array_values($list));
  41. }
  42. }