ArticleController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 Exception;
  7. use Illuminate\Http\UploadedFile;
  8. use Log;
  9. use Str;
  10. class ArticleController extends Controller
  11. {
  12. // 文章列表
  13. public function index()
  14. {
  15. return view('admin.article.index', ['articles' => Article::orderByDesc('sort')->paginate(15)->appends(request('page'))]);
  16. }
  17. // 添加文章页面
  18. public function create()
  19. {
  20. return view('admin.article.create');
  21. }
  22. // 添加文章
  23. public function store(ArticleRequest $request)
  24. {
  25. $data = $request->validated();
  26. // LOGO
  27. if ($data['type'] !== '4' && $request->hasFile('logo')) {
  28. $path = $this->fileUpload($request->file('logo'));
  29. if (is_string($path)) {
  30. $data['logo'] = $path;
  31. } else {
  32. return $path;
  33. }
  34. }
  35. if ($article = Article::create($data)) {
  36. return redirect(route('admin.article.edit', $article))->with('successMsg', '添加成功');
  37. }
  38. return redirect()->back()->withInput()->withErrors('添加失败');
  39. }
  40. // 图片上传
  41. public function fileUpload(UploadedFile $file)
  42. {
  43. $fileName = Str::random(8).time().'.'.$file->getClientOriginalExtension();
  44. if (! $file->storeAs('public', $fileName)) {
  45. return redirect()->back()->withInput()->withErrors('Logo存储失败');
  46. }
  47. return 'upload/'.$fileName;
  48. }
  49. // 文章页面
  50. public function show(Article $article)
  51. {
  52. return view('admin.article.show', compact('article'));
  53. }
  54. // 编辑文章页面
  55. public function edit(Article $article)
  56. {
  57. return view('admin.article.edit', compact('article'));
  58. }
  59. // 编辑文章
  60. public function update(ArticleRequest $request, Article $article)
  61. {
  62. $data = $request->validated();
  63. $data['logo'] = $data['logo'] ?? null;
  64. // LOGO
  65. if ($data['type'] !== '4' && $request->hasFile('logo')) {
  66. $path = $this->fileUpload($request->file('logo'));
  67. if (is_string($path)) {
  68. $data['logo'] = $path;
  69. } else {
  70. return $path;
  71. }
  72. }
  73. if ($article->update($data)) {
  74. return redirect()->back()->with('successMsg', '编辑成功');
  75. }
  76. return redirect()->back()->withInput()->withErrors('编辑失败');
  77. }
  78. // 删除文章
  79. public function destroy(Article $article)
  80. {
  81. try {
  82. $article->delete();
  83. } catch (Exception $e) {
  84. Log::error('删除文章失败:'.$e->getMessage());
  85. return response()->json(['status' => 'fail', 'message' => '删除失败:'.$e->getMessage()]);
  86. }
  87. return response()->json(['status' => 'success', 'message' => '删除成功']);
  88. }
  89. }