OauthController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/2/24
  6. * Time: 下午12:48
  7. */
  8. namespace backendApi\modules\v1\controllers;
  9. use backendApi\modules\v1\components\UserAuth;
  10. use backendApi\modules\v1\models\AdminForm;
  11. use backendApi\modules\v1\models\LoginForm;
  12. use backendApi\modules\v1\models\User;
  13. use common\helpers\Form;
  14. use common\helpers\LoggerTool;
  15. use Yii;
  16. use yii\web\HttpException;
  17. class OauthController extends BaseController
  18. {
  19. public $modelClass = User::class;
  20. public function actionMenu(){
  21. $menu = require Yii::getAlias('@backendApi/config/menu.php');
  22. return $this->_childMenu($menu);
  23. }
  24. private function _childMenu($parentArray){
  25. $menuResult = [];
  26. foreach($parentArray as $key => $parentMenu){
  27. // 菜单是否显示
  28. if(isset($parentMenu['show']) && !$parentMenu['show']){
  29. continue;
  30. }
  31. // 子菜单同样设置
  32. if(isset($parentMenu['child']) && !empty($parentMenu['child'])){
  33. $parentMenu['child'] = $this->_childMenu($parentMenu['child']);
  34. }
  35. $menuResult[] = $parentMenu;
  36. }
  37. return $menuResult;
  38. }
  39. public function actionInfo(){
  40. $userInfo = User::find()->where(['ID'=>Yii::$app->user->id])->asArray()->one();
  41. unset($userInfo['PASSWORD_HASH'], $userInfo['PASSWORD_RESET_TOKEN'], $userInfo['AUTH_KEY']);
  42. return static::notice($userInfo);
  43. }
  44. /**
  45. * 登录
  46. * @return mixed
  47. * @throws HttpException
  48. * @throws \yii\base\Exception
  49. */
  50. public function actionLogin() {
  51. $model = new LoginForm();
  52. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  53. LoggerTool::debug(json_encode(['actionLoginSuccess', Yii::$app->request->post(), $_SERVER]));
  54. $token = Yii::$app->getUser()->getToken();
  55. return static::notice($token);
  56. } else {
  57. LoggerTool::debug(json_encode(['actionLoginFailed', Yii::$app->request->post(), $model->getErrors(), $_SERVER]));
  58. $firstError = $model->getFirstError('LoginForm');
  59. if( $firstError === LoginForm::ERROR_IS_MODIFY_PASSWORD ) {
  60. return static::notice(LoginForm::ERROR_IS_MODIFY_PASSWORD, 403);
  61. }
  62. return static::notice(Form::formatErrorsForApi($model->getErrors()), 401);
  63. }
  64. }
  65. public function actionNoLoginModifyPassword() {
  66. $form = new AdminForm();
  67. $form->scenario = 'noLoginModifyPassword';
  68. if(Yii::$app->request->isPost && $form->load(Yii::$app->request->post(), '') && $result = $form->edit()){
  69. // Log::adminHandle('管理员'.$result->ADMIN_NAME.'重置密码');
  70. return static::notice('重置密码成功');
  71. } else {
  72. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  73. }
  74. }
  75. /**
  76. * 用refreshToken刷新accessToken和refreshToken
  77. * @return mixed
  78. * @throws HttpException
  79. */
  80. public function actionRefreshToken(){
  81. $refreshToken = Yii::$app->request->get('refresh-token');
  82. Yii::$app->user->refreshToken($refreshToken);
  83. $token = Yii::$app->getUser()->getToken();
  84. if($token){
  85. return static::notice($token);
  86. } else {
  87. return static::notice('更新Token失败', 401);
  88. }
  89. }
  90. /**
  91. * 用refreshToken刷新accessToken
  92. * @return mixed
  93. * @throws HttpException
  94. */
  95. public function actionRefreshAccessToken(){
  96. $refreshToken = Yii::$app->request->get('refresh-token');
  97. Yii::$app->user->refreshAccessToken($refreshToken);
  98. $token = Yii::$app->getUser()->getToken();
  99. if($token){
  100. return static::notice($token);
  101. } else {
  102. return static::notice('更新Token失败', 401);
  103. }
  104. }
  105. /**
  106. * 用refreshToken刷新refreshToken
  107. * @return mixed
  108. * @throws HttpException
  109. */
  110. public function actionRefreshRefreshToken(){
  111. $refreshToken = Yii::$app->request->get('refresh-token');
  112. Yii::$app->user->refreshRefreshToken($refreshToken);
  113. $token = Yii::$app->getUser()->getToken();
  114. if($token){
  115. return static::notice($token);
  116. } else {
  117. return static::notice('更新Token失败', 401);
  118. }
  119. }
  120. public function actionSendEmailCode()
  121. {
  122. $adminName = Yii::$app->request->post('adminName');
  123. // 发送邮箱验证码
  124. $result = UserAuth::sendEmailCode($adminName);
  125. if ($result['code'] == 200) {
  126. return static::notice($result['message']);
  127. } else {
  128. return static::notice($result['message'], 401);
  129. }
  130. }
  131. }