From e82f28b670d813c774be721014aaf3ba8dc75b36 Mon Sep 17 00:00:00 2001 From: tokumeikoi Date: Sun, 10 Apr 2022 18:08:26 +0800 Subject: [PATCH] update: add client token in cache --- app/Console/Commands/CacheToken.php | 49 +++++++++++++++++++++++++++++ app/Console/Kernel.php | 1 + app/Http/Middleware/Client.php | 12 ++++++- app/Services/OrderService.php | 4 +++ app/Utils/CacheKey.php | 3 +- 5 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 app/Console/Commands/CacheToken.php diff --git a/app/Console/Commands/CacheToken.php b/app/Console/Commands/CacheToken.php new file mode 100644 index 00000000..499fbd59 --- /dev/null +++ b/app/Console/Commands/CacheToken.php @@ -0,0 +1,49 @@ +getAvailableUsers(); + foreach ($users as $user) { + Cache::put(CacheKey::get('SUBSCRIBE_TOKEN', $user->token), 1, 120); + } + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 5d4d5b99..6e22d0e6 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -29,6 +29,7 @@ class Kernel extends ConsoleKernel Cache::put(CacheKey::get('SCHEDULE_LAST_CHECK_AT', null), time()); // v2board $schedule->command('v2board:statistics')->dailyAt('0:10'); + $schedule->command('cache:token')->everyMinute(); // check $schedule->command('check:order')->everyMinute(); $schedule->command('check:commission')->everyMinute(); diff --git a/app/Http/Middleware/Client.php b/app/Http/Middleware/Client.php index 2b1b3fd2..3fb7c3c4 100755 --- a/app/Http/Middleware/Client.php +++ b/app/Http/Middleware/Client.php @@ -2,8 +2,10 @@ namespace App\Http\Middleware; +use App\Utils\CacheKey; use Closure; use App\Models\User; +use Illuminate\Support\Facades\Cache; class Client { @@ -17,7 +19,7 @@ class Client public function handle($request, Closure $next) { $token = $request->input('token'); - if (empty($token)) { + if (empty($token) || $this->tokenNotInCache($token)) { abort(403, 'token is null'); } $user = User::where('token', $token)->first(); @@ -27,4 +29,12 @@ class Client $request->user = $user; return $next($request); } + + private function tokenNotInCache($token) + { + // schedule init complete? + if (!Cache::get(CacheKey::get('SCHEDULE_LAST_CHECK_AT', null))) return true; + if (Cache::get(CacheKey::get('SUBSCRIBE_TOKEN', $token))) return false; + return true; + } } diff --git a/app/Services/OrderService.php b/app/Services/OrderService.php index 354812b5..000d252f 100644 --- a/app/Services/OrderService.php +++ b/app/Services/OrderService.php @@ -6,6 +6,8 @@ use App\Jobs\OrderHandleJob; use App\Models\Order; use App\Models\Plan; use App\Models\User; +use App\Utils\CacheKey; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; class OrderService @@ -79,6 +81,8 @@ class OrderService abort(500, '开通失败'); } + Cache::put(CacheKey::get('SUBSCRIBE_TOKEN', $this->user->token), 1, 120); + DB::commit(); } diff --git a/app/Utils/CacheKey.php b/app/Utils/CacheKey.php index 8babf579..9238a703 100644 --- a/app/Utils/CacheKey.php +++ b/app/Utils/CacheKey.php @@ -19,7 +19,8 @@ class CacheKey 'TEMP_TOKEN' => '临时令牌', 'LAST_SEND_EMAIL_REMIND_TRAFFIC' => '最后发送流量邮件提醒', 'SCHEDULE_LAST_CHECK_AT' => '计划任务最后检查时间', - 'REGISTER_IP_RATE_LIMIT' => '注册频率限制' + 'REGISTER_IP_RATE_LIMIT' => '注册频率限制', + 'SUBSCRIBE_TOKEN' => '订阅TOKEN' ]; public static function get(string $key, $uniqueValue)