Skip to content

Commit

Permalink
添加身份证认证规则 && 修复命名空间问题
Browse files Browse the repository at this point in the history
  • Loading branch information
forecho committed May 8, 2018
1 parent 8a0dbf7 commit b320f8d
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 10 deletions.
1 change: 1 addition & 0 deletions ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* createTime : 2018/5/4 18:37
* description:
*/
namespace yiier\helpers;

use yii\helpers\ArrayHelper as BaseArrayHelper;

Expand Down
4 changes: 1 addition & 3 deletions DateHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
* description:
*/


namespace common\helpers;

namespace yiier\helpers;

class DateHelper
{
Expand Down
2 changes: 1 addition & 1 deletion MailHelper.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace common\helpers;
namespace yiier\helpers;

/**
* Class MailHelper
Expand Down
2 changes: 1 addition & 1 deletion ModelHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* description:
*/

namespace common\helpers;
namespace yiier\helpers;

class ModelHelper
{
Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,30 @@ if (!ModelHelper::saveAll(Post::tableName(), $rows)) {
}
```

**中国省份证验证**

```php
public function rules()
{
return [
// ...
['id_card', '\yiier\helpers\validators\IdCardValidator'],
// code
];
}
```

**Array Validator**

```php
public function rules()
{
return [
// ...
['product_ids', '\yiier\helpers\validators\ArrayValidator'],
// code
];
}
```

……
4 changes: 2 additions & 2 deletions Security.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

/**
* author : forecho <[email protected]>
* createTime : 2015/12/29 18:37
* description:
*/
namespace common\helpers;

namespace yiier\helpers;

class Security
{
Expand Down
3 changes: 1 addition & 2 deletions Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
* description:
*/

namespace common\helpers;

namespace yiier\helpers;

class Setup
{
Expand Down
2 changes: 1 addition & 1 deletion ArrayValidator.php → validators/ArrayValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* description:
*/

namespace common\helpers;
namespace yiier\helpers\validators;

use yii\validators\Validator;

Expand Down
82 changes: 82 additions & 0 deletions validators/IdCardValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* author : forecho <[email protected]>
* createTime : 2018/05/08 16:21
* description:
*/

namespace yiier\helpers\validators;

use yii\validators\Validator;

class IdCardValidator extends Validator
{
public function validateAttribute($model, $attribute)
{
if (!$this->validationFilterIdCard($model->$attribute)) {
$this->addError($model, $attribute, $this->message ?: '请输入正确的身份证号码');
}
}

private function validationFilterIdCard($idCard)
{
if (strlen($idCard) == 18) {
return $this->idCardChecksum18($idCard);
} elseif ((strlen($idCard) == 15)) {
$idCard = $this->idCard15to18($idCard);
return $this->idCardChecksum18($idCard);
} else {
return false;
}
}

// 计算身份证校验码,根据国家标准GB 11643-1999
private function idCardVerifyNumber($idCardBase)
{
if (strlen($idCardBase) != 17) {
return false;
}
//加权因子
$factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
//校验码对应值
$verifyNumberList = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
$checksum = 0;
for ($i = 0; $i < strlen($idCardBase); $i++) {
$checksum += substr($idCardBase, $i, 1) * $factor[$i];
}
$mod = $checksum % 11;
$verifyNumber = $verifyNumberList[$mod];
return $verifyNumber;
}

// 将15位身份证升级到18位
private function idCard15to18($idCard)
{
if (strlen($idCard) != 15) {
return false;
} else {
// 如果身份证顺序码是996 997 998 999,这些是为百岁以上老人的特殊编码
if (array_search(substr($idCard, 12, 3), ['996', '997', '998', '999']) !== false) {
$idCard = substr($idCard, 0, 6) . '18' . substr($idCard, 6, 9);
} else {
$idCard = substr($idCard, 0, 6) . '19' . substr($idCard, 6, 9);
}
}
$idCard = $idCard . $this->idCardVerifyNumber($idCard);
return $idCard;
}

// 18位身份证校验码有效性检查
private function idCardChecksum18($idCard)
{
if (strlen($idCard) != 18) {
return false;
}
$idCardBase = substr($idCard, 0, 17);
if ($this->idCardVerifyNumber($idCardBase) != strtoupper(substr($idCard, 17, 1))) {
return false;
} else {
return true;
}
}
}

0 comments on commit b320f8d

Please sign in to comment.