NoticeController.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Requests\Admin\NoticeSave;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\Notice;
  7. use Illuminate\Support\Facades\Cache;
  8. class NoticeController extends Controller
  9. {
  10. public function fetch(Request $request)
  11. {
  12. return response([
  13. 'data' => Notice::orderBy('id', 'DESC')->get()
  14. ]);
  15. }
  16. public function save(NoticeSave $request)
  17. {
  18. $data = $request->only([
  19. 'title',
  20. 'content',
  21. 'img_url',
  22. 'tags'
  23. ]);
  24. if (!$request->input('id')) {
  25. if (!Notice::create($data)) {
  26. abort(500, '保存失败');
  27. }
  28. } else {
  29. try {
  30. Notice::find($request->input('id'))->update($data);
  31. } catch (\Exception $e) {
  32. abort(500, '保存失败');
  33. }
  34. }
  35. return response([
  36. 'data' => true
  37. ]);
  38. }
  39. public function show(Request $request)
  40. {
  41. if (empty($request->input('id'))) {
  42. abort(500, '参数有误');
  43. }
  44. $notice = Notice::find($request->input('id'));
  45. if (!$notice) {
  46. abort(500, '公告不存在');
  47. }
  48. $notice->show = $notice->show ? 0 : 1;
  49. if (!$notice->save()) {
  50. abort(500, '保存失败');
  51. }
  52. return response([
  53. 'data' => true
  54. ]);
  55. }
  56. public function drop(Request $request)
  57. {
  58. if (empty($request->input('id'))) {
  59. abort(500, '参数错误');
  60. }
  61. $notice = Notice::find($request->input('id'));
  62. if (!$notice) {
  63. abort(500, '公告不存在');
  64. }
  65. if (!$notice->delete()) {
  66. abort(500, '删除失败');
  67. }
  68. return response([
  69. 'data' => true
  70. ]);
  71. }
  72. }