CateController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace addons\Material\backend\controllers;
  3. use addons\Material\common\models\Material;
  4. use addons\Material\common\models\MaterialCate;
  5. use common\enums\StatusEnum;
  6. use Yii;
  7. use yii\db\Exception;
  8. /**
  9. * 默认控制器
  10. *
  11. * Class DefaultController
  12. * @package addons\Material\backend\controllers
  13. */
  14. class CateController extends BaseController
  15. {
  16. public $enableCsrfValidation = false;
  17. /**
  18. * 首页
  19. *
  20. * @return string
  21. */
  22. public function actionIndex()
  23. {
  24. $retuest = Yii::$app->request;
  25. if ($retuest->isAjax) {
  26. if ($retuest->isGet) {
  27. $get = Yii::$app->request->get();
  28. // 检查提交的参数
  29. $res = Yii::$app->services->validate->validate($get,[
  30. [['id','keyword'], 'safe'],
  31. [['limit'],'default','value' => '15'],
  32. ]);
  33. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  34. $where = [];
  35. if($get['keyword']) $where[] = ['like', 'name', $get['keyword']];
  36. $where[] = ['=', 'mall_id', $this->mall_id];
  37. $where[] = ['>=', 'status', StatusEnum::DISABLED];
  38. $where[] = ['=', 'parent_id', 0];
  39. $order = 'sort,created_at desc';
  40. $with = 'children';
  41. $params = array('where' => $where, 'order'=>$order, 'limit' => $get['limit'], 'with' => $with);
  42. $list = MaterialCate::listPage($params, false, true);
  43. $this->success('获取成功', $list);
  44. }
  45. } else {
  46. return $this->render($this->action->id);
  47. }
  48. }
  49. /**
  50. * 等级编辑
  51. * @Author: ltm
  52. * @DateTime: 2021/10/21 0021 11:29
  53. * @Copyright: copyright (c) 2021 广东七件事集团
  54. * @return mixed|string|void
  55. */
  56. public function actionEdit()
  57. {
  58. try {
  59. $request = Yii::$app->request;
  60. if ($request->isAjax) {
  61. // 提交等级内容
  62. if ($request->isPost) {
  63. $params = Yii::$app->request->post();
  64. $res = Yii::$app->services->validate->validate($params, [
  65. [['name','id','status','parent_id','sort'], 'safe'],
  66. ]);
  67. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  68. $params = MaterialCate::exceptNullVal($params);
  69. if($params['id']) $params['updated_at'] = time();//更新
  70. $params['create_at'] = time();
  71. if(!$params['name']) unset($params['name']);
  72. $params['mall_id'] = $this->mall_id;
  73. if(isset($params['status']) && !in_array($params['status'],[StatusEnum::DISABLED,StatusEnum::ENABLED,StatusEnum::DELETE])) throw new Exception("参数错误");
  74. $res = MaterialCate::setData($params);
  75. if ($res === false) throw new Exception(MaterialCate::getStaticError());
  76. return $this->success('操作成功');
  77. }
  78. }
  79. } catch (\Exception $e) {
  80. return $this->error($e->getMessage(), []);
  81. }
  82. return $this->render($this->action->id);
  83. }
  84. public function actionCountMaterial()
  85. {
  86. $request = Yii::$app->request;
  87. if ($request->isAjax) {
  88. if ($request->isGet) {
  89. $get = Yii::$app->request->get();
  90. $count = Material::count([
  91. 'where' => [
  92. ['mall_id' => $this->mall_id],
  93. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
  94. [ 'second_cate_id' => $get['cate_id']]
  95. ]
  96. ]);
  97. return $this->success('', [
  98. 'count' => $count
  99. ]);
  100. }
  101. }
  102. }
  103. }