ClientController.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace backend\modules\oauth2\controllers;
  3. use Yii;
  4. use common\models\base\SearchModel;
  5. use common\traits\MerchantCurd;
  6. use common\models\oauth2\Client;
  7. use common\enums\StatusEnum;
  8. use common\helpers\StringHelper;
  9. use backend\controllers\BaseController;
  10. /**
  11. * 客户端
  12. *
  13. * Class ClientController
  14. * @package backend\modules\member\controllers
  15. * @author jianyan74 <751393839@qq.com>
  16. */
  17. class ClientController extends BaseController
  18. {
  19. use MerchantCurd;
  20. /**
  21. * @var Client
  22. */
  23. public $modelClass = Client::class;
  24. /**
  25. * 首页
  26. *
  27. * @return string
  28. * @throws \yii\web\NotFoundHttpException
  29. */
  30. public function actionIndex()
  31. {
  32. $searchModel = new SearchModel([
  33. 'model' => $this->modelClass,
  34. 'scenario' => 'default',
  35. 'partialMatchAttributes' => ['title', 'client_id'], // 模糊查询
  36. 'defaultOrder' => [
  37. 'id' => SORT_DESC
  38. ],
  39. 'pageSize' => $this->pageSize
  40. ]);
  41. $dataProvider = $searchModel
  42. ->search(Yii::$app->request->queryParams);
  43. $dataProvider->query
  44. ->andWhere(['>=', 'status', StatusEnum::DISABLED])
  45. ->andWhere(['merchant_id' => 0]);
  46. return $this->render($this->action->id, [
  47. 'dataProvider' => $dataProvider,
  48. 'searchModel' => $searchModel,
  49. ]);
  50. }
  51. /**
  52. * ajax编辑/创建
  53. *
  54. * @return mixed|string|\yii\web\Response
  55. * @throws \yii\base\ExitException
  56. */
  57. public function actionAjaxEdit()
  58. {
  59. $id = Yii::$app->request->get('id');
  60. /** @var Client $model */
  61. $model = $this->findModel($id);
  62. // ajax 校验
  63. $this->activeFormValidate($model);
  64. if ($model->load(Yii::$app->request->post())) {
  65. return $model->save()
  66. ? $this->redirect(Yii::$app->request->referrer)
  67. : $this->message($this->getError($model), $this->redirect(Yii::$app->request->referrer), 'error');
  68. }
  69. if ($model->isNewRecord) {
  70. $model->client_id = StringHelper::random(15);
  71. $model->client_secret = StringHelper::random(30);
  72. }
  73. return $this->renderAjax($this->action->id, [
  74. 'model' => $model,
  75. ]);
  76. }
  77. }
粤ICP备19079148号