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

56 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. //TODO 此处仅为演示数据,调用数据需要再修改
  17. $list = Article::query()
  18. ->where('agent_id', $this->agent_id)
  19. ->select('id', 'image', 'title', 'updated_at')
  20. ->orderBy('id', 'DESC')
  21. ->simplePaginate()->toArray();
  22. $new_data = [];
  23. $prefix = Storage::disk('public')->url('');
  24. foreach ($list['data'] as $k => &$item) {
  25. $item['image'] = $prefix . $item['image'];
  26. $new_key = floor($k / 4);
  27. if ($k % 4 == 0) {
  28. $new_data[$new_key] = [
  29. 'big' => $item,
  30. 'children' => []
  31. ];
  32. } else {
  33. $new_data[$new_key]['children'][] = $item;
  34. }
  35. }
  36. $list['data'] = $new_data;
  37. return $this->success($list);
  38. }
  39. //文章详情
  40. public function show()
  41. {
  42. $id = (int)request()->input('id');
  43. $article = Article::where('agent_id', $this->agent_id)->find($id);
  44. if (!$article) {
  45. return $this->error('文章不存在或已被删除');
  46. }
  47. $article->image = Storage::disk('public')->url('') . $article->image;
  48. return $this->success($article);
  49. }
  50. }