OnAuthController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace oauth2\controllers;
  3. use Yii;
  4. use yii\data\ActiveDataProvider;
  5. use yii\web\NotFoundHttpException;
  6. use common\enums\StatusEnum;
  7. use common\helpers\ResultHelper;
  8. /**
  9. * Class OnAuthController
  10. * @package oauth2\controllers
  11. * @property yii\db\ActiveRecord|yii\base\Model $modelClass
  12. * @author jianyan74 <751393839@qq.com>
  13. */
  14. class OnAuthController extends ActiveController
  15. {
  16. /**
  17. * @return array
  18. */
  19. public function actions()
  20. {
  21. $actions = parent::actions();
  22. // 注销系统自带的实现方法
  23. unset($actions['index'], $actions['update'], $actions['create'], $actions['view'], $actions['delete']);
  24. // 自定义数据indexDataProvider覆盖IndexAction中的prepareDataProvider()方法
  25. // $actions['index']['prepareDataProvider'] = [$this, 'indexDataProvider'];
  26. return $actions;
  27. }
  28. /**
  29. * 首页
  30. *
  31. * @return ActiveDataProvider
  32. */
  33. public function actionIndex()
  34. {
  35. return new ActiveDataProvider([
  36. 'query' => $this->modelClass::find()
  37. ->where(['status' => StatusEnum::ENABLED])
  38. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  39. ->orderBy('id desc')
  40. ->asArray(),
  41. 'pagination' => [
  42. 'pageSize' => $this->pageSize,
  43. 'validatePage' => false,// 超出分页不返回data
  44. ],
  45. ]);
  46. }
  47. /**
  48. * 创建
  49. *
  50. * @return mixed|\yii\db\ActiveRecord
  51. */
  52. public function actionCreate()
  53. {
  54. /* @var $model \yii\db\ActiveRecord */
  55. $model = new $this->modelClass();
  56. $model->attributes = Yii::$app->request->post();
  57. $model->client_id = Yii::$app->user->identity->client_id;
  58. if (!$model->save()) {
  59. return ResultHelper::json(422, $this->getError($model));
  60. }
  61. return $model;
  62. }
  63. /**
  64. * 更新
  65. *
  66. * @param $id
  67. * @return mixed|\yii\db\ActiveRecord
  68. * @throws NotFoundHttpException
  69. */
  70. public function actionUpdate($id)
  71. {
  72. $model = $this->findModel($id);
  73. $model->attributes = Yii::$app->request->post();
  74. if (!$model->save()) {
  75. return ResultHelper::json(422, $this->getError($model));
  76. }
  77. return $model;
  78. }
  79. /**
  80. * 删除
  81. *
  82. * @param $id
  83. * @return bool
  84. * @throws NotFoundHttpException
  85. */
  86. public function actionDelete($id)
  87. {
  88. $model = $this->findModel($id);
  89. $model->status = StatusEnum::DELETE;
  90. return $model->save();
  91. }
  92. /**
  93. * 单个显示
  94. *
  95. * @param $id
  96. * @return \yii\db\ActiveRecord
  97. * @throws NotFoundHttpException
  98. */
  99. public function actionView($id)
  100. {
  101. return $this->findModel($id);
  102. }
  103. /**
  104. * @param $id
  105. * @return \yii\db\ActiveRecord
  106. * @throws NotFoundHttpException
  107. */
  108. protected function findModel($id)
  109. {
  110. /* @var $model \yii\db\ActiveRecord */
  111. if (empty($id) || !($model = $this->modelClass::find()->where([
  112. 'id' => $id,
  113. 'status' => StatusEnum::ENABLED,
  114. ])->andFilterWhere(['merchant_id' => $this->getMerchantId()])->one())) {
  115. throw new NotFoundHttpException('请求的数据不存在');
  116. }
  117. return $model;
  118. }
  119. }
粤ICP备19079148号