Productcategory.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace app\admin\controller\wwh;
  3. use app\common\controller\Backend;
  4. use fast\Tree;
  5. /**
  6. * 产品分类
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Productcategory extends Backend
  11. {
  12. /**
  13. * WwhProductcategory模型对象
  14. * @var \app\admin\model\wwh\Productcategory
  15. */
  16. protected $noNeedRight = ['getjsTree', 'category'];
  17. protected $model = null;
  18. protected $categoryList = [];
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = model('\app\admin\model\wwh\Productcategory');
  23. $productCategoryList = collection($this->model->select())->toArray();
  24. Tree::instance()->init($productCategoryList);
  25. $productCategory = [];
  26. $this->categoryList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
  27. $primary = array(array('name' => '无', 'id' => '0'));
  28. $result = array_merge_recursive($primary, $this->categoryList);
  29. foreach ($result as $k => $v) {
  30. $productCategory[$v['id']] = $v['name'];
  31. }
  32. $this->view->assign("productCategory", $productCategory);
  33. }
  34. /**
  35. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  36. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  37. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  38. */
  39. /**
  40. * 查看
  41. */
  42. public function index()
  43. {
  44. if ($this->request->isAjax()) {
  45. $list = $this->categoryList;
  46. $total = count($this->categoryList);
  47. $result = array("total" => $total, "rows" => $list);
  48. return json($result);
  49. }
  50. return $this->view->fetch();
  51. }
  52. /**
  53. * 添加
  54. */
  55. public function add()
  56. {
  57. if ($this->request->isPost()) {
  58. $params = $this->request->post("row/a");
  59. if ($params) {
  60. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  61. $params[$this->dataLimitField] = $this->auth->id;
  62. }
  63. try {
  64. //是否采用模型验证
  65. if ($this->modelValidate) {
  66. $name = basename(str_replace('\\', '/', get_class($this->model)));
  67. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  68. $this->model->validate($validate);
  69. }
  70. $result = $this->model->allowField(true)->save($params);
  71. if ($result !== false) {
  72. $this->success();
  73. } else {
  74. $this->error($this->model->getError());
  75. }
  76. } catch (\think\exception\PDOException $e) {
  77. $this->error($e->getMessage());
  78. }
  79. }
  80. $this->error(__('Parameter %s can not be empty', ''));
  81. }
  82. return $this->view->fetch();
  83. }
  84. /**
  85. * 删除
  86. */
  87. public function del($ids = "")
  88. {
  89. if (!$this->request->isPost()) {
  90. $this->error(__("Invalid parameters"));
  91. }
  92. $ids = $ids ? $ids : $this->request->post("ids");
  93. if ($ids) {
  94. $pk = $this->model->getPk();
  95. $wwh = model('\app\admin\model\wwh\Productcategory')->where('pid', 'in', $ids)->select();
  96. if ($wwh) {
  97. $this->error('存在下级分类,无法删除!');
  98. }
  99. $res = model('\app\admin\model\wwh\Productcategory')->alias('a')->join('wwh_product w', 'a.id = w.productcategoryid')->where('a.id', 'in', $ids)->select();
  100. if ($res) {
  101. $this->error('分类存在产品,无法删除!');
  102. }
  103. $count = model('\app\admin\model\wwh\Productcategory')->where($pk, 'in', $ids)->delete();
  104. if ($count) {
  105. $this->success();
  106. } else {
  107. $this->error(__('No rows were deleted'));
  108. }
  109. }
  110. }
  111. /**
  112. * 获取产品类别树
  113. */
  114. public function getjsTree()
  115. {
  116. $categoryList = collection($this->model->select())->toArray();
  117. $result = [];
  118. foreach ($categoryList as $k => $v) {
  119. $n = [];
  120. $n['id'] = $v['id'];
  121. $n['parent'] = $v['pid'] == 0 ? "#" : $v['pid'];
  122. $n['text'] = $v['name'];
  123. $n['type'] = $v['id'];
  124. $n['data'] = $v;
  125. $n['state'] = ["opened" => true, "disabled" => false];
  126. $result[]=$n;
  127. }
  128. return json($result);
  129. }
  130. /**
  131. * 获取产品类别名称
  132. */
  133. public function category()
  134. {
  135. $result = collection($this->model->select())->toArray();
  136. return json($result);
  137. }
  138. }