更新后端,采用Springboot+mybatis
This commit is contained in:
28
src/main/java/com/jsh/erp/utils/AnnotationUtils.java
Normal file
28
src/main/java/com/jsh/erp/utils/AnnotationUtils.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.annotation.Documented;
|
||||
|
||||
/**
|
||||
* @author jishenghua qq752718920 2018-10-7 15:26:27
|
||||
*/
|
||||
public class AnnotationUtils {
|
||||
public static <A extends Annotation> A getAnnotation(Class<?> cls, Class<A> annotationClass) {
|
||||
A res = cls.getAnnotation(annotationClass);
|
||||
if (res == null) {
|
||||
for (Annotation annotation : cls.getAnnotations()) {
|
||||
if (annotation instanceof Documented) {
|
||||
break;
|
||||
}
|
||||
res = getAnnotation(annotation.annotationType(), annotationClass);
|
||||
if (res != null)
|
||||
break;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static <T, A extends Annotation> A getAnnotation(T obj, Class<A> annotationClass) {
|
||||
return getAnnotation(obj.getClass(), annotationClass);
|
||||
}
|
||||
}
|
||||
11
src/main/java/com/jsh/erp/utils/BaseResponseInfo.java
Normal file
11
src/main/java/com/jsh/erp/utils/BaseResponseInfo.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
public class BaseResponseInfo {
|
||||
public int code;
|
||||
public Object data;
|
||||
|
||||
public BaseResponseInfo() {
|
||||
code = 400;
|
||||
data = null;
|
||||
}
|
||||
}
|
||||
65
src/main/java/com/jsh/erp/utils/ColumnPropertyUtil.java
Normal file
65
src/main/java/com/jsh/erp/utils/ColumnPropertyUtil.java
Normal file
@@ -0,0 +1,65 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
/**
|
||||
* @author jishenghua qq752718920 2018-10-7 15:26:27
|
||||
*/
|
||||
public class ColumnPropertyUtil {
|
||||
|
||||
/**
|
||||
* 将数据库字段转换成属性
|
||||
*/
|
||||
public static String columnToProperty(String column) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
// 快速检查
|
||||
if (StringUtil.isEmpty(column)) {
|
||||
// 没必要转换
|
||||
return "";
|
||||
} else if (!column.contains("_")) {
|
||||
// 不做转换
|
||||
return column;
|
||||
} else {
|
||||
// 用下划线将原始字符串分割
|
||||
String[] columns = column.split("_");
|
||||
for (String columnSplit : columns) {
|
||||
// 跳过原始字符串中开头、结尾的下换线或双重下划线
|
||||
if (StringUtil.isEmpty(columnSplit)) {
|
||||
continue;
|
||||
}
|
||||
// 处理真正的驼峰片段
|
||||
if (result.length() == 0) {
|
||||
// 第一个驼峰片段,全部字母都小写
|
||||
result.append(columnSplit.toLowerCase());
|
||||
} else {
|
||||
// 其他的驼峰片段,首字母大写
|
||||
result.append(columnSplit.substring(0, 1).toUpperCase()).append(columnSplit.substring(1).toLowerCase());
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 驼峰转换下划线
|
||||
*/
|
||||
public static String propertyToColumn(String property) {
|
||||
if (StringUtil.isEmpty(property)) {
|
||||
return "";
|
||||
}
|
||||
StringBuilder column = new StringBuilder();
|
||||
column.append(property.substring(0, 1).toLowerCase());
|
||||
for (int i = 1; i < property.length(); i++) {
|
||||
String s = property.substring(i, i + 1);
|
||||
// 在小写字母前添加下划线
|
||||
if (!Character.isDigit(s.charAt(0)) && s.equals(s.toUpperCase())) {
|
||||
column.append("_");
|
||||
}
|
||||
// 其他字符直接转成小写
|
||||
column.append(s.toLowerCase());
|
||||
}
|
||||
|
||||
return column.toString();
|
||||
}
|
||||
|
||||
}
|
||||
33
src/main/java/com/jsh/erp/utils/Constants.java
Normal file
33
src/main/java/com/jsh/erp/utils/Constants.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* by jishenghua qq-752718920 2018-10-7 12:01:36
|
||||
*/
|
||||
public class Constants {
|
||||
|
||||
//查询参数
|
||||
public final static String PAGE_SIZE = "pageSize";
|
||||
public final static String CURRENT_PAGE = "currentPage";
|
||||
public final static String ORDER = "order";
|
||||
public final static String FILTER = "filter";
|
||||
public final static String SPLIT = ",";
|
||||
public final static String SEARCH = "search";
|
||||
public final static String DEVICE_ID = "deviceId";
|
||||
public final static String OFFSET = "offset";
|
||||
public final static String IS_RECURSION = "isRecursion";
|
||||
public final static String IS_RECURSION_VALUE = "1";
|
||||
public final static String IS_QUERYBYNODEID = "isquerybyid";
|
||||
public final static String IS_QUERYBYNODEID_VALUE = "1";
|
||||
|
||||
//级联类别
|
||||
public final static String TYPE = "type";
|
||||
|
||||
//转发
|
||||
public final static String TEAM = "team";
|
||||
|
||||
//增加了角色等级常量
|
||||
public final static String LEVEL="level";
|
||||
|
||||
}
|
||||
36
src/main/java/com/jsh/erp/utils/ErpInfo.java
Normal file
36
src/main/java/com/jsh/erp/utils/ErpInfo.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public enum ErpInfo {
|
||||
//通过构造传递参数
|
||||
OK(200, "成功"),
|
||||
BAD_REQUEST(400, "请求错误或参数错误"),
|
||||
UNAUTHORIZED(401, "未认证用户"),
|
||||
INVALID_VERIFY_CODE(461, "错误的验证码"),
|
||||
ERROR(500, "服务内部错误"),
|
||||
WARING_MSG(201, "提醒信息"),
|
||||
REDIRECT(301, "session失效,重定向"),
|
||||
FORWARD_REDIRECT(302, "转发请求session失效"),
|
||||
FORWARD_FAILED(303, "转发请求失败!");
|
||||
|
||||
public final int code;
|
||||
public final String name;
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义枚举构造函数
|
||||
*/
|
||||
ErpInfo(int code, String name) {
|
||||
this.code = code;
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
33
src/main/java/com/jsh/erp/utils/ExceptionCodeConstants.java
Normal file
33
src/main/java/com/jsh/erp/utils/ExceptionCodeConstants.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
public interface ExceptionCodeConstants {
|
||||
/**
|
||||
* 用户错误码定义
|
||||
*/
|
||||
public class UserExceptionCode {
|
||||
/**
|
||||
* 用户不存在
|
||||
*/
|
||||
public static final int USER_NOT_EXIST = 1;
|
||||
|
||||
/**
|
||||
* 用户密码错误
|
||||
*/
|
||||
public static final int USER_PASSWORD_ERROR = 2;
|
||||
|
||||
/**
|
||||
* 被加入黑名单
|
||||
*/
|
||||
public static final int BLACK_USER = 3;
|
||||
|
||||
/**
|
||||
* 可以登录
|
||||
*/
|
||||
public static final int USER_CONDITION_FIT = 4;
|
||||
|
||||
/**
|
||||
* 访问数据库异常
|
||||
*/
|
||||
public static final int USER_ACCESS_EXCEPTION = 5;
|
||||
}
|
||||
}
|
||||
195
src/main/java/com/jsh/erp/utils/ExtJsonUtils.java
Normal file
195
src/main/java/com/jsh/erp/utils/ExtJsonUtils.java
Normal file
@@ -0,0 +1,195 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.parser.ParserConfig;
|
||||
import com.alibaba.fastjson.parser.deserializer.ExtraProcessor;
|
||||
import com.alibaba.fastjson.parser.deserializer.FieldDeserializer;
|
||||
import com.alibaba.fastjson.serializer.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author jishenghua qq752718920 2018-10-7 15:26:27
|
||||
*/
|
||||
public class ExtJsonUtils {
|
||||
private static class NPFloatCodec extends FloatCodec {
|
||||
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType) throws IOException {
|
||||
SerializeWriter out = serializer.getWriter();
|
||||
|
||||
if (object == null) {
|
||||
if (serializer.isEnabled(SerializerFeature.WriteNullNumberAsZero)) {
|
||||
out.write('0');
|
||||
} else {
|
||||
out.writeNull();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
float floatValue = (Float) object;
|
||||
|
||||
if (Float.isNaN(floatValue)) {
|
||||
out.writeNull();
|
||||
} else if (Float.isInfinite(floatValue)) {
|
||||
out.writeNull();
|
||||
} else {
|
||||
String floatText = Float.toString(floatValue);
|
||||
out.write(floatText);
|
||||
|
||||
if (serializer.isEnabled(SerializerFeature.WriteClassName)) {
|
||||
out.write('F');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class NPDoubleSerializer extends DoubleSerializer {
|
||||
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType) throws IOException {
|
||||
SerializeWriter out = serializer.getWriter();
|
||||
|
||||
if (object == null) {
|
||||
if (!serializer.isEnabled(SerializerFeature.WriteNullNumberAsZero)) {
|
||||
out.writeNull();
|
||||
} else {
|
||||
out.write('0');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
double doubleValue = (Double) object;
|
||||
|
||||
if (Double.isNaN(doubleValue)) {
|
||||
out.writeNull();
|
||||
} else if (Double.isInfinite(doubleValue)) {
|
||||
out.writeNull();
|
||||
} else {
|
||||
String doubleText;
|
||||
doubleText = Double.toString(doubleValue);
|
||||
out.append(doubleText);
|
||||
|
||||
if (serializer.isEnabled(SerializerFeature.WriteClassName)) {
|
||||
out.write('D');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static final String EXT_NAME = "ext";
|
||||
|
||||
static class ExtFilter extends AfterFilter implements PropertyFilter {
|
||||
static {
|
||||
SerializeConfig.getGlobalInstance().put(Float.class, new NPFloatCodec());
|
||||
SerializeConfig.getGlobalInstance().put(float.class, new NPFloatCodec());
|
||||
SerializeConfig.getGlobalInstance().put(Double.class, new NPDoubleSerializer());
|
||||
SerializeConfig.getGlobalInstance().put(double.class, new NPDoubleSerializer());
|
||||
}
|
||||
|
||||
private Map<Object, JSONObject> map = new HashMap<>();
|
||||
|
||||
private Map<Object, Set<String>> ignoredKey = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public boolean apply(Object object, String name, Object value) {
|
||||
if (name.equals(EXT_NAME) && value instanceof String) {
|
||||
map.put(object, JSON.parseObject((String) value));
|
||||
return false;
|
||||
}
|
||||
if (!map.containsKey(object)) {
|
||||
ignoredKey.put(object, new HashSet<String>());
|
||||
}
|
||||
ignoredKey.get(object).add(name);
|
||||
// if (value instanceof Float || value instanceof Double) {
|
||||
// if (!floatMap.containsKey(object)) {
|
||||
// floatMap.put(object, new HashMap<String, Object>());
|
||||
// }
|
||||
// floatMap.get(object).put(name, value);
|
||||
// return false;
|
||||
// }
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeAfter(Object object) {
|
||||
if (map.containsKey(object)) {
|
||||
Set<String> ignoredKeys;
|
||||
if (ignoredKey.containsKey(object)) {
|
||||
ignoredKeys = ignoredKey.get(object);
|
||||
} else {
|
||||
ignoredKeys = new HashSet<>();
|
||||
}
|
||||
for (Map.Entry<String, Object> entry : map.get(object).entrySet()) {
|
||||
if (!ignoredKeys.contains(entry.getKey())) {
|
||||
writeKeyValue(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String toJSONString(Object object) {
|
||||
return JSON.toJSONString(object, new ExtFilter());
|
||||
}
|
||||
|
||||
public interface ExtExtractor {
|
||||
String getExt(Object bean);
|
||||
}
|
||||
|
||||
private static class MetaInfo {
|
||||
private final static ParserConfig INSTANCE = ParserConfig.getGlobalInstance();
|
||||
|
||||
private final Object object;
|
||||
private final Map<String, FieldDeserializer> map;
|
||||
private final JSONObject ext = new JSONObject();
|
||||
|
||||
private MetaInfo(Object object) {
|
||||
this.object = object;
|
||||
this.map = INSTANCE.getFieldDeserializers(object.getClass());
|
||||
}
|
||||
|
||||
void gather(String key, Object value) {
|
||||
if (!map.containsKey(key)) {
|
||||
ext.put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
public void update(ExtExtractor extractor) {
|
||||
JSONObject old = JSON.parseObject(extractor.getExt(object));
|
||||
if (old == null) {
|
||||
old = new JSONObject();
|
||||
}
|
||||
old.putAll(ext);
|
||||
map.get(EXT_NAME).setValue(object, old.toJSONString());
|
||||
}
|
||||
|
||||
static boolean hasExt(Class<?> clazz) {
|
||||
return INSTANCE.getFieldDeserializers(clazz).containsKey(EXT_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T parseObject(String text, final Class<T> clazz, ExtExtractor extractor) {
|
||||
final Map<Object, MetaInfo> map = new HashMap<>();
|
||||
|
||||
T object = JSON.parseObject(text, clazz, new ExtraProcessor() {
|
||||
@Override
|
||||
public void processExtra(Object object, String key, Object value) {
|
||||
if (!map.containsKey(object) && MetaInfo.hasExt(object.getClass())) {
|
||||
map.put(object, new MetaInfo(object));
|
||||
}
|
||||
if (map.containsKey(object)) {
|
||||
map.get(object).gather(key, value);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
for (Map.Entry<Object, MetaInfo> entry : map.entrySet()) {
|
||||
entry.getValue().update(extractor);
|
||||
}
|
||||
|
||||
return object;
|
||||
}
|
||||
}
|
||||
63
src/main/java/com/jsh/erp/utils/JshException.java
Normal file
63
src/main/java/com/jsh/erp/utils/JshException.java
Normal file
@@ -0,0 +1,63 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
/**
|
||||
* @author jishenghua
|
||||
* @title: 平台异常基类
|
||||
* @description: 用于包装一些异常信息,打印日志等服务
|
||||
* @qq 7 5 2 7 1 8 9 2 0
|
||||
* @since: 2014-02-24
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class JshException extends Exception {
|
||||
public long errorCode = -1;
|
||||
|
||||
public String message;
|
||||
|
||||
public JshException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public JshException(String message) {
|
||||
super(message);
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public JshException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public JshException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public JshException(long errorCode) {
|
||||
super();
|
||||
this.errorCode = errorCode;
|
||||
}
|
||||
|
||||
public JshException(String message, long errorCode) {
|
||||
super(message);
|
||||
this.errorCode = errorCode;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public JshException(String message, long errorCode, Throwable cause) {
|
||||
super(message, cause);
|
||||
this.errorCode = errorCode;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public JshException(long errorCode, Throwable cause) {
|
||||
super(cause);
|
||||
this.errorCode = errorCode;
|
||||
}
|
||||
|
||||
public long getErrorCode() {
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
21
src/main/java/com/jsh/erp/utils/JsonUtils.java
Normal file
21
src/main/java/com/jsh/erp/utils/JsonUtils.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
/**
|
||||
* Created by jishenghua 2018-5-11 09:48:08
|
||||
*
|
||||
* @author jishenghua
|
||||
*/
|
||||
public class JsonUtils {
|
||||
|
||||
public static JSONObject ok(){
|
||||
JSONObject obj = new JSONObject();
|
||||
JSONObject tmp = new JSONObject();
|
||||
tmp.put("message", "成功");
|
||||
obj.put("code", 200);
|
||||
obj.put("data", tmp);
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
69
src/main/java/com/jsh/erp/utils/OrderUtils.java
Normal file
69
src/main/java/com/jsh/erp/utils/OrderUtils.java
Normal file
@@ -0,0 +1,69 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
/**
|
||||
* @author jishenghua qq752718920 2018-10-7 15:26:27
|
||||
*/
|
||||
public class OrderUtils {
|
||||
|
||||
/**
|
||||
* 将指定字段排序
|
||||
*
|
||||
* @param orders 格式 属性名,排序方式 例如( name,asc或ip,desc)
|
||||
* @return 排序字符串 例如:(name asc 或 ip desc)
|
||||
*/
|
||||
public static String getOrderString(String orders) {
|
||||
if (StringUtil.isNotEmpty(orders)) {
|
||||
String[] splits = orders.split(Constants.SPLIT);
|
||||
if (splits.length == 2) {
|
||||
String column = ColumnPropertyUtil.propertyToColumn(splits[0]);
|
||||
if (column.equals("audit_status")) {
|
||||
// TODO: 2015/12/24 这么处理不好,得相伴办法调整
|
||||
return "IF(`audit_status`=3,-1,`audit_status`) " + splits[1];
|
||||
} else if (column.equals("create_time") || column.equals("modify_time")) {
|
||||
// TODO: 2015/12/24 这么处理不好,得相伴办法调整
|
||||
return column + " " + splits[1];
|
||||
} else {
|
||||
return "convert(" + column + " using gbk) " + splits[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String getJoinTablesOrderString(String orders, String tableName) {
|
||||
if (StringUtil.isNotEmpty(orders)) {
|
||||
String[] splits = orders.split(Constants.SPLIT);
|
||||
if (splits.length == 2) {
|
||||
return "convert(" + tableName + "." + ColumnPropertyUtil.propertyToColumn(splits[0]) + " using gbk) " + splits[1];
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将指定字段排序
|
||||
* inet_aton:mysql将IP 转成 long类别函数
|
||||
*
|
||||
* @param orders 格式 属性名,排序方式 例如( name,asc或ip,desc)
|
||||
* @param ipPropertyName 如果需要按IP属性排序,需要将属性名传入(可不传)
|
||||
* @return 排序字符串 例如:(name asc 或 ip desc)
|
||||
*/
|
||||
public static String getOrderString(String orders, String... ipPropertyName) {
|
||||
if (StringUtil.isNotEmpty(orders)) {
|
||||
String[] splits = orders.split(Constants.SPLIT);
|
||||
if (splits.length == 2) {
|
||||
String column = ColumnPropertyUtil.propertyToColumn(splits[0]);
|
||||
if (ipPropertyName != null && ipPropertyName.length > 0) {
|
||||
for (String ip : ipPropertyName) {
|
||||
if (ip.equals(column)) {
|
||||
return "inet_aton(" + column + ") " + splits[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
return column + " " + splits[1];
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
30
src/main/java/com/jsh/erp/utils/PageQueryInfo.java
Normal file
30
src/main/java/com/jsh/erp/utils/PageQueryInfo.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 分页查询结果
|
||||
*
|
||||
* @author jishenghua qq752718920 2018-10-7 15:26:27
|
||||
*/
|
||||
public class PageQueryInfo {
|
||||
|
||||
private Integer total;
|
||||
private List<?> rows;
|
||||
|
||||
public Integer getTotal() {
|
||||
return total;
|
||||
}
|
||||
|
||||
public void setTotal(Integer total) {
|
||||
this.total = total;
|
||||
}
|
||||
|
||||
public List<?> getRows() {
|
||||
return rows;
|
||||
}
|
||||
|
||||
public void setRows(List<?> rows) {
|
||||
this.rows = rows;
|
||||
}
|
||||
}
|
||||
40
src/main/java/com/jsh/erp/utils/ParamUtils.java
Normal file
40
src/main/java/com/jsh/erp/utils/ParamUtils.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* @author jishenghua qq752718920 2018-10-7 15:26:27
|
||||
*/
|
||||
public class ParamUtils {
|
||||
public static String getPageOffset(Integer currentPage, Integer pageSize) {
|
||||
if (currentPage != null && pageSize != null) {
|
||||
int offset = (currentPage - 1) * pageSize;
|
||||
if (offset < 0) {
|
||||
return 0 + "";
|
||||
} else {
|
||||
return offset + "";
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static HashMap<String, String> requestToMap(HttpServletRequest request) {
|
||||
|
||||
HashMap<String, String> parameterMap = new HashMap<String, String>();
|
||||
Enumeration<String> names = request.getParameterNames();
|
||||
if (names != null) {
|
||||
for (String name : Collections.list(names)) {
|
||||
parameterMap.put(name, request.getParameter(name));
|
||||
/*HttpMethod method = HttpMethod.valueOf(request.getMethod());
|
||||
if (method == GET || method == DELETE)
|
||||
parameterMap.put(name, transcoding(request.getParameter(name)));
|
||||
else
|
||||
parameterMap.put(name, request.getParameter(name));*/
|
||||
}
|
||||
}
|
||||
return parameterMap;
|
||||
}
|
||||
}
|
||||
142
src/main/java/com/jsh/erp/utils/QueryUtils.java
Normal file
142
src/main/java/com/jsh/erp/utils/QueryUtils.java
Normal file
@@ -0,0 +1,142 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.jsh.erp.utils.Constants.CURRENT_PAGE;
|
||||
import static com.jsh.erp.utils.Constants.PAGE_SIZE;
|
||||
|
||||
/**
|
||||
* @author jishenghua qq752718920 2018-10-7 15:26:27
|
||||
*/
|
||||
public class QueryUtils {
|
||||
public static String filterSqlSpecialChar(String search) {
|
||||
return search != null ? search
|
||||
.replaceAll("_", "\\\\_")
|
||||
.replaceAll("!", "\\\\!")
|
||||
.replaceAll("\\[", "\\\\[")
|
||||
.replaceAll("\\]", "\\\\]")
|
||||
.replaceAll("\\^", "\\\\^") : null;
|
||||
}
|
||||
|
||||
public static <T> T list2One(List<T> list, String label) {
|
||||
Assert.notNull(label);
|
||||
Assert.notEmpty(list, label + "对应的记录不存在");
|
||||
Assert.isTrue(list.size() == 1, label + "对应的记录不止一个");
|
||||
return list.get(0);
|
||||
}
|
||||
|
||||
public static <T> T list2One(List<T> list, String label, T defaultValue) {
|
||||
Assert.notNull(list);
|
||||
Assert.notNull(label);
|
||||
if (list.isEmpty())
|
||||
return defaultValue;
|
||||
else {
|
||||
Assert.isTrue(list.size() == 1, label + "对应的记录不止一个");
|
||||
return list.get(0);
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> search(Map<String, String> map) {
|
||||
List<String> search = null;
|
||||
|
||||
String str = map.get(Constants.SEARCH);
|
||||
if (StringUtil.isNotEmpty(str)) {
|
||||
search = StringUtil.searchCondition(str);
|
||||
}
|
||||
return search;
|
||||
}
|
||||
|
||||
public static int rows(Map<String, String> map) {
|
||||
return Integer.parseInt(map.get(PAGE_SIZE));
|
||||
}
|
||||
|
||||
public static int offset(Map<String, String> map) {
|
||||
return (currentPage(map) - 1) * pageSize(map);
|
||||
}
|
||||
|
||||
public static int pageSize(Map<String, String> map) {
|
||||
return Integer.parseInt(map.get(PAGE_SIZE));
|
||||
}
|
||||
|
||||
public static int currentPage(Map<String, String> map) {
|
||||
int val = Integer.parseInt(map.get(CURRENT_PAGE));
|
||||
if (val < 1)
|
||||
throw new RuntimeException("当前页数目:" + val + " 必须大于0");
|
||||
return val;
|
||||
}
|
||||
|
||||
public static String order(Map<String, String> map) {
|
||||
String orderString = OrderUtils.getOrderString(map.get(Constants.ORDER));
|
||||
return orderString.trim().isEmpty() ? null : orderString;
|
||||
}
|
||||
|
||||
public static Integer level(Map<String, String> map) {
|
||||
String levelString = map.get(Constants.LEVEL);
|
||||
return StringUtil.isEmpty(levelString) ? null : Integer.parseInt(levelString);
|
||||
}
|
||||
|
||||
public static boolean isRecursion(Map<String, String> map) {
|
||||
String isRecursion = map.get(Constants.IS_RECURSION);
|
||||
return StringUtil.isNotEmpty(isRecursion) && Constants.IS_RECURSION_VALUE.equals(isRecursion);
|
||||
}
|
||||
|
||||
public static int type(Map<String, String> map) {
|
||||
return Integer.parseInt(map.get(Constants.TYPE));
|
||||
}
|
||||
|
||||
public static String filter(Map<String, String> map) {
|
||||
if (map.containsKey(Constants.FILTER)) {
|
||||
JSONArray array = JSON.parseArray(map.get(Constants.FILTER));
|
||||
if (array.isEmpty()) {
|
||||
return null;
|
||||
} else {
|
||||
boolean first = true;
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (int idx = 0; idx < array.size(); ++idx) {
|
||||
JSONObject object = array.getJSONObject(idx);
|
||||
if (object.get("value") instanceof JSONArray) {
|
||||
|
||||
JSONArray value = object.getJSONArray("value");
|
||||
|
||||
if (!value.isEmpty()) {
|
||||
if (!first) {
|
||||
builder.append(" AND ");
|
||||
} else {
|
||||
first = false;
|
||||
}
|
||||
|
||||
String key = object.getString("name");
|
||||
|
||||
builder.append("(");
|
||||
|
||||
builder.append("`").append(key).append("`");
|
||||
|
||||
builder.append(" IN ");
|
||||
|
||||
builder.append("(");
|
||||
|
||||
for (int vidx = 0; vidx < value.size(); ++vidx) {
|
||||
if (vidx != 0) {
|
||||
builder.append(",");
|
||||
}
|
||||
builder.append(value.getString(vidx));
|
||||
}
|
||||
builder.append(")");
|
||||
|
||||
builder.append(")");
|
||||
}
|
||||
}
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
154
src/main/java/com/jsh/erp/utils/RegExpTools.java
Normal file
154
src/main/java/com/jsh/erp/utils/RegExpTools.java
Normal file
@@ -0,0 +1,154 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Adm on 2015/12/14.
|
||||
*
|
||||
* @author yubiao
|
||||
* <p/>
|
||||
* mysql匹配正则表达式
|
||||
*/
|
||||
public class RegExpTools {
|
||||
/**
|
||||
* @param search 模糊匹配字符串数组
|
||||
*/
|
||||
public static String regexp(List<String> search) {
|
||||
if (search == null || search.isEmpty())
|
||||
return null;
|
||||
String regexp = "";
|
||||
for (String s : search) {
|
||||
if (!regexp.isEmpty()) {
|
||||
regexp = regexp + "|";
|
||||
}
|
||||
regexp = regexp + ".*";
|
||||
regexp = regexp + s.replaceAll("\\.", "\\\\.");
|
||||
regexp = regexp + ".*";
|
||||
}
|
||||
return regexp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key json字段key
|
||||
* @param search 模糊匹配字符串数组
|
||||
* json的mysql匹配正则表达式
|
||||
*/
|
||||
public static String regexp(String key, List<String> search) {
|
||||
if (search == null || search.isEmpty())
|
||||
return null;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String s : search) {
|
||||
if (sb.length() == 0) {
|
||||
sb.append(".*\\\"").append(key).append("\\\":\\\"[a-zA-Z0-9]*(");
|
||||
} else {
|
||||
sb.append("|");
|
||||
}
|
||||
sb.append(s);
|
||||
}
|
||||
sb.append(")[a-zA-Z0-9]*\\\".*");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static class RegExp {
|
||||
public static final String ANY = ".*";
|
||||
public static final String QUOTE = "\\\"";
|
||||
public static final String LFT_PAREN = "(";
|
||||
public static final String RHT_PAREN = ")";
|
||||
public static final String COLON = ":";
|
||||
public static final String OR = "|";
|
||||
|
||||
private final StringBuilder builder = new StringBuilder();
|
||||
|
||||
public RegExp any() {
|
||||
builder.append(ANY);
|
||||
return this;
|
||||
}
|
||||
|
||||
public RegExp lftParen() {
|
||||
builder.append(LFT_PAREN);
|
||||
return this;
|
||||
}
|
||||
|
||||
public RegExp rhtParen() {
|
||||
builder.append(RHT_PAREN);
|
||||
return this;
|
||||
}
|
||||
|
||||
public RegExp colon() {
|
||||
builder.append(COLON);
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
public RegExp quote() {
|
||||
builder.append(QUOTE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public RegExp quote(String str) {
|
||||
Assert.notNull(str, "str为空");
|
||||
builder.append(QUOTE).append(str).append(QUOTE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public RegExp value(String str) {
|
||||
Assert.notNull(str, "str为空");
|
||||
builder.append(str);
|
||||
return this;
|
||||
}
|
||||
|
||||
public RegExp or() {
|
||||
builder.append(OR);
|
||||
return this;
|
||||
}
|
||||
|
||||
public RegExp or(List<String> values) {
|
||||
Assert.notEmpty(values, "values必须非空");
|
||||
lftParen();
|
||||
boolean first = true;
|
||||
for (String value : values) {
|
||||
if (first) {
|
||||
builder.append(value);
|
||||
first = false;
|
||||
} else {
|
||||
builder.append(OR).append(value);
|
||||
}
|
||||
}
|
||||
rhtParen();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<String> values = new ArrayList<String>();
|
||||
|
||||
values.add("310");
|
||||
values.add(String.valueOf(2));
|
||||
values.add(String.valueOf(3));
|
||||
|
||||
RegExp exp = new RegExp();
|
||||
|
||||
exp.any();
|
||||
exp.quote("fullKbNum").colon()
|
||||
.quote()
|
||||
.value("[a-zA-Z0-9]*").or(values).value("[a-zA-Z0-9]*")
|
||||
.quote();
|
||||
exp.or();
|
||||
exp.quote("gbId[a-f0-9-]{36}").colon()
|
||||
.quote()
|
||||
.value("[0-9]*").or(values).value("[0-9]*")
|
||||
.quote();
|
||||
exp.any();
|
||||
|
||||
System.out.println(exp);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
24
src/main/java/com/jsh/erp/utils/ResponseCode.java
Normal file
24
src/main/java/com/jsh/erp/utils/ResponseCode.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONCreator;
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
|
||||
/**
|
||||
* @author jishenghua qq752718920 2018-10-7 15:26:27
|
||||
*/
|
||||
public class ResponseCode {
|
||||
|
||||
public final int code;
|
||||
public final Object data;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param code
|
||||
* @param data
|
||||
*/
|
||||
@JSONCreator
|
||||
public ResponseCode(@JSONField(name = "code") int code, @JSONField(name = "data")Object data) {
|
||||
this.code = code;
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
83
src/main/java/com/jsh/erp/utils/ResponseJsonUtil.java
Normal file
83
src/main/java/com/jsh/erp/utils/ResponseJsonUtil.java
Normal file
@@ -0,0 +1,83 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.serializer.SerializerFeature;
|
||||
import com.alibaba.fastjson.serializer.ValueFilter;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class ResponseJsonUtil {
|
||||
public static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd");
|
||||
|
||||
static {
|
||||
FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+8"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 响应过滤器
|
||||
*/
|
||||
public static final class ResponseFilter extends ExtJsonUtils.ExtFilter implements ValueFilter {
|
||||
@Override
|
||||
public Object process(Object object, String name, Object value) {
|
||||
if (name.equals("createTime") || name.equals("modifyTime")) {
|
||||
return value;
|
||||
} else if (value instanceof Date) {
|
||||
return FORMAT.format(value);
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param responseCode
|
||||
* @return
|
||||
*/
|
||||
public static String backJson4HttpApi(ResponseCode responseCode) {
|
||||
if (responseCode != null) {
|
||||
String result = JSON.toJSONString(responseCode, new ResponseFilter(),
|
||||
SerializerFeature.DisableCircularReferenceDetect,
|
||||
SerializerFeature.WriteNonStringKeyAsString);
|
||||
result = result.replaceFirst("\"data\":\\{", "");
|
||||
return result.substring(0, result.length() - 1);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证失败的json串
|
||||
* @param code
|
||||
* @return
|
||||
*/
|
||||
public static String backJson4VerifyFailure(int code) {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("message", "未通过验证");
|
||||
return JSON.toJSONString(new ResponseCode(code, map), new ResponseFilter(),
|
||||
SerializerFeature.DisableCircularReferenceDetect,
|
||||
SerializerFeature.WriteNonStringKeyAsString);
|
||||
}
|
||||
|
||||
/**
|
||||
* 成功的json串
|
||||
* @param responseCode
|
||||
* @return
|
||||
*/
|
||||
public static String backJson(ResponseCode responseCode) {
|
||||
if (responseCode != null) {
|
||||
return JSON.toJSONString(responseCode, new ResponseFilter(),
|
||||
SerializerFeature.DisableCircularReferenceDetect,
|
||||
SerializerFeature.WriteNonStringKeyAsString);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String returnJson(Map<String, Object> map, String message, int code) {
|
||||
map.put("message", message);
|
||||
return backJson(new ResponseCode(code, map));
|
||||
}
|
||||
}
|
||||
196
src/main/java/com/jsh/erp/utils/StringUtil.java
Normal file
196
src/main/java/com/jsh/erp/utils/StringUtil.java
Normal file
@@ -0,0 +1,196 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author jishenghua qq752718920 2018-10-7 15:26:27
|
||||
*/
|
||||
public class StringUtil {
|
||||
|
||||
private StringUtil() {
|
||||
|
||||
}
|
||||
|
||||
private static String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
|
||||
|
||||
public static String filterNull(String str) {
|
||||
if (str == null) {
|
||||
return "";
|
||||
} else {
|
||||
return str.trim();
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean stringEquels(String source,String target) {
|
||||
if(isEmpty(source)||isEmpty(target)){
|
||||
return false;
|
||||
}else{
|
||||
return source.equals(target);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isEmpty(String str) {
|
||||
return str == null || "".equals(str.trim());
|
||||
}
|
||||
|
||||
public static boolean isNotEmpty(String str) {
|
||||
return !isEmpty(str);
|
||||
}
|
||||
|
||||
public static String getSysDate(String format) {
|
||||
if (StringUtil.isEmpty(format)) {
|
||||
format = DEFAULT_FORMAT;
|
||||
}
|
||||
SimpleDateFormat df = new SimpleDateFormat(format);
|
||||
return df.format(new Date());
|
||||
}
|
||||
|
||||
public static Date getDateByString(String date, String format) {
|
||||
if (StringUtil.isEmpty(format)) {
|
||||
format = DEFAULT_FORMAT;
|
||||
}
|
||||
if (StringUtil.isNotEmpty(date)) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(format);
|
||||
try {
|
||||
return sdf.parse(date);
|
||||
} catch (ParseException e) {
|
||||
throw new RuntimeException("转换为日期类型错误:DATE:" + date + " FORMAT:" + format);
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Date getDateByLongDate(Long millis) {
|
||||
if (millis == null) {
|
||||
return new Date();
|
||||
}
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTimeInMillis(millis);
|
||||
return cal.getTime();
|
||||
|
||||
}
|
||||
|
||||
public static UUID stringToUUID(String id) {
|
||||
if (StringUtil.isNotEmpty(id)) {
|
||||
return UUID.fromString(id);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Integer parseInteger(String str) {
|
||||
if (StringUtil.isNotEmpty(str)) {
|
||||
return Integer.parseInt(str);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<UUID> listToUUID(List<String> listStrs) {
|
||||
if (listStrs != null && listStrs.size() > 0) {
|
||||
List<UUID> uuidList = new ArrayList<UUID>();
|
||||
for (String str : listStrs) {
|
||||
uuidList.add(UUID.fromString(str));
|
||||
}
|
||||
return uuidList;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<UUID> arrayToUUIDList(String[] uuids) {
|
||||
if (uuids != null && uuids.length > 0) {
|
||||
List<UUID> uuidList = new ArrayList<UUID>();
|
||||
for (String str : uuids) {
|
||||
uuidList.add(UUID.fromString(str));
|
||||
}
|
||||
return uuidList;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
//是否是JSON
|
||||
public static boolean containsAny(String str, String... flag) {
|
||||
if (str != null) {
|
||||
if (flag == null || flag.length == 0) {
|
||||
flag = "[-{-}-]-,".split("-");
|
||||
}
|
||||
for (String s : flag) {
|
||||
if (str.contains(s)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static String getModifyOrgOperateData(UUID resourceId, UUID orgId) {
|
||||
if (resourceId != null && orgId != null) {
|
||||
Map<UUID, UUID> map = new HashMap<UUID, UUID>();
|
||||
map.put(resourceId, orgId);
|
||||
return JSON.toJSONString(map);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String[] listToStringArray(List<String> list) {
|
||||
if (list != null && !list.isEmpty()) {
|
||||
return list.toArray(new String[list.size()]);
|
||||
}
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
public static List<String> stringToListArray(String[] strings) {
|
||||
if (strings != null && strings.length > 0) {
|
||||
return Arrays.asList(strings);
|
||||
}
|
||||
return new ArrayList<String>();
|
||||
}
|
||||
|
||||
/**
|
||||
* String字符串转成List<Long>数据格式
|
||||
* String str = "1,2,3,4,5,6" -> List<Long> listLong [1,2,3,4,5,6];
|
||||
*
|
||||
* @param strArr
|
||||
* @return
|
||||
*/
|
||||
public static List<Long> strToLongList(String strArr) {
|
||||
List<Long> idList=new ArrayList<Long>();
|
||||
String[] d=strArr.split(",");
|
||||
for (int i = 0, size = d.length; i < size; i++) {
|
||||
if(d[i]!=null) {
|
||||
idList.add(Long.parseLong(d[i]));
|
||||
}
|
||||
}
|
||||
return idList;
|
||||
}
|
||||
|
||||
public static List<String> searchCondition(String search) {
|
||||
if (isEmpty(search)) {
|
||||
return new ArrayList<String>();
|
||||
}else{
|
||||
//String[] split = search.split(" ");
|
||||
String[] split = search.split("#");
|
||||
return stringToListArray(split);
|
||||
}
|
||||
}
|
||||
|
||||
public static String getInfo(String search, String key){
|
||||
String value = "";
|
||||
if(search!=null) {
|
||||
JSONObject obj = JSONObject.parseObject(search);
|
||||
value = obj.getString(key);
|
||||
if(value.equals("")) {
|
||||
value = null;
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
587
src/main/java/com/jsh/erp/utils/Tools.java
Normal file
587
src/main/java/com/jsh/erp/utils/Tools.java
Normal file
@@ -0,0 +1,587 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.math.BigInteger;
|
||||
import java.net.InetAddress;
|
||||
import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
import java.net.UnknownHostException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 工具类
|
||||
*
|
||||
* @author jishenghua qq:7-5-2-7-1-8-9-2-0
|
||||
*/
|
||||
public class Tools {
|
||||
/**
|
||||
* 获得32位唯一序列号
|
||||
*
|
||||
* @return 32为ID字符串
|
||||
*/
|
||||
public static String getUUID_32() {
|
||||
return UUID.randomUUID().toString().replaceAll("-", "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得当天时间,格式为yyyy-MM-dd
|
||||
*
|
||||
* @return 格式化后的日期格式
|
||||
*/
|
||||
public static String getNow() {
|
||||
return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前月 yyyy-MM
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getCurrentMonth() {
|
||||
return new SimpleDateFormat("yyyy-MM").format(new Date());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定日期格式 yyyy-MM-dd
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getCurrentMonth(Date date) {
|
||||
return new SimpleDateFormat("yyyy-MM-dd").format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得当天时间,格式为yyyyMMddHHmmss
|
||||
*
|
||||
* @return 格式化后的日期格式
|
||||
*/
|
||||
public static String getNow2(Date date) {
|
||||
return new SimpleDateFormat("yyyyMMddHHmmss").format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得当天时间,格式为yyyy-MM-dd HH:mm:ss
|
||||
*
|
||||
* @return 格式化后的日期格式
|
||||
*/
|
||||
public static String getNow3() {
|
||||
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得指定时间,格式为yyyy-MM-dd HH:mm:ss
|
||||
*
|
||||
* @return 格式化后的日期格式
|
||||
*/
|
||||
public static String getCenternTime(Date date) {
|
||||
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得指定时间,格式为mm:ss
|
||||
*
|
||||
* @return 格式化后的日期格式
|
||||
*/
|
||||
public static String getTimeInfo(Date date) {
|
||||
return new SimpleDateFormat("mm:ss").format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前日期是星期几
|
||||
* return 星期几
|
||||
*/
|
||||
public static String getWeekDay() {
|
||||
Calendar c = Calendar.getInstance(Locale.CHINA);
|
||||
c.setTime(new Date());
|
||||
int day = c.get(Calendar.DAY_OF_WEEK);
|
||||
String weekDay = "";
|
||||
switch (day) {
|
||||
case 1:
|
||||
weekDay = "星期日";
|
||||
break;
|
||||
case 2:
|
||||
weekDay = "星期一";
|
||||
break;
|
||||
case 3:
|
||||
weekDay = "星期二";
|
||||
break;
|
||||
case 4:
|
||||
weekDay = "星期三";
|
||||
break;
|
||||
case 5:
|
||||
weekDay = "星期四";
|
||||
break;
|
||||
case 6:
|
||||
weekDay = "星期五";
|
||||
break;
|
||||
case 7:
|
||||
weekDay = "星期六";
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return weekDay;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断字符串是否全部为数字
|
||||
*
|
||||
* @param accountWaste
|
||||
* @return boolean值
|
||||
*/
|
||||
public static boolean checkStrIsNum(String checkStr) {
|
||||
if (checkStr == null || checkStr.length() == 0)
|
||||
return false;
|
||||
return Pattern.compile("^[0-9]*.{1}[0-9]*$").matcher(checkStr).matches();
|
||||
// return Pattern.compile(":^[0-9]+(.[0-9])*$").matcher(checkStr).matches();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得前一天的时间
|
||||
*
|
||||
* @return 前一天日期
|
||||
*/
|
||||
public static String getPreviousDate() {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.add(Calendar.DATE, -1);
|
||||
return new SimpleDateFormat("yyyy-MM").format(cal.getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 截取字符串长度
|
||||
*
|
||||
* @param beforeStr
|
||||
* @param cutLeng
|
||||
* @return 截取后的字符串
|
||||
*/
|
||||
public static String subStr(String beforeStr, int cutLeng) {
|
||||
if (beforeStr.length() > cutLeng)
|
||||
return beforeStr.substring(0, cutLeng) + "...";
|
||||
return beforeStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成随机字符串,字母和数字混合
|
||||
*
|
||||
* @return 组合后的字符串 ^[0-9a-zA-Z]
|
||||
*/
|
||||
public static String getRandomChar() {
|
||||
//生成一个0、1、2的随机数字
|
||||
int rand = (int) Math.round(Math.random() * 1);
|
||||
long itmp = 0;
|
||||
char ctmp = '\u0000';
|
||||
switch (rand) {
|
||||
//生成大写字母 + 1000以内数字
|
||||
case 1:
|
||||
itmp = Math.round(Math.random() * 25 + 65);
|
||||
ctmp = (char) itmp;
|
||||
return String.valueOf(ctmp) + (int) Math.random() * 1000;
|
||||
//生成小写字母
|
||||
case 2:
|
||||
itmp = Math.round(Math.random() * 25 + 97);
|
||||
ctmp = (char) itmp;
|
||||
return String.valueOf(ctmp) + (int) Math.random() * 1000;
|
||||
//生成数字
|
||||
default:
|
||||
itmp = Math.round(Math.random() * 1000);
|
||||
return itmp + "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断首字母以数字开头,字符串包括数字、字母%以及空格
|
||||
*
|
||||
* @param str 检查字符串
|
||||
* @return 是否以数字开头
|
||||
*/
|
||||
public static boolean CheckIsStartWithNum(String str) {
|
||||
return Pattern.compile("^[0-9][a-zA-Z0-9%,\\s]*$").matcher(str).matches();
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断首字母以","开头,字符串包括数字、字母%以及空格
|
||||
*
|
||||
* @param str 检查字符串
|
||||
* @return 是否以数字开头
|
||||
*/
|
||||
public static boolean CheckIsStartWithSpec(String str) {
|
||||
return Pattern.compile("^[,][a-zA-Z0-9%,\\s]*$").matcher(str).matches();
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符转码
|
||||
*
|
||||
* @param aValue
|
||||
* @return
|
||||
* @see 转码后的字符串
|
||||
*/
|
||||
public static String encodeValue(String aValue) {
|
||||
if (aValue.trim().length() == 0) {
|
||||
return "";
|
||||
}
|
||||
String valueAfterTransCode = null;
|
||||
try {
|
||||
valueAfterTransCode = URLEncoder.encode(aValue, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.getMessage();
|
||||
}
|
||||
return valueAfterTransCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符转码
|
||||
*
|
||||
* @param aValue
|
||||
* @return
|
||||
* @see 转码后的字符串
|
||||
*/
|
||||
public static String decodeValue(String aValue) {
|
||||
if (aValue.trim().length() == 0) {
|
||||
return "";
|
||||
}
|
||||
String valueAfterTransCode = null;
|
||||
try {
|
||||
valueAfterTransCode = URLDecoder.decode(aValue, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.getMessage();
|
||||
}
|
||||
return valueAfterTransCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* 去除str中的'
|
||||
*
|
||||
* @param str
|
||||
* @return 除去'后的字符串
|
||||
* @see [类、类#方法、类#成员]
|
||||
*/
|
||||
public static String afterDealStr(String str) {
|
||||
return str.replace("'", "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户IP地址(停用)
|
||||
*
|
||||
* @return 用户IP
|
||||
* @see [类、类#方法、类#成员]
|
||||
*/
|
||||
public static String getCurrentUserIP() {
|
||||
try {
|
||||
return InetAddress.getLocalHost().getHostAddress();
|
||||
} catch (UnknownHostException e) {
|
||||
e.printStackTrace();
|
||||
return "127.0.0.1";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从Request对象中获得客户端IP,处理了HTTP代理服务器和Nginx的反向代理截取了ip
|
||||
*
|
||||
* @param request
|
||||
* @return ip
|
||||
*/
|
||||
public static String getLocalIp(HttpServletRequest request) {
|
||||
String remoteAddr = request.getRemoteAddr();
|
||||
String forwarded = request.getHeader("X-Forwarded-For");
|
||||
String realIp = request.getHeader("X-Real-IP");
|
||||
|
||||
String ip = null;
|
||||
if (realIp == null) {
|
||||
if (forwarded == null) {
|
||||
ip = remoteAddr;
|
||||
} else {
|
||||
ip = remoteAddr + "/" + forwarded.split(",")[0];
|
||||
}
|
||||
} else {
|
||||
if (realIp.equals(forwarded)) {
|
||||
ip = realIp;
|
||||
} else {
|
||||
if (forwarded != null) {
|
||||
forwarded = forwarded.split(",")[0];
|
||||
}
|
||||
ip = realIp + "/" + forwarded;
|
||||
}
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转化前台批量传入的ID值
|
||||
*
|
||||
* @param data
|
||||
* @return 转化后的ID值数组
|
||||
*/
|
||||
public static int[] changeDataForm(String data) {
|
||||
String[] dataStr = data.split(",");
|
||||
int[] dataInt = new int[dataStr.length];
|
||||
for (int i = 0; i < dataStr.length; i++)
|
||||
dataInt[i] = Integer.parseInt(dataStr[i]);
|
||||
return dataInt;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解决导出文件中文乱码问题firefox和ie下中文乱码
|
||||
*/
|
||||
public static String changeUnicode(String fileName, String browserType) {
|
||||
String returnFileName = "";
|
||||
try {
|
||||
if (browserType.equalsIgnoreCase("MSIE")) {
|
||||
returnFileName = URLEncoder.encode(fileName, "ISO8859-1");
|
||||
returnFileName = returnFileName.replace(" ", "%20");
|
||||
if (returnFileName.length() > 150) {
|
||||
returnFileName = new String(fileName.getBytes("GB2312"), "ISO8859-1");
|
||||
returnFileName = returnFileName.replace(" ", "%20");
|
||||
}
|
||||
} else if (browserType.equalsIgnoreCase("Firefox")) {
|
||||
returnFileName = new String(fileName.getBytes("ISO8859-1"), "ISO8859-1");
|
||||
returnFileName = returnFileName.replace(" ", "%20");
|
||||
} else {
|
||||
returnFileName = URLEncoder.encode(fileName, "ISO8859-1");
|
||||
returnFileName = returnFileName.replace(" ", "%20");
|
||||
if (returnFileName.length() > 150) {
|
||||
|
||||
returnFileName = new String(returnFileName.getBytes("GB2312"), "ISO8859-1");
|
||||
returnFileName = returnFileName.replace(" ", "%20");
|
||||
}
|
||||
}
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return returnFileName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写理财日志内容转化特殊字符
|
||||
*
|
||||
* @param str 需要转化的字符
|
||||
* @return 转化后的字符
|
||||
*/
|
||||
public static String htmlspecialchars(String str) {
|
||||
str = str.replaceAll("&", "&");
|
||||
str = str.replaceAll("<", "<");
|
||||
str = str.replaceAll(">", ">");
|
||||
str = str.replaceAll("\"", """);
|
||||
return str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据消费日期获取消费月
|
||||
*
|
||||
* @param consumeDate 消费日期
|
||||
* @return 返回消费月信息
|
||||
*/
|
||||
public static String getConsumeMonth(String consumeDate) {
|
||||
return consumeDate.substring(0, 7);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前日期的前XX个月
|
||||
*
|
||||
* @param 之前的第几个月
|
||||
* @return 前XX个月字符串
|
||||
*/
|
||||
public static String getBeforeMonth(int beforeMonth) {
|
||||
Calendar c = Calendar.getInstance();
|
||||
c.add(Calendar.MONTH, -beforeMonth);
|
||||
return new SimpleDateFormat("yyyy-MM").format(c.getTime());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取email用户姓名
|
||||
*
|
||||
* @param args
|
||||
*/
|
||||
public static String getEmailUserName(String emailAddress) {
|
||||
return emailAddress.substring(0, emailAddress.lastIndexOf("@"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取中文编码,邮件附件乱码问题解决
|
||||
*
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
public static String getChineseString(String emailAttchmentTitle) {
|
||||
if (emailAttchmentTitle != null && !emailAttchmentTitle.equals("")) {
|
||||
try {
|
||||
return new String(emailAttchmentTitle.getBytes(), "ISO-8859-1");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return emailAttchmentTitle;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断userTel是否合法,userTel只能是数字
|
||||
*
|
||||
* @param userTel
|
||||
* @return true 合法 false不合法
|
||||
*/
|
||||
public static boolean isTelNumber(String userTel) {
|
||||
String reg_phone = "^(\\(\\d{3,4}\\)|\\d{3,4}-)?\\d{7,8}$";
|
||||
String reg_tel = "^(1[0-9][0-9]|1[0-9][0|3|6|8|9])\\d{8}$";
|
||||
boolean b_phpne = Pattern.compile(reg_phone).matcher(userTel).matches();
|
||||
boolean b_tel = Pattern.compile(reg_tel).matcher(userTel).matches();
|
||||
return (b_phpne || b_tel);
|
||||
}
|
||||
|
||||
/**
|
||||
* 模糊判断电话号码是否合法,只能是数字
|
||||
*
|
||||
* @param macAddress
|
||||
* @return
|
||||
*/
|
||||
public static boolean isTelNumberBySlur(String userTel) {
|
||||
return Pattern.compile("^([\\s0-9]{0,12}$)").matcher(userTel).matches();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前时间的字符串类型
|
||||
*
|
||||
* @return 处理后的字符串类型
|
||||
*/
|
||||
public static String getNowTime() {
|
||||
return new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar.getInstance().getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 开打指定文件
|
||||
*
|
||||
* @param filePath 文件的绝对路径
|
||||
*/
|
||||
public static void openFile(String filePath) {
|
||||
String viewFilePath = filePath.replace("\\", "/");
|
||||
// Runtime.getRuntime().exec("cmd /c start "+filePath);
|
||||
// 解决路径中带空格问题
|
||||
Runtime r = Runtime.getRuntime();
|
||||
String[] cmdArray = new String[]{"cmd.exe", "/c", viewFilePath};
|
||||
try {
|
||||
r.exec(cmdArray);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断字符串中是否含有中文
|
||||
*
|
||||
* @param str
|
||||
* @return
|
||||
* @author jishenghua
|
||||
*/
|
||||
public static boolean isContainsChinese(String str) {
|
||||
return Pattern.compile("[\u4e00-\u9fa5]").matcher(str).matches();
|
||||
}
|
||||
|
||||
/**
|
||||
* 过滤html文件中的文本
|
||||
*
|
||||
* @param content
|
||||
* @return过滤后的文本
|
||||
*/
|
||||
public static String filterText(String content) {
|
||||
return content.replace("/<(?:.|\\s)*?>/g", "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 去掉字符串中所有符号,不论是全角,还是半角的,或是货币符号或者空格等
|
||||
*
|
||||
* @param s
|
||||
* @return
|
||||
* @author jishenghua
|
||||
*/
|
||||
public static String removeSymbolForString(String s) {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
char[] chars = s.toCharArray();
|
||||
for (int i = 0; i < chars.length; i++) {
|
||||
if ((chars[i] >= 19968 && chars[i] <= 40869) || (chars[i] >= 97 && chars[i] <= 122) || (chars[i] >= 65 && chars[i] <= 90)) {
|
||||
buffer.append(chars[i]);
|
||||
}
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一个字符串的MD5
|
||||
*
|
||||
* @param msg
|
||||
* @return 加密后的MD5字符串
|
||||
* @throws NoSuchAlgorithmException
|
||||
*/
|
||||
public static String md5Encryp(String msg) throws NoSuchAlgorithmException {
|
||||
// 生成一个MD5加密计算摘要
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
// 计算md5函数
|
||||
md.update(msg.getBytes());
|
||||
return new BigInteger(1, md.digest()).toString(16);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理字符串null值
|
||||
*
|
||||
* @param beforeStr 处理前字符串
|
||||
* @return 处理后的字符串
|
||||
*/
|
||||
public static String dealNullStr(String beforeStr) {
|
||||
if (null == beforeStr || beforeStr.length() == 0)
|
||||
return "";
|
||||
return beforeStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用参数Format将字符串转为Date
|
||||
*
|
||||
* @param strDate
|
||||
* @param pattern
|
||||
* @return
|
||||
* @throws ParseException
|
||||
* @author jishenghua
|
||||
*/
|
||||
public static Date parse(String strDate, String pattern)
|
||||
throws ParseException {
|
||||
return new SimpleDateFormat(pattern).parse(strDate);
|
||||
}
|
||||
|
||||
// /**
|
||||
// * 过滤html文件中的图片文件
|
||||
// * @param content
|
||||
// * @return
|
||||
// */
|
||||
// public static String filterImg(String content)
|
||||
// {
|
||||
// return content.matches("/<img(?:.|\\s)*?>/g");
|
||||
// }
|
||||
|
||||
public static void main(String[] args) {
|
||||
String aa = "的付的反对法的发的说法";
|
||||
char[] bb = aa.toCharArray();
|
||||
for (char c : bb) {
|
||||
System.out.println(c);
|
||||
}
|
||||
System.out.println(getBeforeMonth(1));
|
||||
|
||||
try {
|
||||
System.out.println(md5Encryp("guest"));
|
||||
System.out.println(md5Encryp("admin"));
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
String value = "2333";
|
||||
System.out.println(checkStrIsNum(value));
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
System.out.print(getRandomChar() + " || ");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user