| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- namespace app\admin\controller;
- use app\BaseController;
- use app\common\model\ProductCategoryModel;
- use think\App;
- use think\facade\View;
- use think\Request;
- class ProductCategory extends BaseController
- {
- private $categoryModel;
- public function __construct(App $app)
- {
- parent::__construct($app);
- $this->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();
- }
- }
|