ProductCategory.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use app\common\model\ProductCategoryModel;
  5. use think\App;
  6. use think\facade\View;
  7. use think\Request;
  8. class ProductCategory extends BaseController
  9. {
  10. private $categoryModel;
  11. public function __construct(App $app)
  12. {
  13. parent::__construct($app);
  14. $this->categoryModel = new ProductCategoryModel();
  15. }
  16. /**
  17. * @param Request $request
  18. * @return \think\response\View
  19. * @throws \think\db\exception\DbException
  20. */
  21. public function index(Request $request) {
  22. $all_category = $this->categoryModel->findAll();
  23. View::assign([
  24. 'tree' => recursion($all_category,0),
  25. ]);
  26. return view();
  27. }
  28. /**
  29. * @param Request $request
  30. * @return \think\response\Json|\think\response\View
  31. */
  32. public function add(Request $request) {
  33. if($request->isAjax()) {
  34. $params = $request->param();
  35. $isExist = $this->categoryModel->doesItExist($params['name']);
  36. if ($isExist)
  37. return $this->fail(lang("Fail to add. Data duplication"));
  38. if($params['pid'] > 0) {
  39. $parent = $this->categoryModel->findById($params['pid']);
  40. if(!$parent)
  41. return $this->fail(lang("The parent template does not exist"));
  42. }
  43. $category = $this->categoryModel->save($params);
  44. return $this->ok($category);
  45. }
  46. $all_category = $this->categoryModel->findAll();
  47. View::assign('all_category', recursion($all_category,0));
  48. return view();
  49. }
  50. /**
  51. * @param Request $request
  52. * @return \think\response\Json|\think\response\View
  53. * @throws \think\db\exception\DataNotFoundException
  54. * @throws \think\db\exception\DbException
  55. * @throws \think\db\exception\ModelNotFoundException
  56. */
  57. public function edit(Request $request) {
  58. $params = $request->param();
  59. if(!isset($params['id']))
  60. return $this->fail(lang('ID not exist'));
  61. $category = $this->categoryModel->findById($params['id']);
  62. if(!$category)
  63. return $this->fail(lang('"The customer doesn t exist"'));
  64. if ($request->isAjax()) {
  65. if($params['pid'] > 0) {
  66. $parent = $this->categoryModel->findById($params['pid']);
  67. if(!$parent)
  68. return $this->fail(lang("The parent template does not exist"));
  69. }
  70. $this->categoryModel->where('id',$params['id'])->update($params);
  71. return $this->ok(true);
  72. }
  73. $all_category = $this->categoryModel->findAll();
  74. View::assign([
  75. 'category' => $category,
  76. 'all_category' => recursion($all_category, 0),
  77. 'update_time' => time()
  78. ]);
  79. return view();
  80. }
  81. public function delete(Request $request) {
  82. $params = $request->param();
  83. if(!isset($params['ids']))
  84. return $this->fail(lang("Please select the data you want to delete"));
  85. $this->categoryModel->deleteByIds(explode(',',$params['ids']));
  86. return $this->ok();
  87. }
  88. }