MasterWokerInfoValidate.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace app\workerapi\validate;
  3. use app\common\validate\BaseValidate;
  4. /**
  5. * @author 林海涛
  6. * @date 2024/7/10 下午3:41
  7. */
  8. class MasterWokerInfoValidate extends BaseValidate
  9. {
  10. protected $rule = [
  11. 'real_name' => 'require',
  12. 'id_card' => 'require|checkIdCard',
  13. 'id_card_front_img' => 'require',
  14. 'id_card_opposite_img' => 'require',
  15. ];
  16. protected $message = [
  17. 'real_name.require' => '请输入真实姓名',
  18. 'id_card.require' => '请输入身份证号',
  19. 'id_card.checkIdCard' => '身份证格式有误',
  20. 'id_card_front_img.require' => '请上传身份证正面照',
  21. 'id_card_opposite_img.require' => '请上传身份证反面照',
  22. ];
  23. public function checkIdCard($idcard)
  24. {
  25. if(empty($idcard)){
  26. return false;
  27. }else{
  28. $idcard = strtoupper($idcard); # 如果是小写x,转化为大写X
  29. if(strlen($idcard) != 18 && strlen($idcard) != 15){
  30. return false;
  31. }
  32. # 如果是15位身份证,则转化为18位
  33. if(strlen($idcard) == 15){
  34. # 如果身份证顺序码是996 997 998 999,这些是为百岁以上老人的特殊编码
  35. if (array_search(substr($idcard, 12, 3), array('996', '997', '998', '999')) !== false) {
  36. $idcard = substr($idcard, 0, 6) . '18' . substr($idcard, 6, 9);
  37. } else {
  38. $idcard = substr($idcard, 0, 6) . '19' . substr($idcard, 6, 9);
  39. }
  40. # 加权因子
  41. $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
  42. # 校验码对应值
  43. $code = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
  44. $checksum = 0;
  45. for ($i = 0; $i < strlen($idcard); $i++) {
  46. $checksum += substr($idcard, $i, 1) * $factor[$i];
  47. }
  48. $idcard = $idcard . $code[$checksum % 11];
  49. }
  50. # 验证身份证开始
  51. $IDCardBody = substr($idcard, 0, 17); # 身份证主体
  52. $IDCardCode = strtoupper(substr($idcard, 17, 1)); # 身份证最后一位的验证码
  53. # 加权因子
  54. $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
  55. # 校验码对应值
  56. $code = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
  57. $checksum = 0;
  58. for ($i = 0; $i < strlen($IDCardBody); $i++) {
  59. $checksum += substr($IDCardBody, $i, 1) * $factor[$i];
  60. }
  61. $validateIdcard = $code[$checksum % 11]; # 判断身份证是否合理
  62. if($validateIdcard != $IDCardCode){
  63. return false;
  64. }else{
  65. return true;
  66. }
  67. }
  68. }
  69. public function sceneChangeIdCard()
  70. {
  71. return $this->only(['real_name', 'id_card','id_card_front_img','id_card_opposite_img']);
  72. }
  73. }