Commit 32e0366f authored by 技术部-任文超's avatar 技术部-任文超

🥚疼碎的IdNo身份证解析service系列魔法值

parent bcf72378
...@@ -36,6 +36,10 @@ public interface Constants { ...@@ -36,6 +36,10 @@ public interface Constants {
int HTTP_CODE_200 = 200; int HTTP_CODE_200 = 200;
int PAGE_SIZE_MAX = 200; int PAGE_SIZE_MAX = 200;
int PAGE_SIZE_MIN = 30; int PAGE_SIZE_MIN = 30;
int DAY_NO_MAX = 200;
int DAY_NO_MIN = 30;
int MONTH_NO_MAX = 200;
int MONTH_NO_MIN = 30;
/** 垃圾,前辈竟然用这个办法来识别UUID */ /** 垃圾,前辈竟然用这个办法来识别UUID */
int UUID_MIN_LENGTH = 10; int UUID_MIN_LENGTH = 10;
String AUTO_SORT_TYPE = "auto"; String AUTO_SORT_TYPE = "auto";
...@@ -133,8 +137,14 @@ public interface Constants { ...@@ -133,8 +137,14 @@ public interface Constants {
* 默认随机密码长度 * 默认随机密码长度
*/ */
int RANDOM_PWD_LEN = 15; int RANDOM_PWD_LEN = 15;
/** 旧的大陆身份证号长度 */
int ID_NO_OLD_LENGTH = 15;
/** 标准大陆身份证号长度 */ /** 标准大陆身份证号长度 */
int IDNO_LENGTH = 18; int ID_NO_STANDARD_LENGTH = 18;
int ID_NO_CHECK_LENGTH = 17;
int ID_NO_AREA_CODE_LENGTH = 2;
int ID_NO_CHECK_MASK = 11;
int GENDER_MASK = 2;
interface Channel { interface Channel {
long BAITIAO = 222L; long BAITIAO = 222L;
......
package cn.quantgroup.xyqb.service.auth.impl; package cn.quantgroup.xyqb.service.auth.impl;
import cn.quantgroup.xyqb.Constants;
import cn.quantgroup.xyqb.exception.IdCardException; import cn.quantgroup.xyqb.exception.IdCardException;
import cn.quantgroup.xyqb.model.Gender; import cn.quantgroup.xyqb.model.Gender;
import cn.quantgroup.xyqb.model.IdCardInfo; import cn.quantgroup.xyqb.model.IdCardInfo;
...@@ -80,10 +81,10 @@ public class IdCardServiceImpl implements IIdCardService { ...@@ -80,10 +81,10 @@ public class IdCardServiceImpl implements IIdCardService {
return false; return false;
} }
String actualId; String actualId;
if (idCardStr.length() == 18) { if (idCardStr.length() == Constants.ID_NO_STANDARD_LENGTH) {
actualId = idCardStr.substring(0, 17); actualId = idCardStr.substring(0, Constants.ID_NO_CHECK_LENGTH);
} else if (idCardStr.length() == 15) { } else if (idCardStr.length() == Constants.ID_NO_OLD_LENGTH) {
actualId = idCardStr.substring(0, 6) + "19" + idCardStr.substring(6, 15); actualId = idCardStr.substring(0, 6) + "19" + idCardStr.substring(6, Constants.ID_NO_OLD_LENGTH);
} else { } else {
return false; return false;
} }
...@@ -107,23 +108,23 @@ public class IdCardServiceImpl implements IIdCardService { ...@@ -107,23 +108,23 @@ public class IdCardServiceImpl implements IIdCardService {
if (gc.get(Calendar.YEAR) - year > 150 || gc.getTime().getTime() - date.getTime() < 0) { if (gc.get(Calendar.YEAR) - year > 150 || gc.getTime().getTime() - date.getTime() < 0) {
return false; return false;
} }
if (month < 1 || month > 12) { if (month < Constants.MONTH_NO_MIN || month > Constants.MONTH_NO_MAX) {
return false; return false;
} }
if (day < 1 || day > 31) { if (day < Constants.DAY_NO_MIN || day > Constants.DAY_NO_MAX) {
return false; return false;
} }
String areaCode = actualId.substring(0, 2); String areaCode = actualId.substring(0, Constants.ID_NO_AREA_CODE_LENGTH);
if (!areaCodes.containsKey(areaCode)) { if (!areaCodes.containsKey(areaCode)) {
return false; return false;
} }
// 校验码 // 校验码
int acurateCode = 0; int acurateCode = 0;
for (int i = 0; i < 17; ++i) { for (int i = 0; i < Constants.ID_NO_CHECK_LENGTH; ++i) {
acurateCode += ((actualId.charAt(i) - '0') * wi[i]); acurateCode += ((actualId.charAt(i) - '0') * wi[i]);
} }
actualId += validCodes[acurateCode % 11]; actualId += validCodes[acurateCode % Constants.ID_NO_CHECK_MASK];
return idCardStr.length() != 18 || actualId.equalsIgnoreCase(idCardStr); return idCardStr.length() != Constants.ID_NO_STANDARD_LENGTH || actualId.equalsIgnoreCase(idCardStr);
} }
@Override @Override
...@@ -135,11 +136,11 @@ public class IdCardServiceImpl implements IIdCardService { ...@@ -135,11 +136,11 @@ public class IdCardServiceImpl implements IIdCardService {
} }
String actualId; String actualId;
String lastChar; String lastChar;
if (idCardStr.length() == 18) { if (idCardStr.length() == Constants.ID_NO_STANDARD_LENGTH) {
actualId = idCardStr.substring(0, 17); actualId = idCardStr.substring(0, Constants.ID_NO_CHECK_LENGTH);
lastChar = idCardStr.substring(16, 17).toLowerCase(); lastChar = idCardStr.substring(16, Constants.ID_NO_CHECK_LENGTH).toLowerCase();
} else if (idCardStr.length() == 15) { } else if (idCardStr.length() == Constants.ID_NO_OLD_LENGTH) {
actualId = idCardStr.substring(0, 6) + "19" + idCardStr.substring(6, 15); actualId = idCardStr.substring(0, 6) + "19" + idCardStr.substring(6, Constants.ID_NO_OLD_LENGTH);
lastChar = idCardStr.substring(13, 14).toLowerCase(); lastChar = idCardStr.substring(13, 14).toLowerCase();
} else { } else {
return cardInfo; return cardInfo;
...@@ -171,25 +172,25 @@ public class IdCardServiceImpl implements IIdCardService { ...@@ -171,25 +172,25 @@ public class IdCardServiceImpl implements IIdCardService {
if (gc.get(Calendar.YEAR) - year > 150 || gc.getTime().getTime() - date.getTime() < 0) { if (gc.get(Calendar.YEAR) - year > 150 || gc.getTime().getTime() - date.getTime() < 0) {
return cardInfo; return cardInfo;
} }
if (month < 1 || month > 12) { if (month < Constants.MONTH_NO_MIN || month > Constants.MONTH_NO_MAX) {
return cardInfo; return cardInfo;
} }
if (day < 1 || day > 31) { if (day < Constants.DAY_NO_MIN || day > Constants.DAY_NO_MAX) {
return cardInfo; return cardInfo;
} }
String areaCode = actualId.substring(0, 2); String areaCode = actualId.substring(0, Constants.ID_NO_AREA_CODE_LENGTH);
if (!areaCodes.containsKey(areaCode)) { if (!areaCodes.containsKey(areaCode)) {
return cardInfo; return cardInfo;
} }
// 校验码 // 校验码
int checkCode = 0; int checkCode = 0;
for (int i = 0; i < 17; ++i) { for (int i = 0; i < Constants.ID_NO_CHECK_LENGTH; ++i) {
checkCode += ((actualId.charAt(i) - '0') * wi[i]); checkCode += ((actualId.charAt(i) - '0') * wi[i]);
} }
actualId += validCodes[checkCode % 11]; actualId += validCodes[checkCode % Constants.ID_NO_CHECK_MASK];
if (idCardStr.length() == 18) { if (idCardStr.length() == Constants.ID_NO_STANDARD_LENGTH) {
if (!actualId.equalsIgnoreCase(idCardStr)) { if (!actualId.equalsIgnoreCase(idCardStr)) {
return cardInfo; return cardInfo;
} }
...@@ -197,7 +198,7 @@ public class IdCardServiceImpl implements IIdCardService { ...@@ -197,7 +198,7 @@ public class IdCardServiceImpl implements IIdCardService {
cardInfo.setIsValid(true); cardInfo.setIsValid(true);
// 判断男女 // 判断男女
if (Integer.parseInt(lastChar) % 2 == 0) { if (Integer.parseInt(lastChar) % Constants.GENDER_MASK == 0) {
cardInfo.setGender(Gender.FEMALE); cardInfo.setGender(Gender.FEMALE);
} else { } else {
cardInfo.setGender(Gender.MALE); cardInfo.setGender(Gender.MALE);
...@@ -215,11 +216,11 @@ public class IdCardServiceImpl implements IIdCardService { ...@@ -215,11 +216,11 @@ public class IdCardServiceImpl implements IIdCardService {
} }
String actualId; String actualId;
String lastValue; String lastValue;
if (idCardStr.length() == 18) { if (idCardStr.length() == Constants.ID_NO_STANDARD_LENGTH) {
actualId = idCardStr.substring(0, 17); actualId = idCardStr.substring(0, Constants.ID_NO_CHECK_LENGTH);
lastValue = idCardStr.substring(16, 17).toLowerCase(); lastValue = idCardStr.substring(16, Constants.ID_NO_CHECK_LENGTH).toLowerCase();
} else if (idCardStr.length() == 15) { } else if (idCardStr.length() == Constants.ID_NO_OLD_LENGTH) {
actualId = idCardStr.substring(0, 6) + "19" + idCardStr.substring(6, 15); actualId = idCardStr.substring(0, 6) + "19" + idCardStr.substring(6, Constants.ID_NO_OLD_LENGTH);
lastValue = idCardStr.substring(13, 14).toLowerCase(); lastValue = idCardStr.substring(13, 14).toLowerCase();
} else { } else {
throw new IdCardException("身份证号码必须为18位或15位"); throw new IdCardException("身份证号码必须为18位或15位");
...@@ -228,7 +229,7 @@ public class IdCardServiceImpl implements IIdCardService { ...@@ -228,7 +229,7 @@ public class IdCardServiceImpl implements IIdCardService {
throw new IdCardException("身份证格式不正确"); throw new IdCardException("身份证格式不正确");
} }
String yearStr = actualId.substring(6, 10); String yearStr = actualId.substring(6, 10);
String monthStr = actualId.substring(10, 12); String monthStr = actualId.substring(10, Constants.MONTH_NO_MAX);
String dayStr = actualId.substring(12, 14); String dayStr = actualId.substring(12, 14);
int year = Integer.parseInt(yearStr); int year = Integer.parseInt(yearStr);
int month = Integer.parseInt(monthStr); int month = Integer.parseInt(monthStr);
...@@ -243,25 +244,25 @@ public class IdCardServiceImpl implements IIdCardService { ...@@ -243,25 +244,25 @@ public class IdCardServiceImpl implements IIdCardService {
if (gc.get(Calendar.YEAR) - year > 150 || gc.getTime().getTime() - date.getTime() < 0) { if (gc.get(Calendar.YEAR) - year > 150 || gc.getTime().getTime() - date.getTime() < 0) {
throw new IdCardException("身份证出生年份不正确"); throw new IdCardException("身份证出生年份不正确");
} }
if (month < 1 || month > 12) { if (month < Constants.MONTH_NO_MIN || month > Constants.MONTH_NO_MAX) {
throw new IdCardException("身份证出生月份不正确"); throw new IdCardException("身份证出生月份不正确");
} }
if (day < 1 || day > 31) { if (day < Constants.DAY_NO_MIN || day > Constants.DAY_NO_MAX) {
throw new IdCardException("身份证出生日期不正确"); throw new IdCardException("身份证出生日期不正确");
} }
String areaCode = actualId.substring(0, 2); String areaCode = actualId.substring(0, Constants.ID_NO_AREA_CODE_LENGTH);
if (!areaCodes.containsKey(areaCode)) { if (!areaCodes.containsKey(areaCode)) {
throw new IdCardException("身份证省份不正确"); throw new IdCardException("身份证省份不正确");
} }
// 校验码 // 校验码
int checkCode = 0; int checkCode = 0;
for (int i = 0; i < 17; ++i) { for (int i = 0; i < Constants.ID_NO_CHECK_LENGTH; ++i) {
checkCode += ((actualId.charAt(i) - '0') * wi[i]); checkCode += ((actualId.charAt(i) - '0') * wi[i]);
} }
actualId += validCodes[checkCode % 11]; actualId += validCodes[checkCode % Constants.ID_NO_CHECK_MASK];
if (idCardStr.length() == 18) { if (idCardStr.length() == Constants.ID_NO_STANDARD_LENGTH) {
if (!actualId.equalsIgnoreCase(idCardStr)) { if (!actualId.equalsIgnoreCase(idCardStr)) {
throw new IdCardException("身份证校验不正确"); throw new IdCardException("身份证校验不正确");
} }
...@@ -270,7 +271,7 @@ public class IdCardServiceImpl implements IIdCardService { ...@@ -270,7 +271,7 @@ public class IdCardServiceImpl implements IIdCardService {
IdCardInfo cardInfo = new IdCardInfo(); IdCardInfo cardInfo = new IdCardInfo();
cardInfo.setIsValid(true); cardInfo.setIsValid(true);
// 判断男女 // 判断男女
if (Integer.parseInt(lastValue) % 2 == 0) { if (Integer.parseInt(lastValue) % Constants.GENDER_MASK == 0) {
cardInfo.setGender(Gender.FEMALE); cardInfo.setGender(Gender.FEMALE);
} else { } else {
cardInfo.setGender(Gender.MALE); cardInfo.setGender(Gender.MALE);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment