NotifyAnnounceController.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace addons\TinyShop\api\modules\v1\controllers\common;
  3. use Yii;
  4. use yii\data\ActiveDataProvider;
  5. use yii\web\NotFoundHttpException;
  6. use api\controllers\OnAuthController;
  7. use common\enums\StatusEnum;
  8. use addons\TinyShop\common\models\common\NotifyAnnounce;
  9. /**
  10. * Class NotifyAnnounceController
  11. * @package addons\TinyShop\api\modules\v1\controllers\common
  12. * @author jianyan74 <751393839@qq.com>
  13. */
  14. class NotifyAnnounceController extends OnAuthController
  15. {
  16. /**
  17. * @var NotifyAnnounce
  18. */
  19. public $modelClass = NotifyAnnounce::class;
  20. /**
  21. * 不用进行登录验证的方法
  22. * 例如: ['index', 'update', 'create', 'view', 'delete']
  23. * 默认全部需要验证
  24. *
  25. * @var array
  26. */
  27. protected $authOptional = ['index', 'view'];
  28. /**
  29. * 首页
  30. *
  31. * @return ActiveDataProvider
  32. */
  33. public function actionIndex()
  34. {
  35. return new ActiveDataProvider([
  36. 'query' => $this->modelClass::find()
  37. ->select(['id', 'title', 'cover', 'synopsis', 'view', 'created_at'])
  38. ->where(['status' => StatusEnum::ENABLED])
  39. ->andWhere(['merchant_id' => Yii::$app->services->merchant->getNotNullId()])
  40. ->orderBy('id desc')
  41. ->asArray(),
  42. 'pagination' => [
  43. 'pageSize' => $this->pageSize,
  44. 'validatePage' => false,// 超出分页不返回data
  45. ],
  46. ]);
  47. }
  48. /**
  49. * @param $id
  50. * @return \yii\db\ActiveRecord
  51. * @throws NotFoundHttpException
  52. */
  53. protected function findModel($id)
  54. {
  55. /* @var $model \yii\db\ActiveRecord */
  56. if (empty($id) || !($model = $this->modelClass::find()->where([
  57. 'id' => $id,
  58. 'status' => StatusEnum::ENABLED,
  59. ])->andFilterWhere(['merchant_id' => $this->getMerchantId()])->one())) {
  60. throw new NotFoundHttpException('请求的数据不存在');
  61. }
  62. return $model;
  63. }
  64. }
粤ICP备19079148号