ArticleController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace addons\TinyBlog\api\modules\v1\controllers;
  3. use Yii;
  4. use yii\data\ActiveDataProvider;
  5. use yii\data\Pagination;
  6. use common\enums\StatusEnum;
  7. use addons\TinyBlog\common\models\Article;
  8. use api\controllers\OnAuthController;
  9. /**
  10. * 文章接口
  11. *
  12. * Class ArticleController
  13. * @package addons\TinyBlog\api\modules\v1\controllers
  14. * @author jianyan74 <751393839@qq.com>
  15. */
  16. class ArticleController extends OnAuthController
  17. {
  18. /**
  19. * @var Article
  20. */
  21. public $modelClass = Article::class;
  22. /**
  23. * 不用进行登录验证的方法
  24. * 例如: ['index', 'update', 'create', 'view', 'delete']
  25. * 默认全部需要验证
  26. *
  27. * @var array
  28. */
  29. protected $authOptional = ['index', 'view', 'list'];
  30. /**
  31. * 首页
  32. *
  33. * @return ActiveDataProvider
  34. */
  35. public function actionIndex()
  36. {
  37. return new ActiveDataProvider([
  38. 'query' => $this->modelClass::find()
  39. ->where(['status' => StatusEnum::ENABLED])
  40. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  41. ->select(['id', 'title', 'cover', 'author', 'cate_id', 'description', 'view'])
  42. ->orderBy('sort asc, id desc')
  43. ->with(['cate'])
  44. ->asArray(),
  45. 'pagination' => [
  46. 'pageSize' => $this->pageSize,
  47. 'validatePage' => false,// 超出分页不返回data
  48. ],
  49. ]);
  50. }
  51. /**
  52. * 自定义装修可用
  53. *
  54. * 修改数据格式返回
  55. *
  56. * @return array|mixed
  57. */
  58. public function actionList()
  59. {
  60. $keyword = Yii::$app->request->get('keyword');
  61. $data = $this->modelClass::find()
  62. ->select(['id', 'merchant_id', 'title', 'description', 'cover', 'author', 'view'])
  63. ->where(['status' => StatusEnum::ENABLED])
  64. ->andFilterWhere(['like', 'title', $keyword])
  65. ->andFilterWhere(['merchant_id' => $this->getMerchantId()]);
  66. $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
  67. $models = $data->offset($pages->offset)
  68. ->orderBy('id desc')
  69. ->with(['merchant'])
  70. ->limit($pages->limit)
  71. ->asArray()
  72. ->all();
  73. return [
  74. 'list' => $models,
  75. 'pages' => [
  76. 'totalCount' => $pages->totalCount,
  77. 'pageSize' => $pages->pageSize,
  78. ]
  79. ];
  80. }
  81. /**
  82. * 权限验证
  83. *
  84. * @param string $action 当前的方法
  85. * @param null $model 当前的模型类
  86. * @param array $params $_GET变量
  87. * @throws \yii\web\BadRequestHttpException
  88. */
  89. public function checkAccess($action, $model = null, $params = [])
  90. {
  91. // 方法名称
  92. if (in_array($action, ['delete', 'create', 'update'])) {
  93. throw new \yii\web\BadRequestHttpException('权限不足');
  94. }
  95. }
  96. }
粤ICP备19079148号