|
|
<?php
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
use App\Services\Sms\ParticipantSmsVerificationService;
|
|
|
use App\Services\Sms\SmsSender;
|
|
|
use Illuminate\Console\Command;
|
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
|
|
class SendSmsTestCommand extends Command
|
|
|
{
|
|
|
protected $signature = 'sms:send-test
|
|
|
{mobile : 接收参赛者登录验证码的手机号}
|
|
|
{--yes : production 环境真实发送时跳过交互确认}
|
|
|
{--allow-disabled : SMS_ENABLED=false 时允许走 disabled sender dry run}';
|
|
|
|
|
|
protected $description = '向指定手机号发送一条参赛者登录验证码短信,用于短信供应商真实联调';
|
|
|
|
|
|
public function handle(ParticipantSmsVerificationService $service, SmsSender $sender): int
|
|
|
{
|
|
|
$mobile = trim((string) $this->argument('mobile'));
|
|
|
if (preg_match('/^1[3-9]\d{9}$/', $mobile) !== 1) {
|
|
|
$this->error('手机号格式无效。');
|
|
|
|
|
|
return self::FAILURE;
|
|
|
}
|
|
|
|
|
|
$smsEnabled = (bool) config('sms.enabled');
|
|
|
if (! $smsEnabled && ! $this->option('allow-disabled')) {
|
|
|
$this->error('SMS_ENABLED=false,默认拒绝执行短信联调;真实发送请设置 SMS_ENABLED=true,dry run 请显式传入 --allow-disabled。');
|
|
|
|
|
|
return self::FAILURE;
|
|
|
}
|
|
|
|
|
|
if ($smsEnabled && app()->environment('production') && ! $this->option('yes')) {
|
|
|
if (! $this->confirm('production 环境将向真实手机号发送短信,是否继续?', false)) {
|
|
|
$this->warn('已取消发送。');
|
|
|
|
|
|
return self::FAILURE;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
$result = $service->sendLoginCode($mobile, $sender);
|
|
|
} catch (ValidationException $exception) {
|
|
|
$this->error(implode(';', collect($exception->errors())->flatten()->all()));
|
|
|
|
|
|
return self::FAILURE;
|
|
|
}
|
|
|
|
|
|
$verification = $result->verification->refresh();
|
|
|
$this->line('verification_id: '.$verification->id);
|
|
|
$this->line('mobile: '.$this->maskMobile($verification->mobile));
|
|
|
$this->line('status: '.$verification->status);
|
|
|
$this->line('provider: '.$verification->provider);
|
|
|
$this->line('provider_code: '.($verification->provider_code ?? ''));
|
|
|
$this->line('provider_request_id: '.($verification->provider_request_id ?? ''));
|
|
|
$this->line('message: '.($verification->provider_message ?: $result->message));
|
|
|
|
|
|
return $result->success ? self::SUCCESS : self::FAILURE;
|
|
|
}
|
|
|
|
|
|
private function maskMobile(string $mobile): string
|
|
|
{
|
|
|
if (strlen($mobile) < 11) {
|
|
|
return '***';
|
|
|
}
|
|
|
|
|
|
return substr($mobile, 0, 3).'****'.substr($mobile, -4);
|
|
|
}
|
|
|
}
|