Skip to content

Commit

Permalink
Merge pull request #58 from yindz/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
yindz authored Jul 10, 2022
2 parents 951f7c5 + a8a587d commit f37a44f
Show file tree
Hide file tree
Showing 6 changed files with 3,139 additions and 24 deletions.
13 changes: 7 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.apifan.common</groupId>
<artifactId>common-random</artifactId>
<version>1.0.14</version>
<version>1.0.15</version>
<packaging>jar</packaging>
<name>common-random</name>
<description>An easy-to-use random data generator.</description>
Expand All @@ -18,21 +18,22 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<build.jar.name>common-random</build.jar.name>
<commons-lang.version>3.7</commons-lang.version>
<commons-lang.version>3.12.0</commons-lang.version>
<commons-io.version>2.7</commons-io.version>
<commons-collections.version>4.1</commons-collections.version>
<guava.version>30.0-jre</guava.version>
<jackson-databind.version>2.12.6.1</jackson-databind.version>
<commons-collections.version>4.4</commons-collections.version>
<guava.version>31.1-jre</guava.version>
<jackson-databind.version>2.13.3</jackson-databind.version>
<tinypinyin.version>2.0.3.RELEASE</tinypinyin.version>
<slf4j-api.version>1.7.36</slf4j-api.version>
<slf4j-simple.version>1.7.36</slf4j-simple.version>
<junit.version>4.13.2</junit.version>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
87 changes: 87 additions & 0 deletions src/main/java/com/apifan/common/random/source/FinancialSource.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.apifan.common.random.source;

import com.apifan.common.random.constant.CreditCardType;
import com.apifan.common.random.entity.CurrencyInfo;
import com.apifan.common.random.entity.KChartData;
import com.apifan.common.random.util.ResourceUtils;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -55,6 +58,8 @@ public class FinancialSource {
*/
private static List<String> currencyList = Lists.newArrayList();

private static final String UNION_PAY_PREFIX = "62";

private static final FinancialSource instance = new FinancialSource();

private FinancialSource() {
Expand Down Expand Up @@ -211,4 +216,86 @@ public CurrencyInfo randomCurrencyInfo() {
currencyInfo.setCode(tmp[2]);
return currencyInfo;
}

/**
* 随机虚拟信用卡号码
*
* <p> 特别说明: 不会与现实中的真实信用卡号产生重合 </p>
*
* @param type 信用卡类型
* @return 随机虚拟信用卡号码
*/
public String randomCreditCardNo(CreditCardType type) {
String prefix;
int length = 12;
if (CreditCardType.Visa.equals(type)) {
//VISA信用卡前缀为4,增加99是为了避开现实中的信用卡号码
prefix = "499";
} else if (CreditCardType.MasterCard.equals(type)) {
//MasterCard信用卡前缀为5,增加9是为了避开现实中的信用卡号码
prefix = "59";
length = 13;
} else if (CreditCardType.Amex.equals(type)) {
//AMEX信用卡前缀为37,增加9是为了避开现实中的信用卡号码
prefix = "379";
} else if (CreditCardType.UnionPay.equals(type)) {
//银联信用卡前缀为62,增加9是为了避开现实中的信用卡号码
prefix = UNION_PAY_PREFIX + "9";
} else if (CreditCardType.JCB.equals(type)) {
//JCB信用卡前缀为35,增加9是为了避开现实中的信用卡号码
prefix = "359";
} else {
prefix = UNION_PAY_PREFIX + "9";
}
String digits = prefix + RandomStringUtils.randomNumeric(length);
return digits + getCheckDigit(digits);
}

/**
* 随机虚拟借记卡号码(银联)
*
* <p> 特别说明: 不会与现实中的真实借记卡号产生重合 </p>
*
* @return 随机虚拟借记卡号码
*/
public String randomDebitCardNo() {
//发卡机构标识码9000~9999为目前尚未实际使用的号段(为了避免不必要的麻烦)
String digits = UNION_PAY_PREFIX + RandomUtils.nextInt(9000, 10000) + RandomStringUtils.randomNumeric(12);
return digits + getCheckDigit(digits);
}

/**
* 简单计算校验码(符合Luhn算法)
*
* @param toCheck 待计算的数字字符串
* @return 校验码
*/
private static String getCheckDigit(String toCheck) {
Preconditions.checkArgument(StringUtils.isNotBlank(toCheck), "待计算的数字字符串为空");
//所有待计算的数字
List<Integer> digits = Lists.newArrayList();
int length = toCheck.length();
int maxIndex = length - 1;
for (int i = maxIndex; i >= 0; i--) {
int j = maxIndex - i;
int number = Integer.parseInt(String.valueOf(toCheck.charAt(i)));
if (j % 2 == 0) {
int f = number * 2;
if (f >= 10) {
//乘积f大于10时,将十位数与个位数相加重新得到f
f = (f / 10) + (f % 10);
}
digits.add(f);
} else {
digits.add(number);
}
}
//求和
int total = digits.stream().mapToInt(Integer::intValue).sum();
//总和除以10取余数
int mod = total % 10;
//余数=0时校验码为0,余数大于0时校验码为10-余数
int digit = (mod == 0) ? 0 : 10 - mod;
return String.valueOf(digit);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class InternetSource {
/**
* 主流安卓厂商名称
*/
private static final String[] ANDROID_MANUFACTURERS = new String[]{"samsung", "sony", "huawei", "hornor", "xiaomi", "redmi", "mi", "vivo", "oppo", "oneplus", "lg", "lenovo", "motorola", "nokia", "meizu", "zte", "asus", "smartisan", "nubia", "realme", "iqoo", "coolpad", "gionee"};
private static final String[] ANDROID_MANUFACTURERS = new String[]{"samsung", "sony", "huawei", "honor", "xiaomi", "redmi", "mi", "vivo", "oppo", "oneplus", "lg", "lenovo", "motorola", "nokia", "meizu", "zte", "asus", "smartisan", "nubia", "realme", "iqoo", "coolpad", "gionee"};

/**
* 安卓 User-Agent模板
Expand All @@ -44,12 +44,12 @@ public class InternetSource {
/**
* 主流iOS版本号
*/
private static final String[] IOS_VERSIONS = new String[]{"10_0", "10_1", "10_2", "10_3", "11_0", "11_1", "11_2", "11_3", "11_4", "12_0", "12_1", "12_2", "12_3", "12_4", "13_0", "13_1", "13_2", "13_3"};
private static final String[] IOS_VERSIONS = new String[]{"10_0", "10_1", "10_2", "10_3", "11_0", "11_1", "11_2", "11_3", "11_4", "12_0", "12_4", "13_0", "13_7", "14_0", "14_7", "15_0", "15_7", "16_0"};

/**
* 主流Windows版本号
*/
private static final String[] WINDOWS_VERSIONS = new String[]{"6.0", "6.1", "6.2", "6.3", "10.0"};
private static final String[] WINDOWS_VERSIONS = new String[]{"6.0", "6.1", "6.2", "6.3", "10.0", "11.0"};

/**
* PC User-Agent模板
Expand Down Expand Up @@ -190,7 +190,7 @@ public String randomPrivateIpv4() {
public String randomPCUserAgent() {
return String.format(PC_TEMPLATE,
WINDOWS_VERSIONS[RandomUtils.nextInt(0, WINDOWS_VERSIONS.length)],
RandomUtils.nextInt(60, 80),
RandomUtils.nextInt(60, 100),
RandomUtils.nextInt(2000, 4000),
RandomUtils.nextInt(1, 200));
}
Expand All @@ -201,7 +201,7 @@ public String randomPCUserAgent() {
* @return Android User-Agent
*/
public String randomAndroidUserAgent() {
int androidVersion = RandomUtils.nextInt(7, 11);
int androidVersion = RandomUtils.nextInt(7, 13);
return String.format(ANDROID_TEMPLATE, androidVersion,
ANDROID_MANUFACTURERS[RandomUtils.nextInt(0, ANDROID_MANUFACTURERS.length)].toUpperCase(),
RandomStringUtils.randomAlphanumeric(6).toUpperCase(),
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/com/apifan/common/random/source/OtherSource.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.apifan.common.random.source;

import com.apifan.common.random.entity.Area;
import com.apifan.common.random.entity.EconomicCategory;
import com.apifan.common.random.entity.Poem;
import com.apifan.common.random.util.ResourceUtils;
Expand Down Expand Up @@ -154,6 +155,17 @@ public class OtherSource {
*/
private static List<String> chineseIdiomsList = Lists.newArrayList();

/**
* 英文常用词语
*/
private static List<String> englishWordsList = Lists.newArrayList();

/**
* 统一社会信用代码候选字符(不使用I、O、Z、S、V)
*/
private static final List<String> socialCreditCharactersList = Lists.newArrayList(
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "T", "U", "W", "X", "Y");

private static final OtherSource instance = new OtherSource();

private OtherSource() {
Expand Down Expand Up @@ -185,6 +197,7 @@ private OtherSource() {
sensationalTitlesList = ResourceUtils.base64DecodeLines(ResourceUtils.readLines("sensational-titles.txt"));
astonishingPrefixList = ResourceUtils.base64DecodeLines(ResourceUtils.readLines("astonishing-prefix.txt"));
chineseIdiomsList = ResourceUtils.base64DecodeLines(ResourceUtils.readLines("chinese-idioms.txt"));
englishWordsList = ResourceUtils.readLines("word-en.txt");

ObjectMapper objectMapper = new ObjectMapper();
CollectionType poemType = objectMapper.getTypeFactory().constructCollectionType(List.class, Poem.class);
Expand Down Expand Up @@ -506,6 +519,30 @@ public String randomChineseIdiom() {
return ResourceUtils.getRandomElement(chineseIdiomsList);
}

/**
* 随机英文文本
*
* @param words 词语数量
* @return 随机英文文本
*/
public String randomEnglishText(int words) {
Preconditions.checkArgument(words > 1, "词语数量必须大于1");
return StringUtils.capitalize(Joiner.on(" ").join(ResourceUtils.getRandomElement(englishWordsList, words)));
}

/**
* 随机统一社会信用代码(虚拟)
*
* @return 统一社会信用代码(虚拟)
*/
public String randomSocialCreditCode() {
String prefix = "91";
//为避免与真实的社会信用代码重合,不计算校验码而是随机生成
String checkCode = String.valueOf(RandomUtils.nextInt(0, 10));
Area area = AreaSource.getInstance().nextArea();
return prefix + area.getZipCode() + Joiner.on("").join(ResourceUtils.getRandomElement(socialCreditCharactersList, 9)) + checkCode;
}

/**
* 计算校验码
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,24 +469,14 @@ public String randomFemaleIdCard(String province, int age) {

/**
* 随机信用卡号码
* <p>注意: 此方法已移动到 {@link com.apifan.common.random.source.FinancialSource#randomCreditCardNo(CreditCardType) FinancialSource.randomCreditCardNo}</p>
*
* @param type 信用卡类型
* @return 随机信用卡号码
*/
@Deprecated
public String randomCreditCardNo(CreditCardType type) {
if (CreditCardType.Visa.equals(type)) {
return "4" + RandomStringUtils.randomNumeric(15);
} else if (CreditCardType.MasterCard.equals(type)) {
return "5" + RandomStringUtils.randomNumeric(15);
} else if (CreditCardType.Amex.equals(type)) {
return "37" + RandomStringUtils.randomNumeric(14);
} else if (CreditCardType.UnionPay.equals(type)) {
return "62" + RandomStringUtils.randomNumeric(14);
} else if (CreditCardType.JCB.equals(type)) {
return "35" + RandomStringUtils.randomNumeric(14);
} else {
throw new RuntimeException("未知的信用卡类型");
}
return FinancialSource.getInstance().randomCreditCardNo(type);
}

/**
Expand Down
Loading

0 comments on commit f37a44f

Please sign in to comment.