AuthController.php 25 KB

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