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

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