一体化平台对接服务调整
This commit is contained in:
@ -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 {
|
||||
}
|
||||
@ -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 {
|
||||
}
|
||||
@ -6,7 +6,7 @@ import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 【不需要登录就能访问的接口】使用该注解
|
||||
* 【不需要校验就能访问的接口】使用该注解
|
||||
*/
|
||||
@Target({ElementType.METHOD, ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
|
||||
@ -1,11 +1,8 @@
|
||||
package com.tobacco.mp.constant;
|
||||
|
||||
/**
|
||||
* @Author XiongLiJian
|
||||
* @Date 2021/6/4 9:31
|
||||
* @Description 符号工具类
|
||||
* @Version 1.0
|
||||
**/
|
||||
* 符号工具类
|
||||
*/
|
||||
public class Symbol {
|
||||
|
||||
/** 符号:[-] */
|
||||
|
||||
@ -2,8 +2,6 @@ package com.tobacco.mp.enums;
|
||||
|
||||
/**
|
||||
* 响应码管理
|
||||
* @Author: XiongLiJian
|
||||
* @Date: 2020/12/29 09:14
|
||||
*/
|
||||
public enum ResponseCodeEnum {
|
||||
|
||||
|
||||
@ -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("批量删除解析错误!"),
|
||||
|
||||
@ -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<String> PASS_API;
|
||||
|
||||
public static List<String> 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<String> 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;
|
||||
|
||||
@ -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";
|
||||
|
||||
@ -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<Map.Entry<String, Object>> iter = map.entrySet().iterator();
|
||||
while (iter.hasNext()) {
|
||||
Map.Entry<String, Object> 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<Object,Object> map(Object ... args){
|
||||
Map<Object,Object> map = new HashMap<Object, Object>();
|
||||
fillMap(map, args);
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认采用HashMap
|
||||
* @param args
|
||||
* @return
|
||||
*/
|
||||
public static <K,V> Map<K,V> map(Class<K> kClazz,Class<V> vClassz,Object ... args){
|
||||
Map<K,V> map = new HashMap<K, V>();
|
||||
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<Object,Object> treemap(Object ... args){
|
||||
Map<Object,Object> map = new TreeMap<Object, Object>();
|
||||
fillMap(map, args);
|
||||
return map;
|
||||
}
|
||||
|
||||
public static <T,V> Map<T,V> copy(Map<T,V> source){
|
||||
if (source == null){
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
if (source instanceof LinkedHashMap){
|
||||
return new LinkedHashMap<T,V>(source);
|
||||
} else if (source instanceof TreeMap){
|
||||
return new TreeMap<T,V>(source);
|
||||
} else {
|
||||
return new HashMap<T,V>(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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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 <T> List<T> take(List list, String name, Class<T> clazz) {
|
||||
try {
|
||||
List<T> 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 <I, T> Map<I, T> index(List<T> list, String name, Class<I> clazz) {
|
||||
try {
|
||||
Map<I, T> 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<Object, Object> 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<Object, Object> 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<Object, List> group(List list, String prop) {
|
||||
try {
|
||||
Map<Object, List> 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 <T> boolean checkObjFieldIsData(Collection<T> 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 <T> boolean checkObjFieldIsData(Map<String, List<T>> objs, String...keys) {
|
||||
try {
|
||||
Iterator<Map.Entry<String, List<T>>> iter = objs.entrySet().iterator();
|
||||
while (iter.hasNext()) {
|
||||
Map.Entry<String, List<T>> 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 <T> void deleteNullDeviceType(Map<Integer, List<T>> map, String...keys) {
|
||||
List<Integer> eventTypes = new ArrayList<>();
|
||||
Iterator<Map.Entry<Integer, List<T>>> iter = map.entrySet().iterator();
|
||||
while (iter.hasNext()) {
|
||||
Map.Entry<Integer, List<T>> 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;
|
||||
}
|
||||
|
||||
}
|
||||
@ -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 <T>
|
||||
* @return
|
||||
*/
|
||||
public static <T> String toString(List<T> 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();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user