ArticleController.php 4.0 KB

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