LoginController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace App\Http\Controllers\Web\Auth;
  3. use Aix\Sms\Contracts\Smser;
  4. use App\Http\Controllers\Web\WebBaseController;
  5. use App\Services\Auth\AuthService;
  6. use App\Services\Common\GeetestService;
  7. use App\Services\Common\TencentCaptchaService;
  8. use App\Services\Common\SmsService;
  9. use App\Validators\AuthValidatorRequest;
  10. use Closure;
  11. use Illuminate\Support\Facades\Auth;
  12. use Illuminate\Support\Facades\Cache;
  13. class LoginController extends WebBaseController
  14. {
  15. /**
  16. * @var AuthService
  17. */
  18. protected $authService;
  19. /**
  20. * @var SmsService
  21. */
  22. protected $smsService;
  23. /**
  24. * @var GeetestService
  25. */
  26. protected $geetestService;
  27. protected $tencentCaptchaService;
  28. /**
  29. * LoginController constructor.
  30. * @param AuthService $authService
  31. * @param SmsService $smsService
  32. * @param GeetestService $geetestService
  33. */
  34. public function __construct(
  35. AuthService $authService,
  36. SmsService $smsService,
  37. GeetestService $geetestService,
  38. TencentCaptchaService $tencentCaptchaService
  39. ) {
  40. $this->authService = $authService;
  41. $this->smsService = $smsService;
  42. $this->geetestService = $geetestService;
  43. $this->tencentCaptchaService = $tencentCaptchaService;
  44. $this->middleware(function ($request, Closure $next) {
  45. if ($request->route()->getName() != 'logout') {
  46. if (Auth::guard('web-member')->check() || Auth::guard('web-company')->check() || Auth::guard('web-organization')->check()) {
  47. return redirect(route('home'));
  48. }
  49. }
  50. return $next($request);
  51. });
  52. }
  53. public function showLoginForm()
  54. {
  55. if (request()->has('redirect_url')) {
  56. session(['url.intended'=>urldecode(request()->get('redirect_url'))]);
  57. }
  58. return view('app.auth.login');
  59. }
  60. public function showLoginCompanyForm()
  61. {
  62. if (request()->has('redirect_url')) {
  63. session(['url.intended'=>urldecode(request()->get('redirect_url'))]);
  64. }
  65. return view('app.auth.login_company');
  66. }
  67. public function showLoginOrgForm()
  68. {
  69. if (request()->has('redirect_url')) {
  70. session(['url.intended'=>urldecode(request()->get('redirect_url'))]);
  71. }
  72. return view('app.auth.login_org');
  73. }
  74. public function loginByAccount(AuthValidatorRequest $request)
  75. {
  76. // if (!$this->geetestService->checkGeetest(config('aix.system.site_safety.site_vo_code.members_login'))) {
  77. // return $this->sendErrorResponse("验证码不通过,请重新验证", ['is_need_geetest'=>1]);
  78. // }
  79. if(!$this->tencentCaptchaService->check($request->input('randstr'),$request->input('ticket'),$request->ip())){
  80. return $this->sendErrorResponse("验证码不通过,请重新验证");
  81. }
  82. if (!$member=$this->authService->loginByAccount($request->account, $request->password, $request->autoLogin)) {
  83. // $data['is_need_geetest']=$this->geetestService->isNeedGeetest(
  84. // config('aix.system.site_safety.site_vo_code.members_login')
  85. // );
  86. $redis = Cache::getRedis();
  87. $value = $redis->get('user_'.$request->account);
  88. //dd($value);
  89. if(!$value){
  90. $value = 0;
  91. }
  92. if($value >= 5){
  93. $redis->expire('user_'.$request->account,900);
  94. return $this->sendErrorResponse("连续登录失败5次,请15分钟后再尝试!");
  95. }
  96. $redis->set('user_'.$request->account,++$value);
  97. return $this->sendErrorResponse("账号或密码错误或密码已超过90天未修改,请使用找回密码功能");
  98. }
  99. // if(!$this->authService->checkPwdLog($member)){
  100. // return $this->sendErrorResponse("密码已过期,请使用找回密码重新设置密码");
  101. // }
  102. if ($member->status == 0) {
  103. Auth::guard('web-member')->logout();
  104. return $this->sendErrorResponse("你的账号处于封禁状态, 请联系管理员");
  105. }
  106. return $this->sendSuccessResponse([
  107. 'redirect_url'=>url($request->session()
  108. ->pull('url.intended', route('person.index')))]);
  109. }
  110. public function loginBySms(AuthValidatorRequest $request)
  111. {
  112. // if(!$this->tencentCaptchaService->check($request->input('randstr'),$request->input('ticket'),$request->ip())){
  113. // return $this->sendErrorResponse("验证码不通过,请重新验证");
  114. // }
  115. // if (!$this->geetestService->checkGeetest(config('aix.system.site_safety.site_vo_code.members_login'))) {
  116. // return $this->sendErrorResponse("验证码不通过,请重新验证", ['is_need_geetest'=>1]);
  117. // }
  118. if (!$this->smsService->checkAuthSms(
  119. $request->mobile,
  120. Smser::TEMPLATE_AUTH_LOGIN,
  121. $request->code
  122. )) {
  123. // $data['is_need_geetest']=$this->geetestService->isNeedGeetest(
  124. // config('aix.system.site_safety.site_vo_code.members_login')
  125. // );
  126. return $this->sendErrorResponse("短信验证码不对");
  127. }
  128. if (!$member=$this->authService->loginByAccount($request->mobile, '', $request->autoLogin)) {
  129. // $data['is_need_geetest']=$this->geetestService->isNeedGeetest(
  130. // config('aix.system.site_safety.site_vo_code.members_login')
  131. // );
  132. return $this->sendErrorResponse("用户不存在,请先注册账号");
  133. }
  134. if ($member->status == 0) {
  135. Auth::guard('web-member')->logout();
  136. return $this->sendErrorResponse("你的账号处于封禁状态, 请联系管理员");
  137. }
  138. return $this->sendSuccessResponse([
  139. 'redirect_url'=>url($request->session()
  140. ->pull('url.intended', route('person.index')))]);
  141. }
  142. public function loginByCompany(AuthValidatorRequest $request)
  143. {
  144. if(!$this->tencentCaptchaService->check($request->input('randstr'),$request->input('ticket'),$request->ip())){
  145. return $this->sendErrorResponse("验证码不通过,请重新验证");
  146. }
  147. // if (!$this->geetestService->checkGeetest(config('aix.system.site_safety.site_vo_code.members_login'))) {
  148. // return $this->sendErrorResponse("验证码不通过,请重新验证", ['is_need_geetest'=>1]);
  149. // }
  150. if (!$company=$this->authService->loginByCompany(
  151. $request->company_account,
  152. $request->company_password,
  153. $request->autoLogin
  154. )) {
  155. $data['is_need_geetest']=$this->geetestService->isNeedGeetest(
  156. config('aix.system.site_safety.site_vo_code.members_login')
  157. );
  158. $redis = Cache::getRedis();
  159. $value = $redis->get('user_'.$request->account);
  160. //dd($value);
  161. if(!$value){
  162. $value = 0;
  163. }
  164. if($value >= 5){
  165. $redis->expire('user_'.$request->account,900);
  166. return $this->sendErrorResponse("连续登录失败5次,请15分钟后再尝试!", $data);
  167. }
  168. $redis->set('user_'.$request->account,++$value);
  169. return $this->sendErrorResponse("账号或密码错误或密码已超过90天未修改,请使用找回密码功能", $data);
  170. //return $this->sendErrorResponse("账号或密码错误", $data);
  171. }
  172. if ($company->user_status == 0) {
  173. Auth::guard('web-company')->logout();
  174. return $this->sendErrorResponse("你的账号处于封禁状态, 请联系管理员");
  175. }
  176. return $this->sendSuccessResponse([
  177. 'redirect_url'=>url($request->session()
  178. ->pull('url.intended', route('com.index')))]);
  179. }
  180. public function loginByOrg(AuthValidatorRequest $request)
  181. {
  182. if (!$this->geetestService->checkGeetest(config('aix.system.site_safety.site_vo_code.members_login'))) {
  183. return $this->sendErrorResponse("验证码不通过,请重新验证", ['is_need_geetest'=>1]);
  184. }
  185. if (!$organization=$this->authService->loginByOrganization(
  186. $request->org_account,
  187. $request->org_password,
  188. $request->autoLogin
  189. )) {
  190. $data['is_need_geetest']=$this->geetestService->isNeedGeetest(
  191. config('aix.system.site_safety.site_vo_code.members_login')
  192. );
  193. return $this->sendErrorResponse("账号或密码错误", $data);
  194. }
  195. if ($organization->user_status == 0) {
  196. Auth::guard('web-organization')->logout();
  197. return $this->sendErrorResponse("你的账号处于封禁状态, 请联系管理员");
  198. }
  199. return $this->sendSuccessResponse([
  200. 'redirect_url'=>url($request->session()
  201. ->pull('url.intended', route('org.index')))]);
  202. }
  203. public function logout()
  204. {
  205. $this->authService->logout();
  206. return redirect(route('home'));
  207. }
  208. }