CateController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace addons\BlindBox\backend\controllers;
  3. use addons\BlindBox\common\models\BlindBoxCate;
  4. use addons\BlindBox\common\models\BlindBoxGoods;
  5. use common\enums\StatusEnum;
  6. use Yii;
  7. use common\controllers\AddonsController;
  8. /**
  9. * 默认控制器
  10. *
  11. * Class DefaultController
  12. * @package addons\BlindBox\backend\controllers
  13. */
  14. class CateController extends BaseController
  15. {
  16. /**
  17. * 首页
  18. *
  19. * @return string
  20. */
  21. public function actionIndex()
  22. {
  23. if (\Yii::$app->request->isAjax) {
  24. if (\Yii::$app->request->isGet) {
  25. $params = \Yii::$app->request->get();
  26. $where[] = ['mall_id' => $this->mall_id];;
  27. if (!empty($params['name'])) $where[] = ['like', 'name', trim($params['name'])];
  28. if (isset($params['status'])) {
  29. $where[] = ['status' => $params['status']];
  30. } else {
  31. $where[] = ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  32. }
  33. $params['where'] = $where;
  34. $params['order'] = 'id DESC';
  35. $params['select'] = 'id,name,goods_num,status';
  36. $pageData = BlindBoxCate::listPage($params, false);
  37. if ($pageData === false) return $this->error(BlindBoxCate::getStaticError());
  38. return $this->success('操作成功', $pageData);
  39. }
  40. }
  41. return $this->render('index', []);
  42. }
  43. //编辑
  44. public function actionEdit()
  45. {
  46. if (\Yii::$app->request->isAjax) {
  47. if (\Yii::$app->request->isPost) {
  48. $params = \Yii::$app->request->post();
  49. $res = Yii::$app->services->validate->validate($params, [
  50. [['name', 'status'], 'required'],
  51. [['id'], 'safe'],
  52. ]);
  53. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  54. $params['mall_id'] = $this->mall_id;
  55. $res = BlindBoxCate::setData($params);
  56. if ($res === false) return $this->error(BlindBoxCate::getStaticError());
  57. return $this->success('操作成功');
  58. }
  59. if (\Yii::$app->request->isGet) {
  60. $params = \Yii::$app->request->get();
  61. $res = Yii::$app->services->validate->validate($params, [
  62. [['id'], 'required'],
  63. ]);
  64. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  65. $data = BlindBoxCate::getOne([
  66. ['id' => $params['id']],
  67. ['mall_id' => $this->mall_id],
  68. ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]]
  69. ],
  70. true, [], false, 'id,name,status');
  71. return $this->success('操作成功', $data);
  72. }
  73. }
  74. }
  75. public function actionDel()
  76. {
  77. if (\Yii::$app->request->isAjax) {
  78. if (\Yii::$app->request->isPost) {
  79. $params = \Yii::$app->request->post();
  80. $res = Yii::$app->services->validate->validate($params, [
  81. [['id'], 'required'],
  82. ]);
  83. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  84. $ids = $params['id'];
  85. $res = BlindBoxGoods::getOne([
  86. ['cate_id' => $ids],
  87. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
  88. ]);
  89. if (!empty($res)) return $this->error('所选分类关联了商品,不能删除');
  90. $res = BlindBoxCate::updateAll(['status' => StatusEnum::DELETE], ['id' => $ids, 'mall_id' => $this->mall_id]);
  91. if ($res == false) return $this->error(BlindBoxCate::getStaticError());
  92. return $this->success('操作成功');
  93. }
  94. }
  95. }
  96. }