AuthController.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Components\Helpers;
  4. use App\Components\IP;
  5. use App\Mail\activeUser;
  6. use App\Mail\resetPassword;
  7. use App\Mail\sendVerifyCode;
  8. use App\Models\EmailFilter;
  9. use App\Models\Invite;
  10. use App\Models\User;
  11. use App\Models\UserLoginLog;
  12. use App\Models\Verify;
  13. use App\Models\VerifyCode;
  14. use Auth;
  15. use Cache;
  16. use Captcha;
  17. use Cookie;
  18. use Hash;
  19. use Illuminate\Http\RedirectResponse;
  20. use Illuminate\Http\Request;
  21. use Log;
  22. use Mail;
  23. use Redirect;
  24. use Response;
  25. use Session;
  26. use Str;
  27. use Validator;
  28. /**
  29. * 认证控制器.
  30. *
  31. * Class AuthController
  32. */
  33. class AuthController extends Controller
  34. {
  35. // 登录
  36. public function login(Request $request)
  37. {
  38. if ($request->isMethod('POST')) {
  39. $validator = Validator::make($request->all(), [
  40. 'email' => 'required|email',
  41. 'password' => 'required',
  42. ], [
  43. 'email.required' => trans('auth.email_null'),
  44. 'password.required' => trans('auth.password_null'),
  45. ]);
  46. if ($validator->fails()) {
  47. return Redirect::back()->withInput()->withErrors($validator->errors());
  48. }
  49. // 是否校验验证码
  50. $captcha = $this->check_captcha($request);
  51. if ($captcha !== false) {
  52. return $captcha;
  53. }
  54. // 验证账号并创建会话
  55. if (! Auth::attempt($validator->validated(), $request->input('remember'))) {
  56. return Redirect::back()->withInput()->withErrors(trans('auth.login_error'));
  57. }
  58. $user = Auth::getUser();
  59. if (! $user) {
  60. return Redirect::back()->withInput()->withErrors(trans('auth.login_error'));
  61. }
  62. // 校验普通用户账号状态
  63. if ($user->status < 0) {
  64. Auth::logout(); // 强制销毁会话,因为Auth::attempt的时候会产生会话
  65. return Redirect::back()->withInput()->withErrors(trans('auth.login_ban', ['email' => sysConfig('webmaster_email')]));
  66. }
  67. if ($user->status === 0 && sysConfig('is_activate_account')) {
  68. Auth::logout(); // 强制销毁会话,因为Auth::attempt的时候会产生会话
  69. return Redirect::back()->withInput()->withErrors(trans('auth.active_tip').'<a href="'.route('active').'?email='.$user->email.'" target="_blank"><span style="color:#000">【'.trans('auth.active_account').'】</span></a>');
  70. }
  71. // 写入登录日志
  72. $this->addUserLoginLog($user->id, IP::getClientIp());
  73. // 更新登录信息
  74. $user->update(['last_login' => time()]);
  75. }
  76. // 根据权限跳转
  77. if (Auth::check()) {
  78. if (Auth::getUser()->hasPermissionTo('admin.index')) {
  79. return Redirect::route('admin.index');
  80. }
  81. return Redirect::route('home');
  82. }
  83. return view('auth.login');
  84. }
  85. // 校验验证码
  86. private function check_captcha(Request $request)
  87. {
  88. switch (sysConfig('is_captcha')) {
  89. case 1: // 默认图形验证码
  90. if (! Captcha::check($request->input('captcha'))) {
  91. return Redirect::back()->withInput()->withErrors(trans('auth.captcha_error'));
  92. }
  93. break;
  94. case 2: // Geetest
  95. $validator = Validator::make($request->all(), [
  96. 'geetest_challenge' => 'required|geetest',
  97. ], [
  98. 'geetest' => trans('auth.captcha_fail'),
  99. ]);
  100. if ($validator->fails()) {
  101. return Redirect::back()->withInput()->withErrors(trans('auth.captcha_fail'));
  102. }
  103. break;
  104. case 3: // Google reCAPTCHA
  105. $validator = Validator::make($request->all(), [
  106. 'g-recaptcha-response' => 'required|NoCaptcha',
  107. ]);
  108. if ($validator->fails()) {
  109. return Redirect::back()->withInput()->withErrors(trans('auth.captcha_fail'));
  110. }
  111. break;
  112. case 4: // hCaptcha
  113. $validator = Validator::make($request->all(), [
  114. 'h-captcha-response' => 'required|HCaptcha',
  115. ]);
  116. if ($validator->fails()) {
  117. return Redirect::back()->withInput()->withErrors(trans('auth.captcha_fail'));
  118. }
  119. break;
  120. default: // 不启用验证码
  121. break;
  122. }
  123. return false;
  124. }
  125. /**
  126. * 添加用户登录日志.
  127. *
  128. * @param int $userId 用户ID
  129. * @param string $ip IP地址
  130. */
  131. private function addUserLoginLog(int $userId, string $ip): void
  132. {
  133. $ipLocation = IP::getIPInfo($ip);
  134. if (empty($ipLocation) || empty($ipLocation['country'])) {
  135. Log::warning('获取IP信息异常:'.$ip);
  136. }
  137. $log = new UserLoginLog();
  138. $log->user_id = $userId;
  139. $log->ip = $ip;
  140. $log->country = $ipLocation['country'] ?? '';
  141. $log->province = $ipLocation['province'] ?? '';
  142. $log->city = $ipLocation['city'] ?? '';
  143. $log->county = $ipLocation['county'] ?? '';
  144. $log->isp = $ipLocation['isp'] ?? ($ipLocation['organization'] ?? '');
  145. $log->area = $ipLocation['area'] ?? '';
  146. $log->save();
  147. }
  148. // 退出
  149. public function logout(): RedirectResponse
  150. {
  151. Auth::logout();
  152. return Redirect::route('login');
  153. }
  154. // 注册
  155. public function register(Request $request)
  156. {
  157. $cacheKey = 'register_times_'.md5(IP::getClientIp()); // 注册限制缓存key
  158. if ($request->isMethod('POST')) {
  159. $validator = Validator::make($request->all(), [
  160. 'username' => 'required',
  161. 'email' => 'required|email|unique:user',
  162. 'password' => 'required|min:6',
  163. 'confirmPassword' => 'required|same:password',
  164. 'term' => 'accepted',
  165. ], [
  166. 'username.required' => trans('auth.email_null'),
  167. 'email.required' => trans('auth.email_null'),
  168. 'email.email' => trans('auth.email_legitimate'),
  169. 'email.unique' => trans('auth.email_exist'),
  170. 'password.required' => trans('auth.password_null'),
  171. 'password.min' => trans('auth.password_limit'),
  172. 'confirmPassword.required' => trans('auth.confirm_password'),
  173. 'confirmPassword.same' => trans('auth.password_same'),
  174. 'term.accepted' => trans('auth.unaccepted'),
  175. ]);
  176. if ($validator->fails()) {
  177. return Redirect::back()->withInput()->withErrors($validator->errors());
  178. }
  179. $username = $request->input('username');
  180. $email = $request->input('email');
  181. $password = $request->input('password');
  182. $register_token = $request->input('register_token');
  183. $code = $request->input('code');
  184. $verify_code = $request->input('verify_code');
  185. $aff = (int) $request->input('aff');
  186. // 防止重复提交
  187. if ($register_token !== Session::get('register_token')) {
  188. return Redirect::back()->withInput()->withErrors(trans('auth.repeat_request'));
  189. }
  190. Session::forget('register_token');
  191. // 是否开启注册
  192. if (! sysConfig('is_register')) {
  193. return Redirect::back()->withErrors(trans('auth.register_close'));
  194. }
  195. // 校验域名邮箱黑白名单
  196. if (sysConfig('is_email_filtering')) {
  197. $result = $this->emailChecker($email, 1);
  198. if ($result !== false) {
  199. return $result;
  200. }
  201. }
  202. // 如果需要邀请注册
  203. if (sysConfig('is_invite_register')) {
  204. // 校验邀请码合法性
  205. if ($code) {
  206. if (Invite::whereCode($code)->whereStatus(0)->doesntExist()) {
  207. return Redirect::back()->withInput($request->except('code'))->withErrors(trans('auth.code_error'));
  208. }
  209. } elseif (sysConfig('is_invite_register') == 2) { // 必须使用邀请码
  210. return Redirect::back()->withInput()->withErrors(trans('auth.code_null'));
  211. }
  212. }
  213. // 注册前发送激活码
  214. if (sysConfig('is_activate_account') == 1) {
  215. if (! $verify_code) {
  216. return Redirect::back()->withInput($request->except('verify_code'))->withErrors(trans('auth.captcha_null'));
  217. }
  218. $verifyCode = VerifyCode::whereAddress($email)->whereCode($verify_code)->whereStatus(0)->first();
  219. if (! $verifyCode) {
  220. return Redirect::back()->withInput($request->except('verify_code'))->withErrors(trans('auth.captcha_overtime'));
  221. }
  222. $verifyCode->status = 1;
  223. $verifyCode->save();
  224. }
  225. // 是否校验验证码
  226. $captcha = $this->check_captcha($request);
  227. if ($captcha !== false) {
  228. return $captcha;
  229. }
  230. // 24小时内同IP注册限制
  231. if (sysConfig('register_ip_limit') && Cache::has($cacheKey)) {
  232. $registerTimes = Cache::get($cacheKey);
  233. if ($registerTimes >= sysConfig('register_ip_limit')) {
  234. return Redirect::back()->withInput($request->except('code'))->withErrors(trans('auth.register_anti'));
  235. }
  236. }
  237. // 获取可用端口
  238. $port = Helpers::getPort();
  239. if ($port > sysConfig('max_port')) {
  240. return Redirect::back()->withInput()->withErrors(trans('auth.register_close'));
  241. }
  242. // 获取aff
  243. $affArr = $this->getAff($code, $aff);
  244. $inviter_id = $affArr['inviter_id'];
  245. $transfer_enable = MB * ((int) sysConfig('default_traffic') + ($inviter_id ? (int) sysConfig('referral_traffic') : 0));
  246. // 创建新用户
  247. $user = factory(User::class)->create([
  248. 'username' => $username, 'email' => $email, 'password' => $password, 'transfer_enable' => $transfer_enable, 'inviter_id' => $inviter_id,
  249. ]);
  250. // 注册失败,抛出异常
  251. if (! $user) {
  252. return Redirect::back()->withInput()->withErrors(trans('auth.register_fail'));
  253. }
  254. // 注册次数+1
  255. if (Cache::has($cacheKey)) {
  256. Cache::increment($cacheKey);
  257. } else {
  258. Cache::put($cacheKey, 1, Day); // 24小时
  259. }
  260. // 更新邀请码
  261. if ($affArr['code_id'] && sysConfig('is_invite_register')) {
  262. Invite::find($affArr['code_id'])->update(['invitee_id' => $user->id, 'status' => 1]);
  263. }
  264. // 清除邀请人Cookie
  265. Cookie::unqueue('register_aff');
  266. // 注册后发送激活码
  267. if ((int) sysConfig('is_activate_account') === 2) {
  268. // 生成激活账号的地址
  269. $token = $this->addVerifyUrl($user->id, $email);
  270. $activeUserUrl = route('activeAccount', $token);
  271. $logId = Helpers::addNotificationLog('注册激活', '请求地址:'.$activeUserUrl, 1, $email);
  272. Mail::to($email)->send(new activeUser($logId, $activeUserUrl));
  273. Session::flash('successMsg', trans('auth.register_active_tip'));
  274. } else {
  275. // 则直接给推荐人加流量
  276. if ($inviter_id) {
  277. $referralUser = User::find($inviter_id);
  278. if ($referralUser && $referralUser->expired_at >= date('Y-m-d')) {
  279. $referralUser->incrementData(sysConfig('referral_traffic') * MB);
  280. }
  281. }
  282. if ((int) sysConfig('is_activate_account') === 1) {
  283. $user->update(['status' => 1]);
  284. }
  285. Session::flash('successMsg', trans('auth.register_success'));
  286. }
  287. return Redirect::route('login')->withInput();
  288. }
  289. Session::put('register_token', Str::random());
  290. return view('auth.register', ['emailList' => (int) sysConfig('is_email_filtering') !== 2 ? false : EmailFilter::whereType(2)->get()]);
  291. }
  292. //邮箱检查
  293. private function emailChecker($email, $returnType = 0)
  294. {
  295. $emailFilterList = EmailFilter::whereType(sysConfig('is_email_filtering'))->pluck('words')->toArray();
  296. $emailSuffix = explode('@', $email); // 提取邮箱后缀
  297. switch (sysConfig('is_email_filtering')) {
  298. // 黑名单
  299. case 1:
  300. if (in_array(strtolower($emailSuffix[1]), $emailFilterList, true)) {
  301. if ($returnType) {
  302. return Redirect::back()->withErrors(trans('auth.email_banned'));
  303. }
  304. return Response::json(['status' => 'fail', 'message' => trans('auth.email_banned')]);
  305. }
  306. break;
  307. //白名单
  308. case 2:
  309. if (! in_array(strtolower($emailSuffix[1]), $emailFilterList, true)) {
  310. if ($returnType) {
  311. return Redirect::back()->withErrors(trans('auth.email_invalid'));
  312. }
  313. return Response::json(['status' => 'fail', 'message' => trans('auth.email_invalid')]);
  314. }
  315. break;
  316. default:
  317. if ($returnType) {
  318. return Redirect::back()->withErrors(trans('auth.email_invalid'));
  319. }
  320. return Response::json(['status' => 'fail', 'message' => trans('auth.email_invalid')]);
  321. }
  322. return false;
  323. }
  324. /**
  325. * 获取AFF.
  326. *
  327. * @param string|null $code 邀请码
  328. * @param int|null $aff URL中的aff参数
  329. *
  330. * @return array
  331. */
  332. private function getAff($code = null, $aff = null): array
  333. {
  334. $data = ['inviter_id' => null, 'code_id' => 0]; // 邀请人ID 与 邀请码ID
  335. // 有邀请码先用邀请码,用谁的邀请码就给谁返利
  336. if ($code) {
  337. $inviteCode = Invite::whereCode($code)->whereStatus(0)->first();
  338. if ($inviteCode) {
  339. $data['inviter_id'] = $inviteCode->inviter_id;
  340. $data['code_id'] = $inviteCode->id;
  341. }
  342. }
  343. // 没有用邀请码或者邀请码是管理员生成的,则检查cookie或者url链接
  344. if (! $data['inviter_id']) {
  345. // 检查一下cookie里有没有aff
  346. $cookieAff = \Request::hasCookie('register_aff');
  347. if ($cookieAff) {
  348. $data['inviter_id'] = User::find($cookieAff) ? $cookieAff : null;
  349. } elseif ($aff) { // 如果cookie里没有aff,就再检查一下请求的url里有没有aff,因为有些人的浏览器会禁用了cookie,比如chrome开了隐私模式
  350. $data['inviter_id'] = User::find($aff) ? $aff : null;
  351. }
  352. }
  353. return $data;
  354. }
  355. // 生成申请的请求地址
  356. private function addVerifyUrl($uid, $email)
  357. {
  358. $token = md5(sysConfig('website_name').$email.microtime());
  359. $verify = new Verify();
  360. $verify->user_id = $uid;
  361. $verify->token = $token;
  362. $verify->save();
  363. return $token;
  364. }
  365. // 重设密码页
  366. public function resetPassword(Request $request)
  367. {
  368. if ($request->isMethod('POST')) {
  369. // 校验请求
  370. $validator = Validator::make($request->all(), [
  371. 'email' => 'required|email',
  372. ], [
  373. 'email.required' => trans('auth.email_null'),
  374. 'email.email' => trans('auth.email_legitimate'),
  375. ]);
  376. if ($validator->fails()) {
  377. return Redirect::back()->withInput()->withErrors($validator->errors());
  378. }
  379. $email = $request->input('email');
  380. // 是否开启重设密码
  381. if (! sysConfig('is_reset_password')) {
  382. return Redirect::back()->withErrors(trans('auth.reset_password_close', ['email' => sysConfig('webmaster_email')]));
  383. }
  384. // 查找账号
  385. $user = User::whereEmail($email)->first();
  386. if (! $user) {
  387. return Redirect::back()->withErrors(trans('auth.email_notExist'));
  388. }
  389. // 24小时内重设密码次数限制
  390. $resetTimes = 0;
  391. if (Cache::has('resetPassword_'.md5($email))) {
  392. $resetTimes = Cache::get('resetPassword_'.md5($email));
  393. if ($resetTimes >= sysConfig('reset_password_times')) {
  394. return Redirect::back()->withErrors(trans('auth.reset_password_limit', ['time' => sysConfig('reset_password_times')]));
  395. }
  396. }
  397. // 生成取回密码的地址
  398. $token = $this->addVerifyUrl($user->id, $email);
  399. // 发送邮件
  400. $resetPasswordUrl = route('resettingPasswd', $token);
  401. $logId = Helpers::addNotificationLog('重置密码', '请求地址:'.$resetPasswordUrl, 1, $email);
  402. Mail::to($email)->send(new resetPassword($logId, $resetPasswordUrl));
  403. Cache::put('resetPassword_'.md5($email), $resetTimes + 1, Day);
  404. return Redirect::back()->with('successMsg', trans('auth.reset_password_success_tip'));
  405. }
  406. return view('auth.resetPassword');
  407. }
  408. // 重设密码
  409. public function reset(Request $request, $token)
  410. {
  411. if (! $token) {
  412. return Redirect::route('login');
  413. }
  414. if ($request->isMethod('POST')) {
  415. $validator = Validator::make($request->all(), [
  416. 'password' => 'required|min:6',
  417. 'confirmPassword' => 'required|same:password',
  418. ], [
  419. 'password.required' => trans('auth.password_null'),
  420. 'password.min' => trans('auth.password_limit'),
  421. 'confirmPassword.required' => trans('auth.password_null'),
  422. 'confirmPassword.min' => trans('auth.password_limit'),
  423. 'confirmPassword.same' => trans('auth.password_same'),
  424. ]);
  425. if ($validator->fails()) {
  426. return Redirect::back()->withInput()->withErrors($validator->errors());
  427. }
  428. $password = $request->input('password');
  429. // 校验账号
  430. $verify = Verify::type(1)->whereToken($token)->first();
  431. $user = $verify->user;
  432. if (! $verify) {
  433. return Redirect::route('login');
  434. }
  435. if ($verify->status === 1) {
  436. return Redirect::back()->withErrors(trans('auth.overtime'));
  437. }
  438. if ($user->status < 0) {
  439. return Redirect::back()->withErrors(trans('auth.email_banned'));
  440. }
  441. if (Hash::check($password, $verify->user->password)) {
  442. return Redirect::back()->withErrors(trans('auth.reset_password_same_fail'));
  443. }
  444. // 更新密码
  445. if (! $user->update(['password' => $password])) {
  446. return Redirect::back()->withErrors(trans('auth.reset_password_fail'));
  447. }
  448. // 置为已使用
  449. $verify->status = 1;
  450. $verify->save();
  451. return Redirect::route('login')->with('successMsg', trans('auth.reset_password_new'));
  452. }
  453. $verify = Verify::type(1)->whereToken($token)->first();
  454. if (! $verify) {
  455. return Redirect::route('login');
  456. }
  457. if (time() - strtotime($verify->created_at) >= 1800) {
  458. // 置为已失效
  459. $verify->status = 2;
  460. $verify->save();
  461. }
  462. return view('auth.reset', ['verify' => Verify::type(1)->whereToken($token)->first()]); // 重新获取一遍verify
  463. }
  464. // 激活账号页
  465. public function activeUser(Request $request)
  466. {
  467. if ($request->isMethod('POST')) {
  468. $validator = Validator::make($request->all(), [
  469. 'email' => 'required|email|exists:user,email',
  470. ], [
  471. 'email.required' => trans('auth.email_null'),
  472. 'email.email' => trans('auth.email_legitimate'),
  473. 'email.exists' => trans('auth.email_notExist'),
  474. ]);
  475. if ($validator->fails()) {
  476. return Redirect::back()->withInput()->withErrors($validator->errors());
  477. }
  478. $email = $request->input('email');
  479. // 是否开启账号激活
  480. if (sysConfig('is_activate_account') != 2) {
  481. return Redirect::back()->withInput()->withErrors(trans('auth.active_close', ['email' => sysConfig('webmaster_email')]));
  482. }
  483. // 查找账号
  484. $user = User::whereEmail($email)->firstOrFail();
  485. if ($user->status < 0) {
  486. return Redirect::back()->withErrors(trans('auth.login_ban', ['email' => sysConfig('webmaster_email')]));
  487. }
  488. if ($user->status > 0) {
  489. return Redirect::back()->withErrors(trans('auth.email_normal'));
  490. }
  491. // 24小时内激活次数限制
  492. $activeTimes = 0;
  493. if (Cache::has('activeUser_'.md5($email))) {
  494. $activeTimes = Cache::get('activeUser_'.md5($email));
  495. if ($activeTimes >= sysConfig('active_times')) {
  496. return Redirect::back()->withErrors(trans('auth.active_limit', ['time' => sysConfig('webmaster_email')]));
  497. }
  498. }
  499. // 生成激活账号的地址
  500. $token = $this->addVerifyUrl($user->id, $email);
  501. // 发送邮件
  502. $activeUserUrl = route('activeAccount', $token);
  503. $logId = Helpers::addNotificationLog('激活账号', '请求地址:'.$activeUserUrl, 1, $email);
  504. Mail::to($email)->send(new activeUser($logId, $activeUserUrl));
  505. Cache::put('activeUser_'.md5($email), $activeTimes + 1, Day);
  506. return Redirect::back()->with('successMsg', trans('auth.register_active_tip'));
  507. }
  508. return view('auth.activeUser');
  509. }
  510. // 激活账号
  511. public function active($token)
  512. {
  513. if (! $token) {
  514. return Redirect::route('login');
  515. }
  516. $verify = Verify::type(1)->with('user')->whereToken($token)->first();
  517. $user = $verify->user;
  518. if (! $verify) {
  519. return Redirect::route('login');
  520. }
  521. if (empty($user) || $verify->status > 0) {
  522. Session::flash('errorMsg', trans('auth.overtime'));
  523. return view('auth.active');
  524. }
  525. if ($user->status !== 0) {
  526. Session::flash('errorMsg', trans('auth.email_normal'));
  527. return view('auth.active');
  528. }
  529. if (time() - strtotime($verify->created_at) >= 1800) {
  530. Session::flash('errorMsg', trans('auth.overtime'));
  531. // 置为已失效
  532. $verify->status = 2;
  533. $verify->save();
  534. return view('auth.active');
  535. }
  536. // 更新账号状态
  537. if (! $user->update(['status' => 1])) {
  538. Session::flash('errorMsg', trans('auth.active_fail'));
  539. return Redirect::back();
  540. }
  541. // 置为已使用
  542. $verify->status = 1;
  543. $verify->save();
  544. // 账号激活后给邀请人送流量
  545. $inviter = $user->inviter;
  546. if ($inviter) {
  547. $inviter->incrementData(sysConfig('referral_traffic') * MB);
  548. }
  549. Session::flash('successMsg', trans('auth.active_success'));
  550. return view('auth.active');
  551. }
  552. // 发送注册验证码
  553. public function sendCode(Request $request)
  554. {
  555. $validator = Validator::make($request->all(), [
  556. 'email' => 'required|email|unique:user',
  557. ], [
  558. 'email.required' => trans('auth.email_null'),
  559. 'email.email' => trans('auth.email_legitimate'),
  560. 'email.unique' => trans('auth.email_exist'),
  561. ]);
  562. $email = $request->input('email');
  563. if ($validator->fails()) {
  564. return Response::json(['status' => 'fail', 'message' => $validator->getMessageBag()->first()]);
  565. }
  566. $ip = IP::getClientIP();
  567. // 校验域名邮箱黑白名单
  568. if (sysConfig('is_email_filtering')) {
  569. $result = $this->emailChecker($email);
  570. if ($result !== false) {
  571. return $result;
  572. }
  573. }
  574. // 是否开启注册发送验证码
  575. if ((int) sysConfig('is_activate_account') !== 1) {
  576. return Response::json(['status' => 'fail', 'message' => trans('auth.captcha_close')]);
  577. }
  578. // 防刷机制
  579. if (Cache::has('send_verify_code_'.md5($ip))) {
  580. return Response::json(['status' => 'fail', 'message' => trans('auth.register_anti')]);
  581. }
  582. // 发送邮件
  583. $code = Str::random(6);
  584. $logId = Helpers::addNotificationLog('发送注册验证码', '验证码:'.$code, 1, $email);
  585. Mail::to($email)->send(new sendVerifyCode($logId, $code));
  586. $this->addVerifyCode($email, $code);
  587. Cache::put('send_verify_code_'.md5($ip), $ip, Minute);
  588. return Response::json(['status' => 'success', 'message' => trans('auth.captcha_send')]);
  589. }
  590. // 生成注册验证码
  591. private function addVerifyCode(string $email, string $code): void
  592. {
  593. $verify = new VerifyCode();
  594. $verify->address = $email;
  595. $verify->code = $code;
  596. $verify->save();
  597. }
  598. // 公开的邀请码列表
  599. public function free()
  600. {
  601. return view('auth.free', ['inviteList' => Invite::whereInviterId(null)->whereStatus(0)->paginate()]);
  602. }
  603. // 切换语言
  604. public function switchLang(string $locale): RedirectResponse
  605. {
  606. Session::put('locale', $locale);
  607. return Redirect::back();
  608. }
  609. }