身份证随机
This commit is contained in:
@@ -15,6 +15,7 @@ import com.cool.store.exception.ServiceException;
|
|||||||
import com.cool.store.service.AliyunService;
|
import com.cool.store.service.AliyunService;
|
||||||
import com.cool.store.utils.RedisConstantUtil;
|
import com.cool.store.utils.RedisConstantUtil;
|
||||||
import com.cool.store.utils.RedisUtilPool;
|
import com.cool.store.utils.RedisUtilPool;
|
||||||
|
import com.cool.store.utils.UUIDUtils;
|
||||||
import com.cool.store.vo.cuser.IdentityCardInfoVO;
|
import com.cool.store.vo.cuser.IdentityCardInfoVO;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
@@ -29,6 +30,7 @@ import java.time.LocalDate;
|
|||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zhangchenbiao
|
* @author zhangchenbiao
|
||||||
@@ -85,6 +87,10 @@ public class AliyunServiceImpl implements AliyunService {
|
|||||||
// 访问的域名
|
// 访问的域名
|
||||||
config.endpoint = "ocr.cn-shanghai.aliyuncs.com";
|
config.endpoint = "ocr.cn-shanghai.aliyuncs.com";
|
||||||
try {
|
try {
|
||||||
|
if(StringUtils.isNotBlank(faceImageUrl)){
|
||||||
|
String idCard = generateIdCard();
|
||||||
|
return new IdentityCardInfoVO(getRandomName(), generateAddress(), convertDate(idCard.substring(6, 14), "yyyyMMdd"), UUIDUtils.get8UUID().hashCode() % 2 == 0 ? "男":"女", idCard, "汉");
|
||||||
|
}
|
||||||
com.aliyun.ocr20191230.Client client = new com.aliyun.ocr20191230.Client(config);
|
com.aliyun.ocr20191230.Client client = new com.aliyun.ocr20191230.Client(config);
|
||||||
URL url = new URL(faceImageUrl);
|
URL url = new URL(faceImageUrl);
|
||||||
InputStream inputStream = url.openConnection().getInputStream();
|
InputStream inputStream = url.openConnection().getInputStream();
|
||||||
@@ -134,4 +140,85 @@ public class AliyunServiceImpl implements AliyunService {
|
|||||||
throw new ServiceException(ErrorCodeEnum.DATA_CONVERT_ERROR);
|
throw new ServiceException(ErrorCodeEnum.DATA_CONVERT_ERROR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public static String generateAddress() {
|
||||||
|
String address = "";
|
||||||
|
String[] provinceList = {"北京市", "上海市", "广东省", "江苏省", "浙江省", "四川省", "湖南省", "河北省", "山东省", "陕西省"};
|
||||||
|
String[] cityList = {"北京市", "上海市", "广州市", "南京市", "杭州市", "成都市", "长沙市", "石家庄市", "济南市", "西安市"};
|
||||||
|
String[] regionList = {"朝阳区", "浦东新区", "天河区", "玄武区", "拱墅区", "锦江区", "芙蓉区", "桥西区", "历下区", "雁塔区"};
|
||||||
|
String[] streetList = {"中山北路", "人民路", "长安街", "南京西路", "华山路", "锦江大道", "芙蓉中路", "胜利大街", "经十路", "雁塔路"};
|
||||||
|
String[] numberList = {"1号", "2号", "3号", "4号", "5号", "6号", "7号", "8号", "9号", "10号"};
|
||||||
|
String province = provinceList[new Random().nextInt(provinceList.length)];
|
||||||
|
String city = cityList[new Random().nextInt(cityList.length)];
|
||||||
|
String region = regionList[new Random().nextInt(regionList.length)];
|
||||||
|
String street = streetList[new Random().nextInt(streetList.length)];
|
||||||
|
String number = numberList[new Random().nextInt(numberList.length)];
|
||||||
|
address = province + city + region + street + number;
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static String generateIdCard() {
|
||||||
|
String [] CITY_CODES = new String[] {
|
||||||
|
"110000", "120000", "130000", "140000", "150000", "210000", "220000", "230000", "310000", "320000",
|
||||||
|
"330000", "340000", "350000", "360000", "370000", "410000", "420000", "430000", "440000", "450000",
|
||||||
|
"460000", "500000", "510000", "520000", "530000", "540000", "610000", "620000", "630000", "640000",
|
||||||
|
"650000"
|
||||||
|
};
|
||||||
|
String BIRTHDAY_START = "19800101";
|
||||||
|
String BIRTHDAY_END = "20000101";
|
||||||
|
Random random = new Random();
|
||||||
|
// 地址码:前6位数字,表示户籍所在地行政区划代码
|
||||||
|
String addressCode = CITY_CODES[random.nextInt(CITY_CODES.length)];
|
||||||
|
// 出生日期码:7到14位数字,表示出生年月日
|
||||||
|
LocalDate birthday = LocalDate.ofEpochDay(random.nextInt(
|
||||||
|
(int)(LocalDate.parse(BIRTHDAY_END, DateTimeFormatter.BASIC_ISO_DATE).toEpochDay() - LocalDate.parse(BIRTHDAY_START, DateTimeFormatter.BASIC_ISO_DATE).toEpochDay())
|
||||||
|
) + LocalDate.parse(BIRTHDAY_START, DateTimeFormatter.BASIC_ISO_DATE).toEpochDay());
|
||||||
|
String birthdayCode = birthday.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
|
||||||
|
// 顺序码:15到17位数字,表示在同一地址码所在区域范围内,对同年同月同日出生的人的顺序编号
|
||||||
|
String sequenceCode = String.format("%03d", random.nextInt(1000));
|
||||||
|
// 计算校验码
|
||||||
|
String body = addressCode + birthdayCode + sequenceCode;
|
||||||
|
int[] weight = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
|
||||||
|
String[] checkCode = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
|
||||||
|
int sum = 0;
|
||||||
|
for (int i = 0; i < body.length(); i++) {
|
||||||
|
sum += (body.charAt(i) - '0') * weight[i];
|
||||||
|
}
|
||||||
|
int remainder = sum % 11;
|
||||||
|
String checkCodeStr = checkCode[remainder];
|
||||||
|
|
||||||
|
// 构造身份证号码
|
||||||
|
return body + checkCodeStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static char[] lastNames = { '赵', '钱', '孙', '李', '周', '吴', '郑', '王', '冯', '陈', '褚', '卫', '蒋', '沈', '韩', '杨', '朱', '秦', '尤', '许', '何', '吕', '施', '张', '孔', '曹', '严', '华', '金', '魏', '陶', '姜', '戚', '谢', '邹', '喻', '柏', '水', '窦', '章', '云', '苏', '潘', '葛', '奚', '范', '彭', '郎', '鲁', '韦', '昌', '马', '苗', '凤', '花', '方', '俞', '任', '袁', '柳', '酆', '鲍', '史', '唐', '费', '廉', '岑', '薛', '雷', '贺', '倪', '汤', '滕', '殷', '罗', '毕', '郝', '邬', '安', '常', '乐', '于', '时', '傅', '皮', '卞', '齐', '康', '伍', '余', '元', '卜', '顾', '孟', '平', '黄', '和', '穆', '肖', '尹', '营', '姚', '邵', '湛', '汪', '祁', '毛', '禹', '狄', '米', '贝', '明', '臧', '计', '伏', '成', '戴', '谈', '宋', '茅', '庞', '熊', '纪', '舒', '屈', '项', '祝', '董', '粱', '杜', '阮', '蓝', '闵', '席', '季', '麻', '强', '贾', '路', '娄', '危', '江', '童', '颜', '郭', '梅', '盛', '林', '刁', '钟', '徐', '邱', '骆', '高', '夏', '蔡', '田', '樊', '胡', '凌', '霍', '虞', '万', '支', '柯', '昝', '管', '卢', '莫', '经', '房', '裘', '缪', '干', '解', '应', '宗', '丁', '宣', '贲', '郁', '单', '杭', '洪', '包', '诸', '左', '石', '崔', '吉', '钮', '龚', '程', '嵇', '邢', '滑', '裴', '陆', '荣', '翁', '荀', '羊', '於', '惠', '甄', '加', '封', '芮', '麴', '井', '牟', '乘', '裘', '栾', '于', '冷', '甘', '归', '海', '嵩', '福', '景', '祖', '武', '符', '文', '御', '卫', '安', '柴', '颛', '段', '侯', '叶', '孟', '平', '呼', '南', '仇', '胥', '戊', '缑', '雍', '却', '璩', '桑', '桂', '濮', '閻', '充', '慕', '连', '茹', '习', '宦', '艾', '鱼', '容', '向', '古', '易', '慎', '戈', '廖', '庾', '终', '暨', '居', '衡', '步', '都', '耿', '满', '弘', '匡', '国', '文', '寇', '广', '禄', '阙', '东', '欧', '殳', '沃', '利', '蔚', '越', '夔', '隆', '师', '巩', '厍', '聂', '晁', '勾', '敖', '融', '冷', '訾', '辛', '阚', '那', '简', '饶', '空', '曾', '毋', '沙', '乜', '养', '鞠', '须', '丰', '巢', '关', '蒯', '相', '查', '后', '荆', '红', '游', '竺', '权', '逯', '盖', '益', '桓', '公', '牛', '应', '冉', '司', '马'};
|
||||||
|
|
||||||
|
private static char getRandomChar() {
|
||||||
|
Random rand = new Random();
|
||||||
|
return (char) (0x4e00 + rand.nextInt(0x9fa5 - 0x4e00 + 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getRandomName() {
|
||||||
|
Random rand = new Random();
|
||||||
|
String name = "";
|
||||||
|
// 先随机选择一个姓氏
|
||||||
|
char lastName = lastNames[rand.nextInt(lastNames.length)];
|
||||||
|
name += lastName;
|
||||||
|
|
||||||
|
// 再随机生成1-2个名字
|
||||||
|
int nameLen = rand.nextInt(2) + 1;
|
||||||
|
for (int i = 0; i < nameLen; i++) {
|
||||||
|
name += getRandomChar();
|
||||||
|
}
|
||||||
|
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println(generateIdCard().substring(6, 14));
|
||||||
|
System.out.println(getRandomName());
|
||||||
|
System.out.println(generateAddress());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user