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

107 lines
3.0 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. $img_prefix = Storage::disk('public')->url('');
  22. # 轮播图
  23. $slide = Slide::where(['agent_id' => $this->agent_id, 'status' => 1])
  24. ->orderBy('sort')->orderBy('id', 'DESC')->limit(10)
  25. ->get(['title', 'picture', 'type', 'url']);
  26. foreach ($slide as &$v) {
  27. $v->picture = $img_prefix . $v->picture;
  28. }
  29. # 公告
  30. $notice = Notice::where('agent_id', $this->agent_id)->limit(10)->get(['id', 'title', 'updated_at']);
  31. # 我的频道
  32. if ($this->user_id) {
  33. $channel_ids = UserChannel::where('user_id', $this->user_id)->value('channels');
  34. //如果不存在则存入初始数据
  35. if (!$channel_ids) {
  36. $channel_ids = Channel::where([
  37. ['agent_id', '=', $this->agent_id],
  38. ['pid', '<>', 0],
  39. ])
  40. ->orderBy('id')
  41. ->limit(8)
  42. ->pluck('id')
  43. ->toArray();
  44. //存入user_channel
  45. UserChannel::where('user_id', $this->user_id)
  46. ->insert([
  47. 'user_id' => $this->user_id,
  48. 'channels' => json_encode($channel_ids)
  49. ]);
  50. }
  51. $my_channels = Channel::where('agent_id', $this->agent_id)
  52. ->whereIn('id', $channel_ids)
  53. ->get(['id', 'name', 'icon']);
  54. } else {
  55. $my_channels = Channel::where('agent_id', $this->agent_id)
  56. ->where('pid', '<>', 0)
  57. ->orderBy('id')
  58. ->limit(8)
  59. ->get(['id', 'name', 'icon']);
  60. }
  61. if (!$my_channels->isEmpty()) {
  62. foreach ($my_channels as &$v) {
  63. $v->icon = $img_prefix . $v->icon;
  64. }
  65. }
  66. # 专题列表
  67. $special = Special::query()
  68. ->where('agent_id', $this->agent_id)
  69. ->orderBy('sort')->orderBy('id')
  70. ->limit(6)->get(['id', 'picture_ad']);
  71. if (!$special->isEmpty()) {
  72. foreach ($special as $k=>&$v) {
  73. $v->picture_ad = $img_prefix . $v->picture_ad;
  74. }
  75. }
  76. # 人气爆款
  77. $hots = AgentProduct::query()
  78. ->where('agent_id', $this->agent_id)
  79. ->select('id', 'sale', 'product_id', 'price', 'original_price') //必须查询product_id才能with
  80. ->with('product:id,title,pictures') //必须查询ID才能正常查询
  81. ->limit(6)->get();
  82. if (!$hots->isEmpty()) {
  83. foreach ($hots as &$v) {
  84. if (!empty($v->product->pictures) && is_array($v->product->pictures)) {
  85. $v->product->pictures = array_map(function($item) use ($img_prefix) {
  86. return strpos($item, $img_prefix) === false ? $img_prefix . $item : $item;
  87. }, $v->product->pictures);
  88. }
  89. }
  90. }
  91. return $this->success([
  92. 'slide' => $slide,
  93. 'notice' => $notice,
  94. 'my_channels' => $my_channels,
  95. 'special' => $special,
  96. 'hots' => $hots,
  97. ]);
  98. }
  99. }