74 lines
2.3 KiB
PHP
74 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Services\HxbOtpRelayService;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class HxbOtpVerificationController extends Controller
|
|
{
|
|
public function show(Request $request, string $token, HxbOtpRelayService $service): Response
|
|
{
|
|
$verification = $service->findByFillToken($token);
|
|
$submitted = $request->boolean('submitted')
|
|
|| in_array((string) ($verification->status ?? ''), [
|
|
HxbOtpRelayService::STATUS_READY,
|
|
HxbOtpRelayService::STATUS_CONSUMED,
|
|
], true);
|
|
|
|
return response()
|
|
->view('hxb-otp.form', [
|
|
'token' => $token,
|
|
'verification' => $verification,
|
|
'submitted' => $submitted,
|
|
])
|
|
->header('Cache-Control', 'private, no-store');
|
|
}
|
|
|
|
public function submit(
|
|
Request $request,
|
|
string $token,
|
|
HxbOtpRelayService $service,
|
|
): Response|RedirectResponse {
|
|
$request->validate([
|
|
'code' => ['required', 'digits:6'],
|
|
], [
|
|
'code.required' => '请输入 6 位验证码。',
|
|
'code.digits' => '验证码必须是 6 位数字。',
|
|
]);
|
|
|
|
$verification = $service->submit(
|
|
$token,
|
|
(string) $request->input('code'),
|
|
$request->ip(),
|
|
);
|
|
if ($verification === null) {
|
|
return response()
|
|
->view('hxb-otp.form', [
|
|
'token' => $token,
|
|
'verification' => null,
|
|
'submitted' => false,
|
|
], 404)
|
|
->header('Cache-Control', 'private, no-store');
|
|
}
|
|
if (in_array((string) $verification->status, [
|
|
HxbOtpRelayService::STATUS_READY,
|
|
HxbOtpRelayService::STATUS_CONSUMED,
|
|
], true)) {
|
|
return redirect()
|
|
->route('hxb-otp.form', ['token' => $token, 'submitted' => 1]);
|
|
}
|
|
|
|
return response()
|
|
->view('hxb-otp.form', [
|
|
'token' => $token,
|
|
'verification' => $verification,
|
|
'submitted' => false,
|
|
], 410)
|
|
->header('Cache-Control', 'private, no-store');
|
|
}
|
|
}
|