diff --git a/app/Console/Commands/System/Down.php b/app/Console/Commands/System/Down.php new file mode 100644 index 0000000..cc89ae8 --- /dev/null +++ b/app/Console/Commands/System/Down.php @@ -0,0 +1,51 @@ +addHours(2)); + + $this->info('API 已进入维护模式,将在 2 小时后自动关闭。'); + $this->warn('请注意,维护模式只会拦截用户的请求,不会影响模块通信。'); + + } +} diff --git a/app/Console/Commands/System/Up.php b/app/Console/Commands/System/Up.php new file mode 100644 index 0000000..ff26147 --- /dev/null +++ b/app/Console/Commands/System/Up.php @@ -0,0 +1,49 @@ +info('维护模式已关闭。'); + + // maintenance_mode(false); + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index ebad1ea..521ef96 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -34,6 +34,8 @@ class Kernel extends ConsoleKernel UserAddBalance::class, GetUser::class, ReduceBalance::class, + Commands\System\Down::class, + Commands\System\Up::class, ]; /** diff --git a/app/Http/Controllers/User/BalanceController.php b/app/Http/Controllers/User/BalanceController.php index 9f33b46..43995c6 100644 --- a/app/Http/Controllers/User/BalanceController.php +++ b/app/Http/Controllers/User/BalanceController.php @@ -213,7 +213,6 @@ public function drops() 'drops' => $transactions->getDrops($user_id), // 'monthly_usages' => (double) Cache::get($cache_key, 0), 'rate' => config('drops.rate'), - 'decimal' => config('drops.decimal'), ]; return $this->success($resp); diff --git a/app/Http/Middleware/Maintenance.php b/app/Http/Middleware/Maintenance.php new file mode 100644 index 0000000..82a021f --- /dev/null +++ b/app/Http/Middleware/Maintenance.php @@ -0,0 +1,31 @@ +json([ + 'message' => '我们正在进行维护,请稍等 2 小时后再来。', + ], 503); + } + + + // continue + return $next($request); + } +} diff --git a/app/Models/Host.php b/app/Models/Host.php index da2f6c8..01e2d72 100644 --- a/app/Models/Host.php +++ b/app/Models/Host.php @@ -138,8 +138,6 @@ public function cost($price = null, $auto = true) Cache::put($month_cache_key, $hosts_drops, 604800); - $this->price *= config('drops.decimal'); - $transaction->reduceDrops($this->user_id, $this->id, $this->module_id, $auto, $this->price); broadcast(new UserEvent($this->user_id, 'balances.drops.reduced', $this->user)); diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php index 966a90f..be54f52 100644 --- a/app/Models/Transaction.php +++ b/app/Models/Transaction.php @@ -77,13 +77,11 @@ public function getDrops($user_id = null) $cache_key = 'user_drops_' . $user_id; - $decimal = config('drops.decimal'); + $drops = Cache::get($cache_key, [ + 'drops' => 0, + ]); - $drops = Cache::get($cache_key); - - $drops = $drops / $decimal; - - return $drops; + return $drops['drops']; } public function increaseCurrentUserDrops($amount = 0) @@ -96,14 +94,15 @@ public function increaseDrops($user_id, $amount = 0) { $cache_key = 'user_drops_' . $user_id; - $decimal = config('drops.decimal'); + $current_drops = Cache::get($cache_key, [ + 'drops' => 0, + ]); + $current_drops['drops'] += $amount; - $amount = $amount * $decimal; + Cache::forever($cache_key, $current_drops); - $drops = Cache::increment($cache_key, $amount); - - return $drops; + return $current_drops['drops']; } @@ -112,9 +111,15 @@ public function reduceDrops($user_id, $host_id, $module_id, $auto = 1, $amount = $cache_key = 'user_drops_' . $user_id; - $decimal = config('drops.decimal'); + $current_drops = Cache::get($cache_key, [ + 'drops' => 0, + ]); - Cache::decrement($cache_key, $amount); + $current_drops['drops'] = $current_drops['drops'] - $amount; + + $current_drops['drops'] = round($current_drops['drops'], 5); + + Cache::forever($cache_key, $current_drops); if ($auto) { $description = '平台按时间自动扣费。'; @@ -122,8 +127,7 @@ public function reduceDrops($user_id, $host_id, $module_id, $auto = 1, $amount = $description = '集成模块发起的扣费。'; } - $this->addPayoutDrops($user_id, $amount / $decimal, $description, $host_id, $module_id); - // $this->addPayoutDrops($user_id, $amount, $description, $host_id, $module_id); + $this->addPayoutDrops($user_id, $amount, $description, $host_id, $module_id); } diff --git a/bootstrap/app.php b/bootstrap/app.php index f6bbd59..f5b85bc 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -97,6 +97,7 @@ $app->routeMiddleware([ 'auth' => App\Http\Middleware\Authenticate::class, + 'maintenance' => App\Http\Middleware\Maintenance::class, // 'remote' => [ // \Illuminate\Routing\Middleware\SubstituteBindings::class, // \App\Http\Middleware\AllowCors::class, @@ -147,7 +148,7 @@ $app->router->group([ 'namespace' => 'App\Http\Controllers', 'prefix' => 'api', - 'middleware' => ['auth:api'], + 'middleware' => ['maintenance', 'auth:api'], ], function ($router) { require __DIR__ . '/../routes/api.php'; }); diff --git a/database/migrations/2022_10_03_123715_convert_user_drops.php b/database/migrations/2022_10_03_123715_convert_user_drops.php new file mode 100644 index 0000000..817d5f6 --- /dev/null +++ b/database/migrations/2022_10_03_123715_convert_user_drops.php @@ -0,0 +1,53 @@ +id; + + $drops = Cache::get($cache_key); + + if (!is_array($drops)) { + $drops = [ + 'drops' => $drops, + ]; + } + + $drops = $drops['drops'] / $decimal; + + $drops = [ + 'drops' => $drops, + ]; + + Cache::forever($cache_key, $drops); + } + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +};