OauthController.php 6.2 KB

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