@@ -0,0 +1,108 @@
|
||||
package com.yunhai.userhome.utils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 枚举工具类(对齐旧 UserHome utils.EnumUtil,commons-lang → hutool)。
|
||||
*/
|
||||
public final class EnumUtil {
|
||||
|
||||
/**
|
||||
* 枚举转 List<Map> 格式(每个枚举项 → {key, value, enum})。
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static List<Map<String, Object>> toMapList(Class targetEnumClazz) {
|
||||
Method[] methods = targetEnumClazz.getMethods();
|
||||
Field[] fields = targetEnumClazz.getDeclaredFields();
|
||||
List<Field> fieldList = new ArrayList<>();
|
||||
for (Method method : methods) {
|
||||
for (Field field : fields) {
|
||||
String capitalized = Character.toUpperCase(field.getName().charAt(0)) + field.getName().substring(1);
|
||||
if (method.getName().endsWith(capitalized)) {
|
||||
fieldList.add(field);
|
||||
}
|
||||
}
|
||||
}
|
||||
List<Map<String, Object>> resultList = new ArrayList<>();
|
||||
Enum[] enums = (Enum[]) targetEnumClazz.getEnumConstants();
|
||||
Object value = null;
|
||||
try {
|
||||
for (Enum e : enums) {
|
||||
Map<String, Object> eMap = new HashMap<>();
|
||||
for (Field field : fieldList) {
|
||||
field.setAccessible(true);
|
||||
value = field.get(e);
|
||||
eMap.put(field.getName(), value);
|
||||
}
|
||||
eMap.put("enum", e);
|
||||
resultList.add(eMap);
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return resultList;
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static String getValue(Class targetEnumClazz, String key) {
|
||||
List<Map<String, Object>> list = toMapList(targetEnumClazz);
|
||||
for (Map map : list) {
|
||||
if (map.get("key").toString().equals(key)) {
|
||||
return map.get("value").toString();
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static String getKey(Class targetEnumClazz, String value) {
|
||||
List<Map<String, Object>> list = toMapList(targetEnumClazz);
|
||||
for (Map map : list) {
|
||||
if (map.get("value").toString().equals(value)) {
|
||||
return map.get("key").toString();
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static boolean isExistKey(Class targetEnumClazz, String key) {
|
||||
List<Map<String, Object>> list = toMapList(targetEnumClazz);
|
||||
return list.stream().filter(x -> x.get("key").equals(key)).count() > 0;
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static boolean isExistValue(Class targetEnumClazz, String value) {
|
||||
List<Map<String, Object>> list = toMapList(targetEnumClazz);
|
||||
return list.stream().filter(x -> x.get("value").equals(value)).count() > 0;
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static Object getEnumByKey(Class targetEnumClazz, String key) {
|
||||
List<Map<String, Object>> list = toMapList(targetEnumClazz);
|
||||
for (Map map : list) {
|
||||
if (map.get("key").toString().equals(key)) {
|
||||
return map.get("enum");
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static List<Map<String, Object>> toMapList(Class targetEnumClazz, String keyName, String valueName) {
|
||||
List<Map<String, Object>> list = toMapList(targetEnumClazz);
|
||||
List result = new ArrayList();
|
||||
for (Map map : list) {
|
||||
Map newMap = new HashMap();
|
||||
newMap.put(keyName, map.get("key"));
|
||||
newMap.put(valueName, map.get("value"));
|
||||
result.add(newMap);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.yunhai.userhome.utils;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* File工具类(对齐旧 UserHome utils.FileUtil,仅保留 uploadImg 方法——本期迁移实际使用的唯一方法)。
|
||||
*/
|
||||
public class FileUtil {
|
||||
|
||||
/**
|
||||
* 上传图片(byte[] 版),返回生成的文件名。
|
||||
*/
|
||||
public static String uploadImg(byte[] imgData, String pathName) {
|
||||
String imgName = null;
|
||||
String path = null;
|
||||
File floorFile = null;
|
||||
try {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
imgName = UUID.randomUUID().toString().substring(0, 8) + ".png";
|
||||
path = pathName + imgName;
|
||||
floorFile = new File(path);
|
||||
if (floorFile.exists()) continue;
|
||||
else break;
|
||||
}
|
||||
FileOutputStream fos = new FileOutputStream(floorFile);
|
||||
fos.write(imgData);
|
||||
fos.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return imgName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传图片(MultipartFile 版),保留原始扩展名,返回生成的文件名。
|
||||
*/
|
||||
public static String uploadImg(MultipartFile file, String pathName) {
|
||||
String imgName = null;
|
||||
String path = null;
|
||||
File floorFile = null;
|
||||
try {
|
||||
byte[] imgData = file.getBytes();
|
||||
String fileName = file.getOriginalFilename();
|
||||
int index = fileName.lastIndexOf(".");
|
||||
String ext = "";
|
||||
if (index > 0) {
|
||||
ext = fileName.substring(index);
|
||||
}
|
||||
for (int i = 0; i < 100; i++) {
|
||||
imgName = UUID.randomUUID().toString().substring(0, 8) + ext;
|
||||
path = pathName + imgName;
|
||||
floorFile = new File(path);
|
||||
if (floorFile.exists()) continue;
|
||||
else break;
|
||||
}
|
||||
FileOutputStream fos = new FileOutputStream(floorFile);
|
||||
fos.write(imgData);
|
||||
fos.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return imgName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package com.yunhai.userhome.utils;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 轻量内存缓存(替换旧工程 bsch MapCacheUtil)。
|
||||
* 用于 getMsgAddress 的 uuid→userId 鉴权握手缓存,20s 有效。
|
||||
* 单例 + 守护线程周期清扫过期项,线程安全(ConcurrentHashMap)。
|
||||
*/
|
||||
@Component
|
||||
public class MapCacheUtil {
|
||||
|
||||
private static final int CLEAN_UP_PERIOD_IN_SEC = 5;
|
||||
|
||||
private final ConcurrentHashMap<String, CacheObject> cache = new ConcurrentHashMap<>();
|
||||
|
||||
private static MapCacheUtil instance;
|
||||
|
||||
public static synchronized MapCacheUtil getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new MapCacheUtil();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private MapCacheUtil() {
|
||||
Thread cleanerThread = new Thread(() -> {
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
try {
|
||||
Thread.sleep(CLEAN_UP_PERIOD_IN_SEC * 1000L);
|
||||
cache.entrySet().removeIf(entry ->
|
||||
Optional.ofNullable(entry.getValue()).map(CacheObject::isExpired).orElse(false));
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
});
|
||||
cleanerThread.setDaemon(true);
|
||||
cleanerThread.start();
|
||||
}
|
||||
|
||||
public void add(String key, Object value, long periodInMillis) {
|
||||
if (key == null) {
|
||||
return;
|
||||
}
|
||||
if (value == null) {
|
||||
cache.remove(key);
|
||||
} else {
|
||||
long expiryTime = System.currentTimeMillis() + periodInMillis;
|
||||
cache.put(key, new CacheObject(value, expiryTime));
|
||||
}
|
||||
}
|
||||
|
||||
public void remove(String key) {
|
||||
cache.remove(key);
|
||||
}
|
||||
|
||||
public Object get(String key) {
|
||||
return Optional.ofNullable(cache.get(key))
|
||||
.filter(c -> !c.isExpired())
|
||||
.map(CacheObject::getValue)
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
cache.clear();
|
||||
}
|
||||
|
||||
private static class CacheObject {
|
||||
private final Object value;
|
||||
private final long expiryTime;
|
||||
|
||||
private CacheObject(Object value, long expiryTime) {
|
||||
this.value = value;
|
||||
this.expiryTime = expiryTime;
|
||||
}
|
||||
|
||||
boolean isExpired() {
|
||||
return System.currentTimeMillis() > expiryTime;
|
||||
}
|
||||
|
||||
public Object getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.yunhai.userhome.utils;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 杂项公用方法集合(对齐旧 UserHome utils.OtherUtil,fastjson 相关方法移除——新框架用 Jackson)。
|
||||
*/
|
||||
public class OtherUtil {
|
||||
|
||||
/**
|
||||
* 十六进制转字符串
|
||||
*/
|
||||
public final static String hexToString(String hex) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < hex.length(); i += 2) {
|
||||
String hexSubstring = hex.substring(i, i + 2);
|
||||
int decimalValue = Integer.parseInt(hexSubstring, 16);
|
||||
sb.append((char) decimalValue);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为校领导
|
||||
*/
|
||||
public final static Boolean isAdminById(Integer roleId) {
|
||||
return Arrays.asList(2, 5, 7).contains(roleId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user