admin
2024-04-26 5e7b0ed4a154ad067cbcf4aa1a1c7cce32f9864c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.yeshi.fanli.util;
 
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * 口令工具
 * 
 * @author Administrator
 *
 */
public class TokenUtil {
 
    private static String convert10To62(Long num) {
        String numbers = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        BigDecimal numBig = new BigDecimal(num);
 
        int ge = numBig.divideAndRemainder(new BigDecimal(numbers.length()))[1].intValue();
        int n = 1;
        String result = "";
        result += numbers.charAt(ge);
 
        while (numBig.divideAndRemainder(new BigDecimal(numbers.length()).pow(n))[0].compareTo(new BigDecimal(0)) > 0) {
            int w = numBig.divideAndRemainder(new BigDecimal(numbers.length()).pow(n))[0]
                    .divideAndRemainder(new BigDecimal(numbers.length()))[1].intValue();
            result = numbers.charAt(w) + result;
            n++;
        }
        return result;
    }
 
    /**
     * 生成口令
     * 
     * @param id
     * @return
     */
    public static String createToken(Long id) {// 生成口令
        long fid = 10000000000000L * ((int) (Math.random() * 1000) + 1) + id;
        return "&" + convert10To62(fid) + "&";
    }
 
    /**
     * 提取口令
     * 
     * @param content
     * @return
     */
    public static String parseToken(String content) {
        String regex = "&[A-Za-z0-9]{6,10}&";
        Pattern pattern = Pattern.compile(regex);
        String source = content;
        Matcher matcher = pattern.matcher(source);
        while (matcher.find()) {
            String result = matcher.group(0);
            return result;
        }
        return null;
    }
 
}