From 5f573f530628993be3a694b855843b44047d4a95 Mon Sep 17 00:00:00 2001 From: tokumeikoi Date: Thu, 28 Jul 2022 15:05:48 +0800 Subject: [PATCH] update: new auth --- .../Controllers/Passport/AuthController.php | 8 +- app/Http/Routes/PassportRoute.php | 4 +- app/Http/Routes/UserRoute.php | 4 +- app/Services/PlanService.php | 2 +- app/Services/UserService.php | 109 ++++++++++++------ 5 files changed, 82 insertions(+), 45 deletions(-) diff --git a/app/Http/Controllers/Passport/AuthController.php b/app/Http/Controllers/Passport/AuthController.php index fb0f7157..198954ff 100644 --- a/app/Http/Controllers/Passport/AuthController.php +++ b/app/Http/Controllers/Passport/AuthController.php @@ -168,7 +168,6 @@ class AuthController extends Controller 'auth_data' => base64_encode("{$user->email}:{$user->password}") ]; - Helper::setSession($request, $user); $user->last_login_at = time(); $user->save(); @@ -242,10 +241,13 @@ class AuthController extends Controller if ($user->banned) { abort(500, __('Your account has been suspended')); } - Helper::setSession($request, $user); + $data = [ + 'token' => $user->token, + 'auth_data' => base64_encode("{$user->email}:{$user->password}") + ]; Cache::forget($key); return response([ - 'data' => true + 'data' => $data ]); } } diff --git a/app/Http/Routes/PassportRoute.php b/app/Http/Routes/PassportRoute.php index f2d895a2..24080e79 100644 --- a/app/Http/Routes/PassportRoute.php +++ b/app/Http/Routes/PassportRoute.php @@ -14,8 +14,8 @@ class PassportRoute $router->post('/auth/register', 'Passport\\AuthController@register'); $router->post('/auth/login', 'Passport\\AuthController@login'); $router->get ('/auth/token2Login', 'Passport\\AuthController@token2Login'); - $router->post('/auth/forget', 'Passport\\AuthController@forget'); - $router->post('/auth/getTempToken', 'Passport\\AuthController@getTempToken'); + $router->post('/auth/forget', 'Passport\\AuthController@forget'); // TODO: 1.7.0 remove + $router->post('/auth/getTempToken', 'Passport\\AuthController@getTempToken'); // TODO: 1.7.0 remove $router->post('/auth/getQuickLoginUrl', 'Passport\\AuthController@getQuickLoginUrl'); $router->post('/auth/loginWithMailLink', 'Passport\\AuthController@loginWithMailLink'); // Comm diff --git a/app/Http/Routes/UserRoute.php b/app/Http/Routes/UserRoute.php index 01372927..01da8bbf 100644 --- a/app/Http/Routes/UserRoute.php +++ b/app/Http/Routes/UserRoute.php @@ -25,9 +25,7 @@ class UserRoute $router->post('/order/save', 'User\\OrderController@save'); $router->post('/order/checkout', 'User\\OrderController@checkout'); $router->get ('/order/check', 'User\\OrderController@check'); - // TODO: 1.5.6 remove - $router->get ('/order/details', 'User\\OrderController@detail'); - // TODO: 1.5.6 remove + $router->get ('/order/details', 'User\\OrderController@detail'); // TODO: 1.7.0 remove $router->get ('/order/detail', 'User\\OrderController@detail'); $router->get ('/order/fetch', 'User\\OrderController@fetch'); $router->get ('/order/getPaymentMethod', 'User\\OrderController@getPaymentMethod'); diff --git a/app/Services/PlanService.php b/app/Services/PlanService.php index 7f18c951..efd4e449 100644 --- a/app/Services/PlanService.php +++ b/app/Services/PlanService.php @@ -19,7 +19,7 @@ class PlanService { if ($this->plan->capacity_limit === NULL) return true; $count = User::where('plan_id', $this->plan->plan_id)->count(); - return $this->plan->capacity_limit - $count; + return ($this->plan->capacity_limit - $count) > 0; } public static function countActiveUsers() diff --git a/app/Services/UserService.php b/app/Services/UserService.php index a6c402a9..ffa9f398 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -15,48 +15,85 @@ use Illuminate\Support\Facades\DB; class UserService { + private function calcResetDayByMonthFirstDay() + { + $today = date('d'); + $lastDay = date('d', strtotime('last day of +0 months')); + return $lastDay - $today; + } + + private function calcResetDayByExpireDay(int $expiredAt) + { + $day = date('d', $expiredAt); + $today = date('d'); + $lastDay = date('d', strtotime('last day of +0 months')); + if ((int)$day >= (int)$today && (int)$day >= (int)$lastDay) { + return $lastDay - $today; + } + if ((int)$day >= (int)$today) { + return $day - $today; + } else { + return $lastDay - $today + $day; + } + } + + private function calcResetDayByYearFirstDay() + { + $nextYear = strtotime(date("Y-01-01", strtotime('+1 year'))); + return (int)(($nextYear - time()) / 86400); + } + + private function calcResetDayByYearExpiredAt(int $expiredAt) + { + $md = date('m-d', $expiredAt); + $nowYear = strtotime(date("Y-{$md}")); + $nextYear = strtotime('+1 year', $nowYear); + return (int)(($nextYear - time()) / 86400); + } + public function getResetDay(User $user) { if ($user->expired_at <= time() || $user->expired_at === NULL) return null; // if reset method is not reset - if (isset($user->plan->reset_traffic_method) && $user->plan->reset_traffic_method === 2) return null; - - if ((int)config('v2board.reset_traffic_method') === 0 || - (isset($user->plan->reset_traffic_method) && $user->plan->reset_traffic_method === 0)) - { - $day = date('d', $user->expired_at); - $today = date('d'); - $lastDay = date('d', strtotime('last day of +0 months')); - return $lastDay - $today; - } - if ((int)config('v2board.reset_traffic_method') === 1 || - (isset($user->plan->reset_traffic_method) && $user->plan->reset_traffic_method === 1)) - { - $day = date('d', $user->expired_at); - $today = date('d'); - $lastDay = date('d', strtotime('last day of +0 months')); - if ((int)$day >= (int)$today && (int)$day >= (int)$lastDay) { - return $lastDay - $today; + if (!isset($user->plan->reset_traffic_method)) return null; + if ($user->plan->reset_traffic_method === 2) return null; + switch (true) { + case ($user->plan->reset_traffic_method === NULL): { + $resetTrafficMethod = config('v2board.reset_traffic_method', 0); + switch ((int)$resetTrafficMethod) { + // month first day + case 0: + return $this->calcResetDayByMonthFirstDay(); + // expire day + case 1: + return $this->calcResetDayByExpireDay($user->expired_at); + // no action + case 2: + return null; + // year first day + case 3: + return $this->calcResetDayByYearFirstDay(); + // year expire day + case 4: + return $this->calcResetDayByYearExpiredAt($user->expired_at); + } + break; } - if ((int)$day >= (int)$today) { - return $day - $today; - } else { - return $lastDay - $today + $day; + case ($user->plan->reset_traffic_method === 0): { + return $this->calcResetDayByMonthFirstDay(); + } + case ($user->plan->reset_traffic_method === 1): { + return $this->calcResetDayByExpireDay($user->expired_at); + } + case ($user->plan->reset_traffic_method === 2): { + return null; + } + case ($user->plan->reset_traffic_method === 3): { + return $this->calcResetDayByYearFirstDay(); + } + case ($user->plan->reset_traffic_method === 4): { + return $this->calcResetDayByYearExpiredAt($user->expired_at); } - } - if ((int)config('v2board.reset_traffic_method') === 3 || - (isset($user->plan->reset_traffic_method) && $user->plan->reset_traffic_method === 3)) - { - $nextYear = strtotime(date("Y-01-01", strtotime('+1 year'))); - return (int)(($nextYear - time()) / 86400); - } - if ((int)config('v2board.reset_traffic_method') === 4 || - (isset($user->plan->reset_traffic_method) && $user->plan->reset_traffic_method === 4)) - { - $md = date('m-d', $user->expired_at); - $nowYear = strtotime(date("Y-{$md}")); - $nextYear = strtotime('+1 year', $nowYear); - return (int)(($nextYear - time()) / 86400); } return null; }