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

85 lines
2.4 KiB

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