From 9a4e1dcf48075c4df702b5f9f48292a0f20c20a3 Mon Sep 17 00:00:00 2001 From: ctexthuang Date: Thu, 27 Mar 2025 15:08:44 +0800 Subject: [PATCH] feat : banner --- app/Controller/Admin/BannerController.php | 66 +++++++++++++ app/Model/Banner.php | 41 ++++++++ app/Service/Admin/BannerService.php | 114 ++++++++++++++++++++++ 3 files changed, 221 insertions(+) create mode 100644 app/Controller/Admin/BannerController.php create mode 100644 app/Model/Banner.php create mode 100644 app/Service/Admin/BannerService.php diff --git a/app/Controller/Admin/BannerController.php b/app/Controller/Admin/BannerController.php new file mode 100644 index 0000000..be6f37b --- /dev/null +++ b/app/Controller/Admin/BannerController.php @@ -0,0 +1,66 @@ +add(); + } + + /** + * banner + * @return array + */ + #[RequestMapping(path: "edit", methods: "POST")] + #[Scene(scene: "edit")] + public function edit() + { + return (new BannerService)->edit(); + } + + /** + * banner + * @return array + */ + #[RequestMapping(path: "list", methods: "GET")] + #[Scene(scene: "list")] + public function list() + { + return (new BannerService)->handle(); + } + + /** + * banner + * @return array + * @throws Exception + */ + #[RequestMapping(path: "del", methods: "GET")] + #[Scene(scene: "del")] + public function del(): array + { + return (new BannerService)->del(); + } +} diff --git a/app/Model/Banner.php b/app/Model/Banner.php new file mode 100644 index 0000000..063b28a --- /dev/null +++ b/app/Model/Banner.php @@ -0,0 +1,41 @@ + 'integer', 'type' => 'integer','city_id' => 'integer','image_id' => 'integer','status' => 'integer','sort' => 'integer']; + + const string CREATED_AT = 'create_time'; + const string UPDATED_AT = 'update_time'; +} diff --git a/app/Service/Admin/BannerService.php b/app/Service/Admin/BannerService.php new file mode 100644 index 0000000..e71b0ef --- /dev/null +++ b/app/Service/Admin/BannerService.php @@ -0,0 +1,114 @@ +request->input('limit', 10); + + $cityId = (int)$this->request->input('city_id'); + + $list = $this->bannerModel + ->where('city_id',$cityId) + ->paginate($limit) + ->toArray(); + + + if (empty($list['data'])) return $this->return->success('success',$list); + $imageIds = array_column($list['data'],'image_id'); + $imageArr = $this->getOssObjects($imageIds); + foreach ($list['data'] as &$v){ + $v['url'] = $imageArr[$v['image_id']]['url'] ?? ''; + } + + return $this->return->success('success',$list); + } + + /** + * @return array + */ + public function add(): array + { + $insertModel = new Banner(); + + $insertModel->city_id = $this->request->input('city_id'); + $insertModel->title = $this->request->input('title'); + $insertModel->image_id = $this->request->input('image_id'); + $insertModel->sort = $this->request->input('sort'); + $insertModel->status = $this->request->input('status'); + $insertModel->type = $this->request->input('type'); + $insertModel->value = $this->request->input('value'); + + if (!$insertModel->save()) throw new ErrException('添加错误'); + + return $this->return->success(); + } + + /** + * @var Banner + */ + #[Inject] + protected Banner $bannerModel; + + /** + * @return array + */ + public function edit(): array + { + $id = (int)$this->request->input('id'); + + $info = $this->bannerModel = Banner::find($id); + + if (empty($info)) throw new ErrException('无数据'); + + $info->title = $this->request->input('title'); + $info->image_id = $this->request->input('image_id'); + $info->sort = $this->request->input('sort'); + $info->status = $this->request->input('status'); + $info->type = $this->request->input('type'); + $info->value = $this->request->input('value'); + + if (!$info->save()) throw new ErrException('修改错误'); + + return $this->return->success(); + } + + /** + * @return array + * @throws Exception + */ + public function del(): array + { + $id = (int)$this->request->input('id'); + + $info = $this->bannerModel = Banner::find($id); + + if (empty($info)) throw new ErrException('无数据'); + + if (!$info->delete()) throw new ErrException('删除错误'); + + return $this->return->success(); + + } +} \ No newline at end of file