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

79 lines
2.0 KiB

  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\AgentProduct;
  5. use App\Models\Product;
  6. use Illuminate\Support\Facades\DB;
  7. /**
  8. * 代理商产品
  9. * Class AgentProductController
  10. * @package App\Http\Controllers\Api
  11. */
  12. class AgentProductController extends Controller
  13. {
  14. // 代理商产品列表
  15. public function index()
  16. {
  17. $channel_id = request()->input('channel_id');
  18. if ($channel_id) {
  19. $where['channel_id'] = $channel_id;
  20. }
  21. $where['agent_id'] = $this->agent_id;
  22. $list = AgentProduct::where($where)
  23. ->with(['product' => fn($query) => $query->select('id', 'title', 'pictures')])
  24. ->select('id', 'sale', 'product_id', 'price', 'original_price')
  25. ->orderBy('id', 'DESC')
  26. ->simplePaginate();
  27. return $this->success($list);
  28. }
  29. // 产品详情
  30. public function show()
  31. {
  32. $id = request()->input('id');
  33. if (!$id || !ctype_digit($id)) {
  34. return $this->error('无效的ID');
  35. }
  36. // TODO 此处待优化
  37. $data = DB::table('agent_products AS ap')
  38. ->leftJoin('products AS p', 'p.id', '=', 'ap.product_id')
  39. //->leftJoin('product_infos AS pi', 'p.id', '=', 'pi.product_id')
  40. ->select('ap.*', 'p.title', 'p.pictures')
  41. ->where('ap.id', $id)
  42. ->where(['ap.status' => 1, 'p.status' => 1])
  43. ->first();
  44. if (!$data) {
  45. return $this->error('产品不存在或已下架');
  46. }
  47. $coupon_ids = explode(',', $data->coupon_ids);
  48. $data->coupon_ids = DB::table('coupons')->whereIn('id', $coupon_ids)->pluck('tag');
  49. $data->pictures = json_decode($data->pictures, true);
  50. unset($data->deleted_at);
  51. return $this->success($data);
  52. }
  53. // 人气爆款
  54. /*public function hot()
  55. {
  56. $list = Product::get(['title', 'price', 'original_price', 'pictures', 'sale']);
  57. return $this->success($list);
  58. }*/
  59. // 猜你喜欢
  60. public function guessLike()
  61. {
  62. $page = request()->only('page');
  63. if ($page && !ctype_digit($page)) {
  64. return $this->error('页码错误');
  65. }
  66. $list = Product::get(['title', 'price', 'original_price', 'pictures', 'sale'])->simplePaginate();
  67. return $this->success($list);
  68. }
  69. }