Notice.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\common\AdminController;
  4. use app\common\api\NoticeApi;
  5. use app\admin\validate\Notice as NoticeValidate;
  6. /**
  7. * Description of Notice
  8. *
  9. * @author sgq
  10. */
  11. class Notice extends AdminController {
  12. public function index() {
  13. return view();
  14. }
  15. public function list() {
  16. $res = NoticeApi::getList($this->request->param());
  17. return json($res);
  18. }
  19. /**
  20. * @auth {{/notice/add}}
  21. * @return type
  22. */
  23. public function add() {
  24. if ($this->request->isPost()) {
  25. try {
  26. $params = $this->request->param();
  27. validate(NoticeValidate::class)->check($params);
  28. if (NoticeApi::edit($params))
  29. return json(["msg" => "文章发布成功"]);
  30. throw new \think\exception("文章发布失败");
  31. } catch (\think\exception $e) {
  32. return json(["msg" => $e->getMessage()]);
  33. }
  34. }
  35. return view();
  36. }
  37. /**
  38. * @auth {{/notice/update}}
  39. * @return type
  40. */
  41. public function edit() {
  42. $params = $this->request->param();
  43. if ($this->request->isPost()) {
  44. try {
  45. validate(NoticeValidate::class)->check($params);
  46. if (NoticeApi::edit($params))
  47. return json(["msg" => "文章编辑成功"]);
  48. throw new \think\exception("文章编辑失败");
  49. } catch (\think\exception $e) {
  50. return json(["msg" => $e->getMessage()]);
  51. }
  52. }
  53. $row = NoticeApi::getOne($params["id"]);
  54. return view("", ["row" => $row]);
  55. }
  56. /**
  57. * @auth {{/notice/delete}}
  58. * @return type
  59. */
  60. public function delete() {
  61. if ($this->request->isPost()) {
  62. $params = $this->request->param();
  63. $id = $params["id"];
  64. if (NoticeApi::delete($id))
  65. return json(["msg" => "删除成功"]);
  66. return json(["msg" => "删除失败"]);
  67. }
  68. }
  69. /**
  70. * @auth {{/notice/delete}}
  71. * @return type
  72. */
  73. public function top() {
  74. if ($this->request->isPost()) {
  75. $params = $this->request->param();
  76. $id = $params["id"];
  77. $isTop = $params["isTop"] ?: 1;
  78. if ($isTop == 1) {
  79. if (NoticeApi::setTop($id, 1))
  80. return json(["msg" => "置顶成功"]);
  81. return json(["msg" => "置顶失败"]);
  82. } else {
  83. if (NoticeApi::setTop($id, 2))
  84. return json(["msg" => "取消置顶成功"]);
  85. return json(["msg" => "取消置顶失败"]);
  86. }
  87. }
  88. }
  89. }