You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.6 KiB

<?php
namespace App\Console\Commands;
use App\Models\SignupChannel;
use Illuminate\Console\Command;
use Throwable;
class GenerateChannelEncryptionKeys extends Command
{
protected $signature = 'channel:generate-encryption-keys
{--channel= : 渠道 ID 或 channel_code}
{--force : 重新生成已有渠道密钥对,会使已分发的旧公钥失效}';
protected $description = '为报名渠道生成 RSA 非对称加密密钥对';
public function handle(): int
{
$query = SignupChannel::query();
$channel = trim((string) $this->option('channel'));
if ($channel !== '') {
ctype_digit($channel)
? $query->where('id', (int) $channel)
: $query->where('channel_code', $channel);
}
$count = 0;
try {
$query->orderBy('id')->eachById(function (SignupChannel $signupChannel) use (&$count): void {
if ($this->option('force')) {
$signupChannel->encryption_public_key = null;
$signupChannel->encryption_private_key = null;
}
$signupChannel->ensureEncryptionKeyPair();
if ($signupChannel->isDirty()) {
$signupChannel->save();
$count++;
}
});
} catch (Throwable $exception) {
$this->error('生成渠道加密密钥失败:'.$exception->getMessage());
return self::FAILURE;
}
$this->info("已生成或更新 {$count} 个渠道的加密密钥对。");
return self::SUCCESS;
}
}