AuthController.php 27 KB

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