init
This commit is contained in:
50
src/main/java/com/newyetai/common/util/AESUtils.java
Normal file
50
src/main/java/com/newyetai/common/util/AESUtils.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package com.newyetai.common.util;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.Security;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.SecretKey;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
import org.springframework.util.Base64Utils;
|
||||
|
||||
|
||||
/**
|
||||
* @author sr on 2024-02-22
|
||||
*/
|
||||
public class AESUtils {
|
||||
|
||||
public static final String TRANSFORM_ECB_5 = "AES/ECB/PKCS5Padding";
|
||||
public static final String TRANSFORM_ECB_7 = "AES/ECB/PKCS7Padding";
|
||||
public static final String TRANSFORM_ECB = "AES/ECB/NoPadding";
|
||||
|
||||
static {
|
||||
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
|
||||
}
|
||||
|
||||
public static Cipher getCipher(String transform) throws Exception {
|
||||
if (TRANSFORM_ECB_7.equals(transform)) {
|
||||
return Cipher.getInstance(transform, "BC");
|
||||
} else {
|
||||
return Cipher.getInstance(transform);
|
||||
}
|
||||
}
|
||||
|
||||
public static String encrypt(String input, String key, String transform) throws Exception {
|
||||
Cipher cipher = getCipher(transform);
|
||||
SecretKey keySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
|
||||
return Base64Utils.encodeToString(cipher.doFinal(input.getBytes(StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
public static String decrypt(String input, String key, String transform)
|
||||
throws Exception {
|
||||
Cipher cipher = getCipher(transform);
|
||||
SecretKey keySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
|
||||
cipher.init(Cipher.DECRYPT_MODE, keySpec);
|
||||
return new String(cipher.doFinal(Base64Utils.decode(input.getBytes(StandardCharsets.UTF_8))),
|
||||
StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
}
|
||||
36
src/main/java/com/newyetai/common/util/BaseDtoUtils.java
Normal file
36
src/main/java/com/newyetai/common/util/BaseDtoUtils.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package com.newyetai.common.util;
|
||||
|
||||
import com.newyetai.common.dto.BaseDTO;
|
||||
import com.newyetai.common.entity.BaseEntity;
|
||||
|
||||
/**
|
||||
* 基础dto工具类
|
||||
* @author sr on 2019-12-07
|
||||
*/
|
||||
public class BaseDtoUtils {
|
||||
|
||||
/**
|
||||
* 创建回写dto数据
|
||||
* @param entity
|
||||
* @param dto
|
||||
* @author sr on 2019-12-07
|
||||
*/
|
||||
public static void createCopy(BaseEntity entity, BaseDTO dto) {
|
||||
dto.setId(entity.getId());
|
||||
dto.setDeleted(entity.getDeleted());
|
||||
dto.setCreateTime(entity.getCreateTime());
|
||||
dto.setCreateBy(entity.getCreateBy());
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改回写dto数据
|
||||
* @param entity
|
||||
* @param dto
|
||||
* @author sr on 2019-12-07
|
||||
*/
|
||||
public static void updateCopy(BaseEntity entity, BaseDTO dto) {
|
||||
dto.setUpdateTime(entity.getUpdateTime());
|
||||
dto.setUpdateBy(entity.getUpdateBy());
|
||||
}
|
||||
|
||||
}
|
||||
72
src/main/java/com/newyetai/common/util/BigDecimalUtils.java
Normal file
72
src/main/java/com/newyetai/common/util/BigDecimalUtils.java
Normal file
@@ -0,0 +1,72 @@
|
||||
package com.newyetai.common.util;
|
||||
|
||||
import com.newyetai.common.exception.util.Assert;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
* @author sr on 2020-08-14
|
||||
*/
|
||||
@Slf4j
|
||||
public class BigDecimalUtils {
|
||||
|
||||
/**
|
||||
* 保留两位小数
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static BigDecimal keepTwo(BigDecimal amount) {
|
||||
if (amount == null) {
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
return amount.setScale(2, RoundingMode.DOWN);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转BigDecimal(转换失败,默认值null)
|
||||
* @param amountStr
|
||||
* @return
|
||||
* @author sr on 2020-08-14
|
||||
*/
|
||||
public static BigDecimal parse(String amountStr) {
|
||||
return parse(amountStr, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转BigDecimal
|
||||
*
|
||||
* @param amountStr
|
||||
* @param defaultValue 转换失败默认值
|
||||
* @return
|
||||
* @author sr on 2020-08-14
|
||||
*/
|
||||
public static BigDecimal parse(String amountStr, BigDecimal defaultValue) {
|
||||
if (StringUtils.isBlank(amountStr)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return new BigDecimal(amountStr);
|
||||
} catch (NumberFormatException e) {
|
||||
log.info("bigDecimal parse error", e);
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 见N+1进N 例如见厘进分
|
||||
* 0.0321->0.04
|
||||
* 0.0310->0.04
|
||||
* 0.0301->0.03
|
||||
* -0.991->-0.990
|
||||
* @param decimal
|
||||
* @return
|
||||
*/
|
||||
public static BigDecimal rounding(BigDecimal decimal, int n){
|
||||
Assert.isTrue(n > 0, "小数位必须大于0");
|
||||
Assert.notNull(decimal, "金额不能为空");
|
||||
return decimal.setScale(n + 1, RoundingMode.DOWN).setScale(n, RoundingMode.UP);
|
||||
}
|
||||
|
||||
}
|
||||
110
src/main/java/com/newyetai/common/util/CookieUtils.java
Normal file
110
src/main/java/com/newyetai/common/util/CookieUtils.java
Normal file
@@ -0,0 +1,110 @@
|
||||
package com.newyetai.common.util;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.newyetai.common.constant.CommonConstants;
|
||||
import com.newyetai.common.constant.CookieConstants;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* @author Jerry.li
|
||||
* @description Cookie工具类
|
||||
* @date 2020/4/2
|
||||
*/
|
||||
@Slf4j
|
||||
public class CookieUtils {
|
||||
|
||||
/**
|
||||
* 获取登录token存储cookie的key
|
||||
*
|
||||
* @return
|
||||
* @author sr on 2020-06-11
|
||||
*/
|
||||
public static String getLoginTokenCookieKey() {
|
||||
String profile = EnvironmentUtils.getProperty(CommonConstants.PROFILE_KEY);
|
||||
return CookieConstants.LOGIN_COOKIE_NAME_PREFIX
|
||||
+ (Strings.isNullOrEmpty(profile) ? null : profile.substring(0, 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定key的值
|
||||
*
|
||||
* @param key
|
||||
* @param cookies
|
||||
* @return
|
||||
*/
|
||||
public static String getCookieValue(String key, Cookie[] cookies) {
|
||||
String value = null;
|
||||
if (null != cookies && !StringUtils.isEmpty(key)) {
|
||||
for (Cookie cookie : cookies) {
|
||||
String cookieName = cookie.getName();
|
||||
if (key.equals(cookieName)) {
|
||||
value = cookie.getValue();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建Cookie
|
||||
*
|
||||
* @param name
|
||||
* @param value
|
||||
* @param domain
|
||||
* @param path
|
||||
* @param maxAge 单位:秒 -1: 代表关闭浏览器时就清除cookie 0:创建完cookie,使用后马上就删除
|
||||
* @return
|
||||
*/
|
||||
public static Cookie createCookie(String name, String value, String domain, String path,
|
||||
int maxAge) {
|
||||
Cookie cookie = new Cookie(name, value);
|
||||
try {
|
||||
if (StringUtils.isNotEmpty(domain)) {
|
||||
cookie.setDomain(domain);
|
||||
}
|
||||
path = StringUtils.isEmpty(path) ? "/" : path;
|
||||
cookie.setPath(path);
|
||||
cookie.setHttpOnly(false);
|
||||
cookie.setMaxAge(maxAge);
|
||||
return cookie;
|
||||
} catch (Exception e) {
|
||||
log.error("------创建Cookie时,系统异常:{0}", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除Cookie
|
||||
*
|
||||
* @param name 需要移除的cookie名称
|
||||
* @param domain 指定某个域名,不指定就是清空当前tomcat应用下的所有cookie
|
||||
* @param request
|
||||
* @param response
|
||||
*/
|
||||
public static void removeCookie(String name, String domain, HttpServletRequest request,
|
||||
HttpServletResponse response) {
|
||||
Cookie[] cookies = request.getCookies();
|
||||
if (!StringUtils.isEmpty(name) && null != cookies) {
|
||||
for (Cookie cookie : cookies) {
|
||||
if (name.equals(cookie.getName())) {
|
||||
Cookie tempCookie = new Cookie(name, "");
|
||||
tempCookie.setMaxAge(0);
|
||||
tempCookie.setPath("/");
|
||||
if (!StringUtils.isEmpty(domain)) {
|
||||
tempCookie.setDomain(domain);
|
||||
}
|
||||
response.addCookie(tempCookie);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
335
src/main/java/com/newyetai/common/util/DateUtils.java
Normal file
335
src/main/java/com/newyetai/common/util/DateUtils.java
Normal file
@@ -0,0 +1,335 @@
|
||||
package com.newyetai.common.util;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.bytebuddy.dynamic.scaffold.MethodGraph.Linked;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.time.FastDateFormat;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.*;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 日期工具类
|
||||
* @author sr on 2020-08-14
|
||||
*/
|
||||
@Slf4j
|
||||
public class DateUtils {
|
||||
|
||||
public static final String YYYY_MM_DD = "yyyy-MM-dd";
|
||||
public static final String YYYY_MM = "yyyy-MM";
|
||||
|
||||
public static final String YYYY = "yyyy";
|
||||
public static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
|
||||
public static final FastDateFormat YMDHMS_FORMAT =
|
||||
FastDateFormat.getInstance(DATETIME_PATTERN);
|
||||
|
||||
public static final String DATETIME_PATTERN_NO_SS = "yyyy-MM-dd HH:mm";
|
||||
public static final FastDateFormat YMDHM_FORMAT =
|
||||
FastDateFormat.getInstance(DATETIME_PATTERN_NO_SS);
|
||||
|
||||
/** yyyy-MM-dd **/
|
||||
public static final FastDateFormat YMD_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd");
|
||||
/** yyMMdd **/
|
||||
public static final FastDateFormat YMD_FORMAT2 = FastDateFormat.getInstance("yyyyMMdd");
|
||||
|
||||
/** yyMM **/
|
||||
public static final DateTimeFormatter YM_FORMAT = DateTimeFormatter.ofPattern("yyMM");
|
||||
/** yyyyMM **/
|
||||
public static final DateTimeFormatter YM_FORMAT2 = DateTimeFormatter.ofPattern("yyyyMM");
|
||||
/** yyyy-MM-dd HH:mm:ss **/
|
||||
public static final DateTimeFormatter DATETIME_FORMAT =
|
||||
DateTimeFormatter.ofPattern(DATETIME_PATTERN);
|
||||
|
||||
/**
|
||||
* @param date
|
||||
* @return yyyy-MM-dd HH:mm:ss
|
||||
* @author sr on 2020-08-14
|
||||
*/
|
||||
public static String format(Date date) {
|
||||
return YMDHMS_FORMAT.format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param date
|
||||
* @return yyyy-MM-dd HH:mm
|
||||
* @author sr on 2020-08-14
|
||||
*/
|
||||
public static String formatSplitSecond(Date date) {
|
||||
return YMDHM_FORMAT.format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param dateStr yyyy-MM-dd HH:mm:ss
|
||||
* @return
|
||||
* @author sr on 2020-08-14
|
||||
*/
|
||||
public static Date parse(String dateStr) {
|
||||
if (StringUtils.isBlank(dateStr)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return YMDHMS_FORMAT.parse(dateStr);
|
||||
} catch (ParseException e) {
|
||||
log.error("parse date error", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param date
|
||||
* @return yyyy-MM-dd
|
||||
* @author sr on 2020-08-14
|
||||
*/
|
||||
public static String formatToDay(Date date) {
|
||||
return YMD_FORMAT.format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param dateStr yyyy-MM-dd
|
||||
* @return
|
||||
* @author sr on 2020-08-14
|
||||
*/
|
||||
public static Date parseToDay(String dateStr) {
|
||||
if (StringUtils.isBlank(dateStr)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return YMD_FORMAT.parse(dateStr);
|
||||
} catch (ParseException e) {
|
||||
log.error("parse date error", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param date
|
||||
* @return yyMMdd
|
||||
* @author sr on 2022-09-22
|
||||
*/
|
||||
public static String formatToDay2(Date date) {
|
||||
return YMD_FORMAT2.format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param dateStr yyMMdd
|
||||
* @return
|
||||
* @author sr on 2022-09-22
|
||||
*/
|
||||
public static Date parseToDay2(String dateStr) {
|
||||
if (StringUtils.isBlank(dateStr)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return YMD_FORMAT2.parse(dateStr);
|
||||
} catch (ParseException e) {
|
||||
log.error("parse date error", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取当前年月(yyMM)
|
||||
* @return
|
||||
* @author sr on 2020-12-09
|
||||
*/
|
||||
public static String getCurrentYm() {
|
||||
LocalDate localDate = LocalDate.now();
|
||||
return localDate.format(DateUtils.YM_FORMAT);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前年月日(yyyy-MM-dd)
|
||||
* @return yyyy-MM-dd
|
||||
* @author sr on 2023-12-07
|
||||
*/
|
||||
public static String getCurrentYmd() {
|
||||
return formatToDay(new Date());
|
||||
}
|
||||
|
||||
public static LocalDate getLocalDate(Date date) {
|
||||
if (date == null) {
|
||||
return LocalDate.now();
|
||||
} else {
|
||||
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
|
||||
}
|
||||
}
|
||||
|
||||
public static LocalDateTime getLocalDateTime(Date date) {
|
||||
if (date == null) {
|
||||
return LocalDateTime.now();
|
||||
} else {
|
||||
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
|
||||
}
|
||||
}
|
||||
|
||||
public static Date toDate(LocalDateTime date) {
|
||||
ZonedDateTime startZdt = date.atZone(ZoneId.systemDefault());
|
||||
return Date.from(startZdt.toInstant());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定日期开始时间字符串(yyyy-MM-dd 00:00:00)
|
||||
* @param date 为空取当前时间
|
||||
* @return
|
||||
* @author sr on 2021-04-19
|
||||
*/
|
||||
public static String getStartDateTimeStr(Date date) {
|
||||
LocalDateTime localDateTime = LocalDateTime.of(getLocalDate(date), LocalTime.MIN);
|
||||
return DATETIME_FORMAT.format(localDateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前日期开始时间
|
||||
* @return
|
||||
* @author sr on 2021-04-19
|
||||
*/
|
||||
public static Date getCurrentStartDateTime() {
|
||||
return getStartDateTime(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定日期开始时间
|
||||
* @param date 为空取当前时间
|
||||
* @return
|
||||
* @author sr on 2021-04-19
|
||||
*/
|
||||
public static Date getStartDateTime(Date date) {
|
||||
return toDate(LocalDateTime.of(getLocalDate(date), LocalTime.MIN));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定日期开始时间字符串(yyyy-MM-dd 23:59:59)
|
||||
* @param date 为空取当前时间
|
||||
* @return
|
||||
* @author sr on 2021-04-19
|
||||
*/
|
||||
public static String getEndDateTimeStr(Date date) {
|
||||
LocalDateTime localDateTime = LocalDateTime.of(getLocalDate(date), LocalTime.MAX);
|
||||
return DATETIME_FORMAT.format(localDateTime);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取当前日期结束时间
|
||||
* @return
|
||||
* @author sr on 2021-04-19
|
||||
*/
|
||||
public static Date getCurrentEndDateTime() {
|
||||
return getEndDateTime(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定日期结束时间
|
||||
* @param date 为空取当前时间
|
||||
* @return
|
||||
* @author sr on 2021-04-19
|
||||
*/
|
||||
public static Date getEndDateTime(Date date) {
|
||||
return toDate(LocalDateTime.of(getLocalDate(date), LocalTime.MAX));
|
||||
}
|
||||
|
||||
/**
|
||||
* 两个日期间相差的天数 这里不包含起始日期,如果想要结果包含起始日期,则需要再结果上+1
|
||||
* @return
|
||||
*/
|
||||
public static int getDifferenceDayCount(Date date1, Date date2) {
|
||||
|
||||
LocalDate startDate = DateUtils.getLocalDate(date1);
|
||||
LocalDate endDate = DateUtils.getLocalDate(date2);
|
||||
//取正数
|
||||
return Math.abs((int) (endDate.toEpochDay() - startDate.toEpochDay()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期添加月
|
||||
* @param date
|
||||
* @param month
|
||||
* @return
|
||||
* @author sr on 2023-03-02
|
||||
*/
|
||||
public static Date addMonth(Date date, int month) {
|
||||
Calendar c = Calendar.getInstance();
|
||||
c.setTime(date);
|
||||
c.add(Calendar.MONTH, month);
|
||||
return c.getTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加天
|
||||
* @param date
|
||||
* @param amount
|
||||
* @return
|
||||
* @author sr on 2023-12-22
|
||||
*/
|
||||
public static Date addDays(Date date, int amount) {
|
||||
return add(date, Calendar.DAY_OF_MONTH, amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期变化
|
||||
* @param date
|
||||
* @param calendarField {@link Linked Calendar}
|
||||
* @param amount
|
||||
* @return
|
||||
* @author sr on 2023-12-22
|
||||
*/
|
||||
public static Date add(Date date, int calendarField, int amount) {
|
||||
if (date == null) {
|
||||
throw new IllegalArgumentException("The date must not be null");
|
||||
}
|
||||
Calendar c = Calendar.getInstance();
|
||||
c.setTime(date);
|
||||
c.add(calendarField, amount);
|
||||
return c.getTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据日期格式不同计算两个时间内的日期、月份、年
|
||||
* @param beginTime 开始时间
|
||||
* @param endTime 结束时间
|
||||
* @param dateFormat 日期格式
|
||||
* @return
|
||||
*/
|
||||
public static List<String> getDateBetween(Date beginTime, Date endTime,String dateFormat) {
|
||||
List<String> result = new ArrayList<>();
|
||||
try {
|
||||
//格式化日期
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
|
||||
Calendar min = Calendar.getInstance();
|
||||
min.setTime(beginTime);
|
||||
|
||||
Calendar max = Calendar.getInstance();
|
||||
max.setTime(endTime);
|
||||
|
||||
if(YYYY_MM_DD.equals(dateFormat)){
|
||||
while (min.before(max) || min.equals(max)) {
|
||||
result.add(sdf.format(min.getTime()));
|
||||
min.add(Calendar.DATE, 1);
|
||||
}
|
||||
}else if(YYYY_MM.equals(dateFormat)){
|
||||
while (min.before(max) || min.equals(max)) {
|
||||
result.add(sdf.format(min.getTime()));
|
||||
min.add(Calendar.MONTH, 1);
|
||||
}
|
||||
}else {
|
||||
while (min.before(max) || min.equals(max)) {
|
||||
result.add(String.valueOf(min.get(Calendar.YEAR)));
|
||||
min.add(Calendar.YEAR, 1);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.newyetai.common.util;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.newyetai.common.dto.ColumnDTO;
|
||||
import com.newyetai.common.dto.DisplayedComponentDTO;
|
||||
import com.newyetai.common.dto.MessageDTO;
|
||||
|
||||
/**
|
||||
* @author sr on 2020-04-22
|
||||
*/
|
||||
public class DisplayedComponentUtils {
|
||||
|
||||
/**
|
||||
* 数组转列配置(index 0标题,1属性key)
|
||||
*
|
||||
* @param columnArray
|
||||
* @return
|
||||
* @author sr on 2020-04-22
|
||||
*/
|
||||
public static ColumnDTO toColumnDTO(String[] columnArray) {
|
||||
return ColumnDTO.builder().title(columnArray[0]).key(columnArray[1]).dataIndex(columnArray[1]).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量数组转列配置(index 0标题,1属性key)
|
||||
*
|
||||
* @param columnArrays
|
||||
* @return
|
||||
* @author sr on 2020-04-22
|
||||
*/
|
||||
public static List<ColumnDTO> toColumnDtos(List<String[]> columnArrays) {
|
||||
return columnArrays.stream().map(DisplayedComponentUtils::toColumnDTO)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建展示对象
|
||||
*
|
||||
* @param <T>
|
||||
* @param msg
|
||||
* @param columnArrays (index 0标题,1属性key)
|
||||
* @param data
|
||||
* @return
|
||||
* @author sr on 2020-04-22
|
||||
*/
|
||||
public static <T> DisplayedComponentDTO<T> create(String msg, List<String[]> columnArrays,
|
||||
List<T> data) {
|
||||
MessageDTO messageDto = MessageDTO.builder().content(msg).build();
|
||||
return new DisplayedComponentDTO<T>(messageDto, toColumnDtos(columnArrays), data,
|
||||
Collections.emptyList());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
43
src/main/java/com/newyetai/common/util/EnumUtil.java
Normal file
43
src/main/java/com/newyetai/common/util/EnumUtil.java
Normal file
@@ -0,0 +1,43 @@
|
||||
package com.newyetai.common.util;
|
||||
|
||||
import com.newyetai.common.constant.SystemCodeEnum;
|
||||
import com.newyetai.common.functions.SFunction;
|
||||
|
||||
import java.lang.invoke.SerializedLambda;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @author chenyun
|
||||
* @description 枚举工具类
|
||||
* @date 2020/9/16 13:36
|
||||
*/
|
||||
public class EnumUtil {
|
||||
|
||||
private EnumUtil() {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param function 转换函数
|
||||
* @param r 转换的值
|
||||
* @param <E>
|
||||
* @param <R>
|
||||
* @return
|
||||
*/
|
||||
public static <E extends Enum<E>, R> E resolve(SFunction<E, R> function, R r) {
|
||||
SerializedLambda serialized = function.serialized();
|
||||
try {
|
||||
Class<E> aClass = (Class<E>) Class.forName(serialized.getImplClass().replace("/", "."));
|
||||
return (E) Arrays.stream(aClass.getEnumConstants())
|
||||
.filter(e -> {
|
||||
R apply = function.apply(e);
|
||||
return apply != null && apply.equals(r);
|
||||
})
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
74
src/main/java/com/newyetai/common/util/EnvironmentUtils.java
Normal file
74
src/main/java/com/newyetai/common/util/EnvironmentUtils.java
Normal file
@@ -0,0 +1,74 @@
|
||||
package com.newyetai.common.util;
|
||||
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import com.newyetai.common.constant.CommonConstants;
|
||||
import com.newyetai.common.constant.SystemCodeEnum;
|
||||
import com.newyetai.common.exception.util.Assert;
|
||||
|
||||
/**
|
||||
* 环境变量工具类
|
||||
*
|
||||
* @author sr on 2020-06-11
|
||||
*/
|
||||
public class EnvironmentUtils{
|
||||
|
||||
private static Environment ENVIRONMENT;
|
||||
|
||||
public EnvironmentUtils(Environment environment) {
|
||||
EnvironmentUtils.ENVIRONMENT = environment;
|
||||
getSystemCode();
|
||||
}
|
||||
|
||||
private static Integer systemCode;
|
||||
private static String applicationName;
|
||||
|
||||
/**
|
||||
* 获取环境变量
|
||||
*
|
||||
* @param key
|
||||
* @return
|
||||
* @author sr on 2020-06-17
|
||||
*/
|
||||
public static String getProperty(String key) {
|
||||
return ENVIRONMENT.getProperty(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取项目名
|
||||
*
|
||||
* @return
|
||||
* @author sr on 2020-06-17
|
||||
*/
|
||||
public static String getApplicationName() {
|
||||
if (applicationName == null) {
|
||||
applicationName = ENVIRONMENT.getProperty(CommonConstants.APPLICATION_NAME_KEY);
|
||||
}
|
||||
return applicationName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取系统编码
|
||||
*
|
||||
* @return
|
||||
* @author sr on 2020-06-17
|
||||
*/
|
||||
public static Integer getSystemCode() {
|
||||
if (systemCode == null) {
|
||||
systemCode = SystemCodeEnum.getCodeByName(getApplicationName());
|
||||
}
|
||||
Assert.notNull(systemCode, "系统标识不能为空");
|
||||
return systemCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取环境
|
||||
*
|
||||
* @return
|
||||
* @author sr on 2020-07-22
|
||||
*/
|
||||
public static String getProfile() {
|
||||
return EnvironmentUtils.getProperty(CommonConstants.PROFILE_KEY);
|
||||
}
|
||||
|
||||
}
|
||||
161
src/main/java/com/newyetai/common/util/IdcardUtils.java
Normal file
161
src/main/java/com/newyetai/common/util/IdcardUtils.java
Normal file
@@ -0,0 +1,161 @@
|
||||
package com.newyetai.common.util;
|
||||
|
||||
import com.newyetai.common.exception.BusinessException;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* @author : yzq
|
||||
* create at: 2020/10/28 14:39
|
||||
* @description: 身份证工具类
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
public class IdcardUtils {
|
||||
|
||||
// 非法的年龄,用于处理异常。
|
||||
private static final int invalidAge = -1;
|
||||
|
||||
//身份证提取正则
|
||||
private static Pattern idCardPattern = Pattern.compile("\\d{17}[\\d|x|X]|\\d{15}");
|
||||
|
||||
/**
|
||||
* 根据身份证号码计算年龄
|
||||
*
|
||||
* @param idNumber 考虑到了15位身份证,但不一定存在
|
||||
*/
|
||||
public static int getAgeByIDNumber(String idNumber) {
|
||||
String dateStr;
|
||||
if (idNumber.length() == 15) {
|
||||
dateStr = "19" + idNumber.substring(6, 12);
|
||||
} else if (idNumber.length() == 18) {
|
||||
dateStr = idNumber.substring(6, 14);
|
||||
} else {//默认是合法身份证号,但不排除有意外发生
|
||||
return invalidAge;
|
||||
}
|
||||
|
||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
|
||||
try {
|
||||
Date birthday = simpleDateFormat.parse(dateStr);
|
||||
return getAgeByDate(birthday);
|
||||
} catch (ParseException e) {
|
||||
return invalidAge;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据出生日期获取年龄
|
||||
*
|
||||
* @param birthday
|
||||
* @return
|
||||
*/
|
||||
public static int getAgeByDate(Date birthday) {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
if (calendar.getTimeInMillis() - birthday.getTime() < 0L) {
|
||||
return invalidAge;
|
||||
}
|
||||
|
||||
int yearNow = calendar.get(Calendar.YEAR);
|
||||
int monthNow = calendar.get(Calendar.MONTH);
|
||||
int dayOfMonthNow = calendar.get(Calendar.DAY_OF_MONTH);
|
||||
|
||||
calendar.setTime(birthday);
|
||||
|
||||
int yearBirthday = calendar.get(Calendar.YEAR);
|
||||
int monthBirthday = calendar.get(Calendar.MONTH);
|
||||
int dayOfMonthBirthday = calendar.get(Calendar.DAY_OF_MONTH);
|
||||
|
||||
int age = yearNow - yearBirthday;
|
||||
|
||||
if (monthNow <= monthBirthday) {
|
||||
if (monthNow == monthBirthday) {
|
||||
if (dayOfMonthNow < dayOfMonthBirthday) {
|
||||
age--;
|
||||
}
|
||||
} else {
|
||||
age--;
|
||||
}
|
||||
}
|
||||
|
||||
return age;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据身份证号判断性别
|
||||
*
|
||||
* @param idNumber
|
||||
* @return
|
||||
*/
|
||||
public static String judgeGender(String idNumber) {
|
||||
|
||||
if (idNumber.length() != 18 && idNumber.length() != 15) {
|
||||
return "";
|
||||
}
|
||||
int gender = 0;
|
||||
if (idNumber.length() == 18) {
|
||||
//如果身份证号18位,取身份证号倒数第二位
|
||||
char c = idNumber.charAt(idNumber.length() - 2);
|
||||
gender = Integer.parseInt(String.valueOf(c));
|
||||
} else {
|
||||
//如果身份证号15位,取身份证号最后一位
|
||||
char c = idNumber.charAt(idNumber.length() - 1);
|
||||
gender = Integer.parseInt(String.valueOf(c));
|
||||
}
|
||||
|
||||
if (gender % 2 == 1) {
|
||||
return "男";
|
||||
} else {
|
||||
return "女";
|
||||
}
|
||||
}
|
||||
|
||||
public static Integer getGender(String idNumber) {
|
||||
if (idNumber.length() != 18 && idNumber.length() != 15) {
|
||||
return null;
|
||||
}
|
||||
int gender = 0;
|
||||
if (idNumber.length() == 18) {
|
||||
//如果身份证号18位,取身份证号倒数第二位
|
||||
char c = idNumber.charAt(idNumber.length() - 2);
|
||||
gender = Integer.parseInt(String.valueOf(c));
|
||||
} else {
|
||||
//如果身份证号15位,取身份证号最后一位
|
||||
char c = idNumber.charAt(idNumber.length() - 1);
|
||||
gender = Integer.parseInt(String.valueOf(c));
|
||||
}
|
||||
|
||||
if (gender % 2 == 1) {
|
||||
return 1;
|
||||
} else {
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @description: 提取身份证号
|
||||
* @return: 身份证号
|
||||
*/
|
||||
public static String extractCardNo(String str){
|
||||
if (StringUtils.isBlank(str)) {
|
||||
return null;
|
||||
}
|
||||
Matcher matcher = idCardPattern.matcher(str);
|
||||
if (matcher.find()) {
|
||||
return matcher.group();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
54
src/main/java/com/newyetai/common/util/JasyptUtils.java
Normal file
54
src/main/java/com/newyetai/common/util/JasyptUtils.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package com.newyetai.common.util;
|
||||
|
||||
import org.jasypt.encryption.StringEncryptor;
|
||||
|
||||
import com.newyetai.common.exception.util.Assert;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 配置信息加解密工具
|
||||
* @author sr on 2021-11-11
|
||||
*/
|
||||
@Slf4j
|
||||
public class JasyptUtils {
|
||||
|
||||
private static StringEncryptor stringEncryptor;
|
||||
|
||||
public JasyptUtils(StringEncryptor stringEncryptor) {
|
||||
Assert.isNull(JasyptUtils.stringEncryptor, "已存在加密对象");
|
||||
JasyptUtils.stringEncryptor = stringEncryptor;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密
|
||||
* @param str
|
||||
* @return
|
||||
* @author sr on 2021-11-11
|
||||
*/
|
||||
public static String encrypt(String str) {
|
||||
try {
|
||||
return stringEncryptor.encrypt(str);
|
||||
} catch (Exception e) {
|
||||
log.info("加密失败:", e);
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密
|
||||
* @param encodedStr
|
||||
* @return
|
||||
* @author sr on 2021-11-11
|
||||
*/
|
||||
public static String decrypt(String encodedStr) {
|
||||
try {
|
||||
return stringEncryptor.decrypt(encodedStr);
|
||||
} catch (Exception e) {
|
||||
log.info("解密失败:", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
178
src/main/java/com/newyetai/common/util/JsonUtils.java
Normal file
178
src/main/java/com/newyetai/common/util/JsonUtils.java
Normal file
@@ -0,0 +1,178 @@
|
||||
package com.newyetai.common.util;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.newyetai.common.annotation.MaskAnontationIntrospector;
|
||||
import com.newyetai.tkmybatis.pojo.Page;
|
||||
|
||||
/**
|
||||
* json 工具类
|
||||
*
|
||||
* @author on 2019-11-05
|
||||
*
|
||||
*/
|
||||
public class JsonUtils {
|
||||
|
||||
/** 序列化所有(带mask) **/
|
||||
private static ObjectMapper simpleDateMapper;
|
||||
/** 序列化不为null(带mask) **/
|
||||
private static ObjectMapper simpleDateNoEmptyMapper;
|
||||
|
||||
/** 默认(不带mask) **/
|
||||
private static ObjectMapper defaultMapper;
|
||||
|
||||
static {
|
||||
simpleDateMapper = buildObjectMapper();
|
||||
simpleDateMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||
simpleDateMapper.setAnnotationIntrospector(new MaskAnontationIntrospector());
|
||||
simpleDateNoEmptyMapper = buildObjectMapper();
|
||||
simpleDateNoEmptyMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||
// 配置mapper忽略空属性 默认不忽略
|
||||
simpleDateNoEmptyMapper.setSerializationInclusion(Include.NON_EMPTY);
|
||||
simpleDateNoEmptyMapper.setAnnotationIntrospector(new MaskAnontationIntrospector());
|
||||
defaultMapper = buildObjectMapper();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* (mask)对象转json字符串(null也序列化)
|
||||
* @param object
|
||||
* @return
|
||||
* @author sr on 2020-08-14
|
||||
*/
|
||||
public static String toJsonString(Object object) {
|
||||
return toJsonString(simpleDateMapper, object);
|
||||
}
|
||||
|
||||
/**
|
||||
* (mask)对象转json字符串(null不序列化)
|
||||
* @param object
|
||||
* @return
|
||||
* @author sr on 2021-10-21
|
||||
*/
|
||||
public static String toNoEmptyJson(Object object) {
|
||||
return toJsonString(simpleDateNoEmptyMapper, object);
|
||||
}
|
||||
|
||||
/**
|
||||
* (mask)对象转json字符串
|
||||
* @param object
|
||||
* @param nullable null是否序列化
|
||||
*
|
||||
*/
|
||||
public static String toJsonString(Object object, boolean nullable) {
|
||||
ObjectMapper objectMapper;
|
||||
if (nullable) {
|
||||
objectMapper = simpleDateMapper;
|
||||
} else {
|
||||
objectMapper = simpleDateNoEmptyMapper;
|
||||
}
|
||||
return toJsonString(objectMapper, object);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对象转json字符串
|
||||
* @param objectMapper
|
||||
* @param object
|
||||
* @return
|
||||
* @author sr on 2021-10-21
|
||||
*/
|
||||
public static String toJsonString(ObjectMapper objectMapper, Object object) {
|
||||
// 为了使JSON视觉上的可读性,增加一行如下代码,注意,在生产中不需要这样,因为这样会增大Json的内容
|
||||
// mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
|
||||
String jsonString = null;
|
||||
try {
|
||||
jsonString = objectMapper.writeValueAsString(object);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return jsonString;
|
||||
}
|
||||
|
||||
/**
|
||||
* 对象转json字符串(默认)
|
||||
* @param object
|
||||
* @return
|
||||
* @author sr on 2024-04-09
|
||||
*/
|
||||
public static String toDefaultJsonString(Object object) {
|
||||
return toJsonString(defaultMapper, object);
|
||||
}
|
||||
|
||||
/**
|
||||
* json字符串转指定对象
|
||||
*/
|
||||
public static <T> T toObject(String json, Class<T> clazz) {
|
||||
ObjectMapper mapper = defaultMapper;
|
||||
try {
|
||||
return mapper.readValue(json, clazz);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T toObject(String json, TypeReference<T> valueTypeRef) {
|
||||
ObjectMapper mapper = defaultMapper;
|
||||
try {
|
||||
return mapper.readValue(json, valueTypeRef);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* json字符串转指定对象
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Map<String, Object> toMap(String json) {
|
||||
ObjectMapper mapper = defaultMapper;
|
||||
try {
|
||||
JavaType javaType = mapper.getTypeFactory().constructParametricType(LinkedHashMap.class,
|
||||
new Class[] {String.class, Object.class});
|
||||
return (Map<String, Object>) mapper.readValue(json, javaType);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> List<T> toList(String json, Class<T> clazz) {
|
||||
ObjectMapper mapper = defaultMapper;
|
||||
try {
|
||||
JavaType javaType = mapper.getTypeFactory().constructCollectionType(List.class, clazz);
|
||||
return (List<T>) mapper.readValue(json, javaType);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static ObjectMapper buildObjectMapper() {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
// 当反序列化json时,未知属性会引起的反序列化被打断,这里我们禁用未知属性打断反序列化功能,
|
||||
// 因为,例如json里有10个属性,而我们的bean中只定义了2个属性,其它8个属性将被忽略
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
return mapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* object转指定类型对象
|
||||
*
|
||||
* @param <T>
|
||||
* @param object
|
||||
* @param clazz
|
||||
* @return
|
||||
* @author sr on 2020-05-25
|
||||
*/
|
||||
public static <T> T objToClassObj(Object object, Class<T> clazz) {
|
||||
return defaultMapper.convertValue(object, clazz);
|
||||
}
|
||||
|
||||
}
|
||||
56
src/main/java/com/newyetai/common/util/ListUtil.java
Normal file
56
src/main/java/com/newyetai/common/util/ListUtil.java
Normal file
@@ -0,0 +1,56 @@
|
||||
package com.newyetai.common.util;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
|
||||
public class ListUtil {
|
||||
|
||||
private ListUtil() {}
|
||||
|
||||
/**
|
||||
* Collection 转 Map
|
||||
*
|
||||
* @param collection 集合对象
|
||||
* @param func 处理函数
|
||||
* @param <T> 原始类型
|
||||
* @param <R> 目标类型
|
||||
* @return
|
||||
*/
|
||||
public static <T, R> Map<R, List<T>> toMap(Collection<T> collection, Function<T, R> func) {
|
||||
Map<R, List<T>> resMap;
|
||||
if (CollectionUtils.isEmpty(collection)) {
|
||||
return Maps.newHashMap();
|
||||
}
|
||||
int size = collection.size();
|
||||
resMap = Maps.newHashMapWithExpectedSize(size);
|
||||
for (T obj : collection) {
|
||||
R apply = func.apply(obj);
|
||||
List<T> ts = resMap.get(apply);
|
||||
if (ts == null) {
|
||||
ts = Lists.newArrayList();
|
||||
}
|
||||
ts.add(obj);
|
||||
resMap.put(apply, ts);
|
||||
}
|
||||
return resMap;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取首个记录
|
||||
* @param <T>
|
||||
* @param list
|
||||
* @return 可为null
|
||||
* @author sr on 2021-08-10
|
||||
*/
|
||||
public static <T> T first(List<T> list) {
|
||||
return CollectionUtils.isEmpty(list) ? null : list.get(0);
|
||||
}
|
||||
}
|
||||
25
src/main/java/com/newyetai/common/util/MockUtils.java
Normal file
25
src/main/java/com/newyetai/common/util/MockUtils.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package com.newyetai.common.util;
|
||||
|
||||
import com.newyetai.common.service.IMockProvider;
|
||||
|
||||
public class MockUtils {
|
||||
|
||||
public static IMockProvider mockProvider;
|
||||
|
||||
public MockUtils(IMockProvider mockProvider) {
|
||||
MockUtils.mockProvider = mockProvider;
|
||||
}
|
||||
|
||||
public static void mockPlatUser() {
|
||||
mockProvider.mockPlatUser();
|
||||
}
|
||||
|
||||
public static void mockUser(Long userId, Long orgId) {
|
||||
mockProvider.mockUser(userId, orgId);
|
||||
}
|
||||
|
||||
public static void clearUser() {
|
||||
mockProvider.clearUser();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.newyetai.common.util;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* copy fastjson
|
||||
*
|
||||
* @author sr on 2020-09-02
|
||||
*/
|
||||
public class ParameterizedTypeImpl implements ParameterizedType {
|
||||
|
||||
private final Type[] actualTypeArguments;
|
||||
private final Type ownerType;
|
||||
private final Type rawType;
|
||||
|
||||
public ParameterizedTypeImpl(Type[] actualTypeArguments, Type ownerType, Type rawType) {
|
||||
this.actualTypeArguments = actualTypeArguments;
|
||||
this.ownerType = ownerType;
|
||||
this.rawType = rawType;
|
||||
}
|
||||
|
||||
public Type[] getActualTypeArguments() {
|
||||
return actualTypeArguments;
|
||||
}
|
||||
|
||||
public Type getOwnerType() {
|
||||
return ownerType;
|
||||
}
|
||||
|
||||
public Type getRawType() {
|
||||
return rawType;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (o == null || getClass() != o.getClass())
|
||||
return false;
|
||||
|
||||
ParameterizedTypeImpl that = (ParameterizedTypeImpl) o;
|
||||
|
||||
// Probably incorrect - comparing Object[] arrays with Arrays.equals
|
||||
if (!Arrays.equals(actualTypeArguments, that.actualTypeArguments))
|
||||
return false;
|
||||
if (ownerType != null ? !ownerType.equals(that.ownerType) : that.ownerType != null)
|
||||
return false;
|
||||
return rawType != null ? rawType.equals(that.rawType) : that.rawType == null;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = actualTypeArguments != null ? Arrays.hashCode(actualTypeArguments) : 0;
|
||||
result = 31 * result + (ownerType != null ? ownerType.hashCode() : 0);
|
||||
result = 31 * result + (rawType != null ? rawType.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
35
src/main/java/com/newyetai/common/util/ProductConfig.java
Normal file
35
src/main/java/com/newyetai/common/util/ProductConfig.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package com.newyetai.common.util;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Data
|
||||
@ApiModel("限额")
|
||||
public class ProductConfig implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 3619751184797118876L;
|
||||
|
||||
@ApiModelProperty("产品id")
|
||||
private Long productId;
|
||||
|
||||
@ApiModelProperty("年龄上限")
|
||||
private Integer maxAge;
|
||||
|
||||
@ApiModelProperty("年龄下限")
|
||||
private Integer minAge;
|
||||
|
||||
@ApiModelProperty("限制额度-月")
|
||||
private BigDecimal limitAmountMonth;
|
||||
|
||||
@ApiModelProperty("限制额度-年")
|
||||
private BigDecimal limitAmountYear;
|
||||
|
||||
}
|
||||
50
src/main/java/com/newyetai/common/util/RandomUtils.java
Normal file
50
src/main/java/com/newyetai/common/util/RandomUtils.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package com.newyetai.common.util;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
|
||||
/**
|
||||
* 随机工具类型
|
||||
* @author sr on 2020-01-02
|
||||
*/
|
||||
public class RandomUtils {
|
||||
|
||||
/**
|
||||
* 随机六位数
|
||||
* @return
|
||||
* @author sr on 2020-01-02
|
||||
*/
|
||||
public static int nextSixInt() {
|
||||
return org.apache.commons.lang3.RandomUtils.nextInt(100000, 1000000);
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机六位验证码
|
||||
* @return
|
||||
* @author sr on 2020-01-02
|
||||
*/
|
||||
public static String nextSixVerificationCode() {
|
||||
return Integer.toString(RandomUtils.nextSixInt());
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成指定长度的随机字符串
|
||||
* @param len 指定长度
|
||||
* @return
|
||||
*/
|
||||
public static String getRandomString(int len) {
|
||||
return RandomStringUtils.randomAlphabetic(len);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成uuid,去掉-
|
||||
* @return
|
||||
* @author sr on 2019-12-10
|
||||
*/
|
||||
public static String generateUUID() {
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
return uuid.replace("-", "");
|
||||
}
|
||||
|
||||
}
|
||||
53
src/main/java/com/newyetai/common/util/ReflectionUtils.java
Normal file
53
src/main/java/com/newyetai/common/util/ReflectionUtils.java
Normal file
@@ -0,0 +1,53 @@
|
||||
package com.newyetai.common.util;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
* @author Jerry.li
|
||||
* @description 反射工具类
|
||||
* @date 2020/4/21
|
||||
*/
|
||||
@Slf4j
|
||||
public class ReflectionUtils {
|
||||
|
||||
/**
|
||||
* 获取对象属性字段数组
|
||||
* @param object 对象
|
||||
* @return fieldNames
|
||||
*/
|
||||
public static String[] getFiledNames(Object object) {
|
||||
Field[] fields = object.getClass().getDeclaredFields();
|
||||
String[] fieldNames = new String[fields.length];
|
||||
for (int i = 0; i < fields.length; i++) {
|
||||
fieldNames[i] = fields[i].getName();
|
||||
}
|
||||
return fieldNames;
|
||||
}
|
||||
|
||||
/***
|
||||
* 根据属性名称获取对象属性值
|
||||
* @param fieldName 字段名称
|
||||
* @param object 对象
|
||||
* @return Object
|
||||
*/
|
||||
public static Object getValueByFieldName(String fieldName, Object object) {
|
||||
try {
|
||||
// 序列化字段返回null
|
||||
if (StringUtils.equalsIgnoreCase(fieldName,"serialVersionUID")){
|
||||
return null;
|
||||
}
|
||||
String firstLetter = fieldName.substring(0, 1).toUpperCase();
|
||||
String getter = "get" + firstLetter + fieldName.substring(1);
|
||||
Method method = object.getClass().getMethod(getter);
|
||||
return method.invoke(object);
|
||||
} catch (Exception e) {
|
||||
log.error("------ReflectionUtils getValueByFieldName error: {0}", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
179
src/main/java/com/newyetai/common/util/RequestUtils.java
Normal file
179
src/main/java/com/newyetai/common/util/RequestUtils.java
Normal file
@@ -0,0 +1,179 @@
|
||||
package com.newyetai.common.util;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.net.InternetDomainName;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @author Jerry.li
|
||||
* @description 网络请求工具类
|
||||
* @date 2020/4/16
|
||||
*/
|
||||
@Slf4j
|
||||
public class RequestUtils {
|
||||
|
||||
/** ajax请求key **/
|
||||
public static final String AJAX_REQUEST_HEADER_KEY = "x-requested-with";
|
||||
/** ajax请求value **/
|
||||
public static final String AJAX_REQUEST_HEADER_VALUE = "XMLHttpRequest";
|
||||
/** feign请求key **/
|
||||
public static final String FEIGN_REQUEST_HEADER_KEY = "feign-requested";
|
||||
/** feign请求value **/
|
||||
public static final String FEIGN_REQUEST_HEADER_VALUE = "feignHttpRequest";
|
||||
/** 域名分割符 **/
|
||||
public static final String DOMAIN_SEPARATOR = "://";
|
||||
/** 路径分割符 **/
|
||||
public static final String PATH_SEPARATOR = "/";
|
||||
|
||||
/** origin_host **/
|
||||
public static final String ORIGIN_HOST_HEADER_KEY = "origin_host";
|
||||
|
||||
/**
|
||||
* 判断是否是ajax请求
|
||||
*/
|
||||
public static boolean isAjaxRequest(HttpServletRequest request) {
|
||||
String xReq = request.getHeader(AJAX_REQUEST_HEADER_KEY);
|
||||
return !Strings.isNullOrEmpty(xReq) && AJAX_REQUEST_HEADER_VALUE.equalsIgnoreCase(xReq);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否是feign请求
|
||||
*/
|
||||
public static boolean isFeignRequest(HttpServletRequest request) {
|
||||
String xReq = request.getHeader(FEIGN_REQUEST_HEADER_KEY);
|
||||
return !Strings.isNullOrEmpty(xReq) && FEIGN_REQUEST_HEADER_VALUE.equalsIgnoreCase(xReq);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求域名(不带协议)
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
* @author sr on 2020-03-10
|
||||
*/
|
||||
public static String getDomain(HttpServletRequest request) {
|
||||
String hostHeader = request.getHeader(RequestUtils.ORIGIN_HOST_HEADER_KEY);
|
||||
StringBuffer url = request.getRequestURL();
|
||||
log.debug("request url:{}", url);
|
||||
if (StringUtils.isNoneBlank(hostHeader)) {
|
||||
log.debug("get request head origin_host:{}", hostHeader);
|
||||
return hostHeader;
|
||||
}
|
||||
int start = url.length() - request.getRequestURI().length();
|
||||
int subStart =
|
||||
url.indexOf(RequestUtils.DOMAIN_SEPARATOR) + RequestUtils.DOMAIN_SEPARATOR.length();
|
||||
return url.delete(start, url.length()).substring(subStart);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求根域名
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
* @author sr on 2020-06-10
|
||||
*/
|
||||
public static String getRootDomain(HttpServletRequest request) {
|
||||
String domainStr = getDomain(request);
|
||||
try {
|
||||
String rootDomain = InternetDomainName.from(domainStr).topPrivateDomain().toString();
|
||||
return rootDomain;
|
||||
} catch (IllegalArgumentException e) {
|
||||
log.debug("{}-rootDomain error:{}", domainStr, e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取带协议的域名地址
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
* @author sr on 2020-03-15
|
||||
*/
|
||||
public static String getHttpDomain(HttpServletRequest request) {
|
||||
StringBuffer url = request.getRequestURL();
|
||||
int start = url.length() - request.getRequestURI().length();
|
||||
return url.delete(start, url.length()).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换域名
|
||||
*
|
||||
* @param urlString
|
||||
* @param domain
|
||||
* @author sr on 2020-03-10
|
||||
*/
|
||||
public static String replaceDomain(String urlString, String domain) {
|
||||
StringBuilder urlBuilder = new StringBuilder(urlString);
|
||||
int start = urlBuilder.indexOf(DOMAIN_SEPARATOR) + DOMAIN_SEPARATOR.length();
|
||||
int endIndex = urlBuilder.indexOf(PATH_SEPARATOR,
|
||||
urlBuilder.indexOf(DOMAIN_SEPARATOR) + DOMAIN_SEPARATOR.length());
|
||||
if (endIndex <= 0) {
|
||||
endIndex = urlString.length();
|
||||
}
|
||||
return urlBuilder.replace(start, endIndex, domain).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求用户IP地址
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
public static String getIpAddress(HttpServletRequest request) {
|
||||
String ipAddress = request.getHeader("x-forwarded-for");
|
||||
String unknown = "unknown";
|
||||
if (ipAddress == null || ipAddress.length() == 0 || unknown.equalsIgnoreCase(ipAddress)) {
|
||||
ipAddress = request.getHeader("Proxy-Client-IP");
|
||||
}
|
||||
if (ipAddress == null || ipAddress.length() == 0 || unknown.equalsIgnoreCase(ipAddress)) {
|
||||
ipAddress = request.getHeader("WL-Proxy-Client-IP");
|
||||
}
|
||||
if (ipAddress == null || ipAddress.length() == 0 || unknown.equalsIgnoreCase(ipAddress)) {
|
||||
ipAddress = request.getRemoteAddr();
|
||||
String localhost = "127.0.0.1";
|
||||
if (localhost.equals(ipAddress) || "0:0:0:0:0:0:0:1".equals(ipAddress)) {
|
||||
//根据网卡获取本机配置的IP地址
|
||||
InetAddress inetAddress;
|
||||
try {
|
||||
inetAddress = InetAddress.getLocalHost();
|
||||
ipAddress = inetAddress.getHostAddress();
|
||||
} catch (UnknownHostException e) {
|
||||
log.error("------获取请求用户IP地址时,异常: {0}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//对于通过多个代理的情况,第一个IP为客户端真实的IP地址,多个IP按照','分割
|
||||
if (null != ipAddress && ipAddress.length() > 15) {
|
||||
//"***.***.***.***".length() = 15
|
||||
if (ipAddress.indexOf(',') != -1) {
|
||||
ipAddress = ipAddress.substring(0, ipAddress.indexOf(','));
|
||||
}
|
||||
}
|
||||
return ipAddress;
|
||||
}
|
||||
|
||||
public static String getHostIp() {
|
||||
try {
|
||||
String hostname = System.getenv("HOSTNAME");
|
||||
String hostIp = InetAddress.getByName(hostname).getHostAddress();
|
||||
if ("127.0.0.1".equals(hostIp) || "0:0:0:0:0:0:0:1".equals(hostIp)) {
|
||||
return InetAddress.getLocalHost().getHostAddress();
|
||||
}
|
||||
return hostIp;
|
||||
} catch (UnknownHostException e) {
|
||||
}
|
||||
return "127.0.0.1";
|
||||
}
|
||||
|
||||
}
|
||||
27
src/main/java/com/newyetai/common/util/ResponseUtils.java
Normal file
27
src/main/java/com/newyetai/common/util/ResponseUtils.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package com.newyetai.common.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @author Jerry.li
|
||||
* @description 响应工具类
|
||||
* @date 2020/4/16
|
||||
*/
|
||||
@Slf4j
|
||||
public class ResponseUtils {
|
||||
|
||||
public static void printJson(HttpServletResponse response, Object data) {
|
||||
response.setContentType("application/json;charset=utf-8");
|
||||
try (PrintWriter out = response.getWriter();) {
|
||||
out.print(JsonUtils.toJsonString(data));
|
||||
out.flush();
|
||||
} catch (IOException ex) {
|
||||
log.error("------响应异常", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.newyetai.common.util;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
import com.newyetai.common.constant.CommonConstants;
|
||||
|
||||
import toolgood.words.IllegalWordsSearch;
|
||||
import toolgood.words.IllegalWordsSearchResult;
|
||||
|
||||
public class SensitiveWordUtils {
|
||||
|
||||
static IllegalWordsSearch search;
|
||||
|
||||
static {
|
||||
List<String> list = new ArrayList<>();
|
||||
try (BufferedReader bufferedReader = new BufferedReader(
|
||||
new InputStreamReader(new ClassPathResource("config" + File.separator + "sensi_words.txt").getInputStream()))) {
|
||||
for (String line = bufferedReader.readLine(); line != null; line =
|
||||
bufferedReader.readLine()) {
|
||||
list.add(line);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("sensi_words.txt加载失败");
|
||||
}
|
||||
search = new IllegalWordsSearch();
|
||||
search.SetKeywords(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验字符串敏感词是否通过
|
||||
*
|
||||
* @param str
|
||||
* @return
|
||||
* @author sr on 2024-06-28
|
||||
*/
|
||||
public static boolean check(String str) {
|
||||
//todo 先全通过
|
||||
return true;
|
||||
// if (StringUtils.isBlank(str)) {
|
||||
// return true;
|
||||
// }
|
||||
// return !search.ContainsAny(str);
|
||||
}
|
||||
|
||||
public static String findFirst(String str) {
|
||||
IllegalWordsSearchResult result = search.FindFirst(str);
|
||||
if (result != null) {
|
||||
return result.Keyword;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String findAll(String str) {
|
||||
List<IllegalWordsSearchResult> results = search.FindAll(str);
|
||||
if (results != null) {
|
||||
return results.stream().map(r -> r.Keyword).collect(Collectors.joining(CommonConstants.COMMA));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
101
src/main/java/com/newyetai/common/util/SpringContextHolder.java
Normal file
101
src/main/java/com/newyetai/common/util/SpringContextHolder.java
Normal file
@@ -0,0 +1,101 @@
|
||||
package com.newyetai.common.util;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
/**
|
||||
* spring上下文工具类
|
||||
*
|
||||
* @author sr on 2020-05-20
|
||||
*/
|
||||
@Component
|
||||
public class SpringContextHolder implements ApplicationContextAware {
|
||||
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
SpringContextHolder.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
public static ApplicationContext getApplicationContext() {
|
||||
return applicationContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取bean
|
||||
*
|
||||
* @param <T>
|
||||
* @param beanId
|
||||
* @return
|
||||
* @author sr on 2020-05-20
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T getBean(String beanId) {
|
||||
return (T) applicationContext.getBean(beanId);
|
||||
}
|
||||
|
||||
public static <T> T getBean(Class<T> clazz) {
|
||||
return applicationContext.getBean(clazz);
|
||||
}
|
||||
|
||||
public static Optional<ServletRequestAttributes> getRequestAttributes() {
|
||||
return Optional
|
||||
.ofNullable((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前线程的request
|
||||
*/
|
||||
public static HttpServletRequest getRequest() {
|
||||
return getRequestAttributes().map(ServletRequestAttributes :: getRequest).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前线程的response
|
||||
*/
|
||||
public static HttpServletResponse getResponse() {
|
||||
return getRequestAttributes().map(ServletRequestAttributes :: getResponse).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取HttpSession
|
||||
*/
|
||||
public static HttpSession getHttpSession() {
|
||||
return getRequest().getSession();
|
||||
}
|
||||
|
||||
/**
|
||||
* 把值放入session
|
||||
*/
|
||||
public static void setSessionAttribute(String key, Serializable entity) {
|
||||
getHttpSession().setAttribute(key, entity);
|
||||
}
|
||||
|
||||
public static Object getSessionAttribute(String key) {
|
||||
return getHttpSession().getAttribute(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 把值放入当前request
|
||||
*/
|
||||
public static void setRequestAttribute(String key, Object entity) {
|
||||
getRequest().setAttribute(key, entity);
|
||||
}
|
||||
|
||||
public static Object getRequestAttribute(String key) {
|
||||
return getRequest().getAttribute(key);
|
||||
}
|
||||
|
||||
}
|
||||
101
src/main/java/com/newyetai/common/util/StrUtils.java
Normal file
101
src/main/java/com/newyetai/common/util/StrUtils.java
Normal file
@@ -0,0 +1,101 @@
|
||||
package com.newyetai.common.util;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.TreeMap;
|
||||
|
||||
import com.newyetai.common.constant.CommonConstants;
|
||||
import com.newyetai.common.exception.BusinessException;
|
||||
|
||||
/**
|
||||
* @author sr on 2021-02-03
|
||||
*/
|
||||
public class StrUtils {
|
||||
|
||||
/**
|
||||
* 大驼峰转小驼峰(AaBb -> aaBb)
|
||||
*
|
||||
* @param upperCamel
|
||||
* @return
|
||||
* @author sr on 2021-02-03
|
||||
*/
|
||||
public static String toLowerCamel(String upperCamel) {
|
||||
StringBuilder sb =
|
||||
new StringBuilder(Character.toString(Character.toLowerCase(upperCamel.charAt(0))));
|
||||
return sb.append(upperCamel.substring(1)).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 驼峰转蛇形(AaBb -> aa_bb)
|
||||
*
|
||||
* @param upperCamel
|
||||
* @return
|
||||
* @author sr on 2021-02-03
|
||||
*/
|
||||
public static String toSnake(String camel) {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
boolean first = true;
|
||||
for (char c : camel.toCharArray()) {
|
||||
char nc = Character.toLowerCase(c);
|
||||
if (Character.isUpperCase(c) && !first) {
|
||||
stringBuilder.append('_').append(nc);
|
||||
} else {
|
||||
stringBuilder.append(nc);
|
||||
first = false;
|
||||
}
|
||||
}
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建升序参数字符串(xx=**&xx=**)
|
||||
* @param paramData
|
||||
* @return
|
||||
* @author sr on 2022-02-10
|
||||
*/
|
||||
public static String buildAscQueryValue(Object paramData) {
|
||||
Map<String, Object> paramDataMap = null;
|
||||
if (paramData instanceof Map) {
|
||||
paramDataMap = paramDataMap;
|
||||
} else {
|
||||
try {
|
||||
paramDataMap = JsonUtils.objToClassObj(paramData, Map.class);
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException("参数对象格式错误");
|
||||
}
|
||||
}
|
||||
if (paramDataMap == null || paramDataMap.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
Map<String, Object> parameters = new TreeMap<String, Object>();
|
||||
for (Entry<String, Object> entry : paramDataMap.entrySet()) {
|
||||
Object value = entry.getValue();
|
||||
if (value != null) {
|
||||
parameters.put(entry.getKey(), value);
|
||||
}
|
||||
}
|
||||
if (parameters.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
StringBuilder paramstr = new StringBuilder();
|
||||
for (Entry<String, Object> entry : parameters.entrySet()) {
|
||||
paramstr.append(entry.getKey()).append(CommonConstants.EQUAL)
|
||||
.append(entry.getValue()).append(CommonConstants.AND);
|
||||
}
|
||||
return paramstr.substring(0, paramstr.length() - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 不为空/空字符串/"null"字符串
|
||||
* @param str
|
||||
* @return
|
||||
* @author sr on 2022-02-10
|
||||
*/
|
||||
public static boolean isNotNull(String str) {
|
||||
return StringUtils.isNotBlank(str) && !"null".equals(str);
|
||||
}
|
||||
|
||||
}
|
||||
38
src/main/java/com/newyetai/common/util/TracerUtils.java
Normal file
38
src/main/java/com/newyetai/common/util/TracerUtils.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package com.newyetai.common.util;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.newyetai.common.trace.ITracer;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 链路工具类
|
||||
*
|
||||
* @author sr on 2020-10-29
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class TracerUtils {
|
||||
|
||||
private static ITracer tracer;
|
||||
|
||||
@Autowired
|
||||
public void setTracer(ITracer tracer) {
|
||||
TracerUtils.tracer = tracer;
|
||||
}
|
||||
|
||||
public static String getTraceId() {
|
||||
return tracer.getTraceId();
|
||||
}
|
||||
|
||||
public static String getSpanId() {
|
||||
return tracer.getSpanId();
|
||||
}
|
||||
|
||||
public static String getParentId() {
|
||||
return tracer.getParentId();
|
||||
}
|
||||
|
||||
}
|
||||
73
src/main/java/com/newyetai/common/util/TreeHelper.java
Normal file
73
src/main/java/com/newyetai/common/util/TreeHelper.java
Normal file
@@ -0,0 +1,73 @@
|
||||
package com.newyetai.common.util;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 树型结构工具类
|
||||
*
|
||||
* @author sr on 2019-12-11
|
||||
*/
|
||||
public class TreeHelper {
|
||||
|
||||
private TreeHelper() {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 构建树型数据
|
||||
*
|
||||
* @param parentId 父id
|
||||
* @param nodeList 节点列表
|
||||
* @return {@link List<T>}
|
||||
*/
|
||||
public static <I extends Serializable, T extends TreeNode<I, T>> List<T> buildTree(I parentId, List<T> nodeList) {
|
||||
// 根节点列表
|
||||
List<T> list = Lists.newArrayList();
|
||||
Map<Serializable, T> treeMap = nodeList.stream()
|
||||
.collect(Collectors.toMap(TreeNode::getId, treeNode -> treeNode));
|
||||
|
||||
nodeList.forEach(node -> {
|
||||
if (Objects.equal(parentId, node.getParentId())) {
|
||||
list.add(node);
|
||||
}
|
||||
// 父节点
|
||||
TreeNode<I, T> parentNode = treeMap.get(node.getParentId());
|
||||
if (parentNode != null) {
|
||||
parentNode.addChildren(node);
|
||||
}
|
||||
});
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 返回子节点Ids
|
||||
*
|
||||
* @param parentId 父id
|
||||
* @param nodeList 节点列表
|
||||
* @return {@link Set<Long>}
|
||||
*/
|
||||
public static <T extends TreeNode<Long, T>> Set<Long> childrenNodeIds(Long parentId, List<T> nodeList) {
|
||||
// 根节点列表
|
||||
Set<Long> set = Sets.newHashSet(parentId);
|
||||
|
||||
nodeList.sort((a, b) -> a.getParentId().compareTo(b.getParentId()));
|
||||
|
||||
nodeList.forEach(node -> {
|
||||
if (set.contains(node.getParentId())) {
|
||||
set.add(node.getId());
|
||||
}
|
||||
});
|
||||
set.remove(parentId);
|
||||
return set;
|
||||
}
|
||||
|
||||
}
|
||||
37
src/main/java/com/newyetai/common/util/TreeNode.java
Normal file
37
src/main/java/com/newyetai/common/util/TreeNode.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package com.newyetai.common.util;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 树型结构
|
||||
*
|
||||
* @param <I>
|
||||
* @author sr on 2019-12-11
|
||||
*/
|
||||
public interface TreeNode<I extends Serializable, T extends TreeNode> {
|
||||
|
||||
/**
|
||||
* id
|
||||
*
|
||||
* @return
|
||||
* @author sr on 2019-12-11
|
||||
*/
|
||||
I getId();
|
||||
|
||||
/**
|
||||
* 父id
|
||||
*
|
||||
* @return
|
||||
* @author sr on 2019-12-11
|
||||
*/
|
||||
I getParentId();
|
||||
|
||||
/**
|
||||
* 添加子节点
|
||||
*
|
||||
* @param node
|
||||
* @author sr on 2019-12-11
|
||||
*/
|
||||
void addChildren(T node);
|
||||
|
||||
}
|
||||
206
src/main/java/com/newyetai/common/util/UserContextHolder.java
Normal file
206
src/main/java/com/newyetai/common/util/UserContextHolder.java
Normal file
@@ -0,0 +1,206 @@
|
||||
package com.newyetai.common.util;
|
||||
|
||||
import com.newyetai.common.constant.CommonConstants;
|
||||
import com.newyetai.common.exception.NoneLoginException;
|
||||
import com.newyetai.common.exception.util.Assert;
|
||||
import com.newyetai.usercenter.dto.IUserDetail;
|
||||
import com.newyetai.usercenter.user.constant.UserTypeEnum;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 项目上下文
|
||||
*
|
||||
* @author on 2019-11-05
|
||||
*/
|
||||
@Slf4j
|
||||
public class UserContextHolder {
|
||||
|
||||
private static final ThreadLocal<IUserDetail> contextHolder = new ThreadLocal<IUserDetail>();
|
||||
|
||||
/**
|
||||
* 清除
|
||||
*
|
||||
* @author sr on 2020-07-30
|
||||
*/
|
||||
public static void clearContext() {
|
||||
contextHolder.remove();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置
|
||||
*
|
||||
* @param context
|
||||
* @author sr on 2020-07-30
|
||||
*/
|
||||
public static void setContext(IUserDetail context) {
|
||||
contextHolder.set(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录用户
|
||||
*
|
||||
* @param auth
|
||||
* @return
|
||||
* @author sr on 2020-06-16
|
||||
*/
|
||||
public static IUserDetail getLoginUser() {
|
||||
return contextHolder.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录用户ID
|
||||
*/
|
||||
public static Long getLoginUserId() {
|
||||
IUserDetail userDTO = getLoginUser();
|
||||
return userDTO == null ? null : userDTO.getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录机构ID
|
||||
*/
|
||||
public static Long getLoginOrgId() {
|
||||
IUserDetail userDTO = getLoginUser();
|
||||
return userDTO == null ? null : userDTO.getOrgId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取登录用户(未登录抛异常)
|
||||
*
|
||||
* @return
|
||||
* @author sr on 2019-10-11
|
||||
*/
|
||||
public static IUserDetail getExistLoginUser() {
|
||||
IUserDetail userDTO = getLoginUser();
|
||||
if (userDTO == null) {
|
||||
throw new NoneLoginException();
|
||||
}
|
||||
return userDTO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取登录用户id(未登录抛异常)
|
||||
*
|
||||
* @return
|
||||
* @author sr on 2019-10-11
|
||||
*/
|
||||
public static Long getExistLoginUserId() {
|
||||
return getExistLoginUser().getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取登录机构id(未登录抛异常)
|
||||
*
|
||||
* @return
|
||||
* @author sr on 2019-10-11
|
||||
*/
|
||||
public static Long getExistLoginOrgId() {
|
||||
return getExistLoginUser().getOrgId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取机构类型
|
||||
*
|
||||
* @return
|
||||
* @author sr on 2019-10-22
|
||||
*/
|
||||
public static String getOrgType() {
|
||||
IUserDetail userDTO = getLoginUser();
|
||||
return userDTO == null ? null : userDTO.getOrgType();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户类型
|
||||
*
|
||||
* @return
|
||||
* @author sr on 2020-06-17
|
||||
*/
|
||||
public static Integer getUserType() {
|
||||
IUserDetail userDTO = getLoginUser();
|
||||
return userDTO == null ? null : userDTO.getType();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户类型(未登录抛异常)
|
||||
*
|
||||
* @return
|
||||
* @author sr on 2020-06-17
|
||||
*/
|
||||
public static Integer getExistUserType() {
|
||||
return getExistLoginUser().getType();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取登录超管用户id(超管未登录抛异常)
|
||||
*
|
||||
* @return
|
||||
* @author sr on 2019-10-17
|
||||
*/
|
||||
public static Long getExistLoginSuperiorId() {
|
||||
IUserDetail userDTO = getExistLoginUser();
|
||||
Assert.isTrue(userDTO.getSupeiror(), "只有平台管理员能访问");
|
||||
return userDTO.getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否超管用户
|
||||
*
|
||||
* @return
|
||||
* @author sr on 2020-03-12
|
||||
*/
|
||||
public static boolean isSuperior() {
|
||||
IUserDetail userDTO = getLoginUser();
|
||||
return userDTO != null && userDTO.getSupeiror();
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否企业管理员
|
||||
* @return
|
||||
* @author sr on 2021-06-22
|
||||
*/
|
||||
public static boolean isAdministrator() {
|
||||
return isAdministrator(getLoginUser());
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否企业管理员
|
||||
* @param userDTO
|
||||
* @return
|
||||
* @author sr on 2021-06-29
|
||||
*/
|
||||
public static boolean isAdministrator(IUserDetail userDTO) {
|
||||
return userDTO != null && UserTypeEnum.ORG.getType().equals(userDTO.getType())
|
||||
&& userDTO.getAdministrator();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 校验平台机构访问
|
||||
*
|
||||
* @author sr on 2020-12-28
|
||||
*/
|
||||
public static void checkPlatformOrg() {
|
||||
Assert.isTrue(CommonConstants.PLATFORM_ORG_ID.equals(getLoginOrgId()), "只有平台机构能访问");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取除超管以外的机构ID
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static Long getOrgIdNotSupper() {
|
||||
IUserDetail userDTO = getExistLoginUser();
|
||||
return userDTO.getSupeiror() ? null : userDTO.getOrgId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户状态
|
||||
* @return
|
||||
* @author sr on 2021-06-30
|
||||
*/
|
||||
public static Integer getStatus() {
|
||||
IUserDetail userDTO = getLoginUser();
|
||||
return userDTO == null ? null : userDTO.getStatus();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user