LoginController.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace App\Http\Controllers\Mobile\Auth;
  3. use Aix\Sms\Contracts\Smser;
  4. use App\Http\Controllers\Mobile\MobileBaseController;
  5. use App\Services\Auth\AuthService;
  6. use App\Services\Common\GeetestService;
  7. use App\Services\Common\SmsService;
  8. use App\Services\CommonService;
  9. use App\Validators\AuthValidatorRequest;
  10. use Closure;
  11. use Illuminate\Support\Facades\Auth;
  12. use Illuminate\Support\Facades\Cache;
  13. class LoginController extends MobileBaseController
  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 $sub_site = '';
  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. )
  39. {
  40. $this->authService = $authService;
  41. $this->smsService = $smsService;
  42. $this->geetestService = $geetestService;
  43. /*$this->middleware(function ($request, Closure $next) {
  44. if (strpos($request->route()->getName(), 'mobile.logout') === false) {
  45. if (Auth::guard('web-member')->check() || Auth::guard('web-company')->check()) {
  46. return redirect(route('mobile.home'));
  47. }
  48. }
  49. return $next($request);
  50. });*/
  51. if (get_subsite_id() > 0) {
  52. $this->sub_site = 'jkq.';
  53. }
  54. }
  55. public function showLoginForm()
  56. {
  57. if (request()->has('redirect_url')) {
  58. session(['url.intended' => urldecode(request()->get('redirect_url'))]);
  59. }
  60. return view('mobile.app.auth.login', ['wap_title' => "个人登录"]);
  61. }
  62. public function showLoginMobileForm()
  63. {
  64. if (request()->has('redirect_url')) {
  65. session(['url.intended' => urldecode(request()->get('redirect_url'))]);
  66. }
  67. return view('mobile.app.auth.login_mobile', ['wap_title' => "个人登录"]);
  68. }
  69. public function showLoginCompanyForm()
  70. {
  71. if (request()->has('redirect_url')) {
  72. session(['url.intended' => urldecode(request()->get('redirect_url'))]);
  73. }
  74. return view('mobile.app.auth.login_company', ['wap_title' => "企业登录"]);
  75. }
  76. public function loginByAccount(AuthValidatorRequest $request)
  77. {
  78. // if (!$this->geetestService->checkGeetest(config('aix.system.site_safety.site_vo_code.members_login'))) {
  79. // return $this->sendErrorResponse("验证码不通过,请重新验证", ['is_need_geetest' => 1]);
  80. // }
  81. if (!$member = $this->authService->loginByAccount($request->account, $request->password, $request->autoLogin)) {
  82. $data['is_need_geetest'] = $this->geetestService->isNeedGeetest(
  83. config('aix.system.site_safety.site_vo_code.members_login')
  84. );
  85. $redis = Cache::getRedis();
  86. $value = $redis->get('user_' . $request->account);
  87. //dd($value);
  88. if (!$value) {
  89. $value = 0;
  90. }
  91. if ($value >= 5) {
  92. $redis->expire('user_' . $request->account, 900);
  93. return $this->sendErrorResponse("连续登录失败5次,请15分钟后再尝试!", $data);
  94. }
  95. $redis->set('user_' . $request->account, ++$value);
  96. return $this->sendErrorResponse("账号或密码错误或密码已超过90天未修改,请使用找回密码功能", $data);
  97. }
  98. if ($member->status == 0) {
  99. Auth::guard('web-member')->logout();
  100. return $this->sendErrorResponse("你的账号处于封禁状态, 请联系管理员");
  101. }
  102. $token = CommonService::createToken(1, $member->id);
  103. return $this->sendSuccessResponse([
  104. 'redirect_url' => url($request->session()
  105. ->pull('url.intended', route($this->sub_site . 'mobile.person.index'))),
  106. 'token' => $token,
  107. ]);
  108. }
  109. public function loginBySms(AuthValidatorRequest $request)
  110. {
  111. // if (!$this->geetestService->checkGeetest(config('aix.system.site_safety.site_vo_code.members_login'))) {
  112. // return $this->sendErrorResponse("验证码不通过,请重新验证", ['is_need_geetest' => 1]);
  113. // }
  114. if (!$this->smsService->checkAuthSms(
  115. $request->mobile,
  116. Smser::TEMPLATE_AUTH_LOGIN,
  117. $request->code
  118. )) {
  119. $data['is_need_geetest'] = $this->geetestService->isNeedGeetest(
  120. config('aix.system.site_safety.site_vo_code.members_login')
  121. );
  122. return $this->sendErrorResponse("短信验证码不对", $data);
  123. }
  124. if (!$member = $this->authService->loginByAccount($request->mobile, '', $request->autoLogin)) {
  125. $data['is_need_geetest'] = $this->geetestService->isNeedGeetest(
  126. config('aix.system.site_safety.site_vo_code.members_login')
  127. );
  128. return $this->sendErrorResponse("用户不存在,请先注册账号", $data);
  129. }
  130. if ($member->status == 0) {
  131. Auth::guard('web-member')->logout();
  132. return $this->sendErrorResponse("你的账号处于封禁状态, 请联系管理员");
  133. }
  134. $token = CommonService::createToken(1, $member->id);
  135. return $this->sendSuccessResponse([
  136. 'redirect_url' => url($request->session()
  137. ->pull('url.intended', route($this->sub_site . 'mobile.person.index'))),
  138. 'token' => $token,
  139. ]);
  140. }
  141. public function loginByCompany(AuthValidatorRequest $request)
  142. {
  143. // if (!$this->geetestService->checkGeetest(config('aix.system.site_safety.site_vo_code.members_login'))) {
  144. // return $this->sendErrorResponse("验证码不通过,请重新验证", ['is_need_geetest' => 1]);
  145. // }
  146. if (!$company = $this->authService->loginByCompany(
  147. $request->company_account,
  148. $request->company_password,
  149. $request->autoLogin
  150. )) {
  151. // $data['is_need_geetest'] = $this->geetestService->isNeedGeetest(
  152. // config('aix.system.site_safety.site_vo_code.members_login')
  153. // );
  154. $redis = Cache::getRedis();
  155. $value = $redis->get('user_' . $request->company_account);
  156. if (!$value) {
  157. $value = 0;
  158. }
  159. if ($value >= 5) {
  160. $redis->expire('user_' . $request->company_account, 900);
  161. return $this->sendErrorResponse("连续登录失败5次,请15分钟后再尝试!");
  162. }
  163. $redis->set('user_' . $request->company_account, ++$value);
  164. return $this->sendErrorResponse("账号或密码错误或密码已超过90天未修改,请使用找回密码功能");
  165. }
  166. if ($company->user_status == 0) {
  167. Auth::guard('web-company')->logout();
  168. return $this->sendErrorResponse("你的账号处于封禁状态, 请联系管理员");
  169. }
  170. $token = CommonService::createToken(2, $company->id);
  171. return $this->sendSuccessResponse([
  172. 'redirect_url' => url($request->session()->pull('url.intended', route($this->sub_site . 'mobile.firm.index'))),
  173. 'token' => $token,
  174. ]);
  175. }
  176. public function logout()
  177. {
  178. $this->authService->logout();
  179. return redirect(route($this->sub_site . 'mobile.home'));
  180. }
  181. public function loginMZT(){
  182. return view('mobile.app.auth.mzt', ['wap_title' => "闽政通快捷登录"]);
  183. }
  184. }