categoryModel = new ProductCategoryModel(); } /** * @param Request $request * @return \think\response\View * @throws \think\db\exception\DbException */ public function index(Request $request) { $all_category = $this->categoryModel->findAll(); View::assign([ 'tree' => recursion($all_category,0), ]); return view(); } /** * @param Request $request * @return \think\response\Json|\think\response\View */ public function add(Request $request) { if($request->isAjax()) { $params = $request->param(); $isExist = $this->categoryModel->doesItExist($params['name']); if ($isExist) return $this->fail(lang("Fail to add. Data duplication")); if($params['pid'] > 0) { $parent = $this->categoryModel->findById($params['pid']); if(!$parent) return $this->fail(lang("The parent template does not exist")); } $category = $this->categoryModel->save($params); return $this->ok($category); } $all_category = $this->categoryModel->findAll(); View::assign('all_category', recursion($all_category,0)); return view(); } /** * @param Request $request * @return \think\response\Json|\think\response\View * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function edit(Request $request) { $params = $request->param(); if(!isset($params['id'])) return $this->fail(lang('ID not exist')); $category = $this->categoryModel->findById($params['id']); if(!$category) return $this->fail(lang('"The customer doesn t exist"')); if ($request->isAjax()) { if($params['pid'] > 0) { $parent = $this->categoryModel->findById($params['pid']); if(!$parent) return $this->fail(lang("The parent template does not exist")); } $this->categoryModel->where('id',$params['id'])->update($params); return $this->ok(true); } $all_category = $this->categoryModel->findAll(); View::assign([ 'category' => $category, 'all_category' => recursion($all_category, 0), 'update_time' => time() ]); return view(); } public function delete(Request $request) { $params = $request->param(); if(!isset($params['ids'])) return $this->fail(lang("Please select the data you want to delete")); $this->categoryModel->deleteByIds(explode(',',$params['ids'])); return $this->ok(); } }