first
This commit is contained in:
43
app/Rules/AdminG2FARule.php
Normal file
43
app/Rules/AdminG2FARule.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rules;
|
||||
|
||||
use Encore\Admin\Auth\Database\Administrator;
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
|
||||
class AdminG2FARule implements Rule
|
||||
{
|
||||
protected array $formData;
|
||||
|
||||
protected string $errorMessage = '身份校验码 校验失败';
|
||||
|
||||
public function __construct(array $data)
|
||||
{
|
||||
$this->formData = $data;
|
||||
}
|
||||
|
||||
public function passes($attribute, $value): bool
|
||||
{
|
||||
$user = Administrator::where('username', $this->formData['username'])->first();
|
||||
|
||||
if ($user && $user->use_g2fa) {
|
||||
if (blank($value)) {
|
||||
$this->errorMessage = '身份校验码 必须填写';
|
||||
return false;
|
||||
}
|
||||
if (strlen($value) != 6) {
|
||||
$this->errorMessage = '身份校验码 必须是6位';
|
||||
return false;
|
||||
}
|
||||
|
||||
$google2fa = app('pragmarx.google2fa');
|
||||
return $google2fa->verifyGoogle2FA($user->g2fa_secret, $value);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function message(): string
|
||||
{
|
||||
return $this->errorMessage;
|
||||
}
|
||||
}
|
||||
69
app/Rules/IdCardRule.php
Normal file
69
app/Rules/IdCardRule.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rules;
|
||||
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
|
||||
class IdCardRule implements Rule
|
||||
{
|
||||
protected string $errorMessage = '身份证号码 校验错误';
|
||||
|
||||
/**
|
||||
* Notes : 校验
|
||||
*
|
||||
* @Date : 2021/6/23 2:59 下午
|
||||
* @Author : <Jason.C>
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function passes($attribute, $value): bool
|
||||
{
|
||||
if (strlen($value) !== 18) {
|
||||
$this->errorMessage = '请输入18位身份证号码';
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->isIdCard($value);
|
||||
}
|
||||
|
||||
private function isIdCard($id): bool
|
||||
{
|
||||
$id = strtoupper($id);
|
||||
$regx = "/(^\d{17}([0-9]|X)$)/";
|
||||
$arr_split = [];
|
||||
if (! preg_match($regx, $id)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";
|
||||
@preg_match($regx, $id, $arr_split);
|
||||
$dtm_birth = $arr_split[2].'/'.$arr_split[3].'/'.$arr_split[4];
|
||||
if (! strtotime($dtm_birth)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$arr_int = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
|
||||
$arr_ch = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
|
||||
$sign = 0;
|
||||
for ($i = 0; $i < 17; $i++) {
|
||||
$b = (int) $id[$i];
|
||||
$w = $arr_int[$i];
|
||||
$sign += $b * $w;
|
||||
}
|
||||
$n = $sign % 11;
|
||||
$val_num = $arr_ch[$n];
|
||||
|
||||
return ! ($val_num !== substr($id, 17, 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取校验错误信息
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function message(): string
|
||||
{
|
||||
return $this->errorMessage;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user