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

110 lines
3.1 KiB

  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\Slide;
  8. use App\Models\Special;
  9. use App\Models\UserChannel;
  10. use Illuminate\Support\Facades\Storage;
  11. /**
  12. * 小程序首页
  13. * Class IndexController
  14. * @package App\Http\Controllers\Api
  15. */
  16. class IndexController extends Controller
  17. {
  18. // 首页数据大杂烩
  19. public function index()
  20. {
  21. $img_prefix = Storage::disk('public')->url('');
  22. # 轮播图
  23. $slide = Slide::where(['agent_id' => $this->agent_id, 'status' => 1])
  24. ->orderBy('sort')->orderBy('id', 'DESC')->limit(10)
  25. ->get(['title', 'picture', 'type', 'url']);
  26. foreach ($slide as &$v) {
  27. $v->picture = $img_prefix . $v->picture;
  28. if ($v->type == 0) {
  29. $v->url = $v->url ? '/pages/goodsDetail/index?goods_id=' . $v->url : '';
  30. }
  31. }
  32. # 公告
  33. $notice = Notice::where('agent_id', $this->agent_id)->limit(10)->get(['id', 'title', 'updated_at']);
  34. # 我的频道
  35. if ($this->user_id) {
  36. $channel_ids = UserChannel::where('user_id', $this->user_id)->value('channels');
  37. //如果不存在则存入初始数据
  38. if (!$channel_ids) {
  39. $channel_ids = Channel::where([
  40. ['agent_id', '=', $this->agent_id],
  41. ['pid', '<>', 0],
  42. ])
  43. ->orderBy('id')
  44. ->limit(8)
  45. ->pluck('id')
  46. ->toArray();
  47. //存入user_channel
  48. UserChannel::where('user_id', $this->user_id)
  49. ->insert([
  50. 'user_id' => $this->user_id,
  51. 'channels' => json_encode($channel_ids)
  52. ]);
  53. }
  54. $my_channels = Channel::where('agent_id', $this->agent_id)
  55. ->whereIn('id', $channel_ids)
  56. ->get(['id', 'name', 'icon']);
  57. } else {
  58. $my_channels = Channel::where('agent_id', $this->agent_id)
  59. ->where('pid', '<>', 0)
  60. ->orderBy('id')
  61. ->limit(8)
  62. ->get(['id', 'name', 'icon']);
  63. }
  64. if (!$my_channels->isEmpty()) {
  65. foreach ($my_channels as &$v) {
  66. $v->icon = $img_prefix . $v->icon;
  67. }
  68. }
  69. # 专题列表
  70. $special = Special::query()
  71. ->where('agent_id', $this->agent_id)
  72. ->orderBy('sort')->orderBy('id')
  73. ->limit(6)->get(['id', 'picture_ad']);
  74. if (!$special->isEmpty()) {
  75. foreach ($special as $k=>&$v) {
  76. $v->picture_ad = $img_prefix . $v->picture_ad;
  77. }
  78. }
  79. # 人气爆款
  80. $hots = AgentProduct::query()
  81. ->where('agent_id', $this->agent_id)
  82. ->select('id', 'sale', 'product_id', 'price', 'original_price') //必须查询product_id才能with
  83. ->with('product:id,title,pictures') //必须查询ID才能正常查询
  84. ->limit(6)->get();
  85. if (!$hots->isEmpty()) {
  86. foreach ($hots as &$v) {
  87. if (!empty($v->product->pictures) && is_array($v->product->pictures)) {
  88. $v->product->pictures = array_map(function($item) use ($img_prefix) {
  89. return strpos($item, $img_prefix) === false ? $img_prefix . $item : $item;
  90. }, $v->product->pictures);
  91. }
  92. }
  93. }
  94. return $this->success([
  95. 'slide' => $slide,
  96. 'notice' => $notice,
  97. 'my_channels' => $my_channels,
  98. 'special' => $special,
  99. 'hots' => $hots,
  100. ]);
  101. }
  102. }