生成六位数 邀请码

package com.example.utils.utils;

import java.util.Random;

/**
 * @ClassName ShareCodeUtil
 * @Description TODO
 * @Author wushaopei
 * @Date 2019/7/22 11:26
 * @Version 1.0
 */
public class ShareCodeUtil {
    /** 自定义进制(0,1没有加入,容易与o,l混淆) */
    private static char[] r = new char[] { 'Q', 'w', 'E', '8', 'a', 'S', '2',
            'd', 'Z', 'x', '9', 'c', '7', 'p', 'O', '5', 'i', 'K', '3', 'm',
            'j', 'U', 'f', 'r', '4', 'V', 'y', 'L', 't', 'N', '6', 'b', 'g',
            'H' };
    /** 自动补全组(不能与自定义进制有重复) */
    private static char[] b = new char[] { 'q', 'W', 'e', 'A', 's', 'D', 'z',
            'X', 'C', 'P', 'I', 'k', 'M', 'J', 'u', 'F', 'R', 'v', 'Y', 'T',
            'n', 'B', 'G', 'h' };

    /** 自定义进制(0,1没有加入,容易与o,l混淆) */
    private static char[] r1 = new char[] { '0', '1', '2', '3', '4', '5', '6',
            '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
            'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
            's', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
            'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
            'S', 'Y', 'Z' };

    /** 单个分隔符 **/
    private static char b1 = '-';

    /** 进制长度 */
    private static final int binLen = r.length;

    /**
     * 根据ID生成邀请码
     *
     * @param id
     *            ID
     * @return 邀请码
     */
    public static String toSerialCode(long id) {
        return toSerialCode(id, null);
    }

    /**
     * 生成代理编号
     *
     * @return 代理编号
     */
    public static String getProxyCode(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] = r1[(int) (id % binLen)];
        String str = new String(buf, charPos, (32 - charPos));
        return str + b1;
    }

    /**
     * 生成随机邀请码
     *
     * @return 邀请码
     */
    public static String getRadmonCode(int len) {
        Random rd = new Random();
        int i1 = Math.abs(rd.nextInt(1000000000));
        int i2 = Math.abs(rd.nextInt(1000));
        return toSerialCode(Long.parseLong(i1 + "" + i2), len);
    }

    /**
     * 生成随机邀请码
     *
     * @return 邀请码
     */
    public static String getRadmonCodeNew(int len) {
        Random rd = new Random();
        int i1 = Math.abs(rd.nextInt((int) Math.pow(10, len)));
        String resultStr = i1+"";
        while (resultStr.length()<len) {
            resultStr = "0"+resultStr;
        }
        return resultStr;
    }

    /**
     * 8xgtgd 根据ID生成指定长度邀请码
     *
     * @param id
     *            ID
     * @return 邀请码
     */
    public static String toSerialCode(long id, Integer len) {
        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] = r1[(int) (id % binLen)];
        String str = new String(buf, charPos, (32 - charPos));
        // 是否补全
        if (len != null) {
            // 不够长度的自动随机补全
            if (str.length() < len) {
                StringBuilder sb = new StringBuilder();
                Random rnd = new Random();
                for (int i = 0; i < len - str.length(); i++) {
                    sb.append(b[rnd.nextInt(b.length)]);
                }
                str += sb.toString();
            }
        }
        return str;
    }

    /**
     * 邀请码转换成ID
     *
     * @param code
     *            邀请码
     * @return id
     */
    public static long codeToId(String code) {
        char[] chs = code.toCharArray();
        long res = 0L;
        for (int i = 0; i < chs.length; i++) {
            int ind = 0;
            for (int j = 0; j < binLen; j++) {
                if (chs[i] == r[j]) {
                    ind = j;
                    break;
                }
            }
            boolean bol = false;

            for (int j = 0; j < b.length; j++) {
                if (chs[i] == b[j]) {
                    bol = true;
                    break;
                }
            }
            if (bol) {
                break;
            }
            if (i > 0) {
                res = res * binLen + ind;
            } else {
                res = ind;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        System.out.println(getProxyCode(63233333));
    }
}

执行测试结果:

1p9tad-

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值