| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- namespace addons\Store\backend\controllers;
- use addons\Store\common\models\Store;
- use addons\Store\common\models\StoreCate;
- use common\enums\StatusEnum;
- class StoreCategoryController extends BaseController
- {
- public function actionIndex()
- {
- if (!\Yii::$app->request->isAjax) {
- return $this->render('/category/index');
- }
- if (\Yii::$app->request->isGet) {
- $keyword = \Yii::$app->request->get('keyword', '');
- $where = [
- [
- 'mall_id' => $this->mall['id'],
- 'parent_id' => 0,
- 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]
- ],
- ];
- $keyword && $where[] = ['like', 'name', $keyword];
- $order = 'sort asc,created_at desc';
- $with = ['child', 'child.child'];
- $params = compact('where', 'order', 'with');
- $list = StoreCate::lists($params);
- if ($list === false) return $this->error(StoreCate::getStaticError());
- return $this->success('ok', compact('list'));
- }
- }
- /**
- * 门店分类
- * @Author bing
- * @DateTime 2021-08-20 14:49:55
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @return void
- */
- public function actionSortEdit()
- {
- if (\Yii::$app->request->isAjax) {
- $post = \Yii::$app->request->post();
- $res = StoreCate::sort($post);
- if ($res === false) return $this->error(StoreCate::getStaticError());
- return $this->success();
- }
- }
- /**
- * 门店分类转移(暂无此需求)
- * @Author bing
- * @DateTime 2021-08-20 14:49:55
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @return void
- */
- /* public function actionTransfer()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $id = \Yii::$app->request->get('id', 0);
- $keyword = \Yii::$app->request->get('keyword', '');
- $where = [['mall_id' => $this->mall['id'], 'parent_id' => $id, 'status' => [StatusEnum::ENABLED]]];
- $keyword && $where[] = ['like', 'name', $keyword . '%', false];
- $with = ['parent', 'child', 'parent.parent'];
- $params = compact('where', 'order', 'with');
- $page_data = StoreCate::listPage($params, false);
- $list = $page_data['list'];
- $parent = $list[0]['parent'] ? $list[0]['parent'] : null;
- $grandParent = $parent['parent'] ? $parent['parent'] : null;
- if ($list === false) return $this->error(StoreCate::getStaticError());
- return $this->success('ok', compact('list', 'parent', 'grandParent'));
- } else {
- // 分类转移
- $post = \Yii::$app->request->post();
- $res = $this->checkRequireParam($post, ['before', 'after']);
- if ($post['before'] <= 0 || $post['after'] <= 0) return $this->error('请选择转移前和转移后的分类');
- $res = GoodsCateRelate::changeGoodsCate($post['before'], $post['after']);
- if ($res === false) return $this->error(GoodsCateRelate::getStaticError());
- return $this->success('转移成功,一共转移' . $res . '个门店');
- }
- }
- } */
- /**
- * @name:
- * @msg: 门店分类添加 / 编辑
- * @return {*}
- */
- public function actionStorage()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- // 数据获取
- $get = \Yii::$app->request->get();
- $res = \Yii::$app->services->validate->validate($get, [[['id'], 'integer'], [['keyword'], 'string']]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $detail = [];
- // 传了id参数并且无分类搜索则获取分类详细信息
- if (isset($get['id']) && empty($get['keyword'])) {
- $detail = StoreCate::getOne([['id' => $get['id'], 'mall_id' => $this->mall_id]], true, ['parent.parent']);
- if (empty($detail)) return $this->error('不存在此分类');
- $detail['parents'] = [];
- if (isset($detail['parent'])) $detail['parents'][] = $detail['parent'];
- if (isset($detail['parent']['parent'])) $detail['parents'][] = $detail['parent']['parent'];
- }
- if (!empty($detail['advert_url'])) {
- $detail['advert_url'] = json_decode($detail['advert_url'], true);
- }
- if (!empty($detail)) {
- if (!empty($detail['advert_pic'])) {
- $detail['advert_pic'] = json_decode($detail['advert_pic'], true);
- } else {
- $detail['advert_pic'] = [];
- }
- }
- $detail && $detail['advert_params'] = json_decode($detail['advert_params'], true);
- $detail && !is_array($detail['advert_params']) && $detail['advert_params'] = [];
- $cond = [['mall_id' => $this->mall_id]];
- isset($get['keyword']) && $cond[] = ['like', 'name', $get['keyword'] . '%', false];
- $cates = StoreCate::getCates($cond, [], true, 'id,name,parent_id');
- $cates = StoreCate::getTreeCate($cates);
- return $this->success('操作成功', compact('detail', 'cates'));
- } else {
- // 编辑分类
- $form = \Yii::$app->request->post('form');
- $res = \Yii::$app->services->validate->validate($form, [
- [['id'], 'integer'],
- [['name', 'sort', 'pic', 'big_pic_url', 'advert_pic', 'advert_url', 'parent_id', 'status', 'is_show', 'advert_open_type', 'advert_params','is_collect_service_fee','collect_service_fee'], 'safe'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $form['parent_id'] = empty($form['parent_id']) ? 0 : $form['parent_id'];
- $form['mall_id'] = $this->mall['id'];
- $form['advert_open_type'] = $form['advert_url']['open_type'] ?? '';
- if (isset($form['advert_url']['params'])) {
- if (!empty($form['advert_url']['params'])) {
- is_array($form['advert_url']['params']) && $form['advert_params'] = $form['advert_url']['params'];
- } else {
- $form['advert_url']['params'] = '';
- }
- }
- if (!empty($form['advert_pic'])) {
- $form['advert_pic'] = json_encode($form['advert_pic']);
- }
- if (!empty($form['advert_url'])) {
- $form['advert_url'] = json_encode($form['advert_url']);
- }
- $res = StoreCate::setData($form);
- if ($res === false) return $this->error(StoreCate::getStaticError());
- return $this->success('操作成功');
- }
- }
- return $this->render('/category/add-edit');
- }
- /* public function actionTree()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $cond = [['mall_id' => $this->mall_id]];
- $list = GoodsCate::getCates($cond, [], true, 'id,name,parent_id');
- $list = GoodsCate::getTreeCate($list);
- return $this->success('操作成功', compact('list'));
- }
- }
- } */
- /**
- * @desc:删除门店分类
- * @Author: hua
- * @Date: 2021/11/4
- * @Time: 14:20
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionDelete()
- {
- $params = \Yii::$app->request->post();
- $res = \Yii::$app->services->validate->validate($params, [
- [['id'], 'integer'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $params['status'] = StatusEnum::DELETE;
- $params['mall_id'] = $this->mall_id;
- $res = Store::findOne(['c_id' => $params['id'], 'mall_id' => $this->mall_id,'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]);
- if (!empty($res)) return $this->error('该分类关联了门店,不能删除');
- $res = StoreCate::findOne(['parent_id'=>$params['id'],'status'=>StatusEnum::ENABLED,'mall_id'=>$this->mall_id]);
- if (!empty($res)) return $this->error('请先删除子分类');
- $res = StoreCate::setData($params);
- if ($res === false) return $this->error('删除失败');
- return $this->success('操作成功');
- }
- }
|