NoticeController.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. ]);
  23. if (!$request->input('id')) {
  24. if (!Notice::create($data)) {
  25. abort(500, '保存失败');
  26. }
  27. } else {
  28. try {
  29. Notice::find($request->input('id'))->update($data);
  30. } catch (\Exception $e) {
  31. abort(500, '保存失败');
  32. }
  33. }
  34. return response([
  35. 'data' => true
  36. ]);
  37. }
  38. public function show(Request $request)
  39. {
  40. if (empty($request->input('id'))) {
  41. abort(500, '参数有误');
  42. }
  43. $notice = Notice::find($request->input('id'));
  44. if (!$notice) {
  45. abort(500, '公告不存在');
  46. }
  47. $notice->show = $notice->show ? 0 : 1;
  48. if (!$notice->save()) {
  49. abort(500, '保存失败');
  50. }
  51. return response([
  52. 'data' => true
  53. ]);
  54. }
  55. public function drop(Request $request)
  56. {
  57. if (empty($request->input('id'))) {
  58. abort(500, '参数错误');
  59. }
  60. $notice = Notice::find($request->input('id'));
  61. if (!$notice) {
  62. abort(500, '公告不存在');
  63. }
  64. if (!$notice->delete()) {
  65. abort(500, '删除失败');
  66. }
  67. return response([
  68. 'data' => true
  69. ]);
  70. }
  71. }