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

106 lines
2.9 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. //如果不存在则存入初始数据
  37. if (!$channel_ids) {
  38. $channel_ids = Channel::where([
  39. ['agent_id', '=', $this->agent_id],
  40. ['pid', '<>', 0],
  41. ])
  42. ->orderBy('id')
  43. ->limit(8)
  44. ->pluck('id')
  45. ->toArray();
  46. //存入user_channel
  47. UserChannel::where('user_id', $this->user_id)
  48. ->insert([
  49. 'user_id' => $this->user_id,
  50. 'channels' => json_encode($channel_ids)
  51. ]);
  52. }
  53. $my_channels = Channel::where('agent_id', $this->agent_id)
  54. ->whereIn('id', $channel_ids)
  55. ->get(['id', 'name', 'icon']);
  56. } else {
  57. $my_channels = Channel::where('agent_id', $this->agent_id)
  58. ->where('pid', '<>', 0)
  59. ->orderBy('id')
  60. ->limit(8)
  61. ->get(['id', 'name', 'icon']);
  62. }
  63. if (!$my_channels->isEmpty()) {
  64. foreach ($my_channels as &$v) {
  65. $v->icon = $img_prefix . $v->icon;
  66. }
  67. }
  68. # 专题列表
  69. $special = Advertising::where('agent_id', $this->agent_id)
  70. ->orderBy('sort')->orderBy('id', 'desc')->limit(6)
  71. ->get(['title', 'picture', 'type', 'url']);
  72. if (!$special->isEmpty()) {
  73. foreach ($special as $k=>&$v) {
  74. $v->picture = $img_prefix . $v->picture;
  75. }
  76. }
  77. # 人气爆款
  78. $hots = AgentProduct::list()->where('agent_id', $this->agent_id)
  79. ->orderBy('sale', 'desc')->orderBy('id', 'desc')
  80. ->limit(6)->get();
  81. if (!$hots->isEmpty()) {
  82. foreach ($hots as &$v) {
  83. if (!empty($v->pictures) && is_array($v->pictures)) {
  84. $v->pictures = array_map(function($item) use ($img_prefix) {
  85. return strpos($item, $img_prefix) === false ? $img_prefix . $item : $item;
  86. }, $v->pictures);
  87. }
  88. }
  89. }
  90. return $this->success([
  91. 'slide' => $slide,
  92. 'notice' => $notice,
  93. 'my_channels' => $my_channels,
  94. 'special' => $special,
  95. 'hots' => $hots,
  96. ]);
  97. }
  98. }