CategoryController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace backend\controllers;
  3. use common\services\xhGoodsCategoryService;
  4. use common\models\xhGoodsCategory;
  5. use common\models\xhGoods;
  6. use common\models\xhCategory;
  7. use common\services\xhCategoryService;
  8. use Yii;
  9. use yii\web\Controller;
  10. use yii\filters\AccessControl;
  11. use yii\data\Pagination;
  12. use yii\helpers\Json;
  13. use common\components\configDict;
  14. use common\components\util;
  15. class CategoryController extends BackendController
  16. {
  17. /**
  18. * 分类列表
  19. * @return string
  20. */
  21. public function actionList()
  22. {
  23. $list = xhCategoryService::getAll($this->merchantId);
  24. $categoryClassifyName = configDict::getConfig('categoryClassifyName');//商品、文章、图片等分类
  25. $categoryClassify = configDict::getConfig('categoryClassify');
  26. return $this->render('list',['list' => $list,'categoryClassifyName' => $categoryClassifyName,'categoryClassify'=>$categoryClassify]);
  27. }
  28. public function actionGoodsList()
  29. {
  30. $get = Yii::$app->request->get();
  31. $id = isset($get['id']) ? $get['id'] : 0;
  32. $category = xhCategoryService::getList($this->merchantId);
  33. if(!in_array($id,array_keys($category))){
  34. util::stop('访问出错了');
  35. }
  36. $categoryName = $category[$id];
  37. $list = xhGoodsCategoryService::getGoodsListByCId($id);
  38. return $this->render('goodsList',['list' => $list,'categoryName' => $categoryName]);
  39. }
  40. /**
  41. * 添加分类
  42. */
  43. public function actionAdd()
  44. {
  45. $post = Yii::$app->request->post();
  46. $parentId = isset($post['parentId']) ? $post['parentId'] : 0;
  47. xhCategoryService::addCategory($post,$parentId,$this->merchantId);
  48. echo Json::encode(['code' => 'A0001','msg' => '添加成功','data' =>[]]);
  49. }
  50. /**
  51. * 修改分类
  52. */
  53. public function actionModify()
  54. {
  55. $post = Yii::$app->request->post();
  56. $id = $post['id'];
  57. $list = xhCategoryService::getList($this->merchantId);
  58. if(!in_array($id,array_keys($list))){
  59. util::failInfo('出错了,请联系网站管理员');
  60. }
  61. unset($post['_csrf']);
  62. unset($post['id']);
  63. xhCategoryService::updateById($id,$post,$this->merchantId);
  64. echo Json::encode(['code' => 'A0001','msg' => '修改成功','data' =>[]]);
  65. }
  66. /**
  67. * 删除分类
  68. */
  69. public function actionDel()
  70. {
  71. $get = Yii::$app->request->get();
  72. $id = isset($get['id']) ? $get['id'] : 0;
  73. $category = xhCategoryService::getList($this->merchantId);
  74. if(!in_array($id,array_keys($category))){
  75. util::failInfo('访问出错了');
  76. }
  77. xhCategoryService::getByCateogryId($id, $this->merchantId);
  78. $parent = xhCategory::getByCondition(['parentId' => $id]);
  79. if(!empty($parent)){
  80. util::failInfo('还有子分类,不能删除');
  81. }
  82. $goods = xhGoodsCategory::getAllByCondition(['cId' => $id]);
  83. if(!empty($goods)){
  84. util::failInfo("此分类下还有商品<br />确认删除请10秒内再次提交");
  85. }
  86. xhCategoryService::deleteById($id,$this->merchantId);
  87. util::successInfo('删除成功');
  88. }
  89. /**
  90. * 分类上移
  91. */
  92. public function actionMoveUp()
  93. {
  94. $get = Yii::$app->request->get();
  95. $id = $get['id'];
  96. $merchantId = $this->merchantId;
  97. $c = xhCategory::getById($id,$merchantId);
  98. $parentId = $c['parentId'];
  99. $currentInTurn = $c['inTurn'];
  100. $list = xhCategory::getAllByCondition(['parentId' => $parentId,'merchantId' => $merchantId],'inTurn asc');
  101. if(count($list) <= 1){
  102. $this->redirect(Yii::$app->request->getReferrer());
  103. Yii::$app->end();
  104. }
  105. $modify = [];
  106. foreach($list as $val){
  107. $modify[] = $val['id'];
  108. $inTurn[] = $val['inTurn'];//新排序
  109. }
  110. $pos = array_search($currentInTurn,$inTurn);//根据值返回键,新排序中位置
  111. if($pos <= 0){//已经是第一位了
  112. $this->redirect(Yii::$app->request->getReferrer());
  113. Yii::$app->end();
  114. }
  115. $toUpCategoryId = $modify[$pos];
  116. $modify[$pos] = $modify[$pos - 1];
  117. $modify[$pos - 1] = $toUpCategoryId;
  118. foreach($modify as $key => $val){
  119. xhCategoryService::updateById($val, ['inTurn' => $key],$this->merchantId);
  120. }
  121. $this->redirect(Yii::$app->request->getReferrer());
  122. }
  123. /**
  124. * 分类下移
  125. */
  126. public function actionMoveDown()
  127. {
  128. $get = Yii::$app->request->get();
  129. $id = $get['id'];
  130. $merchantId = $this->merchantId;
  131. $c = xhCategory::getById($id,$merchantId);
  132. $parentId = $c['parentId'];
  133. $currentInTurn = $c['inTurn'];
  134. $list = xhCategory::getAllByCondition(['parentId' => $parentId,'merchantId' => $merchantId],'inTurn asc');
  135. $count = count($list);
  136. if($count <= 1){
  137. $this->redirect(Yii::$app->request->getReferrer());
  138. Yii::$app->end();
  139. }
  140. $modify = [];
  141. foreach($list as $val){
  142. $modify[] = $val['id'];
  143. $inTurn[] = $val['inTurn'];//新排序
  144. }
  145. $pos = array_search($currentInTurn,$inTurn);//根据值返回键,新排序中位置
  146. if($pos >= ($count - 1)){//已经是最后位了
  147. $this->redirect(Yii::$app->request->getReferrer());
  148. Yii::$app->end();
  149. }
  150. $toDownCategoryId = $modify[$pos];
  151. $modify[$pos] = $modify[$pos + 1];
  152. $modify[$pos + 1] = $toDownCategoryId;
  153. foreach($modify as $key => $val){
  154. xhCategoryService::updateById($val, ['inTurn' => $key],$this->merchantId);
  155. }
  156. $this->redirect(Yii::$app->request->getReferrer());
  157. }
  158. }