|
@@ -0,0 +1,134 @@
|
|
|
+package com.caimei365.tools.utils;
|
|
|
+
|
|
|
+import java.util.Random;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Description
|
|
|
+ *
|
|
|
+ * @author : Charles
|
|
|
+ * @date : 2021/7/7
|
|
|
+ */
|
|
|
+public class CodeUtil {
|
|
|
+ /**
|
|
|
+ * 随机数源
|
|
|
+ */
|
|
|
+ private static final char[] INT_SEQUENCE = {'2', '3', '4', '5', '6', '7', '8', '9'};
|
|
|
+ /**
|
|
|
+ * 随机字母源
|
|
|
+ */
|
|
|
+ private static final char[] LETTER_SEQUENCE = {
|
|
|
+ 'A', 'a', 'B', 'b', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g', 'H', 'h',
|
|
|
+ 'L', 'N', 'n', 'Q', 'q', 'R', 'r', 'T', 't', 'Y', 'y'
|
|
|
+ };
|
|
|
+ /**
|
|
|
+ * 随机字符源(排除歧义字符)
|
|
|
+ */
|
|
|
+ private static final char[] CODE_SEQUENCE = {
|
|
|
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
|
|
|
+ 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
|
|
|
+ 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7',
|
|
|
+ '8', '9'
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
+ private static final char[] SHORT_LINK_SEQUENCE = {
|
|
|
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
|
|
|
+ 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'O',
|
|
|
+ 'W', 'X', 'Y', 'Z',
|
|
|
+ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
|
|
|
+ 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'o',
|
|
|
+ 'w', 'x', 'y', 'z' ,
|
|
|
+ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'
|
|
|
+ };
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取随机数字
|
|
|
+ * @param size 位数
|
|
|
+ * @return 随机数字符串
|
|
|
+ */
|
|
|
+ public static String randomInt(int size) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ Random random = new Random();
|
|
|
+ for (int i = 0; i < INT_SEQUENCE.length && i < size; ++i) {
|
|
|
+ sb.append(INT_SEQUENCE[random.nextInt(INT_SEQUENCE.length)]);
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取随机字母
|
|
|
+ * @param size 位数
|
|
|
+ * @return 随机字母字符串
|
|
|
+ */
|
|
|
+ public static String randomLetter(int size) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ Random random = new Random();
|
|
|
+ for (int i = 0; i < LETTER_SEQUENCE.length && i < size; ++i) {
|
|
|
+ sb.append(LETTER_SEQUENCE[random.nextInt(LETTER_SEQUENCE.length)]);
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取随机码(字母+数字)
|
|
|
+ * @param size 位数
|
|
|
+ * @return 随机码字符串
|
|
|
+ */
|
|
|
+ public static String generateCode(int size) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ Random random = new Random();
|
|
|
+ for (int i = 0; i < CODE_SEQUENCE.length && i < size; ++i) {
|
|
|
+ sb.append(CODE_SEQUENCE[random.nextInt(CODE_SEQUENCE.length)]);
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成主订单编号
|
|
|
+ * @param orderSource 订单来源:1WWW、2CRM、4客服[适用后台下单]、5外单[适用后台下单]、6小程序[采美,星范]、7呵呵商城小程序、8维沙小程序
|
|
|
+ * @return 主订单编号
|
|
|
+ */
|
|
|
+ public static String generateOrderNo(Integer orderSource) {
|
|
|
+ String platform = "";
|
|
|
+ if (1 == orderSource){
|
|
|
+ platform = "W";
|
|
|
+ } else if (2 == orderSource) {
|
|
|
+ platform = "C";
|
|
|
+ } else if (4 == orderSource) {
|
|
|
+ platform = "P";
|
|
|
+ } else if (5 == orderSource) {
|
|
|
+ platform = "T";
|
|
|
+ } else if (6 == orderSource) {
|
|
|
+ platform = "X";
|
|
|
+ } else if (7 == orderSource) {
|
|
|
+ platform = "H";
|
|
|
+ } else if (8 == orderSource) {
|
|
|
+ platform = "A";
|
|
|
+ } else {
|
|
|
+ // 未知来源
|
|
|
+ platform = "E";
|
|
|
+ }
|
|
|
+ return platform + System.currentTimeMillis() + randomInt(2);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成短链接
|
|
|
+ * @param size 长度
|
|
|
+ * @return 短链接
|
|
|
+ */
|
|
|
+ public static String generateShortLink(int size) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ Random random = new Random();
|
|
|
+ for (int i = 0; i < SHORT_LINK_SEQUENCE.length && i < size; ++i) {
|
|
|
+ sb.append(SHORT_LINK_SEQUENCE[random.nextInt(SHORT_LINK_SEQUENCE.length)]);
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|