StoreCategoryController.php 8.5 KB

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