AuthController.php 27 KB

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