GoodsCateController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace api\modules\v1\controllers\goods;
  3. use api\controllers\OnAuthController;
  4. use common\enums\StatusEnum;
  5. use common\models\goods\GoodsCate;
  6. use Yii;
  7. /**
  8. * Class GoodsCateController
  9. * @package api\modules\goods\controllers
  10. */
  11. class GoodsCateController extends OnAuthController
  12. {
  13. public $modelClass = '';
  14. /**
  15. *
  16. * 不用进行登录验证的方法
  17. *
  18. * 例如: ['index', 'update', 'create', 'view', 'delete']
  19. * 默认全部需要验证
  20. *
  21. * @var array
  22. */
  23. protected $authOptional = ['index','child-cate-list', 'child', 'tree'];
  24. /**
  25. * 分类列表
  26. * @return mixed|void
  27. */
  28. public function actionIndex()
  29. {
  30. $params = \Yii::$app->request->get();
  31. $params['where'] = [['mall_id'=>$this->mall_id,'parent_id'=>empty($params['cate_id'])?0:$params['cate_id'],'mch_id'=>0,'is_show'=>1,'status'=>StatusEnum::ENABLED]];
  32. $params['select'] = 'id,name,pic';
  33. $params['order'] = 'sort asc';
  34. $cate_list['list'] = GoodsCate::lists($params);
  35. $cats_third_style = "{\"aLevelBgColor\":\"#fff\",\"bLevelBgColor\":\"#fff\",\"cLevelBgColor\":\"#fff\",\"fontColor\":\"#000\",\"fontSize\":\"14\",\"fontWeight\":\"normal\",\"bFontColor\":\"#000\",\"cFontColor\":\"#000\"}";
  36. // 类目风格
  37. $default = [
  38. 'cat_style' => 1,
  39. 'recommend_count' => 3,
  40. 'cat_goods_count' => 1,
  41. 'cat_goods_cols' => 1,
  42. 'cats_third_style' => $cats_third_style
  43. ];
  44. $cate_setting = Yii::$app->services->setting->mall->get($this->mall_id,'cate_setting',null,true);
  45. if(is_array($cate_setting)){
  46. foreach($default as $key => $val){
  47. if(isset($cate_setting[$key]) && !empty($cate_setting[$key])){
  48. $default[$key] = $cate_setting[$key];
  49. }
  50. }
  51. }
  52. is_string($default['cats_third_style']) && $default['cats_third_style'] = json_decode($default['cats_third_style'], true);
  53. // 处理后面添加的字段
  54. foreach(json_decode($cats_third_style) as $key => $val){
  55. if(!isset($default['cats_third_style'][$key])){
  56. $default['cats_third_style'][$key] = $val;
  57. }
  58. }
  59. $cate_list['config'] = $default;
  60. return $this->success('操作成功',$cate_list);
  61. }
  62. /**
  63. * 二级分类列表
  64. * @return mixed|void
  65. */
  66. public function actionChildCateList()
  67. {
  68. $params = \Yii::$app->request->get();
  69. $params['where'] = [['mall_id'=>$this->mall_id,'parent_id'=>0,'mch_id'=>0,'is_show'=>1,'status'=>StatusEnum::ENABLED]];
  70. $params['select'] = 'id,name,pic';
  71. $params['order'] = 'sort asc';
  72. $parent_list= GoodsCate::lists($params);
  73. $list=array_column($parent_list,'id');
  74. $params['where'] = [['mall_id'=>$this->mall_id,'parent_id'=>$list,'mch_id'=>0,'is_show'=>1,'status'=>StatusEnum::ENABLED]];
  75. $child_list= GoodsCate::lists($params);
  76. return $this->success('操作成功',$child_list);
  77. }
  78. public function actionChild()
  79. {
  80. $params = \Yii::$app->request->get();
  81. $res = \Yii::$app->services->validate->validate($params, [
  82. [['pid'], 'required', 'message' => 'pid必传'],
  83. [['pid'], 'integer']
  84. ]);
  85. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  86. $params['where'] = [['mall_id'=>$this->mall_id,'parent_id'=> $params['pid'],'mch_id'=>0,'is_show'=>1,'status'=>StatusEnum::ENABLED]];
  87. $params['select'] = 'id,name,pic';
  88. $child_list= GoodsCate::lists($params);
  89. return $this->success('操作成功',$child_list);
  90. }
  91. /**
  92. * @return mixed|null
  93. */
  94. public function actionTree()
  95. {
  96. $cats = GoodsCate::lists([
  97. 'where' => [
  98. ['mall_id' => $this->mall_id],
  99. ['status' => StatusEnum::ENABLED],
  100. ['mch_id' => StatusEnum::DISABLED],
  101. ],
  102. 'select'=> 'id, parent_id, pic, name'
  103. ]);
  104. return $this->success('操作成功', GoodsCate::getTreeCate($cats));
  105. }
  106. }