添加 IdGen.randomShortString() 方法

This commit is contained in:
thinkgem
2024-04-25 23:31:11 +08:00
parent aedd50c8c2
commit 9573da0309
3 changed files with 23 additions and 9 deletions

View File

@@ -27,13 +27,6 @@ public class IdGen {
public static String uuid() {
return StringUtils.replace(UUID.randomUUID().toString(),"-", "");
}
/**
* 使用SecureRandom随机生成Long.
*/
public static long randomLong() {
return Math.abs(random.nextLong());
}
/**
* 基于Base62编码的SecureRandom随机生成bytes.
@@ -44,12 +37,33 @@ public class IdGen {
return EncodeUtils.encodeBase62(randomBytes);
}
/**
* 随机小写字符串
*/
public static String randomString(int length) {
return randomBase62(length).toLowerCase();
}
/**
* 随机简短小写字符串(对重复频率要求不高的使用)
*/
public static String randomShortString() {
return randomBase62(4).toLowerCase();
}
/**
* 使用SecureRandom随机生成指定范围的Integer.
*/
public static int randomInt(int min, int max) {
return random.nextInt(max) % (max - min + 1) + min;
}
/**
* 使用SecureRandom随机生成Long.
*/
public static long randomLong() {
return Math.abs(random.nextLong());
}
/**
* 获取新唯一编号18为数值

View File

@@ -35,7 +35,7 @@ public class LogInterceptor extends BaseService implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
if (StringUtils.isBlank(MDC.get(TRACE_ID))) {
MDC.put(TRACE_ID, IdGen.randomBase62(4));
MDC.put(TRACE_ID, IdGen.randomShortString());
}
long beginTime = System.currentTimeMillis();// 1、开始时间
startTimeThreadLocal.set(beginTime); // 线程绑定变量(该数据只有当前请求的线程可见)

View File

@@ -139,7 +139,7 @@ public class EmpUserServiceSupport extends CrudService<EmpUserDao, EmpUser>
// 如果没有设置用户编码,则根据登录名生成一个
if (StringUtils.isBlank(user.getUserCode())){
userService.genId(user, user.getLoginCode());
user.setUserCode(user.getUserCode()+"_"+IdGen.randomBase62(4).toLowerCase());
user.setUserCode(user.getUserCode()+"_"+IdGen.randomShortString());
}
user.setUserType(EmpUser.USER_TYPE_EMPLOYEE);
user.setMgrType(EmpUser.MGR_TYPE_NOT_ADMIN);