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

82 lines
2.2 KiB

  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Channel;
  5. use App\Models\Notice;
  6. use App\Models\AgentProduct;
  7. use App\Models\Slide;
  8. use App\Models\Special;
  9. use App\Models\UserChannel;
  10. /**
  11. * 小程序首页
  12. * Class IndexController
  13. * @package App\Http\Controllers\Api
  14. */
  15. class IndexController extends Controller
  16. {
  17. // 首页数据大杂烩
  18. public function index()
  19. {
  20. # 轮播图
  21. $slide = Slide::where('agent_id', $this->agent_id)->where('status', 1)
  22. ->orderBy('sort')->orderBy('id', 'DESC')->limit(10)->get(['title','url']);
  23. # 公告
  24. $notice = Notice::where('agent_id', $this->agent_id)->limit(10)->get(['id', 'title', 'updated_at']);
  25. # 我的频道
  26. if ($this->user_id) {
  27. $channel_ids = UserChannel::where('user_id', $this->user_id)->value('channels');
  28. //如果不存在则存入初始数据
  29. if (!$channel_ids) {
  30. $channel_ids = Channel::where([
  31. ['agent_id', '=', $this->agent_id],
  32. ['pid', '<>', 0],
  33. ])
  34. ->orderBy('id')
  35. ->limit(8)
  36. ->pluck('id')
  37. ->toArray();
  38. //存入user_channel
  39. UserChannel::where('user_id', $this->user_id)
  40. ->insert([
  41. 'user_id' => $this->user_id,
  42. 'channels' => json_encode($channel_ids)
  43. ]);
  44. }
  45. $my_channels = Channel::where('agent_id', $this->agent_id)
  46. ->whereIn('id', $channel_ids)
  47. ->get(['id', 'name', 'icon']);
  48. } else {
  49. $my_channels = Channel::where('agent_id', $this->agent_id)
  50. ->where('pid', '<>', 0)
  51. ->orderBy('id')
  52. ->limit(8)
  53. ->get(['id', 'name', 'icon']);
  54. }
  55. # 专题列表
  56. $special = Special::query()
  57. ->where('agent_id', $this->agent_id)
  58. ->orderBy('sort')
  59. ->orderBy('id')
  60. ->get(['id', 'picture_ad']);
  61. # 人气爆款
  62. $hots = AgentProduct::query()
  63. ->where('agent_id', $this->agent_id)
  64. ->select('id', 'sale', 'product_id', 'price', 'original_price') //必须查询product_id才能with
  65. ->with('product:id,title,pictures') //必须查询ID才能正常查询
  66. ->limit(6)->get();
  67. return $this->success([
  68. 'slide' => $slide,
  69. 'notice' => $notice,
  70. 'my_channels' => $my_channels,
  71. 'special' => $special,
  72. 'hots' => $hots,
  73. ]);
  74. }
  75. }