v2board/app/Http/Controllers/Passport/RegisterController.php

89 lines
2.9 KiB
PHP
Raw Normal View History

2019-10-29 15:33:36 +08:00
<?php
namespace App\Http\Controllers\Passport;
use App\Http\Requests\Passport\RegisterIndex;
use App\Http\Controllers\Controller;
use App\Models\User;
2020-01-04 17:46:53 +08:00
use App\Models\Plan;
2020-01-12 01:27:36 +08:00
use Illuminate\Support\Facades\Cache;
2019-10-29 15:33:36 +08:00
use App\Utils\Helper;
use App\Models\InviteCode;
class RegisterController extends Controller
{
2020-01-11 13:36:52 +08:00
private function setTryOut()
{
2020-01-04 17:46:53 +08:00
}
2020-01-11 13:36:52 +08:00
public function index(RegisterIndex $request)
{
2019-11-18 19:52:30 +08:00
if ((int)config('v2board.stop_register', 0)) {
2019-10-29 15:33:36 +08:00
abort(500, '本站已关闭注册');
}
2019-11-18 19:52:30 +08:00
if ((int)config('v2board.invite_force', 0)) {
2019-10-29 15:33:36 +08:00
if (empty($request->input('invite_code'))) {
abort(500, '必须使用邀请码才可以注册');
}
}
2019-11-18 19:52:30 +08:00
if ((int)config('v2board.email_verify', 0)) {
2019-10-29 15:33:36 +08:00
$redisKey = 'sendEmailVerify:' . $request->input('email');
if (empty($request->input('email_code'))) {
abort(500, '邮箱验证码不能为空');
}
if (Cache::get($redisKey) !== $request->input('email_code')) {
2019-10-29 15:33:36 +08:00
abort(500, '邮箱验证码有误');
}
}
$email = $request->input('email');
$password = $request->input('password');
$exist = User::where('email', $email)->first();
if ($exist) {
abort(500, '邮箱已存在系统中');
}
$user = new User();
$user->email = $email;
$user->password = password_hash($password, PASSWORD_DEFAULT);
$user->v2ray_uuid = Helper::guid(true);
$user->token = Helper::guid();
if ($request->input('invite_code')) {
$inviteCode = InviteCode::where('code', $request->input('invite_code'))
->where('status', 0)
->first();
if (!$inviteCode) {
2019-12-01 21:13:27 +08:00
if ((int)config('v2board.invite_force', 0)) {
2019-10-29 15:33:36 +08:00
abort(500, '邀请码无效');
}
2019-12-01 21:13:27 +08:00
} else {
$user->invite_user_id = $inviteCode->user_id ? $inviteCode->user_id : null;
if (!(int)config('v2board.invite_never_expire', env('V2BOARD_INVITE_NEVER_EXPIRE'))) {
$inviteCode->status = 1;
$inviteCode->save();
}
2019-11-18 18:41:33 +08:00
}
2019-10-29 15:33:36 +08:00
}
2020-01-04 17:46:53 +08:00
// try out
2020-01-04 18:08:05 +08:00
if ((int)config('v2board.try_out_enable', 0)) {
2020-01-04 17:46:53 +08:00
$plan = Plan::find(config('v2board.try_out_plan_id'));
if ($plan) {
2020-01-06 13:48:58 +08:00
$user->transfer_enable = $plan->transfer_enable * 1073741824;
2020-01-04 17:46:53 +08:00
$user->plan_id = $plan->id;
$user->group_id = $plan->group_id;
2020-01-06 13:48:58 +08:00
$user->expired_at = time() + (config('v2board.try_out_hour', 1) * 3600);
2020-01-04 17:46:53 +08:00
}
}
2019-10-29 15:33:36 +08:00
if (!$user->save()) {
abort(500, '注册失败');
}
2019-11-18 19:52:30 +08:00
if ((int)config('v2board.email_verify', 0)) {
Cache::forget($redisKey);
2019-10-29 15:33:36 +08:00
}
return response()->json([
'data' => true
]);
}
}