MasterWokerInfoValidate.php 3.2 KB

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