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

79 lines
2.2 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\Category;
  5. use App\Models\Notice;
  6. use App\Models\AgentProduct;
  7. use App\Models\Advertising;
  8. use App\Models\UserCategory;
  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_cats = [];
  35. if ($this->user_id) {
  36. $cate_ids = UserCategory::where('user_id', $this->user_id)->value('categories') ?? [];
  37. $my_cats = Category::where('agent_id', $this->agent_id)
  38. ->whereIn('id', $cate_ids)
  39. ->get(['id', 'name']);
  40. }
  41. # 专题列表
  42. $special = Advertising::where(['agent_id' => $this->agent_id, 'display' => 1])
  43. ->orderBy('sort')->orderBy('id', 'desc')->limit(6)
  44. ->get(['title', 'picture', 'type', 'url']);
  45. if (!$special->isEmpty()) {
  46. foreach ($special as $k=>&$v) {
  47. $v->picture = $img_prefix . $v->picture;
  48. }
  49. }
  50. # 人气爆款
  51. $hots = AgentProduct::list($this->agent_id)
  52. ->orderBy('sale', 'desc')->orderBy('id', 'desc')
  53. ->limit(6)->get();
  54. if (!$hots->isEmpty()) {
  55. foreach ($hots as &$v) {
  56. if (!empty($v->pictures) && is_array($v->pictures)) {
  57. $v->pictures = array_map(function($item) use ($img_prefix) {
  58. return strpos($item, $img_prefix) === false ? $img_prefix . $item : $item;
  59. }, $v->pictures);
  60. }
  61. }
  62. }
  63. return $this->success([
  64. 'slide' => $slide,
  65. 'notice' => $notice,
  66. 'my_channels' => $my_cats,
  67. 'special' => $special,
  68. 'hots' => $hots,
  69. ]);
  70. }
  71. }