Browse Source

通过category_id获取产品列表时获取所有子分类

master
李可松 4 years ago
parent
commit
971ca000d5
  1. 48
      app/Http/Controllers/Api/AgentProductController.php

48
app/Http/Controllers/Api/AgentProductController.php

@ -5,7 +5,7 @@ use App\Common\ProductStatus;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\Advertising; use App\Models\Advertising;
use App\Models\AgentProduct; use App\Models\AgentProduct;
use App\Models\Product;
use App\Models\Category;
use App\Models\UserFav; use App\Models\UserFav;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
@ -19,19 +19,47 @@ class AgentProductController extends Controller
// 代理商产品列表 // 代理商产品列表
public function index() public function index()
{ {
$category_id = request()->input('category_id');
$where = [];
$formData = request()->only(['category_id', 'type', 'by']);
$category_id = $formData['category_id'] ?? 0;
$type = $formData['type'] ?? 0;
$by = $formData['by'] ?? 0;
$list = AgentProduct::list($this->agent_id);
if ($category_id) { if ($category_id) {
$where['category_id'] = $category_id;
$list = $list->whereIn('category_id', [$category_id, ...$this->get_category_child_ids($category_id)]);
} }
$list = AgentProduct::list($this->agent_id)->where($where)->orderBy('id', 'DESC')->simplePaginate();
$fields = ['id', 'sale', 'updated_at', 'price']; //排序字段 TODO 还有距离排序
$field = $fields[$type] ?? $fields[0];
$by = $by == 0 ? 'desc' : 'asc';
$list = $list->orderBy($field, $by)->simplePaginate();
$list = $this->paginatePicAddHost($list); $list = $this->paginatePicAddHost($list);
$list = $this->insertAd($list); $list = $this->insertAd($list);
return $this->success($list); return $this->success($list);
} }
//递归获取指定分类下的所有子分类
private function get_category_child_ids($category_id): array
{
static $category = null;
if ($category === null) {
$category = Category::where('agent_id', $this->agent_id)->get()->toArray();
}
$child = [];
foreach ($category as $cat) {
if ($cat['pid'] == $category_id) {
$child[] = $cat['id'];
$child = array_merge($child, $this->get_category_child_ids($cat['id']));
}
}
return $child;
}
//首页搜索框 //首页搜索框
public function search() public function search()
{ {
@ -43,13 +71,13 @@ class AgentProductController extends Controller
$list = AgentProduct::list($this->agent_id); $list = AgentProduct::list($this->agent_id);
if ($category_id) { if ($category_id) {
$list = $list->where('category_id', $category_id);
$list = $list->whereIn('category_id', [$category_id, ...$this->get_category_child_ids($category_id)]);
} }
if ($keywords) { if ($keywords) {
$list = $list->where('title', 'like', "%$keywords%"); $list = $list->where('title', 'like', "%$keywords%");
} }
$fields = ['id', 'sale', 'updated_at', 'price']; //排序字段 TODO 还有距离排序
$fields = ['id', 'sale', 'updated_at', 'price']; //排序字段
$field = $fields[$type] ?? $fields[0]; $field = $fields[$type] ?? $fields[0];
$by = $by == 0 ? 'desc' : 'asc'; $by = $by == 0 ? 'desc' : 'asc';
@ -64,6 +92,8 @@ class AgentProductController extends Controller
public function travel_search() public function travel_search()
{ {
$formData = request()->only(['departure_place', 'destination', 'type', 'by']); $formData = request()->only(['departure_place', 'destination', 'type', 'by']);
$type = $formData['type'] ?? 0;
$by = $formData['by'] ?? 0;
if (empty($formData['departure_place']) && empty($formData['destination'])) { if (empty($formData['departure_place']) && empty($formData['destination'])) {
return $this->error('请输入出发地和目的地'); return $this->error('请输入出发地和目的地');
@ -71,8 +101,8 @@ class AgentProductController extends Controller
$fields = ['id', 'sale', 'updated_at', 'price']; //排序字段 $fields = ['id', 'sale', 'updated_at', 'price']; //排序字段
$field = $fields[$formData['type']] ?? $fields[0];
$by = $formData['by'] == 0 ? 'desc' : 'asc';
$field = $fields[$type] ?? $fields[0];
$by = $by == 0 ? 'desc' : 'asc';
$list = AgentProduct::list($this->agent_id)->whereHas('product', function($query) use ($formData) { $list = AgentProduct::list($this->agent_id)->whereHas('product', function($query) use ($formData) {
//出发地 //出发地

Loading…
Cancel
Save