AuthController.php 26 KB

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