ArticleController.php 3.4 KB

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