update: new auth

This commit is contained in:
tokumeikoi
2022-07-28 15:13:17 +08:00
48 changed files with 306 additions and 278 deletions

View File

@ -4,6 +4,7 @@ namespace App\Services;
use App\Models\Plan;
use App\Models\User;
use Illuminate\Support\Facades\DB;
class PlanService
{
@ -18,6 +19,22 @@ 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()
{
return User::select(
DB::raw("plan_id"),
DB::raw("count(*) as count")
)
->where('plan_id', '!=', NULL)
->where(function ($query) {
$query->where('expired_at', '>=', time())
->orWhere('expired_at', NULL);
})
->groupBy("plan_id")
->get()
->keyBy('plan_id');
}
}

View File

@ -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;
}