| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- package com.goafanti.common.utils;
- import java.util.Date;
- import java.util.Random;
- public class InvitationCodeUtils {
- /** 自定义进制(选择你想要的进制数,不能重复且最好不要0、1这些容易混淆的字符) */
- private static final char[] r=new char[]{'Q', 'W', 'E', '8', 'S', '2', 'D', 'Z', 'X', '9', 'C', '7', 'P', '5', 'K', '3', 'M', 'J', 'U', 'F', 'R', '4', 'V', 'Y', 'T', 'N', '6', 'B', 'G', 'H'};
-
-
-
- /** 进制长度 */
- private static final int binLen=r.length;
-
- /** 邀请码长度 */
- private static final int s=12;
-
- /**
- * 根据ID生成随机码
- * @param id ID
- * @return 随机码
- */
- public static String toSerialCode(long id) {
- 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();
-
- Random rnd=new Random();
- for(int i=1; i <= s - str.length(); i++) {
- sb.append(r[rnd.nextInt(binLen)]);
- }
- str+=sb.toString();
- }
- return str;
- }
- /**
- * 根据ID生成随机码
- * @param id ID
- * @return 随机码
- */
- public static String randomCode(long id,Integer size) {
- 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() < size) {
- StringBuilder sb=new StringBuilder();
- Random rnd=new Random();
- for(int i=1; i <= size - str.length(); i++) {
- sb.append(r[rnd.nextInt(binLen)]);
- }
- str+=sb.toString();
- }else if(str.length() > size){
- System.out.println(str.length()-size);
- System.out.println(str.length());
- str=str.substring(str.length()-size,str.length());
- }
- return str;
- }
-
- /*public static void main(String[] args) {
- System.out.println(randomCode(new Date().getTime(),6));
- System.out.println(toSerialCode(new Date().getTime()));
- }*/
-
- }
|