增加 维护模式,更新 计费系统
This commit is contained in:
parent
e04e164162
commit
b268d28c01
51
app/Console/Commands/System/Down.php
Normal file
51
app/Console/Commands/System/Down.php
Normal 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('请注意,维护模式只会拦截用户的请求,不会影响模块通信。');
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
49
app/Console/Commands/System/Up.php
Normal file
49
app/Console/Commands/System/Up.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -34,6 +34,8 @@ class Kernel extends ConsoleKernel
|
|||||||
UserAddBalance::class,
|
UserAddBalance::class,
|
||||||
GetUser::class,
|
GetUser::class,
|
||||||
ReduceBalance::class,
|
ReduceBalance::class,
|
||||||
|
Commands\System\Down::class,
|
||||||
|
Commands\System\Up::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -213,7 +213,6 @@ public function drops()
|
|||||||
'drops' => $transactions->getDrops($user_id),
|
'drops' => $transactions->getDrops($user_id),
|
||||||
// 'monthly_usages' => (double) Cache::get($cache_key, 0),
|
// 'monthly_usages' => (double) Cache::get($cache_key, 0),
|
||||||
'rate' => config('drops.rate'),
|
'rate' => config('drops.rate'),
|
||||||
'decimal' => config('drops.decimal'),
|
|
||||||
];
|
];
|
||||||
|
|
||||||
return $this->success($resp);
|
return $this->success($resp);
|
||||||
|
31
app/Http/Middleware/Maintenance.php
Normal file
31
app/Http/Middleware/Maintenance.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -138,8 +138,6 @@ public function cost($price = null, $auto = true)
|
|||||||
|
|
||||||
Cache::put($month_cache_key, $hosts_drops, 604800);
|
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);
|
$transaction->reduceDrops($this->user_id, $this->id, $this->module_id, $auto, $this->price);
|
||||||
|
|
||||||
broadcast(new UserEvent($this->user_id, 'balances.drops.reduced', $this->user));
|
broadcast(new UserEvent($this->user_id, 'balances.drops.reduced', $this->user));
|
||||||
|
@ -77,13 +77,11 @@ public function getDrops($user_id = null)
|
|||||||
|
|
||||||
$cache_key = 'user_drops_' . $user_id;
|
$cache_key = 'user_drops_' . $user_id;
|
||||||
|
|
||||||
$decimal = config('drops.decimal');
|
$drops = Cache::get($cache_key, [
|
||||||
|
'drops' => 0,
|
||||||
|
]);
|
||||||
|
|
||||||
$drops = Cache::get($cache_key);
|
return $drops['drops'];
|
||||||
|
|
||||||
$drops = $drops / $decimal;
|
|
||||||
|
|
||||||
return $drops;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function increaseCurrentUserDrops($amount = 0)
|
public function increaseCurrentUserDrops($amount = 0)
|
||||||
@ -96,14 +94,15 @@ public function increaseDrops($user_id, $amount = 0)
|
|||||||
{
|
{
|
||||||
$cache_key = 'user_drops_' . $user_id;
|
$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 $current_drops['drops'];
|
||||||
|
|
||||||
return $drops;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -112,9 +111,15 @@ public function reduceDrops($user_id, $host_id, $module_id, $auto = 1, $amount =
|
|||||||
|
|
||||||
$cache_key = 'user_drops_' . $user_id;
|
$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) {
|
if ($auto) {
|
||||||
$description = '平台按时间自动扣费。';
|
$description = '平台按时间自动扣费。';
|
||||||
@ -122,8 +127,7 @@ public function reduceDrops($user_id, $host_id, $module_id, $auto = 1, $amount =
|
|||||||
$description = '集成模块发起的扣费。';
|
$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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -97,6 +97,7 @@
|
|||||||
|
|
||||||
$app->routeMiddleware([
|
$app->routeMiddleware([
|
||||||
'auth' => App\Http\Middleware\Authenticate::class,
|
'auth' => App\Http\Middleware\Authenticate::class,
|
||||||
|
'maintenance' => App\Http\Middleware\Maintenance::class,
|
||||||
// 'remote' => [
|
// 'remote' => [
|
||||||
// \Illuminate\Routing\Middleware\SubstituteBindings::class,
|
// \Illuminate\Routing\Middleware\SubstituteBindings::class,
|
||||||
// \App\Http\Middleware\AllowCors::class,
|
// \App\Http\Middleware\AllowCors::class,
|
||||||
@ -147,7 +148,7 @@
|
|||||||
$app->router->group([
|
$app->router->group([
|
||||||
'namespace' => 'App\Http\Controllers',
|
'namespace' => 'App\Http\Controllers',
|
||||||
'prefix' => 'api',
|
'prefix' => 'api',
|
||||||
'middleware' => ['auth:api'],
|
'middleware' => ['maintenance', 'auth:api'],
|
||||||
], function ($router) {
|
], function ($router) {
|
||||||
require __DIR__ . '/../routes/api.php';
|
require __DIR__ . '/../routes/api.php';
|
||||||
});
|
});
|
||||||
|
53
database/migrations/2022_10_03_123715_convert_user_drops.php
Normal file
53
database/migrations/2022_10_03_123715_convert_user_drops.php
Normal 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()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user