CateController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace backend\modules\mall\controllers;
  3. use backend\controllers\MallController;
  4. use common\enums\StatusEnum;
  5. use common\models\goods\GoodsCate;
  6. use common\models\goods\GoodsCateRelate;
  7. use Yii;
  8. /**
  9. *
  10. * 商品控制器
  11. * @Author bing
  12. * @DateTime 2021-08-16 13:12:44
  13. * @copyright: Copyright (c) 2020 广东七件事集团
  14. */
  15. class CateController extends MallController
  16. {
  17. /**
  18. * 商品分类
  19. * @Author bing
  20. * @DateTime 2021-08-20 14:49:55
  21. * @copyright: Copyright (c) 2020 广东七件事集团
  22. * @return void
  23. */
  24. public function actionIndex(){
  25. if (Yii::$app->request->isAjax) {
  26. $keyword = Yii::$app->request->get('keyword','');
  27. $level = Yii::$app->request->get('level', 0);
  28. $where = [['mall_id' => $this->mall['id'],'parent_id' => 0,'mch_id' => $this->admin->mch_id,'status' => [StatusEnum::ENABLED,StatusEnum::DISABLED]]];
  29. $keyword && $where[] = ['like','name',$keyword.'%',false];
  30. $order = 'sort asc,created_at desc';
  31. $with = [];
  32. empty($level) && $with = ['child','child.child'];
  33. $params = compact('where','order','with');
  34. $list = GoodsCate::lists($params);
  35. if($list === false) return $this->error(GoodsCate::getStaticError());
  36. return $this->success('ok',compact('list'));
  37. }
  38. return $this->render($this->action->id);
  39. }
  40. /**
  41. * 商品分类
  42. * @Author bing
  43. * @DateTime 2021-08-20 14:49:55
  44. * @copyright: Copyright (c) 2020 广东七件事集团
  45. * @return void
  46. */
  47. public function actionSortEdit(){
  48. if (Yii::$app->request->isAjax) {
  49. $post = Yii::$app->request->post();
  50. $res = GoodsCate::sort($post);
  51. if($res === false) return $this->error(GoodsCate::getStaticError());
  52. return $this->success();
  53. }
  54. }
  55. /**
  56. * 商品分类转移
  57. * @Author bing
  58. * @DateTime 2021-08-20 14:49:55
  59. * @copyright: Copyright (c) 2020 广东七件事集团
  60. * @return void
  61. */
  62. public function actionTransfer(){
  63. if (Yii::$app->request->isAjax) {
  64. if(Yii::$app->request->isGet){
  65. $id = Yii::$app->request->get('id',0);
  66. $keyword = Yii::$app->request->get('keyword','');
  67. $where = [['mall_id' => $this->mall['id'],'parent_id' =>$id,'mch_id' => $this->admin->mch_id,'status' => [StatusEnum::ENABLED]]];
  68. $keyword && $where[] = ['like','name',$keyword.'%',false];
  69. $with = ['parent', 'child', 'parent.parent'];
  70. $params = compact('where','order','with');
  71. $params['limit'] = 1000;
  72. $page_data = GoodsCate::listPage($params,false);
  73. $list = $page_data['list'];
  74. $parent = $list[0]['parent'] ? $list[0]['parent'] : null;
  75. $grandParent = $parent['parent'] ? $parent['parent'] : null;
  76. if($list === false) return $this->error(GoodsCate::getStaticError());
  77. return $this->success('ok',compact('list','parent','grandParent'));
  78. }else{
  79. // 分类转移
  80. $post = Yii::$app->request->post();
  81. $res = $this->checkRequireParam($post,['before','after']);
  82. if($post['before'] <= 0 || $post['after'] <= 0) return $this->error('请选择转移前和转移后的分类');
  83. $res = GoodsCateRelate::changeGoodsCate($post['before'],$post['after']);
  84. if($res === false) return $this->error(GoodsCateRelate::getStaticError());
  85. return $this->success('转移成功,一共转移' . $res . '个商品');
  86. }
  87. }
  88. }
  89. /**
  90. * 商品样式设置
  91. * @Author bing
  92. * @DateTime 2021-08-23 09:55:11
  93. * @copyright: Copyright (c) 2020 广东七件事集团
  94. * @return void
  95. */
  96. public function actionStyle(){
  97. if (Yii::$app->request->isAjax) {
  98. $default = [
  99. 'cat_style' => 3,
  100. 'recommend_count' => 3,
  101. 'cat_goods_count' => 1,
  102. 'cat_goods_cols' => 1
  103. ];
  104. if(Yii::$app->request->isGet){
  105. Yii::$app->services->setting->mall->get();
  106. $cate_setting = Yii::$app->services->setting->mall->get($this->mall_id,'cate_setting',null,true);
  107. $cate_setting['cats_third_style'] = isset($cate_setting['cats_third_style']) ? json_decode($cate_setting['cats_third_style'], true) : [];
  108. $setting = array_merge($default,$cate_setting);
  109. $this->success('ok',compact('setting'));
  110. }else{
  111. $post = Yii::$app->request->post();
  112. $res = Yii::$app->services->validate->validate($post,[
  113. [['cat_style'], 'required'],
  114. [['recommend_count','cat_goods_count','cat_goods_cols'], 'integer'],
  115. [['cats_third_style'], 'safe']
  116. ]);
  117. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  118. $post['cats_third_style'] = json_encode($post['cats_third_style']);
  119. $cate_setting = array_merge($default,$post);
  120. $res = Yii::$app->services->setting->mall->set($this->mall_id,compact('cate_setting'));
  121. $this->success('保存成功');
  122. }
  123. }
  124. }
  125. /**
  126. * 商品分类
  127. * @Author bing
  128. * @DateTime 2021-08-20 14:49:55
  129. * @copyright: Copyright (c) 2020 广东七件事集团
  130. * @return void
  131. */
  132. public function actionEdit(){
  133. if (Yii::$app->request->isAjax) {
  134. if (Yii::$app->request->isGet) {
  135. // 数据获取
  136. $get = Yii::$app->request->get();
  137. $res = Yii::$app->services->validate->validate($get, [[['id'], 'integer'],[['keyword'], 'string']]);
  138. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  139. $detail = [];
  140. // 传了id参数并且无分类搜索则获取分类详细信息
  141. if(isset($get['id']) && empty($get['keyword'])){
  142. $detail = GoodsCate::getOne([['id' => $get['id'], 'mall_id' => $this->mall_id, 'mch_id' => $this->admin->mch_id]], true,['parent.parent']);
  143. if(empty($detail)) return $this->error('不存在此分类');
  144. $detail['parents'] = [];
  145. if(isset($detail['parent'])) $detail['parents'][] = $detail['parent'];
  146. if(isset($detail['parent']['parent'])) $detail['parents'][] = $detail['parent']['parent'];
  147. }
  148. $cond = [['mall_id' => $this->mall_id,'mch_id' => $this->admin->mch_id,'status' => [StatusEnum::ENABLED,StatusEnum::DISABLED]]];
  149. isset($get['keyword']) && $cond[] = ['like', 'name', $get['keyword'] . '%', false];
  150. $cates = GoodsCate::getCates($cond,[],true,'id,name,parent_id');
  151. $cates = GoodsCate::getTreeCate($cates);
  152. return $this->success('操作成功',compact('detail','cates'));
  153. } else {
  154. // 编辑分类
  155. $form = Yii::$app->request->post('form');
  156. $res = Yii::$app->services->validate->validate($form, [
  157. [['id'], 'integer'],
  158. [['name','sort','pic','big_pic_url','advert_pic','advert_url','parent_id','status','is_show','advert_open_type','advert_params'],'safe'],
  159. ]);
  160. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  161. $form['parent_id'] = empty($form['parent_id']) ? 0 : $form['parent_id'];
  162. $form['mall_id'] = $this->mall['id'];
  163. $form['mch_id'] = $this->admin->mch_id;
  164. unset($form['advert_params']);
  165. if (empty($form['pic'])) {
  166. $static_url = Yii::$app->services->setting->get('system.static_url') ?? '';
  167. $form['pic'] = $static_url . '/static/system-icon/goods_category_default_icon.png';
  168. }
  169. $res = GoodsCate::setData($form);
  170. if ($res === false) return $this->error(GoodsCate::getStaticError());
  171. return $this->success('操作成功');
  172. }
  173. }
  174. return $this->render($this->action->id);
  175. }
  176. public function actionTree(){
  177. if (Yii::$app->request->isAjax) {
  178. if (Yii::$app->request->isGet) {
  179. $cond = [['mall_id' => $this->mall_id, 'mch_id' => $this->admin->mch_id]];
  180. $list = GoodsCate::getCates($cond,[],true,'id,name,parent_id');
  181. $list = GoodsCate::getTreeCate($list);
  182. return $this->success('操作成功',compact('list'));
  183. }
  184. }
  185. }
  186. /**
  187. * @desc:删除商品分类
  188. * @Author: hua
  189. * @Date: 2021/11/4
  190. * @Time: 14:20
  191. * @Copyright: copyright (c) 2021 广东七件事集团
  192. * @return mixed|void
  193. */
  194. public function actionDelete(){
  195. $params = Yii::$app->request->post();
  196. $params['status'] = StatusEnum::DELETE;
  197. $params['mall_id'] = $this->mall_id;
  198. $res = Yii::$app->services->validate->validate($form, [
  199. [['id'], 'integer'],
  200. ]);
  201. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  202. // $res = GoodsCateRelate::findOne(['cate_id'=>$params['id']]);
  203. // if (!empty($res)) return $this->error('该分类关联了商品,不能删除!');
  204. $res = GoodsCate::del($params);
  205. if ($res === false) return $this->error(GoodsCate::getStaticError());
  206. return $this->success('操作成功',[]);
  207. }
  208. }