代码提交
parent
543af053b6
commit
ae7f5f5417
@ -0,0 +1,50 @@
|
||||
package com.baiye.util;
|
||||
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class InvitationCodeUtil {
|
||||
|
||||
/** 自定义进制(0,1没有加入,容易与o,l混淆) */
|
||||
private static final char[] r=new char[]{'Q', 'W', 'E', '8', 'A', 'S', '2', 'D', 'Z', 'X', '9', 'C', '7', 'P', '5', 'I', 'K', '3', 'M', 'J', 'U', 'F', 'R', '4', 'V', 'Y', 'l', 'T', 'N', '6', 'B', 'G', 'H'};
|
||||
|
||||
/** (不能与自定义进制有重复) */
|
||||
private static final char b='O';
|
||||
|
||||
/** 进制长度 */
|
||||
private static final int binLen=r.length;
|
||||
|
||||
/** 序列最小长度 */
|
||||
private static final int s=6;
|
||||
|
||||
/**
|
||||
* 根据ID生成六位随机码
|
||||
* @return 随机码
|
||||
*/
|
||||
public static String toSerialCode() {
|
||||
Random rnd=new Random();
|
||||
// long id = IdUtil.getSnowflake(9, 9).nextId();
|
||||
int id = RandomUtil.randomInt(0, 999999999);
|
||||
char[] buf=new char[32];
|
||||
int charPos=32;
|
||||
|
||||
while((id / binLen) > 0) {
|
||||
int ind=(int)(id % binLen);
|
||||
buf[--charPos]=r[ind];
|
||||
id /= binLen;
|
||||
}
|
||||
buf[--charPos]=r[(int)(id % binLen)];
|
||||
String str=new String(buf, charPos, (32 - charPos));
|
||||
// 不够长度的自动随机补全
|
||||
if(str.length() < s) {
|
||||
StringBuilder sb=new StringBuilder();
|
||||
sb.append(b);
|
||||
for(int i=1; i < s - str.length(); i++) {
|
||||
sb.append(r[rnd.nextInt(binLen)]);
|
||||
}
|
||||
str+=sb.toString();
|
||||
}
|
||||
return str;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue