update: add client token in cache

This commit is contained in:
tokumeikoi 2022-04-10 18:08:26 +08:00
parent 447ff0f554
commit e82f28b670
5 changed files with 67 additions and 2 deletions

View File

@ -0,0 +1,49 @@
<?php
namespace App\Console\Commands;
use App\Utils\CacheKey;
use Illuminate\Console\Command;
use App\Services\UserService;
use Illuminate\Support\Facades\Cache;
class CacheToken extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'cache:token';
/**
* 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()
{
$userService = new UserService();
$users = $userService->getAvailableUsers();
foreach ($users as $user) {
Cache::put(CacheKey::get('SUBSCRIBE_TOKEN', $user->token), 1, 120);
}
}
}

View File

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

View File

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

View File

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

View File

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