ArticleController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\Admin\ArticleRequest;
  5. use App\Models\Article;
  6. use App\Services\ArticleService;
  7. use Exception;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Http\UploadedFile;
  10. use Log;
  11. use Str;
  12. class ArticleController extends Controller
  13. {
  14. public function index(Request $request)
  15. { // 文章列表
  16. $categories = Article::whereNotNull('category')->distinct()->get('category');
  17. $articles = Article::query();
  18. foreach (['id', 'category', 'language', 'type'] as $field) {
  19. $request->whenFilled($field, function ($value) use ($articles, $field) {
  20. $articles->where($field, $value);
  21. });
  22. }
  23. $articles = $articles->latest()->orderByDesc('sort')->paginate()->appends($request->except('page'));
  24. return view('admin.article.index', compact('articles', 'categories'));
  25. }
  26. public function store(ArticleRequest $request)
  27. { // 添加文章
  28. $data = $request->validated();
  29. // LOGO
  30. try {
  31. if ($data['type'] !== '4' && $request->hasFile('logo')) {
  32. $path = $this->fileUpload($request->file('logo'));
  33. if (is_string($path)) {
  34. $data['logo'] = $path;
  35. } else {
  36. return $path;
  37. }
  38. }
  39. if ($article = Article::create($data)) {
  40. return redirect(route('admin.article.edit', $article))->with('successMsg', '添加成功');
  41. }
  42. } catch (Exception $e) {
  43. Log::error('添加文章错误:'.$e->getMessage());
  44. return redirect()->back()->withInput()->withErrors($e->getMessage());
  45. }
  46. return redirect()->back()->withInput()->withErrors('添加失败');
  47. }
  48. public function fileUpload(UploadedFile $file)
  49. { // 图片上传
  50. $fileName = Str::random(8).time().'.'.$file->getClientOriginalExtension();
  51. if (! $file->storeAs('public', $fileName)) {
  52. return redirect()->back()->withInput()->withErrors('Logo存储失败');
  53. }
  54. return 'upload/'.$fileName;
  55. }
  56. public function create()
  57. { // 添加文章页面
  58. $categories = Article::whereNotNull('category')->distinct()->get('category');
  59. return view('admin.article.info', compact('categories'));
  60. }
  61. public function show(Article $article)
  62. { // 文章页面
  63. $article->content = (new ArticleService($article))->getContent();
  64. return view('admin.article.show', compact('article'));
  65. }
  66. public function edit(Article $article)
  67. { // 编辑文章页面
  68. $categories = Article::whereNotNull('category')->distinct()->get('category');
  69. return view('admin.article.info', compact('article', 'categories'));
  70. }
  71. public function update(ArticleRequest $request, Article $article)
  72. { // 编辑文章
  73. $data = $request->validated();
  74. $data['logo'] = $data['logo'] ?? null;
  75. // LOGO
  76. if ($data['type'] !== '4' && $request->hasFile('logo')) {
  77. $path = $this->fileUpload($request->file('logo'));
  78. if (is_string($path)) {
  79. $data['logo'] = $path;
  80. } else {
  81. return $path;
  82. }
  83. }
  84. if ($article->update($data)) {
  85. return redirect()->back()->with('successMsg', '编辑成功');
  86. }
  87. return redirect()->back()->withInput()->withErrors('编辑失败');
  88. }
  89. public function destroy(Article $article)
  90. { // 删除文章
  91. try {
  92. $article->delete();
  93. } catch (Exception $e) {
  94. Log::error('删除文章失败:'.$e->getMessage());
  95. return response()->json(['status' => 'fail', 'message' => '删除失败:'.$e->getMessage()]);
  96. }
  97. return response()->json(['status' => 'success', 'message' => '删除成功']);
  98. }
  99. }