RegisterController.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Http\Controllers\Passport;
  3. use App\Http\Requests\Passport\RegisterIndex;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\User;
  6. use App\Models\Plan;
  7. use Illuminate\Support\Facades\Cache;
  8. use App\Utils\Helper;
  9. use App\Models\InviteCode;
  10. class RegisterController extends Controller
  11. {
  12. private function setTryOut()
  13. {
  14. }
  15. public function index(RegisterIndex $request)
  16. {
  17. if ((int)config('v2board.stop_register', 0)) {
  18. abort(500, '本站已关闭注册');
  19. }
  20. if ((int)config('v2board.invite_force', 0)) {
  21. if (empty($request->input('invite_code'))) {
  22. abort(500, '必须使用邀请码才可以注册');
  23. }
  24. }
  25. if ((int)config('v2board.email_verify', 0)) {
  26. $redisKey = 'sendEmailVerify:' . $request->input('email');
  27. if (empty($request->input('email_code'))) {
  28. abort(500, '邮箱验证码不能为空');
  29. }
  30. if (Cache::get($redisKey) !== $request->input('email_code')) {
  31. abort(500, '邮箱验证码有误');
  32. }
  33. }
  34. $email = $request->input('email');
  35. $password = $request->input('password');
  36. $exist = User::where('email', $email)->first();
  37. if ($exist) {
  38. abort(500, '邮箱已存在系统中');
  39. }
  40. $user = new User();
  41. $user->email = $email;
  42. $user->password = password_hash($password, PASSWORD_DEFAULT);
  43. $user->v2ray_uuid = Helper::guid(true);
  44. $user->token = Helper::guid();
  45. if ($request->input('invite_code')) {
  46. $inviteCode = InviteCode::where('code', $request->input('invite_code'))
  47. ->where('status', 0)
  48. ->first();
  49. if (!$inviteCode) {
  50. if ((int)config('v2board.invite_force', 0)) {
  51. abort(500, '邀请码无效');
  52. }
  53. } else {
  54. $user->invite_user_id = $inviteCode->user_id ? $inviteCode->user_id : null;
  55. if (!(int)config('v2board.invite_never_expire', env('V2BOARD_INVITE_NEVER_EXPIRE'))) {
  56. $inviteCode->status = 1;
  57. $inviteCode->save();
  58. }
  59. }
  60. }
  61. // try out
  62. if ((int)config('v2board.try_out_enable', 0)) {
  63. $plan = Plan::find(config('v2board.try_out_plan_id'));
  64. if ($plan) {
  65. $user->transfer_enable = $plan->transfer_enable * 1073741824;
  66. $user->plan_id = $plan->id;
  67. $user->group_id = $plan->group_id;
  68. $user->expired_at = time() + (config('v2board.try_out_hour', 1) * 3600);
  69. }
  70. }
  71. if (!$user->save()) {
  72. abort(500, '注册失败');
  73. }
  74. if ((int)config('v2board.email_verify', 0)) {
  75. Cache::forget($redisKey);
  76. }
  77. return response()->json([
  78. 'data' => true
  79. ]);
  80. }
  81. }