OnAuthController.php 3.4 KB

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