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

99 lines
2.7 KiB

  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\UserFav;
  7. use Illuminate\Support\Facades\Storage;
  8. /**
  9. * 代理商产品
  10. * Class AgentProductController
  11. * @package App\Http\Controllers\Api
  12. */
  13. class AgentProductController extends Controller
  14. {
  15. // 代理商产品列表
  16. public function index()
  17. {
  18. $category_id = request()->input('category_id');
  19. if ($category_id) {
  20. $where['category_id'] = $category_id;
  21. }
  22. $where['agent_id'] = $this->agent_id;
  23. $list = AgentProduct::list()->where($where)->simplePaginate();
  24. return $this->success($this->paginatePicAddHost($list));
  25. }
  26. // 产品详情
  27. public function show()
  28. {
  29. $id = (int)request()->input('id');
  30. // TODO 优惠券查询待优化
  31. $agent_product = AgentProduct::query()
  32. ->with('product:id,title,pictures,know,stock,content')
  33. ->with('coupon:tag,agent_product_id')
  34. ->with('fav:agent_product_id')
  35. ->whereHas('product', function ($query) {
  36. return $query->where('status', ProductStatus::ON_SALE)->where('stock', '>', 0);
  37. })
  38. ->where('stock', '>', 0)
  39. ->firstWhere(['id' => $id, 'agent_id' => $this->agent_id, 'status' => ProductStatus::ON_SALE]);
  40. if (!$agent_product) {
  41. return $this->error('产品不存在或已下架');
  42. }
  43. $prefix = Storage::disk('public')->url('');
  44. $agent_product->product->pictures = array_map(fn($item) => ($prefix . $item), $agent_product->product->pictures);
  45. $agent_product->is_collect = !is_null($agent_product->fav); //判断是否收藏
  46. //计算折扣
  47. if ($agent_product->price < $agent_product->original_price) {
  48. $agent_product->cost = round($agent_product->price / $agent_product->original_price * 10, 1);
  49. } else {
  50. $agent_product->cost = '';
  51. }
  52. unset($agent_product->agent_id, $agent_product->status, $agent_product->deleted_at);
  53. return $this->success($agent_product);
  54. }
  55. // 猜你喜欢
  56. public function guessLike()
  57. {
  58. // TODO 此处需要再优化排序规则,并增加广告功能
  59. return $this->index();
  60. }
  61. //【我的】页面下方推荐
  62. public function recommendList()
  63. {
  64. //TODO 推荐数据暂时使用产品列表,后期需要通过后台设置获取或根据用户购买过的关键词获取
  65. return $this->index();
  66. }
  67. //人气爆款列表
  68. public function hotList()
  69. {
  70. //TODO 具体排序规则,后期再做修改
  71. return $this->index();
  72. }
  73. //分页列表产品图片加域名
  74. private function paginatePicAddHost($list)
  75. {
  76. if (!$list->isEmpty()) {
  77. $prefix = Storage::disk('public')->url('');
  78. foreach ($list->items() as $k=>&$v) {
  79. $v->product->pictures = array_map(fn($item) => ($prefix . $item), $v->product->pictures);
  80. }
  81. }
  82. return $list;
  83. }
  84. }