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

108 lines
3.1 KiB

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