| package com.vincent.rsf.framework.common; | 
|   | 
| import com.vincent.rsf.framework.annotations.CoolTranslate; | 
|   | 
| import java.lang.reflect.*; | 
| import java.nio.charset.StandardCharsets; | 
| import java.security.MessageDigest; | 
| import java.security.NoSuchAlgorithmException; | 
| import java.util.*; | 
|   | 
| /** | 
|  * Created by vincent on 2019-06-09 | 
|  */ | 
| public class Cools { | 
|   | 
|     public static boolean isEmpty(Object... objects) { | 
|         for (Object obj : objects){ | 
|             if (isEmpty(obj)){ | 
|                 return true; | 
|             } | 
|         } | 
|         return false; | 
|     } | 
|   | 
|     @SuppressWarnings("rawtypes") | 
|     public static boolean isEmpty(Object o) { | 
|         if (o == null) { | 
|             return true; | 
|         } | 
|         if (o instanceof String) { | 
|             if (o.toString().trim().equals("")) { | 
|                 return true; | 
|             } | 
|         } else if (o instanceof List) { | 
|             if (((List) o).size() == 0) { | 
|                 return true; | 
|             } | 
|         } else if (o instanceof Map) { | 
|             if (((Map) o).size() == 0) { | 
|                 return true; | 
|             } | 
|         } else if (o instanceof Set) { | 
|             if (((Set) o).size() == 0) { | 
|                 return true; | 
|             } | 
|         } else if (o instanceof Object[]) { | 
|             if (((Object[]) o).length == 0) { | 
|                 return true; | 
|             } | 
|         } else if (o instanceof int[]) { | 
|             if (((int[]) o).length == 0) { | 
|                 return true; | 
|             } | 
|         } else if (o instanceof long[]) { | 
|             if (((long[]) o).length == 0) { | 
|                 return true; | 
|             } | 
|         } | 
|         return false; | 
|     } | 
|   | 
|     public static int sqlLimitIndex(Integer pageIndex, Integer pageSize){ | 
|         return (pageIndex - 1) * pageSize; | 
|     } | 
|   | 
|     public static String enToken(String username, String password){ | 
|         return AesUtils.encrypt(username, zerofill(password, 16)); | 
|     } | 
|   | 
|     public static String deTokn(String token, String password){ | 
|         return AesUtils.decrypt(token, zerofill(password, 16)); | 
|     } | 
|   | 
|     public static String zerofill(String msg, Integer count){ | 
|         if (msg.length() == count){ | 
|             return msg; | 
|         } else if (msg.length() > count){ | 
|             return msg.substring(0, 16); | 
|         } else { | 
|             StringBuilder msgBuilder = new StringBuilder(msg); | 
|             for (int i = 0; i<count-msg.length(); i++){ | 
|                 msgBuilder.append("0"); | 
|             } | 
|             return msgBuilder.toString(); | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 截取字符串(默认end=true) | 
|      * @param str 被截字符串 | 
|      * @param end true:最后一个字符 / false:第一个字符 | 
|      */ | 
|     public static String deleteChar(String str, boolean end){ | 
|         if (isEmpty(str)){ | 
|             return ""; | 
|         } | 
|         if (end){ | 
|             return str.substring(0, str.length()-1); | 
|         } else { | 
|             return str.substring(1); | 
|         } | 
|     } | 
|   | 
|     public static String deleteChar(String str){ | 
|         return deleteChar(str, true); | 
|     } | 
|   | 
|   | 
|     /** | 
|      * map 转 对象 | 
|      */ | 
|     public static <T> T conver(Map<? extends String, ?> map, Class<T> cls){ | 
|         T instance = null; | 
|         try { | 
|             Constructor<T> constructor = cls.getDeclaredConstructor(); | 
|             boolean constructorAccessible = constructor.isAccessible(); | 
|             constructor.setAccessible(true); | 
|             instance = constructor.newInstance(); | 
|             constructor.setAccessible(constructorAccessible); | 
|         } catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) { | 
|             e.printStackTrace(); | 
|         } | 
|         Class<?> prototype = cls; | 
|         do { | 
|             for (Field field : prototype.getDeclaredFields()){ | 
|                 if (Modifier.isFinal(field.getModifiers()) | 
|                         || Modifier.isStatic(field.getModifiers()) | 
|                         || Modifier.isTransient(field.getModifiers())){ | 
|                     continue; | 
|                 } | 
|                 String fieldName = field.getName(); | 
|                 Object val = null; | 
|                 if (map.containsKey(fieldName)){ | 
|                     val = map.get(fieldName); | 
|                 } | 
|                 if (val != null){ | 
|                     boolean fieldAccessible = field.isAccessible(); | 
|                     field.setAccessible(true); | 
|                     Class<?> type = field.getType(); | 
|                     try { | 
|                         Constructor<?> constructor = type.getDeclaredConstructor(String.class); | 
|                         boolean constructorAccessible = constructor.isAccessible(); | 
|                         constructor.setAccessible(true); | 
|                         field.set(instance, constructor.newInstance(String.valueOf(val))); | 
|                         constructor.setAccessible(constructorAccessible); | 
|                     } catch (IllegalAccessException | 
|                             | InstantiationException | 
|                             | InvocationTargetException | 
|                             | NoSuchMethodException e) { | 
|                         System.err.println("convert error ===> Class["+prototype+"],Field:["+fieldName+"],Type:["+type+"],Value:["+val+"]"); | 
|                     } | 
|                     field.setAccessible(fieldAccessible); | 
|                 } | 
|             } | 
|             prototype = prototype.getSuperclass(); | 
|         } while (!Object.class.equals(prototype)); | 
|         return instance; | 
|     } | 
|   | 
|     /** | 
|      * 对象 转 map | 
|      */ | 
|     public static Map<String, Object> conver(Object obj){ | 
|         Class<?> cls = obj.getClass(); | 
|         Field[] fields = getAllFields(cls); | 
|         Map<String, Object> map = new HashMap<>(); | 
|         for (Field field : fields) { | 
|             if (Modifier.isFinal(field.getModifiers()) | 
|                     || Modifier.isStatic(field.getModifiers()) | 
|                     || Modifier.isTransient(field.getModifiers())){ | 
|                 continue; | 
|             } | 
|             String key = field.getName(); | 
|             boolean flag = field.isAccessible(); | 
|             field.setAccessible(true); | 
|             Object val = null; | 
|             try { | 
|                 val = field.get(obj); | 
|             } catch (IllegalAccessException e) { | 
|                 e.printStackTrace(); | 
|             } | 
|             field.setAccessible(flag); | 
|             if (val != null){ | 
|                 map.put(key, val); | 
|             } | 
|         } | 
|         return map; | 
|     } | 
|   | 
|     /** | 
|      * 获取指定Class(及其SuperClass)的成员变量 | 
|      */ | 
|     public static Field[] getAllFields(Class<?> cls){ | 
|         return getAllFields(cls, null); | 
|     } | 
|   | 
|     /** | 
|      * 递归合并基类Field | 
|      */ | 
|     private static Field[] getAllFields(Class<?> cls, Field[] params){ | 
|         Field[] fields = (params == null) ? cls.getDeclaredFields() : params; | 
|         Class<?> superCls = cls.getSuperclass(); | 
|         if (superCls == null || superCls == Object.class){ | 
|             return fields; | 
|         } | 
|         Field[] superClsFields = superCls.getDeclaredFields(); | 
|         fields = addAll(fields, superClsFields); | 
|         return getAllFields(superCls, fields); | 
|     } | 
|   | 
|     /** | 
|      * 根据fieldName获取Field对象 | 
|      */ | 
|     public static Field getField(Class<?> cls, String fieldName) { | 
|         Field[] allFields = getAllFields(cls); | 
|         for (Field field : allFields) { | 
|             if (field.getName().equals(fieldName)) { | 
|                 return field; | 
|             } | 
|         } | 
|         return null; | 
|     } | 
|   | 
|     /** | 
|      * 获取对象中某个Field的值 | 
|      */ | 
|     public static Object getFieldValue(Object obj, Field field) { | 
|         if (null == field) { | 
|             return null; | 
|         } else { | 
|             if (obj instanceof Class) { | 
|                 obj = null; | 
|             } | 
|             if (!field.isAccessible()) { | 
|                 field.setAccessible(true); | 
|             } | 
|             try { | 
|                 return field.get(obj); | 
|             } catch (IllegalAccessException e) { | 
|                 throw new RuntimeException(e); | 
|             } | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 数组叠加 | 
|      */ | 
|     @SuppressWarnings("unchecked") | 
|     public static <T> T[] addAll(T[] array1, T... array2) { | 
|         if (array1 == null) { | 
|             return clone(array2); | 
|         } else if (array2 == null) { | 
|             return clone(array1); | 
|         } else { | 
|             Class<?> cls = array1.getClass().getComponentType(); | 
|             T[] joinedArray = (T[]) Array.newInstance(cls, array1.length + array2.length); | 
|             System.arraycopy(array1, 0, joinedArray, 0, array1.length); | 
|   | 
|             try { | 
|                 System.arraycopy(array2, 0, joinedArray, array1.length, array2.length); | 
|                 return joinedArray; | 
|             } catch (ArrayStoreException e) { | 
|                 Class<?> type2 = array2.getClass().getComponentType(); | 
|                 if (!cls.isAssignableFrom(type2)) { | 
|                     throw new RuntimeException("Cannot store " + type2.getName() + " in an array of " + cls.getName(), e); | 
|                 } else { | 
|                     throw e; | 
|                 } | 
|             } | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 克隆 | 
|      */ | 
|     private static <T> T[] clone(T[] array) { | 
|         return array == null ? null : array.clone(); | 
|     } | 
|   | 
|     /** | 
|      * map操作 | 
|      */ | 
|     public static CoolMap add(String key,Object value){ | 
|         CoolMap map = new CoolMap(); | 
|         map.put(key, value); | 
|         return map; | 
|     } | 
|   | 
|     public static class CoolMap extends HashMap<String, Object>{ | 
|   | 
|         public CoolMap add(String key,Object value){ | 
|             this.put(key, value); | 
|             return this; | 
|         } | 
|   | 
|         public CoolMap $(String key,Object value){ | 
|             this.put(key, value); | 
|             return this; | 
|         } | 
|   | 
|     } | 
|   | 
|     private final static char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; | 
|   | 
|     public static String md5(String string){ | 
|         try{ | 
|             MessageDigest md5 = MessageDigest.getInstance("MD5"); | 
|             byte[] bytes = md5.digest(string.getBytes(StandardCharsets.UTF_8)); | 
|             char[] chars = new char[bytes.length * 2]; | 
|             for (int i = 0; i < bytes.length; i++) { | 
|                 int b = bytes[i]; | 
|                 chars[i * 2] = hexDigits[(b & 0xF0) >> 4]; | 
|                 chars[i * 2 + 1] = hexDigits[b & 0x0F]; | 
|             } | 
|             return new String(chars).toLowerCase(); | 
|         } catch (Exception e) { | 
|             e.printStackTrace(); | 
|             throw new RuntimeException("md5加密失败,str=".concat(string)); | 
|         } | 
|     } | 
|   | 
|     public static Map<String, Object> translate(Object obj){ | 
|         Class<?> cls = obj.getClass(); | 
|         Field[] fields = getAllFields(cls); | 
|         Map<String, Object> map = new HashMap<>(); | 
|         for (Field field : fields) { | 
|             String key = field.getName(); | 
|             if (field.isAnnotationPresent(CoolTranslate.class)){ | 
|                 CoolTranslate annotation = field.getAnnotation(CoolTranslate.class); | 
|                 if (!isEmpty(annotation.value())) { | 
|                     key = annotation.value(); | 
|                 } | 
|             } | 
|             boolean flag = field.isAccessible(); | 
|             field.setAccessible(true); | 
|             Object val = null; | 
|             try { | 
|                 val = field.get(obj); | 
|             } catch (IllegalAccessException e) { | 
|                 e.printStackTrace(); | 
|             } | 
|             field.setAccessible(flag); | 
|             if (val != null){ | 
|                 map.put(key, val); | 
|             } | 
|         } | 
|         return map; | 
|     } | 
|   | 
|     public static boolean eq(String str, String str0) { | 
|         if (Cools.isEmpty(str) && Cools.isEmpty(str0)) { | 
|             return true; | 
|         } | 
|         if (Cools.isEmpty(str) && !Cools.isEmpty(str0)) { | 
|             return false; | 
|         } | 
|         if (Cools.isEmpty(str0) && !Cools.isEmpty(str)) { | 
|             return false; | 
|         } | 
|         if (str.equals(str0)) { | 
|             return true; | 
|         } | 
|         return false; | 
|     } | 
|   | 
|     public static List<String> getIntersection(List<String> list1, List<String> list2) { | 
|         if (isEmpty(list1) || isEmpty(list2)) { | 
|             return new ArrayList<>(); | 
|         } | 
|         List<String> intersection = new ArrayList<>(list1); | 
|         intersection.retainAll(list2); | 
|         return intersection; | 
|     } | 
|   | 
|   | 
|     /** | 
|      * 对汉字字符串进行MD5加密 | 
|      * @param input 要加密的汉字字符串 | 
|      * @return 32位小写MD5哈希值 | 
|      * @throws NoSuchAlgorithmException | 
|      */ | 
|     public static String md5Chinese(String input) throws NoSuchAlgorithmException { | 
|         if (input == null) { | 
|             return null; | 
|         } | 
|   | 
|         // 统一使用UTF-8编码 | 
|         byte[] inputBytes = input.getBytes(StandardCharsets.UTF_8); | 
|   | 
|         MessageDigest md = MessageDigest.getInstance("MD5"); | 
|         byte[] digest = md.digest(inputBytes); | 
|   | 
|         // 将字节数组转换为16进制字符串 | 
|         StringBuilder sb = new StringBuilder(); | 
|         for (byte b : digest) { | 
|             sb.append(String.format("%02x", b)); | 
|         } | 
|   | 
|         return sb.toString(); | 
|     } | 
|   | 
|     /** | 
|      * 验证汉字字符串与MD5哈希是否匹配 | 
|      * @param input 要验证的汉字字符串 | 
|      * @param md5Hash 存储的MD5哈希值 | 
|      * @return 如果匹配返回true,否则false | 
|      */ | 
|     public static boolean verifyChinese(String input, String md5Hash) { | 
|         try { | 
|             String computedHash = md5Chinese(input); | 
|             return computedHash.equals(md5Hash.toLowerCase()); | 
|         } catch (NoSuchAlgorithmException e) { | 
|             // 理论上不会发生,因为MD5是Java标准库支持的 | 
|             return false; | 
|         } | 
|     } | 
|   | 
| } |