CateController.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace addons\TinyBlog\api\modules\v1\controllers;
  3. use addons\TinyBlog\common\models\Cate;
  4. use api\controllers\OnAuthController;
  5. use common\enums\StatusEnum;
  6. use yii\data\ActiveDataProvider;
  7. /**
  8. * 文章分类
  9. *
  10. * Class CateController
  11. * @package addons\TinyBlog\api\modules\v1\controllers
  12. * @author jianyan74 <751393839@qq.com>
  13. */
  14. class CateController extends OnAuthController
  15. {
  16. /**
  17. * @var Cate
  18. */
  19. public $modelClass = Cate::class;
  20. /**
  21. * 不用进行登录验证的方法
  22. * 例如: ['index', 'update', 'create', 'view', 'delete']
  23. * 默认全部需要验证
  24. *
  25. * @var array
  26. */
  27. protected $authOptional = ['index'];
  28. /**
  29. * @return array|ActiveDataProvider|\yii\db\ActiveRecord[]
  30. */
  31. public function actionIndex()
  32. {
  33. return $this->modelClass::find()
  34. ->select(['id', 'title'])
  35. ->where(['status' => StatusEnum::ENABLED])
  36. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  37. ->orderBy('sort asc, id desc')
  38. ->asArray()
  39. ->all();
  40. }
  41. /**
  42. * 权限验证
  43. *
  44. * @param string $action 当前的方法
  45. * @param null $model 当前的模型类
  46. * @param array $params $_GET变量
  47. * @throws \yii\web\BadRequestHttpException
  48. */
  49. public function checkAccess($action, $model = null, $params = [])
  50. {
  51. // 方法名称
  52. if (in_array($action, ['delete', 'create', 'update', 'view'])) {
  53. throw new \yii\web\BadRequestHttpException('权限不足');
  54. }
  55. }
  56. }
粤ICP备19079148号