request->param('page', 1); $size = $this->request->param('size', 15); if ($this->request->isAjax()) { $data = ArticleCategory::paginate([ 'page' => $page, 'list_rows' => $size, ]); $this->result->success($data->items()); } $this->view->assign('title', '登录'); return $this->view->fetch('category/index'); } public function save() { if (Request::isPost()) { $data = Request::post(); $data['status'] = isset($data['status']) ? 1 : 0; $validate = new Validate([ 'name|名称' => 'require|max:50', 'alias|别名' => 'require|max:50', ]); if (!$validate->check($data)) { $this->result->error($validate->getError()); } ArticleCategory::create($data); $this->result->success(); } } public function edit($id = null) { $category = ArticleCategory::find($id); if (!$category) { return redirect('/articles/backend/category'); } return View::fetch('category/edit', ['category' => $category]); } public function update($id = 0) { $data = Request::post(); $id = $id ?: (int) ($data['id'] ?? 0); $data['update_at'] = time(); $data['status'] = isset($data['status']) ? 1 : 0; $validate = new Validate([ 'name|名称' => 'require|max:50', 'alias|别名' => 'require|max:50', ]); if (!$validate->check($data)) { return json(['code' => 0, 'msg' => $validate->getError()]); } if ($id) { $category = ArticleCategory::find($id); if (!$category) { return json(['code' => 0, 'msg' => '分类不存在']); } $category->save($data); } else { $data['create_at'] = time(); ArticleCategory::create($data); } return json(['code' => 0, 'msg' => '保存成功', 'url' => '/articles/backend/category']); } public function delete($id = 0) { $id = $id ?: (int) Request::post('id', 0); $category = ArticleCategory::find($id); if ($category) { if (ArticleCategory::hasArticles($id)) { return json(['code' => 0, 'msg' => '该分类下还有文章,无法删除']); } $category->delete(); return json(['code' => 0, 'msg' => '已删除']); } return json(['code' => 0, 'msg' => '分类不存在']); } }