Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3a6cc8ce03 | |||
| c8380fd9cd | |||
| 8c6eb5d81b | |||
| 30a761e8c4 | |||
| fbad4ccde8 | |||
|
|
0f5b7603b4 | ||
| 1f41ef2d01 | |||
| 75cb5163f1 | |||
| c401f04bbe | |||
| f069a16e43 | |||
|
|
2454d84919 |
@@ -186,6 +186,11 @@ public interface PatternRegex {
|
|||||||
*/
|
*/
|
||||||
String TYPE_REGEX = "^[0-9a-zA-Z\\u0391-\\uFFE5]{1,100}$";
|
String TYPE_REGEX = "^[0-9a-zA-Z\\u0391-\\uFFE5]{1,100}$";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 描述32
|
||||||
|
*/
|
||||||
|
String DES32_REGEX = "^.{0,32}$";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 描述64
|
* 描述64
|
||||||
*/
|
*/
|
||||||
@@ -196,6 +201,11 @@ public interface PatternRegex {
|
|||||||
*/
|
*/
|
||||||
String DES200_REGEX = "^.{0,200}$";
|
String DES200_REGEX = "^.{0,200}$";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 描述400
|
||||||
|
*/
|
||||||
|
String DES400_REGEX = "^.{0,400}$";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 描述500
|
* 描述500
|
||||||
*/
|
*/
|
||||||
@@ -319,4 +329,5 @@ public interface PatternRegex {
|
|||||||
String DICT_DATA_CODE_REGEX = "^[\\u4e00-\\u9fa5\\w\\-\\s\\.\\/\\\\%℃]{1,30}$";
|
String DICT_DATA_CODE_REGEX = "^[\\u4e00-\\u9fa5\\w\\-\\s\\.\\/\\\\%℃]{1,30}$";
|
||||||
|
|
||||||
String DICT_PQ_OTHER_NAME_REGEX = "^[\\u4e00-\\u9fa5\\w\\-\\s]{0,32}$";
|
String DICT_PQ_OTHER_NAME_REGEX = "^[\\u4e00-\\u9fa5\\w\\-\\s]{0,32}$";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ public interface SecurityConstants {
|
|||||||
* 认证成功后,返回信息包含的内容-
|
* 认证成功后,返回信息包含的内容-
|
||||||
*/
|
*/
|
||||||
String USER_ID = "userId";
|
String USER_ID = "userId";
|
||||||
|
String LOGIN_NAME = "loginName";
|
||||||
String USER_TYPE = "userType";
|
String USER_TYPE = "userType";
|
||||||
String USER_NAME_KEY = "name";
|
String USER_NAME_KEY = "name";
|
||||||
String USER_HEAD_KEY = "headSculpture";
|
String USER_HEAD_KEY = "headSculpture";
|
||||||
|
|||||||
@@ -34,14 +34,15 @@ public class JwtUtil {
|
|||||||
*/
|
*/
|
||||||
private final static String JWT_ISS = "NJCN";
|
private final static String JWT_ISS = "NJCN";
|
||||||
|
|
||||||
public static String getAccessToken(String userId) {
|
public static String getAccessToken(String userId,String loginName) {
|
||||||
Map<String, Object> headers = new HashMap<>();
|
Map<String, Object> headers = new HashMap<>();
|
||||||
headers.put("typ", "JWT");
|
headers.put("typ", "JWT");
|
||||||
headers.put("alg", "HS256");
|
headers.put("alg", "HS256");
|
||||||
Map<String, Object> payload = new HashMap<>();
|
Map<String, Object> payload = new HashMap<>();
|
||||||
payload.put(SecurityConstants.USER_ID, userId);
|
payload.put(SecurityConstants.USER_ID, userId);
|
||||||
|
payload.put(SecurityConstants.LOGIN_NAME, loginName);
|
||||||
// 永不过期
|
// 永不过期
|
||||||
payload.put("exp", Instant.now().plusSeconds(DAY_SECOND * Integer.MAX_VALUE).getEpochSecond());
|
payload.put("exp", 4910070710L);
|
||||||
payload.put("sub", SUBJECT);
|
payload.put("sub", SUBJECT);
|
||||||
payload.put("iss", JWT_ISS);
|
payload.put("iss", JWT_ISS);
|
||||||
payload.put("iat", Instant.now().getEpochSecond());
|
payload.put("iat", Instant.now().getEpochSecond());
|
||||||
|
|||||||
@@ -1,11 +1,23 @@
|
|||||||
package com.njcn.common.utils;
|
package com.njcn.common.utils;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||||
|
import com.fasterxml.jackson.databind.JavaType;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||||
|
import com.fasterxml.jackson.databind.type.TypeFactory;
|
||||||
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||||
|
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
||||||
|
import com.njcn.common.pojo.exception.BusinessException;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
import java.util.List;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Map;
|
import java.time.Instant;
|
||||||
import java.util.Objects;
|
import java.util.*;
|
||||||
import java.util.Optional;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author hongawen
|
* @author hongawen
|
||||||
@@ -14,6 +26,9 @@ import java.util.Optional;
|
|||||||
*/
|
*/
|
||||||
public class PubUtils {
|
public class PubUtils {
|
||||||
|
|
||||||
|
private final static ObjectMapper MAPPER = new ObjectMapper();
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据参数返回double的四舍五入值
|
* 根据参数返回double的四舍五入值
|
||||||
*
|
*
|
||||||
@@ -68,4 +83,128 @@ public class PubUtils {
|
|||||||
.orElse(Double.MIN_VALUE);
|
.orElse(Double.MIN_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* 将instant转为date 处理8小时误差
|
||||||
|
* @author hongawen
|
||||||
|
* @date 2023/7/20 15:58
|
||||||
|
* @param instant 日期
|
||||||
|
* @return Instant
|
||||||
|
*/
|
||||||
|
public static Date instantToDate(Instant instant){
|
||||||
|
return Date.from(instant.minusMillis(TimeUnit.HOURS.toMillis(8)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据起始时间和截止时间返回yyyy-MM-dd的日期,
|
||||||
|
*
|
||||||
|
* @param startTime 起始时间
|
||||||
|
* @param endTime 截止时间
|
||||||
|
*/
|
||||||
|
public static List<String> getTimes(Date startTime, Date endTime) {
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
List<String> result = new ArrayList<>();
|
||||||
|
Calendar start = Calendar.getInstance();
|
||||||
|
start.setTime(startTime);
|
||||||
|
Calendar end = Calendar.getInstance();
|
||||||
|
end.setTime(endTime);
|
||||||
|
end.set(end.get(Calendar.YEAR), end.get(Calendar.MONTH), end.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
|
||||||
|
long interval = end.getTimeInMillis() - start.getTimeInMillis();
|
||||||
|
result.add(sdf.format(start.getTime()));
|
||||||
|
if (interval > 0) {
|
||||||
|
int days = (int) (interval / 86400000);
|
||||||
|
for (int i = 0; i < days; i++) {
|
||||||
|
start.add(Calendar.DAY_OF_MONTH, 1);
|
||||||
|
result.add(sdf.format(start.getTime()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将当前时间的秒数置为0
|
||||||
|
*
|
||||||
|
* @param date 时间
|
||||||
|
*/
|
||||||
|
public static Date getSecondsAsZero(Date date) {
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
calendar.setTime(date);
|
||||||
|
calendar.set(Calendar.SECOND, 0);
|
||||||
|
return calendar.getTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/***
|
||||||
|
* 将date转为instant 处理8小时误差
|
||||||
|
* @author hongawen
|
||||||
|
* @date 2023/7/20 15:58
|
||||||
|
* @param date 日期
|
||||||
|
* @return Instant
|
||||||
|
*/
|
||||||
|
public static Instant dateToInstant(Date date){
|
||||||
|
return date.toInstant().plusMillis(TimeUnit.HOURS.toMillis(8));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用于获取对象中,前缀一样,后缀为2~50的属性值
|
||||||
|
*
|
||||||
|
* @param object 待操作对象
|
||||||
|
* @param methodPrefix 方法前缀
|
||||||
|
* @param number 方法后缀
|
||||||
|
* @return 对象属性值
|
||||||
|
*/
|
||||||
|
public static Float getValueByMethod(Object object, String methodPrefix, Integer number) {
|
||||||
|
try {
|
||||||
|
Method method = object.getClass().getMethod(methodPrefix + number);
|
||||||
|
return (Float) method.invoke(object);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.REFLECT_METHOD_EXCEPTION);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据参数返回float的四舍五入值
|
||||||
|
*
|
||||||
|
* @param i 保留的位数
|
||||||
|
* @param value float原值
|
||||||
|
*/
|
||||||
|
public static Float floatRound(int i, float value) {
|
||||||
|
BigDecimal bp = new BigDecimal(value);
|
||||||
|
return bp.setScale(i, RoundingMode.HALF_UP).floatValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将JSON转为实体对象
|
||||||
|
*
|
||||||
|
* @param jsonStr json
|
||||||
|
* @param targetType 对象类型
|
||||||
|
* @param <T> 对象
|
||||||
|
*/
|
||||||
|
public static <T> T json2obj(String jsonStr, Type targetType) {
|
||||||
|
try {
|
||||||
|
JavaType javaType = TypeFactory.defaultInstance().constructType(targetType);
|
||||||
|
MAPPER.registerModule(new JavaTimeModule());
|
||||||
|
MAPPER.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
||||||
|
return MAPPER.readValue(jsonStr, javaType);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IllegalArgumentException("将JSON转换为对象时发生错误:" + jsonStr, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将实体对象转为JSON
|
||||||
|
*
|
||||||
|
* @param object 实体对象
|
||||||
|
*/
|
||||||
|
public static String obj2json(Object object) {
|
||||||
|
try {
|
||||||
|
MAPPER.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
|
||||||
|
return MAPPER.writeValueAsString(object);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IllegalArgumentException("将实体对象转为JSON时发生错误:" + object, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,8 +17,10 @@ import org.springframework.web.multipart.MultipartFile;
|
|||||||
|
|
||||||
import javax.servlet.ServletOutputStream;
|
import javax.servlet.ServletOutputStream;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -115,6 +117,26 @@ public class ExcelUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 指定名称、sheet名(多个sheet)、数据(多组数据)导出数据到本地
|
||||||
|
*
|
||||||
|
* @param path 文件路径
|
||||||
|
* @param fileName 文件名
|
||||||
|
* @param sheetsList
|
||||||
|
*/
|
||||||
|
public static void saveExcel(String path, String fileName, List<Map<String, Object>> sheetsList) {
|
||||||
|
String filePath = path + "\\" + fileName;
|
||||||
|
try {
|
||||||
|
OutputStream outputStream = new FileOutputStream(filePath);
|
||||||
|
Workbook workbook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.XSSF);
|
||||||
|
workbook.write(outputStream);
|
||||||
|
|
||||||
|
outputStream.flush();
|
||||||
|
outputStream.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error(">>> 导出数据异常:{}", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 指定名称、数据下载报表(带指定标题将*显示比必填信息),带有下拉信息
|
* 指定名称、数据下载报表(带指定标题将*显示比必填信息),带有下拉信息
|
||||||
@@ -158,13 +180,11 @@ public class ExcelUtil {
|
|||||||
//没有表格标题,只有表格头
|
//没有表格标题,只有表格头
|
||||||
for (int i = 0; i < headerRowNumber; i++) {
|
for (int i = 0; i < headerRowNumber; i++) {
|
||||||
//获取列数
|
//获取列数
|
||||||
int physicalNumberOfCells = sheetAt.getRow(i).getPhysicalNumberOfCells();
|
|
||||||
//获取行
|
|
||||||
Row row = sheetAt.getRow(i);
|
Row row = sheetAt.getRow(i);
|
||||||
if (Objects.isNull(row)) {
|
if (Objects.isNull(row)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
for (int j = 0; j < physicalNumberOfCells; j++) {
|
for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
|
||||||
//获取单元格对象
|
//获取单元格对象
|
||||||
Cell cell = row.getCell(j);
|
Cell cell = row.getCell(j);
|
||||||
//获取单元格样式对象
|
//获取单元格样式对象
|
||||||
|
|||||||
@@ -39,7 +39,8 @@ public class HttpResultUtil {
|
|||||||
* 业务异常组装结果集
|
* 业务异常组装结果集
|
||||||
*/
|
*/
|
||||||
public static <T> HttpResult<T> assembleBusinessExceptionResult(BusinessException businessException, T result, String methodDescribe) {
|
public static <T> HttpResult<T> assembleBusinessExceptionResult(BusinessException businessException, T result, String methodDescribe) {
|
||||||
return assembleResult(businessException.getCode(), result, StrFormatter.format("{}{}{}", methodDescribe, StrUtil.C_COMMA, businessException.getMessage()));
|
// return assembleResult(businessException.getCode(), result, StrFormatter.format("{}{}{}", methodDescribe, StrUtil.C_COMMA, businessException.getMessage()));
|
||||||
|
return assembleResult(businessException.getCode(), result, businessException.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,15 +65,15 @@ public class RequestUtil {
|
|||||||
*
|
*
|
||||||
* @return 若成功,返回当前登录用户的ID;若失败,返回null
|
* @return 若成功,返回当前登录用户的ID;若失败,返回null
|
||||||
*/
|
*/
|
||||||
public static String getUserId(ServerHttpRequest serverHttpRequest) {
|
// public static String getUserId(ServerHttpRequest serverHttpRequest) {
|
||||||
String accessToken = getAccessToken(serverHttpRequest);
|
// String accessToken = getAccessToken(serverHttpRequest);
|
||||||
String userId = null;
|
// String userId = null;
|
||||||
if (StrUtil.isNotBlank(accessToken)) {
|
// if (StrUtil.isNotBlank(accessToken)) {
|
||||||
Map<String, Object> map = JwtUtil.parseToken(accessToken);
|
// Map<String, Object> map = JwtUtil.parseToken(accessToken);
|
||||||
userId = (String) map.get(SecurityConstants.USER_ID);
|
// userId = (String) map.get(SecurityConstants.USER_ID);
|
||||||
}
|
// }
|
||||||
return userId;
|
// return userId;
|
||||||
}
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前登录用户的ID
|
* 获取当前登录用户的ID
|
||||||
@@ -90,6 +90,33 @@ public class RequestUtil {
|
|||||||
return userId;
|
return userId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前登录用户的ID
|
||||||
|
*
|
||||||
|
* @return 若成功,返回当前登录用户的ID;若失败,返回null
|
||||||
|
*/
|
||||||
|
public static String getLoginNameByToken() {
|
||||||
|
String accessToken = getAccessToken();
|
||||||
|
String loginName = null;
|
||||||
|
if (StrUtil.isNotBlank(accessToken)) {
|
||||||
|
Map<String, Object> map = JwtUtil.parseToken(accessToken);
|
||||||
|
loginName = (String) map.get(SecurityConstants.LOGIN_NAME);
|
||||||
|
}
|
||||||
|
return StrUtil.isBlank(loginName) ? LogInfo.UNKNOWN_USER : loginName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取用户登录名称
|
||||||
|
* 该方法通常用于未登录阶段,无token信息
|
||||||
|
*/
|
||||||
|
public static String getLoginName() {
|
||||||
|
HttpServletRequest request = HttpServletUtil.getRequest();;
|
||||||
|
String loginName = (String) request.getAttribute(SecurityConstants.AUTHENTICATE_USERNAME);
|
||||||
|
return StrUtil.isBlank(loginName) ? LogInfo.UNKNOWN_USER : loginName;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前登录用户的用户名
|
* 获取当前登录用户的用户名
|
||||||
*
|
*
|
||||||
@@ -106,15 +133,6 @@ public class RequestUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取用户登录名称
|
|
||||||
* 该方法通常用于未登录阶段,无token信息
|
|
||||||
*/
|
|
||||||
public static String getLoginName() {
|
|
||||||
HttpServletRequest request = HttpServletUtil.getRequest();;
|
|
||||||
String loginName = (String) request.getAttribute(SecurityConstants.AUTHENTICATE_USERNAME);
|
|
||||||
return StrUtil.isBlank(loginName) ? LogInfo.UNKNOWN_USER : loginName;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取请求体
|
* 获取请求体
|
||||||
|
|||||||
5
pom.xml
5
pom.xml
@@ -3,7 +3,7 @@
|
|||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<!--灿能微服务生态系统-->
|
<!--灿能微服务生态系统 -->
|
||||||
<groupId>com.njcn</groupId>
|
<groupId>com.njcn</groupId>
|
||||||
<artifactId>BasicDependVersion</artifactId>
|
<artifactId>BasicDependVersion</artifactId>
|
||||||
<version>1.0.0</version>
|
<version>1.0.0</version>
|
||||||
@@ -29,6 +29,9 @@
|
|||||||
</distributionManagement>
|
</distributionManagement>
|
||||||
<properties>
|
<properties>
|
||||||
<spring-boot.version>2.3.12.RELEASE</spring-boot.version>
|
<spring-boot.version>2.3.12.RELEASE</spring-boot.version>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
|
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||||
</properties>
|
</properties>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
|||||||
Reference in New Issue
Block a user