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.

53 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 CommonController extends ActiveController
  11. {
  12. public function behaviors()
  13. {
  14. return ArrayHelper::merge(parent::behaviors(), [
  15. 'authenticatior' => [
  16. 'class' => HttpBearerAuth::className(),
  17. 'except' => [],
  18. ]
  19. ]);
  20. }
  21. public function actions()
  22. {
  23. $action = parent::actions();
  24. unset($action['delete']);
  25. unset($action['create']);
  26. unset($action['update']);
  27. $action['index'] = [
  28. 'class' => 'yii\rest\IndexAction',
  29. 'checkAccess' => [$this, 'checkAccess'],
  30. 'modelClass' => $this->modelClass,
  31. 'dataFilter' => $this->getFilter()
  32. ];
  33. return $action;
  34. }
  35. protected function getFilter()
  36. {
  37. $data = \Yii::$app->request->getBodyParams();
  38. $data['user'] = true;
  39. \Yii::$app->request->setBodyParams($data);
  40. $filter = ['class' => 'yii\data\ActiveDataFilter',
  41. 'filter' => ['user_id' => \Yii::$app->user->getId()],
  42. 'searchModel' => ['class' => 'antgoods\goods\models\searchs\GoodsSearch']];
  43. return $filter;
  44. }
  45. }