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

58 lines
1.3 KiB

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