OauthController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/2/24
  6. * Time: 下午12:48
  7. */
  8. namespace frontendApi\modules\v1\controllers;
  9. use common\helpers\Cache;
  10. use common\helpers\Form;
  11. use common\models\forms\UserForm;
  12. use common\models\UserInfo;
  13. use common\models\UserToken;
  14. use frontendApi\modules\v1\components\UserAuth;
  15. use frontendApi\modules\v1\models\LoginForm;
  16. use Yii;
  17. use frontendApi\modules\v1\models\User;
  18. use yii\web\HttpException;
  19. class OauthController extends BaseController
  20. {
  21. public $modelClass = User::class;
  22. public function actionMenu(){
  23. $menu = require Yii::getAlias('@frontendApi/config/menu.php');
  24. return $this->_childMenu($menu);
  25. }
  26. private function _childMenu($parentArray){
  27. $menuResult = [];
  28. foreach($parentArray as $key => $parentMenu){
  29. // 菜单是否显示
  30. if(isset($parentMenu['show']) && (!$parentMenu['show'] || !$this->_allowDec($parentMenu))){
  31. continue;
  32. }
  33. // 子菜单同样设置
  34. if(isset($parentMenu['child']) && !empty($parentMenu['child'])){
  35. $parentMenu['child'] = $this->_childMenu($parentMenu['child']);
  36. }
  37. $menuResult[] = $parentMenu;
  38. }
  39. return $menuResult;
  40. }
  41. private function _allowDec($item){
  42. if(!isset($item['allow'])){
  43. return true;
  44. }
  45. $isDecReg = Cache::getSystemConfig()['isDecReg']['VALUE'];
  46. if(!$isDecReg) return true;
  47. if(!\Yii::$app->user->id){
  48. return true;
  49. }
  50. $isDec = User::getEnCodeInfo(\Yii::$app->user->id)['IS_DEC'];
  51. if($isDec==1 && $item['allow']=='declarer'){
  52. return true;
  53. }
  54. return false;
  55. }
  56. /**
  57. * 个人信息
  58. * @return mixed
  59. * @throws HttpException
  60. */
  61. public function actionInfo(){
  62. User::updateBaseInfoToRedis(\Yii::$app->user->id);
  63. return static::notice(User::getEnCodeInfo(\Yii::$app->user->id));
  64. }
  65. /**
  66. * 登录是否需要验证码
  67. * @return mixed
  68. * @throws HttpException
  69. */
  70. public function actionIsLoginVerify() {
  71. $userName = Yii::$app->request->post('userName');
  72. $model = new LoginForm(
  73. [
  74. 'userName' =>$userName
  75. ]
  76. );
  77. $isLoginVerify = $model->isLoginVerify();
  78. return static::notice($isLoginVerify ? 1 : 0);
  79. }
  80. /**
  81. * 登录
  82. * @return mixed
  83. * @throws HttpException
  84. * @throws \yii\base\Exception
  85. */
  86. public function actionLogin() {
  87. $userName = preg_replace("/[^A-Z0-9]/", "", Yii::$app->request->post('userName'));
  88. $posts = Yii::$app->request->post();
  89. $posts['userName'] = $userName;
  90. $model = new LoginForm(
  91. [
  92. 'userName' =>$userName
  93. ]
  94. );
  95. if ( $model->isLoginVerify() ) {
  96. $model->scenario = 'loginVerify';
  97. }else {
  98. $model->scenario = 'login';
  99. }
  100. if ($model->load($posts, '') && $model->login()) {
  101. $token = Yii::$app->getUser()->getToken();
  102. return static::notice($token);
  103. } else {
  104. $firstError = $model->getFirstError('LoginForm');
  105. if( $firstError === LoginForm::ERROR_IS_MODIFY_PASSWORD ) {
  106. return static::notice(LoginForm::ERROR_IS_MODIFY_PASSWORD, 403);
  107. }
  108. return static::notice(Form::formatErrorsForApi($model->getErrors()), 401);
  109. }
  110. }
  111. /**
  112. * 用refreshToken刷新accessToken和refreshToken
  113. * @return mixed
  114. * @throws HttpException
  115. */
  116. public function actionRefreshToken(){
  117. $refreshToken = Yii::$app->request->get('refresh-token');
  118. Yii::$app->user->refreshToken($refreshToken);
  119. $token = Yii::$app->getUser()->getToken();
  120. if($token){
  121. return static::notice($token);
  122. } else {
  123. return static::notice('更新Token失败', 401);
  124. }
  125. }
  126. /**
  127. * 用refreshToken刷新accessToken
  128. * @return mixed
  129. * @throws HttpException
  130. */
  131. public function actionRefreshAccessToken(){
  132. $refreshToken = Yii::$app->request->get('refresh-token');
  133. Yii::$app->user->refreshAccessToken($refreshToken);
  134. $token = Yii::$app->getUser()->getToken();
  135. if($token){
  136. return static::notice($token);
  137. } else {
  138. return static::notice('更新Token失败', 401);
  139. }
  140. }
  141. /**
  142. * 用refreshToken刷新refreshToken
  143. * @return mixed
  144. * @throws HttpException
  145. */
  146. public function actionRefreshRefreshToken(){
  147. $refreshToken = Yii::$app->request->get('refresh-token');
  148. Yii::$app->user->refreshRefreshToken($refreshToken);
  149. $token = Yii::$app->getUser()->getToken();
  150. if($token){
  151. return static::notice($token);
  152. } else {
  153. return static::notice('更新Token失败', 401);
  154. }
  155. }
  156. /**
  157. * 后台登录前台
  158. * @return mixed
  159. * @throws HttpException
  160. */
  161. public function actionLoginByBackend(){
  162. if(Yii::$app->user->validateBackendAuth()){
  163. $userId = Yii::$app->request->post('id');
  164. if($result = Yii::$app->user->loginByBackend($userId)){
  165. return static::notice($result);
  166. }
  167. }
  168. return static::notice('非法请求', 400);
  169. }
  170. /**
  171. * @return mixed
  172. * @throws HttpException
  173. */
  174. public function actionNoLoginModifyPassword() {
  175. if(\Yii::$app->request->isPost){
  176. $form = new UserForm();
  177. $form->scenario = 'noLoginModifyPassword';
  178. $post = \Yii::$app->request->post();
  179. if($form->load($post, '') && $result = $form->noLoginModifyPassword()){
  180. return static::notice('密码修改成功');
  181. } else {
  182. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  183. }
  184. }
  185. return static::notice('非法访问', 400);
  186. }
  187. }