Browse Source

图片域名拼接,并且使用list作用域来获取产品列表

dev
李可松 4 years ago
parent
commit
e924c81462
  1. 44
      app/Http/Controllers/Api/AgentProductController.php

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

@ -5,6 +5,7 @@ use App\Common\ProductStatus;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\AgentProduct; use App\Models\AgentProduct;
use App\Models\UserFav; use App\Models\UserFav;
use Illuminate\Support\Facades\Storage;
/** /**
* 代理商产品 * 代理商产品
@ -23,15 +24,9 @@ class AgentProductController extends Controller
$where['agent_id'] = $this->agent_id; $where['agent_id'] = $this->agent_id;
$list = AgentProduct::where($where)
->with('product:id,title,pictures')
->whereHas('product', function ($query) {
return $query->where('status', ProductStatus::ON_SALE);
})
->select('id', 'sale', 'product_id', 'price', 'original_price')
->orderBy('id', 'DESC')
->simplePaginate();
return $this->success($list);
$list = AgentProduct::list()->where($where)->simplePaginate();
return $this->success($this->paginatePicAddHost($list));
} }
// 产品详情 // 产品详情
@ -45,14 +40,18 @@ class AgentProductController extends Controller
->with('coupon:tag,agent_product_id') ->with('coupon:tag,agent_product_id')
->with('fav:agent_product_id') ->with('fav:agent_product_id')
->whereHas('product', function ($query) { ->whereHas('product', function ($query) {
return $query->where('status', ProductStatus::ON_SALE);
return $query->where('status', ProductStatus::ON_SALE)->where('stock', '>', 0);
}) })
->where('stock', '>', 0)
->firstWhere(['id' => $id, 'agent_id' => $this->agent_id, 'status' => ProductStatus::ON_SALE]); ->firstWhere(['id' => $id, 'agent_id' => $this->agent_id, 'status' => ProductStatus::ON_SALE]);
if (!$agent_product || !$agent_product->product) {
if (!$agent_product) {
return $this->error('产品不存在或已下架'); return $this->error('产品不存在或已下架');
} }
$prefix = Storage::disk('public')->url('');
$agent_product->product->pictures = array_map(fn($item) => ($prefix . $item), $agent_product->product->pictures);
$agent_product->is_collect = !is_null($agent_product->fav); //判断是否收藏 $agent_product->is_collect = !is_null($agent_product->fav); //判断是否收藏
//计算折扣 //计算折扣
if ($agent_product->price < $agent_product->original_price) { if ($agent_product->price < $agent_product->original_price) {
@ -61,7 +60,6 @@ class AgentProductController extends Controller
$agent_product->cost = ''; $agent_product->cost = '';
} }
unset($agent_product->agent_id, $agent_product->status, $agent_product->deleted_at); unset($agent_product->agent_id, $agent_product->status, $agent_product->deleted_at);
return $this->success($agent_product); return $this->success($agent_product);
} }
@ -70,15 +68,7 @@ class AgentProductController extends Controller
public function guessLike() public function guessLike()
{ {
// TODO 此处需要再优化排序规则,并增加广告功能 // TODO 此处需要再优化排序规则,并增加广告功能
$list = AgentProduct::where('agent_id', $this->agent_id)
->with('product:id,title,pictures')
->whereHas('product', function ($query) {
return $query->where('status', ProductStatus::ON_SALE);
})
->select('id', 'sale', 'product_id', 'price', 'original_price')
->orderBy('id', 'DESC')
->simplePaginate();
return $this->success($list);
return $this->index();
} }
//【我的】页面下方推荐 //【我的】页面下方推荐
@ -94,4 +84,16 @@ class AgentProductController extends Controller
//TODO 具体排序规则,后期再做修改 //TODO 具体排序规则,后期再做修改
return $this->index(); return $this->index();
} }
//分页列表产品图片加域名
private function paginatePicAddHost($list)
{
if (!$list->isEmpty()) {
$prefix = Storage::disk('public')->url('');
foreach ($list->items() as $k=>&$v) {
$v->product->pictures = array_map(fn($item) => ($prefix . $item), $v->product->pictures);
}
}
return $list;
}
} }
Loading…
Cancel
Save