改进 实名认证

This commit is contained in:
iVampireSP.com 2023-02-10 13:06:42 +08:00
parent 7666c4f7a8
commit 6676ccfc94
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
8 changed files with 70 additions and 26 deletions

View File

@ -15,12 +15,32 @@ class RealNameController extends Controller
{ {
public function verify(Request $request): JsonResponse 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()); $result = (new RealNameSupport())->verify($request->all());
if (! $result) { if (! $result) {
Log::warning('实名认证失败', $request->all()); Log::warning('实名认证失败', $request->all());
return $this->error('实名认证失败。'); return false;
} }
$user = (new User)->find($result['user_id']); $user = (new User)->find($result['user_id']);
@ -32,11 +52,6 @@ public function verify(Request $request): JsonResponse
$user->notify(new UserNotification('再次欢迎您!', '再次欢迎您!您的实人认证已通过。', true)); $user->notify(new UserNotification('再次欢迎您!', '再次欢迎您!您的实人认证已通过。', true));
return $this->success('实名认证成功。'); return true;
}
public function process(): View
{
return view('real_name.process');
} }
} }

View File

@ -5,6 +5,7 @@
use App\Exceptions\CommonException; use App\Exceptions\CommonException;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Support\RealNameSupport; use App\Support\RealNameSupport;
use Carbon\Exceptions\InvalidFormatException;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
@ -22,7 +23,12 @@ public function store(Request $request): RedirectResponse
$realNameSupport = new RealNameSupport(); $realNameSupport = new RealNameSupport();
try {
$birthday = $realNameSupport->getBirthday($request->input('id_card')); $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 // 检查年龄是否在区间内 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')) { 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').' 岁。'; $message = '至少需要 '.config('settings.supports.real_name.min_age').' 岁,最大 '.config('settings.supports.real_name.max_age').' 岁。';

View File

@ -19,6 +19,8 @@ class ValidateReferer
*/ */
public function handle(Request $request, Closure $next): mixed public function handle(Request $request, Closure $next): mixed
{ {
// return $next($request);
// 如果 referer 不为空,且不是来自本站的请求,则返回 403 // 如果 referer 不为空,且不是来自本站的请求,则返回 403
if ($request->headers->get('referer') && ! Str::contains($request->headers->get('referer'), config('app.url'))) { if ($request->headers->get('referer') && ! Str::contains($request->headers->get('referer'), config('app.url'))) {
abort(403, '来源不属于后台。'); abort(403, '来源不属于后台。');

View File

@ -18,6 +18,7 @@
use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Crypt; use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Str; use Illuminate\Support\Str;
@ -110,11 +111,11 @@ protected static function boot()
$user->real_name_verified_at = now(); $user->real_name_verified_at = now();
// 更新生日 // 更新生日
try { // try {
$user->birthday_at = $user->getBirthdayFromIdCard(); // $user->birthday_at = $user->getBirthdayFromIdCard();
} catch (InvalidFormatException) { // } catch (InvalidFormatException) {
$user->birthday_at = null; // $user->birthday_at = null;
} // }
} }
} }
}); });
@ -130,16 +131,18 @@ public function hosts(): HasMany
return $this->hasMany(Host::class); 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); $year = (int) substr($bir, 0, 4);
$month = (int) substr($bir, 4, 2); $month = (int) substr($bir, 4, 2);
$day = (int) substr($bir, 6, 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 public function hasBalance(string $amount = '0.01'): bool

View File

@ -3,7 +3,9 @@
namespace App\Support; namespace App\Support;
use App\Exceptions\CommonException; use App\Exceptions\CommonException;
use App\Models\User;
use Illuminate\Http\Client\PendingRequest; use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
@ -77,6 +79,8 @@ private function submit(string $id): string
'idName' => $real_name['name'], 'idName' => $real_name['name'],
'pageTitle' => config('app.display_name').' 实名认证', 'pageTitle' => config('app.display_name').' 实名认证',
'notifyUrl' => route('public.real-name.notify'), 'notifyUrl' => route('public.real-name.notify'),
// 'notifyUrl' => 'http://99dsazj8qp.sharedwithexpose.com/public/real_name/notify',
'procedureType' => 'video', 'procedureType' => 'video',
'txtBgColor' => '#cccccc', 'txtBgColor' => '#cccccc',
@ -86,6 +90,8 @@ private function submit(string $id): string
'retIdImg' => 'false', 'retIdImg' => 'false',
'returnImg' => 'false', 'returnImg' => 'false',
'returnUrl' => route('public.real-name.process'), '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(); $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); $data = json_decode($request['data'], true);
Log::debug('实名认证回调', $request);
$verify = $this->verifyIfSuccess($request['data'], $request['sign']); $verify = $this->verifyIfSuccess($request['data'], $request['sign']);
if (! $verify) { if (! $verify) {
@ -149,12 +153,8 @@ private function verifyIfSuccess(string $request, string $sign): bool
return $flag === 1; return $flag === 1;
} }
public function getBirthday(string $id_card): string public function getBirthday(string $id_card): Carbon
{ {
$year = substr($id_card, 6, 4); return (new User())->getBirthdayFromIdCard($id_card);
$month = substr($id_card, 10, 2);
$day = substr($id_card, 12, 2);
return $year.'-'.$month.'-'.$day;
} }
} }

View File

@ -0,0 +1,9 @@
@extends('layouts.app')
@section('content')
<h3>我们不能确定这是你。</h3>
<p>
请尝试重新登录。
</p>
@endsection

View File

@ -0,0 +1,9 @@
@extends('layouts.app')
@section('content')
<h3>再次欢迎您。</h3>
<p>
您已成功完成实人认证,再次欢迎您。
</p>
@endsection

View File

@ -8,7 +8,7 @@
use App\Http\Controllers\Public\RealNameController; use App\Http\Controllers\Public\RealNameController;
use Illuminate\Support\Facades\Route; 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::match(['post', 'get'], 'real_name/process', [RealNameController::class, 'process'])->name('real-name.process');
Route::post('auth_request', [AuthRequestController::class, 'store']); Route::post('auth_request', [AuthRequestController::class, 'store']);