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

66 lines
1.7 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\AgentProduct;
  5. use App\Models\Channel;
  6. use App\Models\UserChannel;
  7. use Illuminate\Support\Facades\Storage;
  8. /**
  9. * 频道列表
  10. * Class Channel
  11. * @package App\Http\Controllers\Api
  12. */
  13. class ChannelController extends Controller
  14. {
  15. // 所有频道列表
  16. public function index()
  17. {
  18. $list = Channel::query()
  19. ->where('agent_id', $this->agent_id)
  20. ->orderBy('sort')
  21. ->orderBy('id')
  22. ->get(['id', 'pid', 'name', 'icon']);
  23. $prefix = Storage::disk('public')->url('');
  24. foreach ($list as $k => &$v) {
  25. $v->icon = $prefix . $v->icon;
  26. }
  27. return $this->success($list);
  28. }
  29. // 我的频道
  30. public function my()
  31. {
  32. $channel_ids = UserChannel::where('user_id', $this->user_id)->value('channels');
  33. $list = Channel::whereIn('id', $channel_ids)->get(['id', 'pid', 'name', 'icon']);
  34. return $this->success($list);
  35. }
  36. //根据频道ID获取产品
  37. public function product()
  38. {
  39. $channel_id = (int)request()->input('channel_id');
  40. $list = AgentProduct::list($this->agent_id)
  41. ->whereRaw("FIND_IN_SET($channel_id, `channel_id`)")
  42. ->orderBy('id', 'DESC')
  43. ->simplePaginate();
  44. $list = $this->paginatePicAddHost($list);
  45. return $this->success($list);
  46. }
  47. //分页列表产品图片加域名
  48. private function paginatePicAddHost($list)
  49. {
  50. if (!$list->isEmpty()) {
  51. $prefix = Storage::disk('public')->url('');
  52. foreach ($list->items() as $k=>&$v) {
  53. $v->pictures = array_map(function($item) use ($prefix) {
  54. return strpos($item, $prefix) === false ? $prefix . $item : $item;
  55. }, $v->pictures);
  56. }
  57. }
  58. return $list;
  59. }
  60. }