Browse Source

1、增加recommend推荐产品列表;2、产品详情增加折扣计算;3、关联查询指定字段代码简化;

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

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

@ -3,6 +3,7 @@
namespace App\Http\Controllers\Api; namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\AgentProduct; use App\Models\AgentProduct;
use App\Models\UserFav;
/** /**
* 代理商产品 * 代理商产品
@ -22,7 +23,7 @@ class AgentProductController extends Controller
$where['agent_id'] = $this->agent_id; $where['agent_id'] = $this->agent_id;
$list = AgentProduct::where($where) $list = AgentProduct::where($where)
->with(['product' => fn($query) => $query->select('id', 'title', 'pictures')])
->with('product:id,title,pictures')
->select('id', 'sale', 'product_id', 'price', 'original_price') ->select('id', 'sale', 'product_id', 'price', 'original_price')
->orderBy('id', 'DESC') ->orderBy('id', 'DESC')
->simplePaginate(); ->simplePaginate();
@ -35,16 +36,27 @@ class AgentProductController extends Controller
$id = (int)request()->input('id'); $id = (int)request()->input('id');
// TODO 优惠券查询待优化 // TODO 优惠券查询待优化
$data = AgentProduct::query()
->with(['product' => fn($query) => $query->select('id', 'title', 'pictures', 'know', 'content')])
->with(['coupon' => fn($query) => $query->select('id', 'tag', 'agent_product_id')])
$agent_product = AgentProduct::query()
->with('product:id,title,pictures,know,stock,content')
->with('coupon:tag,agent_product_id')
->with('fav:agent_product_id')
->firstWhere(['id' => $id, 'agent_id' => $this->agent_id, 'status' => 1]); ->firstWhere(['id' => $id, 'agent_id' => $this->agent_id, 'status' => 1]);
if (!$data) {
if (!$agent_product || !$agent_product->product) {
return $this->error('产品不存在或已下架'); return $this->error('产品不存在或已下架');
} }
unset($data->agent_id, $data->status, $data->created_at, $data->deleted_at);
return $this->success($data);
$agent_product->is_collect = !is_null($agent_product->fav); //判断是否收藏
//计算折扣
if ($agent_product->price < $agent_product->original_price) {
$agent_product->cost = round($agent_product->price / $agent_product->original_price * 10, 1);
} else {
$agent_product->cost = '';
}
unset($agent_product->agent_id, $agent_product->status, $agent_product->deleted_at);
return $this->success($agent_product);
} }
// 猜你喜欢 // 猜你喜欢
@ -52,10 +64,17 @@ class AgentProductController extends Controller
{ {
// TODO 此处需要再优化排序规则,并增加广告功能 // TODO 此处需要再优化排序规则,并增加广告功能
$list = AgentProduct::where('agent_id', $this->agent_id) $list = AgentProduct::where('agent_id', $this->agent_id)
->with(['product' => fn($query) => $query->select('id', 'title', 'pictures')])
->with('product:id,title,pictures')
->select('id', 'sale', 'product_id', 'price', 'original_price') ->select('id', 'sale', 'product_id', 'price', 'original_price')
->orderBy('id', 'DESC') ->orderBy('id', 'DESC')
->simplePaginate(); ->simplePaginate();
return $this->success($list); return $this->success($list);
} }
//我的下方推荐
public function recommend()
{
//TODO 推荐数据暂时使用产品列表,后期需要通过后台设置获取或根据用户购买过的关键词获取
return $this->index();
}
} }
Loading…
Cancel
Save