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

72 lines
1.7 KiB

  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\AgentProduct;
  5. use App\Models\Article;
  6. use Illuminate\Support\Facades\Storage;
  7. /**
  8. * 文章
  9. * Class ArticleController
  10. * @package App\Http\Controllers\Api
  11. */
  12. class ArticleController extends Controller
  13. {
  14. //文章列表
  15. public function index()
  16. {
  17. $list = Article::where('agent_id', $this->agent_id)
  18. ->select('id', 'image', 'title', 'type', 'updated_at')
  19. ->orderBy('sort')->orderBy('id', 'DESC')
  20. ->simplePaginate()->toArray();
  21. $new_data = [];
  22. $prefix = Storage::disk('public')->url('');
  23. foreach ($list['data'] as $k => &$item) {
  24. $item['image'] = $prefix . $item['image'];
  25. //type=1是大图
  26. if ($item['type'] == 1) {
  27. $new_data[] = ['big' => $item];
  28. continue;
  29. }
  30. //普通列表
  31. $count = count($new_data);
  32. if ($count) {
  33. $new_data[$count - 1]['children'][] = $item;
  34. } else {
  35. $new_data[] = ['children' => [$item]];
  36. }
  37. }
  38. $list['data'] = $new_data;
  39. return $this->success($list);
  40. }
  41. //文章详情
  42. public function show()
  43. {
  44. $id = (int)request()->input('id');
  45. $article = Article::where('agent_id', $this->agent_id)->find($id);
  46. if (!$article) {
  47. return $this->error('文章不存在或已被删除');
  48. }
  49. $article->image = Storage::disk('public')->url('') . $article->image;
  50. if ($article->agent_product_ids) {
  51. $product = AgentProduct::list($this->agent_id)
  52. ->whereIn('id', $article->agent_product_ids)
  53. ->get();
  54. if (!$product->isEmpty()) {
  55. $prefix = Storage::disk('public')->url('');
  56. foreach ($product as &$v) {
  57. $v->pictures = array_map(fn($item) => $prefix . $item, $v->pictures);
  58. }
  59. }
  60. }
  61. $article->product = $product ?? [];
  62. return $this->success($article);
  63. }
  64. }