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

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