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

83 lines
2.3 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, 'status' => 1])
  22. ->orderBy('sort')->orderBy('id', 'DESC')->limit(10)
  23. ->get(['title', 'picture', 'type', 'url']);
  24. # 公告
  25. $notice = Notice::where('agent_id', $this->agent_id)->limit(10)->get(['id', 'title', 'updated_at']);
  26. # 我的频道
  27. if ($this->user_id) {
  28. $channel_ids = UserChannel::where('user_id', $this->user_id)->value('channels');
  29. //如果不存在则存入初始数据
  30. if (!$channel_ids) {
  31. $channel_ids = Channel::where([
  32. ['agent_id', '=', $this->agent_id],
  33. ['pid', '<>', 0],
  34. ])
  35. ->orderBy('id')
  36. ->limit(8)
  37. ->pluck('id')
  38. ->toArray();
  39. //存入user_channel
  40. UserChannel::where('user_id', $this->user_id)
  41. ->insert([
  42. 'user_id' => $this->user_id,
  43. 'channels' => json_encode($channel_ids)
  44. ]);
  45. }
  46. $my_channels = Channel::where('agent_id', $this->agent_id)
  47. ->whereIn('id', $channel_ids)
  48. ->get(['id', 'name', 'icon']);
  49. } else {
  50. $my_channels = Channel::where('agent_id', $this->agent_id)
  51. ->where('pid', '<>', 0)
  52. ->orderBy('id')
  53. ->limit(8)
  54. ->get(['id', 'name', 'icon']);
  55. }
  56. # 专题列表
  57. $special = Special::query()
  58. ->where('agent_id', $this->agent_id)
  59. ->orderBy('sort')
  60. ->orderBy('id')
  61. ->get(['id', 'picture_ad']);
  62. # 人气爆款
  63. $hots = AgentProduct::query()
  64. ->where('agent_id', $this->agent_id)
  65. ->select('id', 'sale', 'product_id', 'price', 'original_price') //必须查询product_id才能with
  66. ->with('product:id,title,pictures') //必须查询ID才能正常查询
  67. ->limit(6)->get();
  68. return $this->success([
  69. 'slide' => $slide,
  70. 'notice' => $notice,
  71. 'my_channels' => $my_channels,
  72. 'special' => $special,
  73. 'hots' => $hots,
  74. ]);
  75. }
  76. }