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

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\Advertising;
  8. use App\Models\UserChannel;
  9. use Illuminate\Support\Facades\Storage;
  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. $img_prefix = Storage::disk('public')->url('');
  21. # 轮播图
  22. $slide = Advertising::where(['agent_id' => $this->agent_id, 'status' => 1, 'display' => 0])
  23. ->orderBy('sort')->orderBy('id', 'DESC')->limit(10)
  24. ->get(['title', 'picture', 'type', 'url']);
  25. if (!$slide->isEmpty()) {
  26. foreach ($slide as &$v) {
  27. $v->picture = $img_prefix . $v->picture;
  28. }
  29. }
  30. # 公告
  31. $notice = Notice::where('agent_id', $this->agent_id)->limit(10)
  32. ->orderBy('sort')->orderBy('id', 'desc')->get(['id', 'title']);
  33. # 我的频道
  34. if ($this->user_id) {
  35. $channel_ids = UserChannel::where('user_id', $this->user_id)->value('channels');
  36. $my_channels = Channel::where('agent_id', $this->agent_id)
  37. ->whereIn('id', $channel_ids)
  38. ->get(['id', 'name', 'icon']);
  39. } else {
  40. $my_channels = Channel::where('agent_id', $this->agent_id)
  41. ->where('pid', '<>', 0)
  42. ->orderBy('id')
  43. ->limit(8)
  44. ->get(['id', 'name', 'icon']);
  45. }
  46. if (!$my_channels->isEmpty()) {
  47. foreach ($my_channels as &$v) {
  48. $v->icon = $img_prefix . $v->icon;
  49. }
  50. }
  51. # 专题列表
  52. $special = Advertising::where(['agent_id' => $this->agent_id, 'display' => 1])
  53. ->orderBy('sort')->orderBy('id', 'desc')->limit(6)
  54. ->get(['title', 'picture', 'type', 'url']);
  55. if (!$special->isEmpty()) {
  56. foreach ($special as $k=>&$v) {
  57. $v->picture = $img_prefix . $v->picture;
  58. }
  59. }
  60. # 人气爆款
  61. $hots = AgentProduct::list($this->agent_id)
  62. ->orderBy('sale', 'desc')->orderBy('id', 'desc')
  63. ->limit(6)->get();
  64. if (!$hots->isEmpty()) {
  65. foreach ($hots as &$v) {
  66. if (!empty($v->pictures) && is_array($v->pictures)) {
  67. $v->pictures = array_map(function($item) use ($img_prefix) {
  68. return strpos($item, $img_prefix) === false ? $img_prefix . $item : $item;
  69. }, $v->pictures);
  70. }
  71. }
  72. }
  73. return $this->success([
  74. 'slide' => $slide,
  75. 'notice' => $notice,
  76. 'my_channels' => $my_channels,
  77. 'special' => $special,
  78. 'hots' => $hots,
  79. ]);
  80. }
  81. }