From bb08dee91d4665ca8e736355e7e69d9db387d416 Mon Sep 17 00:00:00 2001 From: augushong Date: Tue, 28 Apr 2026 21:01:27 +0800 Subject: [PATCH] =?UTF-8?q?feat(api):=20=E6=96=B0=E5=A2=9E=E5=88=86?= =?UTF-8?q?=E7=B1=BB=E6=9F=A5=E8=AF=A2=E6=8E=A5=E5=8F=A3=EF=BC=88=E5=88=97?= =?UTF-8?q?=E8=A1=A8+=E8=AF=A6=E6=83=85=EF=BC=8C=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=A0=91=E5=BD=A2=E7=BB=93=E6=9E=84=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AGENTS.md | 4 +- app/api/controller/Categories.php | 91 +++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 app/api/controller/Categories.php diff --git a/AGENTS.md b/AGENTS.md index c5ad204..38f41d6 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -15,7 +15,7 @@ ulthon_information/ ├── app/ # 应用核心(多应用模式) │ ├── admin/controller/ # 后台管理(15个控制器) │ ├── index/controller/ # 前台展示(5个控制器) -│ ├── api/controller/ # API接口(微信/文件/验证码) +│ ├── api/controller/ # API接口(文章/附件/分类/文件/验证码/密钥) │ ├── model/ # 全局模型(17个,非各应用内) │ ├── common/ # 跨应用共享层(tools/traits/model) │ └── middleware/ # 中间件(ConfigInit/AdminLog/PermissionRecord) @@ -56,7 +56,7 @@ ulthon_information/ app\BaseController (abstract: validate/success/error/redirect) +-- admin\controller\Common (Session鉴权) -> 15个子控制器 +-- index\controller\BaseController (域名跳转/HTTPS) -> Common -> 3个子控制器 - +-- api\controller\* (直接继承 BaseController) -> 3个控制器 + +-- api\controller\* (直接继承 BaseController, ApiKeyAuth中间件) -> 7个控制器(含分类查询) ``` ### 响应格式 diff --git a/app/api/controller/Categories.php b/app/api/controller/Categories.php new file mode 100644 index 0000000..90ea8dd --- /dev/null +++ b/app/api/controller/Categories.php @@ -0,0 +1,91 @@ +request->param('page', 1, 'intval'); + $limit = $this->request->param('limit', 15, 'intval'); + $type = $this->request->param('type', ''); + $pid = $this->request->param('pid', '', 'intval'); + $tree = $this->request->param('tree', 0, 'intval'); + + $status_param = $this->request->param('status'); + $status = is_null($status_param) ? 1 : $status_param; + + if ($limit < 1) { + $limit = 1; + } + if ($limit > 100) { + $limit = 100; + } + + $query = Category::where('delete_time', 0); + + if ($status !== '') { + $query->where('status', $status); + } + + if (!empty($type)) { + $query->where('type', $type); + } + + if ($pid !== '') { + $query->where('pid', $pid); + } + + $query->order('sort asc'); + + if ($tree) { + $list = $query->select()->toArray(); + $treeList = array2level($list, 0, 0); + + return json_message(['list' => $treeList]); + } + + $list = $query->paginate([ + 'page' => $page, + 'list_rows' => $limit, + ]); + + return json_message([ + 'list' => $list->items(), + 'total' => $list->total(), + 'page' => $page, + ]); + } + + /** + * 分类详情 + */ + public function read() + { + $id = $this->request->param('id', 0, 'intval'); + if ($id <= 0) { + return json_message('参数错误'); + } + + $category = Category::where('delete_time', 0) + ->where('id', $id) + ->find(); + + if (empty($category)) { + return json_message('分类不存在'); + } + + $category->append(['model_parent', 'model_siblings']); + + return json_message($category); + } +}