You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.3 KiB

  1. <?php
  2. namespace api\controllers;
  3. use yii\filters\auth\HttpBearerAuth;
  4. use yii\helpers\ArrayHelper;
  5. use yii\rest\ActiveController;
  6. /**
  7. * @author iron
  8. * @email weiriron@gmail.com
  9. */
  10. class GoodsController extends CommonController
  11. {
  12. public $modelClass = 'antgoods\goods\models\ars\Goods';
  13. public function actions()
  14. {
  15. $action = parent::actions();
  16. unset($action['delete']);
  17. unset($action['create']);
  18. $action['index'] = [
  19. 'class' => 'yii\rest\IndexAction',
  20. 'modelClass' => $this->modelClass,
  21. 'dataFilter' => $this->getFilter()
  22. ];
  23. return $action;
  24. }
  25. protected function getFilter()
  26. {
  27. $keyword = \Yii::$app->request->getBodyParam('keyword');
  28. $category = \Yii::$app->request->getBodyParam('category');
  29. if (empty($keyword) && empty($category)) {
  30. return null;
  31. }
  32. $array = [];
  33. if ($keyword) {
  34. $array['name'] = ['like' => $keyword];
  35. }
  36. if ($category) {
  37. $array['cat_id'] = $category;
  38. }
  39. $filter = ['class' => 'yii\data\ActiveDataFilter',
  40. 'filter' => $array,
  41. 'searchModel' => ['class' => 'antgoods\goods\models\searchs\GoodsSearch']];
  42. return $filter;
  43. }
  44. }