AuthController.php 24 KB

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