增加 维护模式,更新 计费系统

This commit is contained in:
iVampireSP.com 2022-10-03 13:05:39 +08:00
parent e04e164162
commit b268d28c01
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
9 changed files with 207 additions and 19 deletions

View File

@ -0,0 +1,51 @@
<?php
namespace App\Console\Commands\System;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
class Down extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'down';
/**
* The console command description.
*
* @var string
*/
protected $description = '启动维护模式。';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//
// 记录到缓存(维护最长 2 小时)
Cache::put('system_down', true, now()->addHours(2));
$this->info('API 已进入维护模式,将在 2 小时后自动关闭。');
$this->warn('请注意,维护模式只会拦截用户的请求,不会影响模块通信。');
}
}

View File

@ -0,0 +1,49 @@
<?php
namespace App\Console\Commands\System;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
class Up extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'up';
/**
* The console command description.
*
* @var string
*/
protected $description = '关闭维护模式。';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//
Cache::delete('system_down');
$this->info('维护模式已关闭。');
// maintenance_mode(false);
}
}

View File

@ -34,6 +34,8 @@ class Kernel extends ConsoleKernel
UserAddBalance::class,
GetUser::class,
ReduceBalance::class,
Commands\System\Down::class,
Commands\System\Up::class,
];
/**

View File

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

View File

@ -0,0 +1,31 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Cache;
class Maintenance
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// if cache has system down
if (Cache::has('system_down')) {
return response()->json([
'message' => '我们正在进行维护,请稍等 2 小时后再来。',
], 503);
}
// continue
return $next($request);
}
}

View File

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

View File

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

View File

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

View File

@ -0,0 +1,53 @@
<?php
use App\Models\User;
use Illuminate\Support\Facades\Cache;
use Illuminate\Database\Migrations\Migration;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//
$decimal = config('drops.decimal');
User::chunk(100, function ($users) use ($decimal) {
foreach ($users as $user) {
$cache_key = 'user_drops_' . $user->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()
{
//
}
};