'id DESC']); $list = list_tree($list, 'id', 'parent_id'); View::assign('list', $list); View::assign('types', ['','一般','目录','单页','锚点','链接']); return View::fetch(); } /** * 新增 or 修改 * @param int $id info id * @return mix */ public function save($id = 0) { if ($id != 0) { $data = CategoryModel::find($id); } else { $data = new CategoryModel(); $data->is_nav = 1; } View::assign('data', $data); $list = list_tree(CategoryModel::select()); View::assign('list', $list); return View::fetch(); } public function doSave($id) { if ($this->app->request->isPost()) { $params = $this->app->request->param(); if ($params['name'] == '') { $this->error("名称不能为空"); } try { $id = $params['id']; unset($params['id']); if ($id != 0) { CategoryModel::update($params, ['id' => $id]); } else { CategoryModel::create($params); } } catch (\Exception $e) { $msg = $e->getMessage(); $this->error("错误提示:" . $msg); } $this->success('操作成功', (string)url('index')); } } // 删除 public function delete($id) { if ($this->app->request->isPost()) { if (CategoryModel::where('parent_id', $id)->value('id')) { return ['code' => 0, 'msg' => '子栏目不为空, 若要删除请先清空子栏目']; } if (CategoryModel::destroy($id)) { return ['code' => 0,'msg'=>'删除成功']; } else { return ['code' => 1,'msg'=>'删除失败']; } } } // 导航状态变更 public function navStatus(int $id) { if ($this->app->request->isPost()) { return CategoryModel::navStaus($id); } } }