From 706e51bee1a5ce7f417d78eb079ea084795d48c8 Mon Sep 17 00:00:00 2001 From: karl Date: Mon, 29 Jul 2024 10:19:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=AC=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AnnouncementsController.php | 111 ++++++++++++++++++ service/app/model/Announcements.php | 11 ++ 2 files changed, 122 insertions(+) create mode 100644 service/app/admin/controller/AnnouncementsController.php create mode 100644 service/app/model/Announcements.php diff --git a/service/app/admin/controller/AnnouncementsController.php b/service/app/admin/controller/AnnouncementsController.php new file mode 100644 index 00000000..13105163 --- /dev/null +++ b/service/app/admin/controller/AnnouncementsController.php @@ -0,0 +1,111 @@ +admin->is_super) return $this->error(2000, '管理员才能查看'); + + $list = Announcements::with('adminInfo')->order('id desc')->paginate($request->get('limit',10)); + + return $this->success($list); + } + + /** + * 添加 + * @param Request $request + * @return \support\Response + */ + public function add(Request $request) + { + $title = $request->post('title'); + $content = $request->post('content'); + + if (empty($title) || empty($content)) { + return $this->error(2001, '请填写完整参数'); + } + + $annModel = new Announcements(); + $annModel->title = $title; + $annModel->content = $content; + $annModel->admin_id = $request->admin->id; + + if ($annModel->save()) { + return $this->success(null, '添加成功'); + } + + return $this->error(2000, '添加失败'); + } + + /** + * 获取详情 + * @param Request $request + * @return \support\Response + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function detail(Request $request) + { + $id = $request->post('id'); + + $data = Announcements::find(['id' => $id]); + + return $this->success($data); + } + + /** + * 编辑 + * @param Request $request + * @return \support\Response + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function edit(Request $request) + { + $title = $request->post('title'); + $content = $request->post('content'); + $id = $request->post('id'); + + if (empty($title) || empty($content)) { + return $this->error(2001, '请填写完整参数'); + } + + $annModel = Announcements::find(['id' => $id]); + $annModel->title = $title; + $annModel->content = $content; + $annModel->admin_id = $request->admin->id; + + if ($annModel->save()) { + return $this->success(null, '修改成功'); + } + + return $this->error(2000, '修改失败'); + } + + /** + * 拉取最新的 + * @return \support\Response + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function getLast() + { + $data = Announcements::order('id desc')->find(); + + return $this->success($data); + } +} \ No newline at end of file diff --git a/service/app/model/Announcements.php b/service/app/model/Announcements.php new file mode 100644 index 00000000..b7123bb4 --- /dev/null +++ b/service/app/model/Announcements.php @@ -0,0 +1,11 @@ +belongsTo(Admins::class, 'admin_id')->visible(['name','username','avatar']); + } +} \ No newline at end of file