request->post('name'); $where = []; if (!empty($name)) { $where[] = ['name', 'like', `%$name%`]; } $model = new ServerCategory(); try { $list = $model->getList($where); } catch (DataNotFoundException|ModelNotFoundException|DbException $e) { return $this->renderError($e->getMessage() ?: '接口异常'); } return $this->renderSuccess(compact('list')); } /** * @notes:添加分类 * @return Json * @author: wanghousheng */ public function addCategory(): Json { $data = $this->postForm(); if (!$data) { return $this->renderError('缺少必要参数'); } $model = new ServerCategory(); if ($model->add($data)) { return $this->renderSuccess('添加成功'); } return $this->renderError($model->getError() ?: '添加失败'); } /** * @notes:编辑分类 * @param int $categoryId * @return Json * @author: wanghousheng */ public function editCategory(int $categoryId) { $data = $this->postForm(); if (!$data) { return $this->renderError('缺少必要参数'); } $model = ServerCategory::detail($categoryId); if ($model->edit($data)) { return $this->renderSuccess('编辑成功'); } return $this->renderError($model->getError() ?: '编辑失败'); } public function deleteCategory(int $categoryId) { $model = ServerCategory::detail($categoryId); if ($model->remove()) { return $this->renderSuccess('删除成功'); } return $this->renderError('删除失败'); } }