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

91 lines
2.5 KiB

5 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. if ($channel_ids) {
  37. $my_channels = Channel::where('agent_id', $this->agent_id)
  38. ->whereIn('id', $channel_ids)
  39. ->get(['id', 'name', 'icon']);
  40. }
  41. } else {
  42. $my_channels = Channel::where('agent_id', $this->agent_id)
  43. ->where('pid', '<>', 0)
  44. ->orderBy('id')
  45. ->limit(8)
  46. ->get(['id', 'name', 'icon']);
  47. }
  48. if (!$my_channels->isEmpty()) {
  49. foreach ($my_channels as &$v) {
  50. $v->icon = $img_prefix . $v->icon;
  51. }
  52. }
  53. # 专题列表
  54. $special = Advertising::where(['agent_id' => $this->agent_id, 'display' => 1])
  55. ->orderBy('sort')->orderBy('id', 'desc')->limit(6)
  56. ->get(['title', 'picture', 'type', 'url']);
  57. if (!$special->isEmpty()) {
  58. foreach ($special as $k=>&$v) {
  59. $v->picture = $img_prefix . $v->picture;
  60. }
  61. }
  62. # 人气爆款
  63. $hots = AgentProduct::list($this->agent_id)
  64. ->orderBy('sale', 'desc')->orderBy('id', 'desc')
  65. ->limit(6)->get();
  66. if (!$hots->isEmpty()) {
  67. foreach ($hots as &$v) {
  68. if (!empty($v->pictures) && is_array($v->pictures)) {
  69. $v->pictures = array_map(function($item) use ($img_prefix) {
  70. return strpos($item, $img_prefix) === false ? $img_prefix . $item : $item;
  71. }, $v->pictures);
  72. }
  73. }
  74. }
  75. return $this->success([
  76. 'slide' => $slide,
  77. 'notice' => $notice,
  78. 'my_channels' => $my_channels,
  79. 'special' => $special,
  80. 'hots' => $hots,
  81. ]);
  82. }
  83. }