2023-01-14 21:37:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Web;
|
|
|
|
|
2023-02-06 11:27:05 +00:00
|
|
|
use App\Exceptions\CommonException;
|
2023-01-14 21:37:25 +00:00
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Support\RealNameSupport;
|
2023-01-16 20:36:43 +00:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
2023-01-14 21:37:25 +00:00
|
|
|
use Illuminate\Http\Request;
|
2023-01-15 01:25:34 +00:00
|
|
|
use Illuminate\Support\Carbon;
|
2023-01-14 21:37:25 +00:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2023-01-16 20:36:43 +00:00
|
|
|
use Illuminate\View\View;
|
2023-01-14 21:37:25 +00:00
|
|
|
|
|
|
|
class RealNameController extends Controller
|
|
|
|
{
|
2023-01-16 20:36:43 +00:00
|
|
|
public function store(Request $request): RedirectResponse
|
2023-01-14 21:37:25 +00:00
|
|
|
{
|
|
|
|
$request->validate([
|
|
|
|
'real_name' => 'required|string',
|
|
|
|
'id_card' => 'required|string|size:18|unique:users,id_card',
|
|
|
|
]);
|
|
|
|
|
2023-01-15 01:25:34 +00:00
|
|
|
$realNameSupport = new RealNameSupport();
|
|
|
|
|
|
|
|
$birthday = $realNameSupport->getBirthday($request->input('id_card'));
|
|
|
|
// 检查年龄是否在区间内 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')) {
|
2023-01-30 16:14:07 +00:00
|
|
|
$message = '至少需要 '.config('settings.supports.real_name.min_age').' 岁,最大 '.config('settings.supports.real_name.max_age').' 岁。';
|
2023-01-15 01:25:34 +00:00
|
|
|
|
|
|
|
return back()->with('error', $message);
|
|
|
|
}
|
|
|
|
|
2023-01-14 21:37:25 +00:00
|
|
|
$user = $request->user();
|
|
|
|
|
|
|
|
if ($user->real_name_verified_at !== null) {
|
|
|
|
return back()->with('error', '您已经实名认证过了。');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($user->balance < 1) {
|
|
|
|
return back()->with('error', '您的余额不足。请保证余额大于 1 元。');
|
|
|
|
}
|
|
|
|
|
2023-02-06 11:27:05 +00:00
|
|
|
try {
|
|
|
|
$output = $realNameSupport->create($user->id, $request->input('real_name'), $request->input('id_card'));
|
|
|
|
} catch (CommonException $e) {
|
|
|
|
return back()->with('error', $e->getMessage());
|
|
|
|
}
|
2023-01-14 21:37:25 +00:00
|
|
|
|
|
|
|
// 标记用户正在实名,缓存 600s
|
2023-01-30 16:14:07 +00:00
|
|
|
if (Cache::has('real_name:user:'.$user->id)) {
|
2023-01-14 21:37:25 +00:00
|
|
|
// 获取缓存
|
2023-01-30 16:14:07 +00:00
|
|
|
$output = Cache::get('real_name:user:'.$user->id);
|
2023-01-14 21:37:25 +00:00
|
|
|
|
|
|
|
return back()->with('error', '因为您有一个正在进行的实名认证,请等待 10 分钟后重试。')->with('output', $output);
|
|
|
|
}
|
|
|
|
|
2023-01-30 16:14:07 +00:00
|
|
|
Cache::set('real_name:user:'.$user->id, $output, 600);
|
2023-01-14 21:37:25 +00:00
|
|
|
|
|
|
|
return redirect($output);
|
|
|
|
}
|
2023-01-14 23:25:05 +00:00
|
|
|
|
2023-01-16 20:36:43 +00:00
|
|
|
public function create(): View
|
2023-01-14 23:25:05 +00:00
|
|
|
{
|
|
|
|
return view('real_name.create');
|
|
|
|
}
|
2023-01-14 21:37:25 +00:00
|
|
|
}
|