From 6676ccfc9493feda20e5a86c115230700579a6b0 Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Fri, 10 Feb 2023 13:06:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=20=E5=AE=9E=E5=90=8D?= =?UTF-8?q?=E8=AE=A4=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Public/RealNameController.php | 29 ++++++++++++++----- .../Controllers/Web/RealNameController.php | 8 ++++- app/Http/Middleware/Admin/ValidateReferer.php | 2 ++ app/Models/User.php | 21 ++++++++------ app/Support/RealNameSupport.php | 16 +++++----- resources/views/real_name/failed.blade.php | 9 ++++++ resources/views/real_name/success.blade.php | 9 ++++++ routes/public.php | 2 +- 8 files changed, 70 insertions(+), 26 deletions(-) create mode 100644 resources/views/real_name/failed.blade.php create mode 100644 resources/views/real_name/success.blade.php diff --git a/app/Http/Controllers/Public/RealNameController.php b/app/Http/Controllers/Public/RealNameController.php index 8bc641d..5e9589d 100644 --- a/app/Http/Controllers/Public/RealNameController.php +++ b/app/Http/Controllers/Public/RealNameController.php @@ -15,12 +15,32 @@ class RealNameController extends Controller { public function verify(Request $request): JsonResponse { + Log::debug('实名认证回调', $request->all()); + + return $this->validateOrSave($request) + ? $this->success() + : $this->failed(); + } + + public function process(Request $request): View + { + Log::debug('实名认证回调', $request->all()); + + return $this->validateOrSave($request) + ? view('real_name.success') + : view('real_name.failed'); + } + + public function validateOrSave(Request $request): bool + { + Log::debug('实名认证回调', $request->all()); + $result = (new RealNameSupport())->verify($request->all()); if (! $result) { Log::warning('实名认证失败', $request->all()); - return $this->error('实名认证失败。'); + return false; } $user = (new User)->find($result['user_id']); @@ -32,11 +52,6 @@ public function verify(Request $request): JsonResponse $user->notify(new UserNotification('再次欢迎您!', '再次欢迎您!您的实人认证已通过。', true)); - return $this->success('实名认证成功。'); - } - - public function process(): View - { - return view('real_name.process'); + return true; } } diff --git a/app/Http/Controllers/Web/RealNameController.php b/app/Http/Controllers/Web/RealNameController.php index dfc9dd4..a7b26d9 100644 --- a/app/Http/Controllers/Web/RealNameController.php +++ b/app/Http/Controllers/Web/RealNameController.php @@ -5,6 +5,7 @@ use App\Exceptions\CommonException; use App\Http\Controllers\Controller; use App\Support\RealNameSupport; +use Carbon\Exceptions\InvalidFormatException; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Support\Carbon; @@ -22,7 +23,12 @@ public function store(Request $request): RedirectResponse $realNameSupport = new RealNameSupport(); - $birthday = $realNameSupport->getBirthday($request->input('id_card')); + try { + $birthday = $realNameSupport->getBirthday($request->input('id_card')); + } catch (InvalidFormatException) { + return back()->with('error', '身份证号码格式错误。'); + } + // 检查年龄是否在区间内 settings.supports.real_name.min_age ~ settings.supports.real_name.max_age if (Carbon::now()->diffInYears($birthday) < config('settings.supports.real_name.min_age') || Carbon::now()->diffInYears($birthday) > config('settings.supports.real_name.max_age')) { $message = '至少需要 '.config('settings.supports.real_name.min_age').' 岁,最大 '.config('settings.supports.real_name.max_age').' 岁。'; diff --git a/app/Http/Middleware/Admin/ValidateReferer.php b/app/Http/Middleware/Admin/ValidateReferer.php index cdee3d7..5029d29 100644 --- a/app/Http/Middleware/Admin/ValidateReferer.php +++ b/app/Http/Middleware/Admin/ValidateReferer.php @@ -19,6 +19,8 @@ class ValidateReferer */ public function handle(Request $request, Closure $next): mixed { + // return $next($request); + // 如果 referer 不为空,且不是来自本站的请求,则返回 403 if ($request->headers->get('referer') && ! Str::contains($request->headers->get('referer'), config('app.url'))) { abort(403, '来源不属于后台。'); diff --git a/app/Models/User.php b/app/Models/User.php index d9a1cf6..e6ac4ee 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -18,6 +18,7 @@ use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Illuminate\Support\Arr; +use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Crypt; use Illuminate\Support\Str; @@ -110,11 +111,11 @@ protected static function boot() $user->real_name_verified_at = now(); // 更新生日 - try { - $user->birthday_at = $user->getBirthdayFromIdCard(); - } catch (InvalidFormatException) { - $user->birthday_at = null; - } + // try { + // $user->birthday_at = $user->getBirthdayFromIdCard(); + // } catch (InvalidFormatException) { + // $user->birthday_at = null; + // } } } }); @@ -130,16 +131,18 @@ public function hosts(): HasMany return $this->hasMany(Host::class); } - private function getBirthdayFromIdCard(): string + public function getBirthdayFromIdCard(string|null $id_card = null): Carbon { - $idCard = $this->id_card; + if (empty($id_card)) { + $id_card = $this->id_card; + } - $bir = substr($idCard, 6, 8); + $bir = substr($id_card, 6, 8); $year = (int) substr($bir, 0, 4); $month = (int) substr($bir, 4, 2); $day = (int) substr($bir, 6, 2); - return $year.'-'.$month.'-'.$day; + return Carbon::parse($year.'-'.$month.'-'.$day); } public function hasBalance(string $amount = '0.01'): bool diff --git a/app/Support/RealNameSupport.php b/app/Support/RealNameSupport.php index 1630864..9136489 100644 --- a/app/Support/RealNameSupport.php +++ b/app/Support/RealNameSupport.php @@ -3,7 +3,9 @@ namespace App\Support; use App\Exceptions\CommonException; +use App\Models\User; use Illuminate\Http\Client\PendingRequest; +use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; @@ -77,6 +79,8 @@ private function submit(string $id): string 'idName' => $real_name['name'], 'pageTitle' => config('app.display_name').' 实名认证', 'notifyUrl' => route('public.real-name.notify'), + // 'notifyUrl' => 'http://99dsazj8qp.sharedwithexpose.com/public/real_name/notify', + 'procedureType' => 'video', 'txtBgColor' => '#cccccc', @@ -86,6 +90,8 @@ private function submit(string $id): string 'retIdImg' => 'false', 'returnImg' => 'false', 'returnUrl' => route('public.real-name.process'), + // 'returnUrl' => 'http://99dsazj8qp.sharedwithexpose.com/public/real_name/process', + ]; $resp = $this->http->asForm()->post('/edis_ctid_id_name_video_ocr_h5', $data)->json(); @@ -107,8 +113,6 @@ public function verify(array $request): array|bool { $data = json_decode($request['data'], true); - Log::debug('实名认证回调', $request); - $verify = $this->verifyIfSuccess($request['data'], $request['sign']); if (! $verify) { @@ -149,12 +153,8 @@ private function verifyIfSuccess(string $request, string $sign): bool return $flag === 1; } - public function getBirthday(string $id_card): string + public function getBirthday(string $id_card): Carbon { - $year = substr($id_card, 6, 4); - $month = substr($id_card, 10, 2); - $day = substr($id_card, 12, 2); - - return $year.'-'.$month.'-'.$day; + return (new User())->getBirthdayFromIdCard($id_card); } } diff --git a/resources/views/real_name/failed.blade.php b/resources/views/real_name/failed.blade.php new file mode 100644 index 0000000..9dfc3d6 --- /dev/null +++ b/resources/views/real_name/failed.blade.php @@ -0,0 +1,9 @@ +@extends('layouts.app') + +@section('content') +

我们不能确定这是你。

+ +

+ 请尝试重新登录。 +

+@endsection diff --git a/resources/views/real_name/success.blade.php b/resources/views/real_name/success.blade.php new file mode 100644 index 0000000..73fb202 --- /dev/null +++ b/resources/views/real_name/success.blade.php @@ -0,0 +1,9 @@ +@extends('layouts.app') + +@section('content') +

再次欢迎您。

+ +

+ 您已成功完成实人认证,再次欢迎您。 +

+@endsection diff --git a/routes/public.php b/routes/public.php index 884f1f6..3c47575 100644 --- a/routes/public.php +++ b/routes/public.php @@ -8,7 +8,7 @@ use App\Http\Controllers\Public\RealNameController; use Illuminate\Support\Facades\Route; -Route::post('real_name/notify', [RealNameController::class, 'verify'])->name('real-name.notify'); +Route::match(['post', 'get'], 'real_name/notify', [RealNameController::class, 'verify'])->name('real-name.notify'); Route::match(['post', 'get'], 'real_name/process', [RealNameController::class, 'process'])->name('real-name.process'); Route::post('auth_request', [AuthRequestController::class, 'store']);