This commit is contained in:
yexuejc 2018-05-12 22:36:03 +08:00
parent 051ddc1eb4
commit 4b7e86de85
9 changed files with 398 additions and 100 deletions

View File

@ -1,7 +1,13 @@
通用工具包 通用工具包
###说明
>1. 支持环境java8
>2. 该工具包基于springboot提取按理说适用于所有java工程
>3. 其中依赖jjwt、validation-api但不传递依赖
### 使用 ### 使用
>yexuejc.base.version=1.0.0 >yexuejc.base.version=1.0.1
pom.xml pom.xml
``` ```
@ -18,4 +24,10 @@ pom.xml
<url>https://jitpack.io</url> <url>https://jitpack.io</url>
</repository> </repository>
</repositories> </repositories>
``` ```
### 工具文档
[Wiki](WIKI.md)
### 更新日志
[更新记录](UPDATE.md)

16
UPDATE.md Normal file
View File

@ -0,0 +1,16 @@
yexuejc-base 更新记录
------------------
#### version 1.0.1
**time** 2018-5-12 22:25:05<br/>
**branch** master <br/>
**update** <br/>
>1.添加支持加密功能
#
#### version 1.0.0
**time** 2018-1-31 12:16:10<br/>
**branch** master <br/>
**update** <br/>
>1.基于java8开发的web应用工具包
#

9
WIKI.md Normal file
View File

@ -0,0 +1,9 @@
yexuejc-base 文档
------------------
##### 1. RespsConsts 网络请求统一返回 常量
##### 2. Resps 网络请求统一返回类
##### 3. ApiVO 接口交互API
##### 4. BaseVO 基类VO
待完善......

10
pom.xml
View File

@ -6,7 +6,7 @@
<groupId>com.yexuejc.base</groupId> <groupId>com.yexuejc.base</groupId>
<artifactId>yexuejc-base</artifactId> <artifactId>yexuejc-base</artifactId>
<version>1.0.0</version> <version>1.0.1</version>
<name>${project.artifactId}</name> <name>${project.artifactId}</name>
@ -20,6 +20,7 @@
<jjwt.version>0.7.0</jjwt.version> <jjwt.version>0.7.0</jjwt.version>
<maven.compiler.verbose>true</maven.compiler.verbose> <maven.compiler.verbose>true</maven.compiler.verbose>
<java.version>1.8</java.version> <java.version>1.8</java.version>
<validation-api.version>1.1.0.Final</validation-api.version>
</properties> </properties>
<dependencies> <dependencies>
@ -30,6 +31,13 @@
<version>${jjwt.version}</version> <version>${jjwt.version}</version>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<!-- 数据校验框架 -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>${validation-api.version}</version>
<optional>true</optional>
</dependency>
</dependencies> </dependencies>

View File

@ -20,6 +20,10 @@ public class Resps<T> implements Serializable {
* 状态 * 状态
*/ */
private String code; private String code;
/**
* md5码
*/
private String sign;
/** /**
* 内容 * 内容
*/ */
@ -133,6 +137,14 @@ public class Resps<T> implements Serializable {
this.msg = msg; this.msg = msg;
} }
public String getSign() {
return sign;
}
public void setSign(String sign) {
this.sign = sign;
}
@Override @Override
public String toString() { public String toString() {
return JsonUtil.obj2Json(this); return JsonUtil.obj2Json(this);

View File

@ -5,6 +5,7 @@ import com.yexuejc.base.util.JsonUtil;
import java.io.Serializable; import java.io.Serializable;
/** /**
* 基类VO
* @PackageName: com.yexuejc.util.base.pojo * @PackageName: com.yexuejc.util.base.pojo
* @Description: * @Description:
* @author: maxf * @author: maxf

View File

@ -0,0 +1,49 @@
package com.yexuejc.base.pojo;
import com.yexuejc.base.util.JsonUtil;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
/**
* 分页 VO
*
* @author: maxf
* @date: 2018/3/28 14:23
*/
public class PagerVO extends BaseVO {
private static final long serialVersionUID = 3490440129554644587L;
@NotNull
@Min(1L)
private Integer page = 1;
@NotNull
@Min(1L)
private Integer size = 20;
public int getOffset() {
return (this.page - 1) * this.size;
}
@Override
public String toString() {
return JsonUtil.obj2Json(this);
}
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getSize() {
return size;
}
public void setSize(Integer size) {
this.size = size;
}
}

View File

@ -0,0 +1,45 @@
package com.yexuejc.base.pojo;
import com.yexuejc.base.util.JsonUtil;
import java.io.Serializable;
/**
* 解密前的请求参数格式
*
* @author: maxf
* @date: 2018/5/12 14:52
*/
public class ParamsPO implements Serializable {
private static final long serialVersionUID = 9171765814642105098L;
/**
* 参数
*/
private String data;
/**
* md5
*/
private String sign;
@Override
public String toString() {
return JsonUtil.obj2Json(this);
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public String getSign() {
return sign;
}
public void setSign(String sign) {
this.sign = sign;
}
}

View File

@ -3,37 +3,52 @@ package com.yexuejc.base.util;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Collection; import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static java.util.regex.Pattern.compile;
/** /**
* @PackageName: com.yexuejc.util.base.util * 字符串工具类
*
* @ClassName: StrUtil
* @Description: * @Description:
* @author: maxf * @author: maxf
* @date: 2017/12/28 17:34 * @date: 2018/5/12 19:13
*/ */
public final class StrUtil { public final class StrUtil {
public static char[] HEX_CHAR = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', public static char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
'e', 'f'};
private StrUtil() {
}
/**
* 判断字符串数组集合 是否为空
*
* @param obj
* @return
*/
public static boolean isEmpty(Object obj) { public static boolean isEmpty(Object obj) {
if (obj instanceof String) { if (obj instanceof String) {
return obj == null || "".equals((String) obj); if (obj == null || "".equals((String) obj)) {
return true;
} else {
return false;
}
} else if (obj instanceof Object[]) { } else if (obj instanceof Object[]) {
return obj == null || ((Object[]) ((Object[]) obj)).length == 0; if (obj == null || ((Object[]) obj).length == 0) {
} else if (obj instanceof Collection) { return true;
return obj == null || ((Collection) obj).size() == 0; } else {
return false;
}
} else if (obj instanceof Collection<?>) {
if (obj == null || ((Collection<?>) obj).size() == 0) {
return true;
} else {
return false;
}
} else { } else {
return obj == null; if (obj == null) {
return true;
} else {
return false;
}
} }
} }
@ -41,10 +56,23 @@ public final class StrUtil {
return !isEmpty(obj); return !isEmpty(obj);
} }
/**
* 生成UUID
*
* @return
*/
public static String genUUID() { public static String genUUID() {
return UUID.randomUUID().toString().replaceAll("-", ""); return UUID.randomUUID().toString().replaceAll("-", "");
} }
/**
* 生成11位编号可以用作订单号有很小几率出现重复需要做异常处理<br/>
* 左边第一位为正负标识正数1 负数0<br/>
* 剩余位数为UUID的hashcode值<br/>
* 可以再生成的编号基础上嵌入其他标识编码
*
* @return
*/
public static String genNum() { public static String genNum() {
int hashCode = UUID.randomUUID().toString().hashCode(); int hashCode = UUID.randomUUID().toString().hashCode();
StringBuffer num = new StringBuffer(); StringBuffer num = new StringBuffer();
@ -54,131 +82,215 @@ public final class StrUtil {
} else { } else {
num.append("1"); num.append("1");
} }
return num.append(String.format("%010d", hashCode)).toString().substring(0, 8); return num.append(String.format("%010d", hashCode)).toString().substring(0, 8);
} }
/**
* 解析aa=bb&cc=dd&ee=ff格式的字符串返回HashMap
*
* @param urlencoded
* @return
*/
public static Map<String, String> parseUrlencoded(String urlencoded) { public static Map<String, String> parseUrlencoded(String urlencoded) {
if (isEmpty(urlencoded)) { if (isEmpty(urlencoded)) {
return null; return null;
} else { }
String[] entrys = urlencoded.split("&"); String[] entrys = urlencoded.split("&");
if (isEmpty(entrys)) { if (isEmpty(entrys)) {
return null; return null;
}
Map<String, String> map = new HashMap<String, String>();
String[] kv = null;
for (String entry : entrys) {
if (isEmpty(entry)) {
continue;
}
kv = entry.split("=");
if (isEmpty(kv)) {
continue;
}
if (kv.length > 1) {
map.put(kv[0], kv[1]);
} else { } else {
Map<String, String> map = new HashMap(); map.put(kv[0], null);
String[] kv = null;
String[] var4 = entrys;
int var5 = entrys.length;
for (int var6 = 0; var6 < var5; ++var6) {
String entry = var4[var6];
if (!isEmpty(entry)) {
kv = entry.split("=");
if (!isEmpty(kv)) {
if (kv.length > 1) {
map.put(kv[0], kv[1]);
} else {
map.put(kv[0], null);
}
}
}
}
return map;
} }
} }
return map;
} }
public static String toHex(byte[] buf) { /**
* 字符串转换方法 把字节数组转换成16进制字符串
*
* @param buf 初始字节数组
* @return 转换后字符串
*/
public static String toHex(byte buf[]) {
StringBuffer strbuf = new StringBuffer(buf.length * 2); StringBuffer strbuf = new StringBuffer(buf.length * 2);
int i;
for (int i = 0; i < buf.length; ++i) { for (i = 0; i < buf.length; i++) {
if ((buf[i] & 255) < 16) { if (((int) buf[i] & 0xff) < 0x10) {
strbuf.append("0"); strbuf.append("0");
} }
strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
strbuf.append(Long.toString((long) (buf[i] & 255), 16));
} }
return strbuf.toString(); return strbuf.toString();
} }
/**
* 获取字符串的MD5码
*
* @param str
* @return
*/
public static String toMD5(String str) { public static String toMD5(String str) {
if (str == null) { if (str == null) {
return null; return null;
} else {
MessageDigest md = null;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException var3) {
var3.printStackTrace();
return null;
}
md.update(str.getBytes());
byte[] tmp = md.digest();
return toHex(tmp);
} }
MessageDigest md = null;
try {
md = java.security.MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
md.update(str.getBytes());
byte tmp[] = md.digest();
return toHex(tmp);
} }
/**
* SHA256加密
*
* @param str
* @return
*/
public static String toSHA256(final String str) {
return toSHA(str, "SHA-256");
}
/**
* SHA加密
*
* @param str
* @param key
* @return
*/
public static String toSHA(final String str, final String key) {
// 是否是有效字符串
if (str == null) {
return null;
}
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance(key);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
messageDigest.update(str.getBytes());
byte tmp[] = messageDigest.digest();
return toHex(tmp);
}
/**
* 用ISO-8859-1解码 再用UFT-8编码
*
* @param str
* @return
*/
public static String iso2utf(String str) { public static String iso2utf(String str) {
String utfStr = null; String utfStr = null;
try { try {
utfStr = new String(str.getBytes("ISO-8859-1"), "utf-8"); utfStr = new String(str.getBytes("ISO-8859-1"), "utf-8");
} catch (UnsupportedEncodingException var3) { } catch (UnsupportedEncodingException e) {
var3.printStackTrace(); e.printStackTrace();
} }
return utfStr; return utfStr;
} }
/**
* 判断字符串是否是数字
*
* @param str
* @return
*/
public static boolean isNumeric(String str) { public static boolean isNumeric(String str) {
Pattern pattern = compile("[0-9]*"); Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str); Matcher isNum = pattern.matcher(str);
return isNum.matches(); if (!isNum.matches()) {
return false;
}
return true;
} }
/**
* 对ID32位进行编码
*
* @param id
* @return
*/
public static String codeId(String id) { public static String codeId(String id) {
if (id != null && id.length() == 32) { if (id == null || id.length() != 32) {
StringBuilder coded = new StringBuilder();
int i;
for (i = 0; i < 13; ++i) {
coded.append(HEX_CHAR[(int) (Math.random() * 15.0D) + 1]);
}
coded.append(id.substring(0, 11));
for (i = 0; i < 7; ++i) {
coded.append(HEX_CHAR[(int) (Math.random() * 15.0D) + 1]);
}
coded.append(id.substring(11));
for (i = 0; i < 12; ++i) {
coded.append(HEX_CHAR[(int) (Math.random() * 15.0D) + 1]);
}
return coded.toString();
} else {
return id; return id;
} }
StringBuilder coded = new StringBuilder();
for (int i = 0; i < 13; i++) {
coded.append(HEX_CHAR[(int) (Math.random() * 15L) + 1]);
}
coded.append(id.substring(0, 11));
for (int i = 0; i < 7; i++) {
coded.append(HEX_CHAR[(int) (Math.random() * 15L) + 1]);
}
coded.append(id.substring(11));
for (int i = 0; i < 12; i++) {
coded.append(HEX_CHAR[(int) (Math.random() * 15L) + 1]);
}
return coded.toString();
} }
/**
* 对ID32位进行解码
*
* @param coded
* @return
*/
public static String decodeId(String coded) { public static String decodeId(String coded) {
if (coded != null && coded.length() == 64) { if (coded == null || coded.length() != 64) {
StringBuilder id = new StringBuilder();
id.append(coded.substring(13, 24));
id.append(coded.substring(31, 52));
return id.toString();
} else {
return coded; return coded;
} }
StringBuilder id = new StringBuilder();
id.append(coded.substring(13, 24));
id.append(coded.substring(31, 52));
return id.toString();
} }
/**
* map parameters 转url parameters
*
* @param sortedParams
* @return
*/
public static String getSignContent(Map<String, ?> sortedParams) {
StringBuffer content = new StringBuffer();
List<String> keys = new ArrayList<>(sortedParams.keySet());
Collections.sort(keys);
int index = 0;
for (int i = 0; i < keys.size(); ++i) {
String key = keys.get(i);
Object value = sortedParams.get(key);
if (isNotEmpty(key) && isNotEmpty(value)) {
content.append((index == 0 ? "" : "&") + key + "=" + value);
++index;
}
}
return content.toString();
}
/** /**
* 替换手机号中间4位为* * 替换手机号中间4位为*
@ -192,4 +304,38 @@ public final class StrUtil {
public static String replaceMobile(String mobile) { public static String replaceMobile(String mobile) {
return mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"); return mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
} }
/**
* map 排序
*
* @param sortedParams
* @return
*/
public static Map<String, Object> mapSort(Map<String, ?> sortedParams) {
Map<String, Object> map = new HashMap<>();
List<String> keys = new ArrayList<>(sortedParams.keySet());
Collections.sort(keys);
int index = 0;
for (int i = 0; i < keys.size(); ++i) {
String key = keys.get(i);
Object value = sortedParams.get(key);
map.put(key, value);
++index;
}
return map;
}
/**
* 设置Str带默认值
*
* @param msg
* @param defMsg
* @return
*/
public static String setStr(String msg, String defMsg) {
if (StrUtil.isEmpty(msg)) {
return defMsg;
}
return msg;
}
} }