| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
- namespace addons\BlindBox\backend\controllers;
- use addons\BlindBox\common\models\BlindBoxCate;
- use addons\BlindBox\common\models\BlindBoxGoods;
- use common\enums\StatusEnum;
- use Yii;
- use common\controllers\AddonsController;
- /**
- * 默认控制器
- *
- * Class DefaultController
- * @package addons\BlindBox\backend\controllers
- */
- class CateController extends BaseController
- {
- /**
- * 首页
- *
- * @return string
- */
- public function actionIndex()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $params = \Yii::$app->request->get();
- $where[] = ['mall_id' => $this->mall_id];;
- if (!empty($params['name'])) $where[] = ['like', 'name', trim($params['name'])];
- if (isset($params['status'])) {
- $where[] = ['status' => $params['status']];
- } else {
- $where[] = ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
- }
- $params['where'] = $where;
- $params['order'] = 'id DESC';
- $params['select'] = 'id,name,goods_num,status';
- $pageData = BlindBoxCate::listPage($params, false);
- if ($pageData === false) return $this->error(BlindBoxCate::getStaticError());
- return $this->success('操作成功', $pageData);
- }
- }
- return $this->render('index', []);
- }
- //编辑
- public function actionEdit()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['name', 'status'], 'required'],
- [['id'], 'safe'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $res = BlindBoxCate::setData($params);
- if ($res === false) return $this->error(BlindBoxCate::getStaticError());
- return $this->success('操作成功');
- }
- if (\Yii::$app->request->isGet) {
- $params = \Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'required'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $data = BlindBoxCate::getOne([
- ['id' => $params['id']],
- ['mall_id' => $this->mall_id],
- ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]]
- ],
- true, [], false, 'id,name,status');
- return $this->success('操作成功', $data);
- }
- }
- }
- public function actionDel()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'required'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $ids = $params['id'];
- $res = BlindBoxGoods::getOne([
- ['cate_id' => $ids],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
- ]);
- if (!empty($res)) return $this->error('所选分类关联了商品,不能删除');
- $res = BlindBoxCate::updateAll(['status' => StatusEnum::DELETE], ['id' => $ids, 'mall_id' => $this->mall_id]);
- if ($res == false) return $this->error(BlindBoxCate::getStaticError());
- return $this->success('操作成功');
- }
- }
- }
- }
|