CateController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace addons\Purchase\backend\controllers;
  3. use addons\Purchase\common\models\PurchaseCate;
  4. use addons\Purchase\common\models\PurchaseGoodsCateRelate;
  5. use common\enums\StatusEnum;
  6. use Yii;
  7. /**
  8. * 商品分类控制器
  9. *
  10. * Class DefaultController
  11. * @package addons\Purchase\backend\controllers
  12. */
  13. class CateController extends BaseController
  14. {
  15. public $enableCsrfValidation = false;
  16. /**
  17. * @notes:商品分类
  18. * @return mixed|string|null
  19. * @author: lun
  20. * @Time: 2022/10/22 11:14
  21. */
  22. public function actionIndex(){
  23. if (Yii::$app->request->isAjax) {
  24. $keyword = Yii::$app->request->get('keyword','');
  25. $where = [['mall_id' => $this->mall['id'],'parent_id' => 0,'status' => [StatusEnum::ENABLED,StatusEnum::DISABLED]]];
  26. $keyword && $where[] = ['like','name',$keyword.'%',false];
  27. $order = 'sort asc,created_at desc';
  28. $with = ['child','child.child'];
  29. $params = compact('where','order','with');
  30. $list = PurchaseCate::lists($params);
  31. if($list === false) return $this->error(PurchaseCate::getStaticError());
  32. return $this->success('ok',compact('list'));
  33. }
  34. return $this->render($this->action->id);
  35. }
  36. /**
  37. * 商品分类排序
  38. * @Author bing
  39. * @DateTime 2021-08-20 14:49:55
  40. * @copyright: Copyright (c) 2020 广东七件事集团
  41. * @return void
  42. */
  43. public function actionSortEdit(){
  44. if (Yii::$app->request->isAjax) {
  45. $post = Yii::$app->request->post();
  46. $res = PurchaseCate::sort($post);
  47. if($res === false) return $this->error(PurchaseCate::getStaticError());
  48. return $this->success();
  49. }
  50. }
  51. /**
  52. * 商品样式设置
  53. * @Author bing
  54. * @DateTime 2021-08-23 09:55:11
  55. * @copyright: Copyright (c) 2020 广东七件事集团
  56. * @return void
  57. */
  58. public function actionStyle(){
  59. if (Yii::$app->request->isAjax) {
  60. $default = [
  61. 'cat_style' => 3,
  62. 'recommend_count' => 3,
  63. 'cat_goods_count' => 1,
  64. 'cat_goods_cols' => 1
  65. ];
  66. if(Yii::$app->request->isGet){
  67. Yii::$app->services->setting->mall->get();
  68. $cate_setting = Yii::$app->services->setting->mall->get($this->mall_id,'cate_setting',null,true);
  69. $setting = array_merge($default,$cate_setting);
  70. $this->success('ok',compact('setting'));
  71. }else{
  72. $post = Yii::$app->request->post();
  73. $res = Yii::$app->services->validate->validate($post,[
  74. [['cat_style'], 'required'],
  75. [['recommend_count','cat_goods_count','cat_goods_cols'], 'integer'],
  76. ]);
  77. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  78. $cate_setting = array_merge($default,$post);
  79. $res = Yii::$app->services->setting->mall->set($this->mall_id,compact('cate_setting'));
  80. $this->success('保存成功');
  81. }
  82. }
  83. }
  84. /**
  85. * 商品分类
  86. * @Author bing
  87. * @DateTime 2021-08-20 14:49:55
  88. * @copyright: Copyright (c) 2020 广东七件事集团
  89. * @return void
  90. */
  91. public function actionEdit(){
  92. if (Yii::$app->request->isAjax) {
  93. if (Yii::$app->request->isGet) {
  94. // 数据获取
  95. $get = Yii::$app->request->get();
  96. $res = Yii::$app->services->validate->validate($get, [[['id'], 'integer'],[['keyword'], 'string']]);
  97. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  98. $detail = [];
  99. // 传了id参数并且无分类搜索则获取分类详细信息
  100. if(isset($get['id']) && empty($get['keyword'])){
  101. $detail = PurchaseCate::getOne([['id' => $get['id'], 'mall_id' => $this->mall_id]], true,['parent.parent']);
  102. if(empty($detail)) return $this->error('不存在此分类');
  103. $detail['parents'] = [];
  104. if(isset($detail['parent'])) $detail['parents'][] = $detail['parent'];
  105. if(isset($detail['parent']['parent'])) $detail['parents'][] = $detail['parent']['parent'];
  106. }
  107. $cond = [['mall_id' => $this->mall_id]];
  108. isset($get['keyword']) && $cond[] = ['like', 'name', $get['keyword'] . '%', false];
  109. $cates = PurchaseCate::getCates($cond,[],true,'id,name,parent_id');
  110. $cates = PurchaseCate::getTreeCate($cates);
  111. return $this->success('操作成功',compact('detail','cates'));
  112. } else {
  113. // 编辑分类
  114. $form = Yii::$app->request->post('form');
  115. $res = Yii::$app->services->validate->validate($form, [
  116. [['id'], 'integer'],
  117. [['name','sort','pic','big_pic_url','advert_pic','advert_url','parent_id','status','is_show','advert_open_type','advert_params'],'safe'],
  118. ]);
  119. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  120. $form['parent_id'] = empty($form['parent_id']) ? 0 : $form['parent_id'];
  121. $form['mall_id'] = $this->mall['id'];
  122. unset($form['advert_params']);
  123. $res = PurchaseCate::setData($form);
  124. if ($res === false) return $this->error(PurchaseCate::getStaticError());
  125. return $this->success('操作成功');
  126. }
  127. }
  128. return $this->render($this->action->id);
  129. }
  130. public function actionTree(){
  131. if (Yii::$app->request->isAjax) {
  132. if (Yii::$app->request->isGet) {
  133. $cond = [['mall_id' => $this->mall_id]];
  134. $list = PurchaseCate::getCates($cond,[],true,'id,name,parent_id');
  135. $list = PurchaseCate::getTreeCate($list);
  136. return $this->success('操作成功',compact('list'));
  137. }
  138. }
  139. }
  140. /**
  141. * @desc:删除商品分类
  142. * @Author: hua
  143. * @Date: 2021/11/4
  144. * @Time: 14:20
  145. * @Copyright: copyright (c) 2021 广东七件事集团
  146. * @return mixed|void
  147. */
  148. public function actionDelete(){
  149. $params = Yii::$app->request->post();
  150. $params['status'] = StatusEnum::DELETE;
  151. $params['mall_id'] = $this->mall_id;
  152. $res = Yii::$app->services->validate->validate($form, [
  153. [['id'], 'integer'],
  154. ]);
  155. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  156. $res = PurchaseGoodsCateRelate::findOne(['cate_id'=>$params['id']]);
  157. // if (!empty($res)) return $this->error('该分类关联了商品,不能删除!');
  158. $res = PurchaseCate::setData($params);
  159. if ($res === false) return $this->error('删除失败!');
  160. return $this->success('操作成功',[]);
  161. }
  162. }