diff --git a/cdaxxt-bootstrap/src/main/java/com/tobacco/mp/config/WebMvcConfig.java b/cdaxxt-bootstrap/src/main/java/com/tobacco/mp/config/WebMvcConfig.java index dc94773..5001d8c 100644 --- a/cdaxxt-bootstrap/src/main/java/com/tobacco/mp/config/WebMvcConfig.java +++ b/cdaxxt-bootstrap/src/main/java/com/tobacco/mp/config/WebMvcConfig.java @@ -6,11 +6,8 @@ import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** - * @Author XiongLiJian - * @Date 2021/12/28 15:36 - * @Description 资源管理配置 - * @Version 1.0 - **/ + * 资源管理配置 + */ @Configuration public class WebMvcConfig implements WebMvcConfigurer { diff --git a/cdaxxt-business/src/main/java/com/tobacco/mp/controller/PlatformController.java b/cdaxxt-business/src/main/java/com/tobacco/mp/controller/PlatformController.java index 7f9022f..6e5b672 100644 --- a/cdaxxt-business/src/main/java/com/tobacco/mp/controller/PlatformController.java +++ b/cdaxxt-business/src/main/java/com/tobacco/mp/controller/PlatformController.java @@ -13,7 +13,9 @@ import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; -// 一体化平台对接 +/** + * 一体化平台对接 + */ @RestController @AllArgsConstructor @RequestMapping("/platform") diff --git a/cdaxxt-business/src/main/java/com/tobacco/mp/service/PlatformMessageService.java b/cdaxxt-business/src/main/java/com/tobacco/mp/service/PlatformMessageService.java index 2fcf293..15456f8 100644 --- a/cdaxxt-business/src/main/java/com/tobacco/mp/service/PlatformMessageService.java +++ b/cdaxxt-business/src/main/java/com/tobacco/mp/service/PlatformMessageService.java @@ -21,7 +21,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; - +import com.tobacco.mp.utils.StringUtils; import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; diff --git a/cdaxxt-business/src/main/java/com/tobacco/mp/service/PlatformUserService.java b/cdaxxt-business/src/main/java/com/tobacco/mp/service/PlatformUserService.java index 2a206cf..bb30567 100644 --- a/cdaxxt-business/src/main/java/com/tobacco/mp/service/PlatformUserService.java +++ b/cdaxxt-business/src/main/java/com/tobacco/mp/service/PlatformUserService.java @@ -17,6 +17,7 @@ import com.tobacco.mp.uc.client.api.user.req.PageQueryUserRequest; import com.tobacco.mp.utils.JWTUtil; import com.tobacco.mp.utils.RandomUtil; import com.tobacco.mp.utils.RedisUtils; +import com.tobacco.mp.utils.StringUtils; import io.jsonwebtoken.Claims; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; diff --git a/cdaxxt-common/src/main/java/com/tobacco/mp/annotation/AuthOperation.java b/cdaxxt-common/src/main/java/com/tobacco/mp/annotation/AuthOperation.java deleted file mode 100644 index ba1c817..0000000 --- a/cdaxxt-common/src/main/java/com/tobacco/mp/annotation/AuthOperation.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.tobacco.mp.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * 【允许已登录的社会用户访问的接口】使用该注解(无效) - */ -@Target({ElementType.METHOD, ElementType.TYPE}) -@Retention(RetentionPolicy.RUNTIME) -public @interface AuthOperation { -} diff --git a/cdaxxt-common/src/main/java/com/tobacco/mp/annotation/LogOperation.java b/cdaxxt-common/src/main/java/com/tobacco/mp/annotation/LogOperation.java deleted file mode 100644 index 0d8fd8b..0000000 --- a/cdaxxt-common/src/main/java/com/tobacco/mp/annotation/LogOperation.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.tobacco.mp.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * 【需要记录操作日志】使用该注解 - */ -@Target({ElementType.METHOD, ElementType.TYPE}) -@Retention(RetentionPolicy.RUNTIME) -public @interface LogOperation { -} diff --git a/cdaxxt-common/src/main/java/com/tobacco/mp/annotation/PassOperation.java b/cdaxxt-common/src/main/java/com/tobacco/mp/annotation/PassOperation.java index 4ed2106..510da8f 100644 --- a/cdaxxt-common/src/main/java/com/tobacco/mp/annotation/PassOperation.java +++ b/cdaxxt-common/src/main/java/com/tobacco/mp/annotation/PassOperation.java @@ -6,7 +6,7 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** - * 【不需要登录就能访问的接口】使用该注解 + * 【不需要校验就能访问的接口】使用该注解 */ @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) diff --git a/cdaxxt-common/src/main/java/com/tobacco/mp/constant/Symbol.java b/cdaxxt-common/src/main/java/com/tobacco/mp/constant/Symbol.java index 3105155..e1be32e 100644 --- a/cdaxxt-common/src/main/java/com/tobacco/mp/constant/Symbol.java +++ b/cdaxxt-common/src/main/java/com/tobacco/mp/constant/Symbol.java @@ -1,11 +1,8 @@ package com.tobacco.mp.constant; /** - * @Author XiongLiJian - * @Date 2021/6/4 9:31 - * @Description 符号工具类 - * @Version 1.0 - **/ + * 符号工具类 + */ public class Symbol { /** 符号:[-] */ diff --git a/cdaxxt-common/src/main/java/com/tobacco/mp/enums/ResponseCodeEnum.java b/cdaxxt-common/src/main/java/com/tobacco/mp/enums/ResponseCodeEnum.java index 0abac37..f0352f6 100644 --- a/cdaxxt-common/src/main/java/com/tobacco/mp/enums/ResponseCodeEnum.java +++ b/cdaxxt-common/src/main/java/com/tobacco/mp/enums/ResponseCodeEnum.java @@ -2,8 +2,6 @@ package com.tobacco.mp.enums; /** * 响应码管理 - * @Author: XiongLiJian - * @Date: 2020/12/29 09:14 */ public enum ResponseCodeEnum { diff --git a/cdaxxt-common/src/main/java/com/tobacco/mp/enums/TipEnum.java b/cdaxxt-common/src/main/java/com/tobacco/mp/enums/TipEnum.java index d3b57de..daf692c 100644 --- a/cdaxxt-common/src/main/java/com/tobacco/mp/enums/TipEnum.java +++ b/cdaxxt-common/src/main/java/com/tobacco/mp/enums/TipEnum.java @@ -1,11 +1,8 @@ package com.tobacco.mp.enums; /** - * @Author XiongLiJian - * @Date 2021/12/29 9:25 - * @Description 消息提示 - * @Version 1.0 - **/ + * 消息提示 + */ public enum TipEnum { BATCH_DELETE_DATA_ERROR("批量删除解析错误!"), diff --git a/cdaxxt-common/src/main/java/com/tobacco/mp/utils/AnnotationUtil.java b/cdaxxt-common/src/main/java/com/tobacco/mp/utils/AnnotationUtil.java index 77268bd..1d9012d 100644 --- a/cdaxxt-common/src/main/java/com/tobacco/mp/utils/AnnotationUtil.java +++ b/cdaxxt-common/src/main/java/com/tobacco/mp/utils/AnnotationUtil.java @@ -1,13 +1,11 @@ package com.tobacco.mp.utils; -import com.tobacco.mp.annotation.AuthOperation; import com.tobacco.mp.annotation.OpenOperation; import com.tobacco.mp.annotation.PassOperation; import com.tobacco.mp.constant.Constant; import com.tobacco.mp.constant.Symbol; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections.CollectionUtils; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; @@ -16,7 +14,6 @@ import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.type.classreading.MetadataReaderFactory; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.*; - import java.lang.reflect.Method; import java.util.*; @@ -24,15 +21,15 @@ import java.util.*; @Component public class AnnotationUtil { - @Autowired + @javax.annotation.Resource RedisUtils redisUtils; - private final String RESOURCE_PATTERN = "com/tobacco/mp/**/controller/*.class"; + // 扫描路径 + private String RESOURCE_PATTERN = "com/tobacco/mp/**/controller/*.class"; + // 放行的接口 public static List PASS_API; - public static List AUTH_API; - /** * 加载自定义注解 */ @@ -42,16 +39,11 @@ public class AnnotationUtil { if (mapApis.get(OpenOperation.class.getName()) != null) { redisUtils.set(Constant.OPEN_API, StringUtils.toString(mapApis.get(OpenOperation.class.getName()))); } - // 不需要登录就能访问的接口 + // 不需要校验就能访问的接口 PASS_API = mapApis.get(PassOperation.class.getName()); if (PASS_API == null) { PASS_API = new ArrayList<>(); } - // 允许已登录的社会用户访问的接口 - AUTH_API = mapApis.get(AuthOperation.class.getName()); - if (AUTH_API == null) { - AUTH_API = new ArrayList<>(); - } } /** @@ -87,18 +79,6 @@ public class AnnotationUtil { } openApi.add(api); } - AuthOperation authAnno = method.getAnnotation(AuthOperation.class); - // 判断是否有指定主解 - if (authAnno != null) { - // 获取注解的方法与接口请求注解 - String api = getApi(clazz, method); - List authApi = map.get(AuthOperation.class.getName()); - if (CollectionUtils.isEmpty(authApi)) { - authApi = new ArrayList<>(); - map.put(AuthOperation.class.getName(), authApi); - } - authApi.add(api); - } PassOperation passAnno = method.getAnnotation(PassOperation.class); // 判断是否有指定主解 if (passAnno != null) { @@ -114,7 +94,7 @@ public class AnnotationUtil { } } } catch (Exception e) { - + e.printStackTrace(); } return map; diff --git a/cdaxxt-common/src/main/java/com/tobacco/mp/utils/JWTUtil.java b/cdaxxt-common/src/main/java/com/tobacco/mp/utils/JWTUtil.java index 9cb44d9..17d509b 100644 --- a/cdaxxt-common/src/main/java/com/tobacco/mp/utils/JWTUtil.java +++ b/cdaxxt-common/src/main/java/com/tobacco/mp/utils/JWTUtil.java @@ -2,15 +2,14 @@ package com.tobacco.mp.utils; import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jwts; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; import java.security.KeyFactory; import java.security.PublicKey; import java.security.spec.X509EncodedKeySpec; import java.util.Base64; +/** + * JWT用户信息校验工具 + */ public class JWTUtil { // 公钥 @@ -23,10 +22,8 @@ public class JWTUtil { */ public static PublicKey loadPublicKey(String publicKeyStr) throws Exception { // 移除PEM格式的标记 - String publicKeyPEM = publicKeyStr - .replace("-----BEGIN PUBLIC KEY-----", "") - .replace("-----END PUBLIC KEY-----", "") - .replaceAll("\\s", ""); + String publicKeyPEM = publicKeyStr.replace("-----BEGIN PUBLIC KEY-----", "") + .replace("-----END PUBLIC KEY-----", "").replaceAll("\\s", ""); // Base64解码 byte[] encoded = Base64.getDecoder().decode(publicKeyPEM); // 创建公钥规范 @@ -69,28 +66,6 @@ public class JWTUtil { } } - /** - * 读取txt文件的内容 - * @param file 想要读取的文件对象 - * @return 返回文件内容 - */ - public static String readTxt(File file) { - StringBuilder result = new StringBuilder(); - try { - // 构造一个BufferedReader类来读取文件 - BufferedReader br = new BufferedReader(new FileReader(file)); - String s = null; - // 使用readLine方法,一次读一行 - while ((s = br.readLine()) != null) { - result.append(System.lineSeparator()+s); - } - br.close(); - } catch(Exception e) { - e.printStackTrace(); - } - return result.toString(); - } - public static void main(String[] args) { String publicKeyStr = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwHOi7T3dx95MADXlRPkLeFS+08f7dGg6RxbG5gug+yQLnw1ARNDYx6zK0gtHU+qmUlBCVqHS5vqAt73ydDKUGY9IgdMcxWtbPj456wF7W86xDv6EfiV/G9ZRVvPWmgNUxw1RXtQa91sIuyCxp4xIFd43wROxWUbmN+Omiv2ZYqYBquSdmzslL1dDypPCZ53ZCs1aY6TodbhndySp8E7YAhw8o+F2uGPW9p1Xz1w4hIZBo10b49rOpR5h0t2U4OloBbAC8Too6Smb5ZdYseUZLLD+PW1O0l7uMBlmJuqjPXRUxuTwXUO+EA4Z/ymBXalE4Zi3uEomISqWajSDPOG4pwIDAQAB"; String jwtToken = "eyJhbGciOiJSUzI1NiJ9.eyJlbXBsb3llZUpvYklkIjoiNjIxMzI1MDMwNzAwMDAwMDAwMSIsIm1hbmFnZVVuaXRJZCI6IjAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDExNjIwNTAxIiwib3JpZ2luQXBwIjoiYzQ1Y2I1YzMwZDQzNGMyNDlkZWYyZGNiMzM2ODg4NGQiLCJ0YXJnZXRDbGllbnRzIjoiNmY5MzhkYTBlMjI5MTFmMGJjNzFiYzE2OTUwYTU1ZmIiLCJlbXBsb3llZUlkIjoiMDAwMDAwMDAwMDAwMDAwMDEwNjIwNTAxMDAwMDA0MjIiLCJ1c2VyVHlwZSI6IjAwMDAwMSIsInVzZXJOYW1lIjoi5p2c5YevIiwib3JnVW5pdElkIjoiMDAwMDAwMDAwMDAwMDAwMDAwMDAxMTYyMDUwMTAxMDciLCJnbXRDcmVhdGUiOiIyMDI1LTEyLTI2IDA4OjAxOjA5IiwidXNlcklkIjoiNjIxMzIzMDIyMzAwMDAwMDAzNDAwMDAiLCJhY2NvdW50IjoiRFVLQUkifQ.LzvpEPcGBeOnfV0tcO9yl11PeWuGQR0lzrRAmdUAcSGhvL-J_UF57P4_Q64qfMrzotoSEiVKiCErs4XcgexDFg4WIpx-wbZWdaQ7zIaFMWY-4eVe3R8Th72ABohwuQ7YRFJuWuNE21m3olfqbnbQ1DztDlsWrjd0l6LohrycK9A0teCAlP4Hhjl9kjT2UrRnMFVep1t5dTz8oPSE4KXNjseG8aTBGYbg304JYFQSfkdn3rB6ucp9FG0b9HGrpw1kCdq16m2VXGmKlqGizrOFw30SEZ9z9dABWiNMZjt3AqhY1ByNFtx0bNY-51DOdfVHeWTUDqItuwOak86r-ZVH_w"; diff --git a/cdaxxt-common/src/main/java/com/tobacco/mp/utils/Maps.java b/cdaxxt-common/src/main/java/com/tobacco/mp/utils/Maps.java deleted file mode 100644 index 7ba7079..0000000 --- a/cdaxxt-common/src/main/java/com/tobacco/mp/utils/Maps.java +++ /dev/null @@ -1,80 +0,0 @@ -package com.tobacco.mp.utils; - -import com.alibaba.fastjson.JSON; - -import java.util.*; - -public class Maps { - - /** - * json去除key-value处理 - * @param json - * @return - */ - public static String jsonRemoveKey(String json, String key) { - Map map = JSON.parseObject(json); - Iterator> iter = map.entrySet().iterator(); - while (iter.hasNext()) { - Map.Entry entry = iter.next(); - if (entry.getKey() != null && entry.getKey().equals(key)) { - map.remove(key); - break; - } - } - return JSON.toJSONString(map); - } - - /** - * 默认采用HashMap - * @param args - * @return - */ - public static Map map(Object ... args){ - Map map = new HashMap(); - fillMap(map, args); - return map; - } - - /** - * 默认采用HashMap - * @param args - * @return - */ - public static Map map(Class kClazz,Class vClassz,Object ... args){ - Map map = new HashMap(); - if (args.length % 2 == 0){ - for (int i = 0;i < args.length; i+=2){ - map.put((K)args[i], (V)args[i+1]); - } - } - return map; - } - - public static Map treemap(Object ... args){ - Map map = new TreeMap(); - fillMap(map, args); - return map; - } - - public static Map copy(Map source){ - if (source == null){ - return Collections.emptyMap(); - } - if (source instanceof LinkedHashMap){ - return new LinkedHashMap(source); - } else if (source instanceof TreeMap){ - return new TreeMap(source); - } else { - return new HashMap(source); - } - } - - private static void fillMap(Map target,Object ... args){ - if (args.length % 2 == 0){ - for (int i = 0;i < args.length; i+=2){ - target.put(args[i], args[i+1]); - } - } - } -} - diff --git a/cdaxxt-common/src/main/java/com/tobacco/mp/utils/Objects.java b/cdaxxt-common/src/main/java/com/tobacco/mp/utils/Objects.java deleted file mode 100644 index 2638d43..0000000 --- a/cdaxxt-common/src/main/java/com/tobacco/mp/utils/Objects.java +++ /dev/null @@ -1,249 +0,0 @@ -package com.tobacco.mp.utils; - - -import org.apache.commons.beanutils.PropertyUtils; -import org.apache.commons.lang3.StringUtils; - -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.util.*; - -/** - * - */ -public final class Objects { - public static List take(List list, String name, Class clazz) { - try { - List values = new ArrayList<>(); - for (Object item : list) { - - Object valueObj = PropertyUtils.getProperty(item, name); - T value = (T) valueObj; - if (value != null) { - values.add(value); - } - } - return values; - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } catch (InvocationTargetException e) { - throw new RuntimeException(e); - } catch (NoSuchMethodException e) { - throw new RuntimeException(e); - } - } - - public static Map index(List list, String name, Class clazz) { - try { - Map index = new HashMap<>(); - for (T t : list) { - Object valueObj = PropertyUtils.getProperty(t, name); - I key = (I) valueObj; - index.put(key, t); - } - return index; - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } catch (InvocationTargetException e) { - throw new RuntimeException(e); - } catch (NoSuchMethodException e) { - throw new RuntimeException(e); - } - } - - /** - * @param target - * @param source - * @param mapKey 默认两个对象之间通过mapKey关联,即source中对应的是id,target中对应的对象的属性是mapKey.replace("Id") - */ - public static void orm(List target, List source, String mapKey) { - try { - String defaultID = "id"; - Map index = index(source, defaultID, Object.class); - String name = mapKey.substring(0, mapKey.length() - defaultID.length()); - for (Object t : target) { - Object mapValue = PropertyUtils.getProperty(t, mapKey); - Object value = index.get(mapValue); - PropertyUtils.setProperty(t, name, value); - } - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } catch (InvocationTargetException e) { - throw new RuntimeException(e); - } catch (NoSuchMethodException e) { - throw new RuntimeException(e); - } - } - - public static void orm(List target, List source, String mapKey, String name) { - try { - String defaultID = "id"; - Map index = index(source, defaultID, Object.class); - for (Object t : target) { - Object mapValue = PropertyUtils.getProperty(t, mapKey); - if(mapValue != null) { - Object value = index.get(Long.parseLong(mapValue.toString())); - PropertyUtils.setProperty(t, name, value); - } - } - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } catch (InvocationTargetException e) { - throw new RuntimeException(e); - } catch (NoSuchMethodException e) { - throw new RuntimeException(e); - } - } - - public static boolean isEmpty(Object value) { - if (value == null) { - return true; - } - if (value instanceof Integer) { - return value.equals(Integer.valueOf(0)); - } else if (value instanceof String) { - return StringUtils.isEmpty(value.toString()); - } - return false; - } - - - public static Map group(List list, String prop) { - try { - Map groupIndex = new HashMap<>(); - for (Object item : list) { - Object value = PropertyUtils.getProperty(item, prop); - if (value != null) { - List group = groupIndex.get(value); - if (group == null) { - group = new LinkedList(); - groupIndex.put(value, group); - } - group.add(item); - } - } - return groupIndex; - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } catch (InvocationTargetException e) { - throw new RuntimeException(e); - } catch (NoSuchMethodException e) { - throw new RuntimeException(e); - } - } - - /** - * list去重 - * @param list - * @return - */ - public static List removeDuplicate(List list) { - for ( int i = 0 ; i < list.size() - 1 ; i ++ ) { - for ( int j = list.size() - 1 ; j > i; j -- ) { - if (list.get(j).equals(list.get(i))) { - list.remove(j); - } - } - } - return list; - } - - /** - * 检查一个列表对象是否有数据 - * @param objs - * @return - */ - public static boolean checkObjFieldIsData(Collection objs, String...keys) { - try { - for (T obj : objs) { - if (checkObjField(obj, keys)) { - return true; - } - } - } catch (Exception e) { - e.printStackTrace(); - } - return false; - } - - - /** - * 检查一个列表对象是否有数据 - * @param objs false 表示 - * @return - */ - public static boolean checkObjFieldIsData(Map> objs, String...keys) { - try { - Iterator>> iter = objs.entrySet().iterator(); - while (iter.hasNext()) { - Map.Entry> entry = iter.next(); - for (Object obj : entry.getValue()) { - if (checkObjField(obj, keys)) { - return true; - } - } - } - } catch (Exception e) { - e.printStackTrace(); - } - return false; - } - - /** - * 去掉设备类型没有在线率的统计 - * @param map - */ - public static void deleteNullDeviceType(Map> map, String...keys) { - List eventTypes = new ArrayList<>(); - Iterator>> iter = map.entrySet().iterator(); - while (iter.hasNext()) { - Map.Entry> entry = iter.next(); - boolean result = checkObjFieldIsData(entry.getValue(), keys); - if (!result) { - eventTypes.add(entry.getKey()); - } - } - for (Integer s: eventTypes) { - map.remove(s); - } - } - - /** - * 检测对象是否有数据 - * @param obj - * @return - * @throws IllegalAccessException - */ - private static boolean checkObjField(Object obj, String...keys) throws IllegalAccessException{ - for (Field f : obj.getClass().getDeclaredFields()) { - f.setAccessible(true); - if (keys != null) { - boolean isCheck = true; - for (String key : keys) { - if (key.equals(f.getName())) { - isCheck = false; - break; - } - } - if (!isCheck) { - continue; - } - } - if (obj instanceof String && f.get(obj) != null) { - if (!"".equals(String.valueOf(f.get(obj)))) { - return true; - } - } else if (obj instanceof Number && f.get(obj) != null) { - if (!"0".equals(String.valueOf(f.get(obj)))) { - return true; - } - } else if (f.get(obj) != null) { - if (!"0".equals(String.valueOf(f.get(obj)))) { - return true; - } - } - } - return false; - } - -} diff --git a/cdaxxt-common/src/main/java/com/tobacco/mp/utils/StringUtils.java b/cdaxxt-common/src/main/java/com/tobacco/mp/utils/StringUtils.java new file mode 100644 index 0000000..cdd3dbd --- /dev/null +++ b/cdaxxt-common/src/main/java/com/tobacco/mp/utils/StringUtils.java @@ -0,0 +1,89 @@ +package com.tobacco.mp.utils; + +import com.tobacco.mp.constant.Symbol; +import org.apache.commons.collections4.CollectionUtils; +import java.util.List; +import java.util.Random; + +/** + * 字符串工具类 + */ +public class StringUtils { + + /** + * 将集合对象转换成字符串 + * @param list + * @param + * @return + */ + public static String toString(List list) { + if (CollectionUtils.isEmpty(list)) { + return null; + } + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < list.size(); i++) { + if (i != 0) { + sb.append(Symbol.COMMA); + } + sb.append(list.get(i)); + } + return sb.toString(); + } + + /** + * 判断字符串是否不为null + * @param value + * @return + */ + public static boolean isNotEmpty(String value) { + if (!isEmpty(value)) { + return true; + } + return false; + } + + /** + * 判断字符串是否为null + * @param value + * @return + */ + public static boolean isEmpty(String value) { + if (value == null || value.length() == 0) { + return true; + } + return false; + } + + /** + * 生成指定位数的随机字符串 + * @param num 需要的位数 + * @return + */ + public static String generateRandomString(int num) { + String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + Random random = new Random(); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < num; i++) { + int number = random.nextInt(62); + sb.append(str.charAt(number)); + } + return sb.toString(); + } + + /** + * 生成指定位数的随机数字 + * @param num 需要的位数 + * @return + */ + public static String generateRandomNumber(int num) { + String str = "0123456789"; + Random random = new Random(); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < num; i++) { + int number = random.nextInt(10); + sb.append(str.charAt(number)); + } + return sb.toString(); + } + +}